This repository has been archived by the owner on Nov 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Category): Add Categories Manage Pane
- Loading branch information
Showing
8 changed files
with
451 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Rhilip | ||
* Date: 7/15/2019 | ||
* Time: 10:20 PM | ||
*/ | ||
|
||
namespace apps\controllers; | ||
|
||
use Rid\Http\Controller; | ||
use apps\models\form\Manage\Categories; | ||
|
||
class ManageController extends Controller | ||
{ | ||
public function actionCategories() | ||
{ | ||
if (app()->request->isPost()) { | ||
if (app()->request->post('action') == 'cat_edit') { | ||
$edit_form = new Categories\EditForm(); | ||
$edit_form->setData(app()->request->post()); | ||
$success = $edit_form->validate(); | ||
if ($success) { | ||
$edit_form->flush(); | ||
return $this->render('action/action_success'); | ||
} else { | ||
return $this->render('action/action_fail', ['msg' => $edit_form->getError()]); | ||
} | ||
} elseif (app()->request->post('action') == 'cat_delete') { | ||
$delete_form = new Categories\RemoveForm(); | ||
$delete_form->setData(app()->request->post()); | ||
$success = $delete_form->validate(); | ||
if ($success) { | ||
$delete_form->flush(); | ||
return $this->render('action/action_success'); | ||
} else { | ||
return $this->render('action/action_fail', ['msg' => $delete_form->getError()]); | ||
} | ||
} | ||
} | ||
|
||
$parent_id = app()->request->get('parent_id', 0); | ||
$parent_category = []; | ||
if ($parent_id !== 0) { | ||
$parent_category = app()->pdo->createCommand('SELECT * FROM `torrents_categories` WHERE `id` = :id')->bindParams([ | ||
'id' => $parent_id | ||
])->queryOne(); | ||
} | ||
|
||
$categories = app()->pdo->createCommand('SELECT * FROM `torrents_categories` WHERE `parent_id` = :pid ORDER BY `sort_index`,`id`')->bindParams([ | ||
'pid' => $parent_id | ||
])->queryAll(); | ||
|
||
foreach ($categories as &$category) { | ||
$child_count = app()->pdo->createCommand('SELECT COUNT(`id`) FROM `torrents_categories` WHERE `parent_id` = :pid')->bindParams([ | ||
'pid' => $category['id'] | ||
])->queryScalar(); | ||
$category['child_count'] = $child_count; | ||
} | ||
|
||
return $this->render('manage/categories', ['parent_id' => $parent_id, 'parent_category' => $parent_category, 'categories' => $categories]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Rhilip | ||
* Date: 7/16/2019 | ||
* Time: 4:13 PM | ||
*/ | ||
|
||
namespace apps\models\form\Manage\Categories; | ||
|
||
|
||
use Rid\Validators\Validator; | ||
|
||
class EditForm extends Validator | ||
{ | ||
public $cat_id; | ||
public $cat_parent_id; | ||
public $cat_name; | ||
public $cat_enabled = 0; | ||
public $cat_sort_index; | ||
public $cat_image; | ||
public $cat_class_name; | ||
|
||
private $cat_new_data; | ||
private $cat_old_data; | ||
private $cat_data_diff; | ||
|
||
public static function inputRules() | ||
{ | ||
return [ | ||
'cat_id' => 'Required | Integer', | ||
'cat_parent_id' => 'Required | Integer', | ||
'cat_name' => 'Required | AlphaNumHyphen', | ||
'cat_enabled' => 'Integer', | ||
'cat_sort_index' => 'Integer', | ||
'cat_image' => [ | ||
['Regex', ['pattern' => '/^[a-z0-9_.\/]*$/']] | ||
], | ||
'cat_class_name' => [ | ||
['Regex', ['pattern' => '/^[a-z][a-z0-9_\-]*?$/']] | ||
], | ||
]; | ||
} | ||
|
||
public static function callbackRules() | ||
{ | ||
return ['checkCategoryData']; | ||
} | ||
|
||
protected function checkCategoryData() | ||
{ | ||
$this->cat_new_data = [ | ||
'parent_id' => (int)$this->cat_parent_id, | ||
'name' => $this->cat_name, 'enabled' => $this->cat_enabled, | ||
'sort_index' => (int)$this->cat_sort_index, | ||
'image' => $this->cat_image, 'class_name' => $this->cat_class_name | ||
]; | ||
|
||
// Generate New Full Path Key | ||
$parent_cat_fpath = app()->pdo->createCommand('SELECT `full_path` FROM `torrents_categories` WHERE `id` = :pid')->bindParams([ | ||
'pid' => $this->cat_parent_id | ||
])->queryScalar(); | ||
if ($parent_cat_fpath === false) { | ||
$full_path = $this->cat_name; | ||
} else { | ||
$full_path = $parent_cat_fpath . ' - ' . $this->cat_name; | ||
} | ||
$this->cat_new_data['full_path'] = $full_path; | ||
$flag_check_full_path = true; | ||
|
||
if ((int)$this->cat_id !== 0) { // Check if old links should be update | ||
$this->cat_old_data = app()->pdo->createCommand('SELECT * FROM `torrents_categories` WHERE id = :id')->bindParams([ | ||
'id' => $this->cat_id | ||
])->queryOne(); | ||
if ($this->cat_old_data === false) { | ||
$this->buildCallbackFailMsg('Category:exist', 'the link data not found in our database'); | ||
return; | ||
} | ||
$this->cat_new_data['id'] = (int)$this->cat_id; | ||
|
||
// Diff old and new data. | ||
$this->cat_data_diff = array_diff_assoc($this->cat_new_data, $this->cat_old_data); | ||
if (count($this->cat_data_diff) === 0) { | ||
$this->buildCallbackFailMsg('Category:update', 'No data update'); | ||
return; | ||
} | ||
if (!isset($this->cat_data_diff['full_path'])) $flag_check_full_path = false; // It means full path key not update, We shouldn't check anymore. | ||
} | ||
|
||
if ($flag_check_full_path) { // Check if full path key is duplicate or not. | ||
$check_full_path = app()->pdo->createCommand('SELECT COUNT(`id`) FROM `torrents_categories` WHERE `full_path` = :fpath')->bindParams([ | ||
'fpath' => $full_path | ||
])->queryScalar(); | ||
if ($check_full_path > 0) { | ||
$this->buildCallbackFailMsg('Category:duplicate', 'This Path is already exist.'); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
public function flush() | ||
{ | ||
if ((int)$this->cat_id !== 0) { // to edit exist cat | ||
app()->pdo->update('torrents_categories', $this->cat_data_diff, [['id', '=', $this->cat_id]])->execute(); | ||
// TODO Add site log | ||
} else { // to new a cat | ||
app()->pdo->insert('torrents_categories', $this->cat_new_data)->execute(); | ||
// TODO Add site log | ||
} | ||
// TODO flush Redis Cache | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Rhilip | ||
* Date: 7/16/2019 | ||
* Time: 4:13 PM | ||
*/ | ||
|
||
namespace apps\models\form\Manage\Categories; | ||
|
||
|
||
use Rid\Validators\Validator; | ||
|
||
class RemoveForm extends Validator | ||
{ | ||
public $cat_id; | ||
|
||
private $category_data; | ||
|
||
public static function inputRules() | ||
{ | ||
return [ | ||
'cat_id' => 'Required | Integer', | ||
]; | ||
} | ||
|
||
public static function callbackRules() | ||
{ | ||
return ['getExistCategoryData', 'checkChildNode']; | ||
} | ||
|
||
protected function getExistCategoryData() | ||
{ | ||
$this->category_data = app()->pdo->createCommand('SELECT * FROM `torrents_categories` WHERE id = :id')->bindParams([ | ||
'id' => $this->cat_id | ||
])->queryScalar(); | ||
if ($this->category_data === false) { | ||
$this->buildCallbackFailMsg('Categories:exist', 'This category isn\'t exist in our site.'); | ||
} | ||
} | ||
|
||
protected function checkChildNode() | ||
{ | ||
$child_chount = app()->pdo->createCommand('SELECT COUNT(`id`) FROM `torrents_categories` WHERE `parent_id` = :pid')->bindParams([ | ||
'pid' => $this->cat_id | ||
])->queryScalar(); | ||
if ($child_chount !== 0) { | ||
$this->buildCallbackFailMsg('Categories;child', 'This category has sub category exist, Please clean subcategory first.'); | ||
} | ||
} | ||
|
||
public function flush() | ||
{ | ||
// FIXME Move Category's torrent from this to it's parent | ||
app()->pdo->createCommand('UPDATE `torrents` SET `category` = :new WHERE `category` = :old ')->bindParams([ | ||
'new' => $this->category_data['parent_id'], 'old' => $this->category_data['id'] | ||
])->execute(); | ||
|
||
// Delete it~ | ||
app()->pdo->createCommand('DELETE FROM `torrents_categories` WHERE id = :id')->bindParams([ | ||
'id' => $this->cat_id | ||
])->execute(); | ||
// TODO flush Redis cache | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
jQuery(document).ready(function () { | ||
let edit_form = $('#cat_edit_form'); | ||
let remove_form = $('#cat_remove_form'); | ||
|
||
$('.cat-edit').click(function () { | ||
let that = $(this); | ||
|
||
$('#cat_modal').modal(); | ||
|
||
// Get category data from <tr> and Fill data to form | ||
let tr = $('#cat_manager_table tr[data-id=' + that.data('id') + ']'); | ||
for (let datum in tr.data()) { | ||
let input = edit_form.find('[name="cat_' + datum + '"]'); | ||
if (datum === 'enabled') { | ||
input.prop('checked',tr.data(datum) ? 'checked' : ''); | ||
} else { | ||
input.val(tr.data(datum)); | ||
} | ||
} | ||
}); | ||
|
||
$('.cat-remove').click(function () { | ||
let that = $(this); | ||
if (confirm('Confirm to remove this Category ?')) { | ||
remove_form.find('input[name=cat_id]').val(that.data('id')); | ||
remove_form.submit(); | ||
} | ||
}); | ||
|
||
}); |
Oops, something went wrong.