Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
feat(Category): Add Categories Manage Pane
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Jul 17, 2019
1 parent 9facda1 commit 77aba91
Show file tree
Hide file tree
Showing 8 changed files with 451 additions and 6 deletions.
10 changes: 6 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@
- **Database:** Fix table `links` miss
- **Form:** Miss use flag since namespace change
- **Form:** Link\EditForm Update diff
- **Requests:** Fix fullUrl() may add unnecessary `?`

### Perf
- **Tracker:** Use brpoplpush to get announce data from redis

### Refactor
- **View:** Rename folder `error` to `action`
- **action_success:** Simple The Action Template

### Style
- **Auth:** Sort Auth Form
- **printIn:** Add datetime tag


<a name="0.1.4-alpha"></a>
## [0.1.4-alpha] - 2019-06-28
<a name="v0.1.4-alpha"></a>
## [v0.1.4-alpha] - 2019-06-28
### Chore
- **User:** Make User component as Part of App but not framework

Expand Down Expand Up @@ -260,8 +262,8 @@ Structure of Table `users_session_log` Change
<a name="v0.1.0-alpha"></a>
## v0.1.0-alpha - 2019-01-30

[Unreleased]: https://github.com/Rhilip/ridpt/compare/0.1.4-alpha...HEAD
[0.1.4-alpha]: https://github.com/Rhilip/ridpt/compare/v0.1.3-alpha...0.1.4-alpha
[Unreleased]: https://github.com/Rhilip/ridpt/compare/v0.1.4-alpha...HEAD
[v0.1.4-alpha]: https://github.com/Rhilip/ridpt/compare/v0.1.3-alpha...v0.1.4-alpha
[v0.1.3-alpha]: https://github.com/Rhilip/ridpt/compare/v0.1.2-alpha...v0.1.3-alpha
[v0.1.2-alpha]: https://github.com/Rhilip/ridpt/compare/v0.1.1-alpha...v0.1.2-alpha
[v0.1.1-alpha]: https://github.com/Rhilip/ridpt/compare/v0.1.0-alpha...v0.1.1-alpha
63 changes: 63 additions & 0 deletions apps/controllers/ManageController.php
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]);
}
}
112 changes: 112 additions & 0 deletions apps/models/form/Manage/Categories/EditForm.php
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
}
}
65 changes: 65 additions & 0 deletions apps/models/form/Manage/Categories/RemoveForm.php
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
}
}
4 changes: 4 additions & 0 deletions apps/public/static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ body{background-color:#f6f6f6}
#footer_menu a{color:#C3C0B9}
#footer_menu a:hover{color:#E84807}

.modal-content form{padding:0 10px}
.switch>label{font-weight:700}

/*-----------------------------------------------------------------------------------*/
/* 3. Page of '/auth'
/*-----------------------------------------------------------------------------------*/
Expand Down Expand Up @@ -114,3 +117,4 @@ body{background-color:#f6f6f6}
#torrent_structure li div.dictionary span.icon{color:#909;padding:2px}
#torrent_structure li div.list span.icon{color:#009;padding:2px}
#torrent_structure li span.title{font-weight:bold}

30 changes: 30 additions & 0 deletions apps/public/static/js/manage_categories.js
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();
}
});

});
Loading

0 comments on commit 77aba91

Please sign in to comment.