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

Commit

Permalink
fix(categories): Remove key sort_index
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Table `torrents_categories` rename to `categories`
  • Loading branch information
Rhilip committed Jul 21, 2019
1 parent 9f02aae commit f8c1475
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 109 deletions.
21 changes: 2 additions & 19 deletions apps/controllers/ManageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,9 @@ public function actionCategories()
}
}

$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;
}
$categories = app()->pdo->createCommand('SELECT * FROM `categories` ORDER BY `full_path`')->queryAll();

return $this->render('manage/categories', ['parent_id' => $parent_id, 'parent_category' => $parent_category, 'categories' => $categories]);
return $this->render('manage/categories', ['categories' => $categories]);
}

public function actionQualities()
Expand Down
2 changes: 1 addition & 1 deletion apps/models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct()
{
$this->categories = app()->redis->hGetAll($this->CacheKey);
if (empty($this->categories)) {
$self = app()->pdo->createCommand("SELECT * FROM `torrents_categories`")->queryAll();
$self = app()->pdo->createCommand("SELECT * FROM `categories`")->queryAll();
$this->categories = array_column($self, 'name', 'id');
app()->redis->hMset($this->CacheKey, $this->categories);
app()->redis->expire($this->CacheKey, 3600);
Expand Down
27 changes: 19 additions & 8 deletions apps/models/form/Manage/Categories/EditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class EditForm extends Validator
public $cat_parent_id;
public $cat_name;
public $cat_enabled = 0;
public $cat_sort_index;
public $cat_image;
public $cat_class_name;

Expand All @@ -32,7 +31,6 @@ public static function inputRules()
'cat_parent_id' => 'Required | Integer',
'cat_name' => 'Required | AlphaNumHyphen',
'cat_enabled' => 'Integer',
'cat_sort_index' => 'Integer',
'cat_image' => [
['Regex', ['pattern' => '/^[a-z0-9_.\/]*$/']]
],
Expand All @@ -52,24 +50,30 @@ 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([
$parent_cat_fpath = app()->pdo->createCommand('SELECT `full_path` FROM `categories` WHERE `id` = :pid')->bindParams([
'pid' => $this->cat_parent_id
])->queryScalar();
if ($parent_cat_fpath === false) {
$this->buildCallbackFailMsg('Category:parent', 'The parent category can\'t found.');
return;
}

if ($this->cat_parent_id == 0) {
$full_path = $this->cat_name;
} else {
$full_path = $parent_cat_fpath . ' - ' . $this->cat_name;
}

$this->cat_new_data['full_path'] = $full_path;
$this->cat_new_data['level'] = substr_count($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([
$this->cat_old_data = app()->pdo->createCommand('SELECT * FROM `categories` WHERE id = :id')->bindParams([
'id' => $this->cat_id
])->queryOne();
if ($this->cat_old_data === false) {
Expand All @@ -88,7 +92,7 @@ protected function checkCategoryData()
}

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([
$check_full_path = app()->pdo->createCommand('SELECT COUNT(`id`) FROM `categories` WHERE `full_path` = :fpath')->bindParams([
'fpath' => $full_path
])->queryScalar();
if ($check_full_path > 0) {
Expand All @@ -101,12 +105,19 @@ protected function checkCategoryData()
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();
app()->pdo->update('categories', $this->cat_data_diff, [['id', '=', $this->cat_id]])->execute();
if ($this->cat_parent_id !== 0) { // Disabled post to parent cat
app()->pdo->createCommand('UPDATE `categories` SET `enabled` = 0 WHERE `id` = :pid')->bindParams([
'pid' => $this->cat_parent_id
])->execute();
}
// TODO Add site log
} else { // to new a cat
app()->pdo->insert('torrents_categories', $this->cat_new_data)->execute();
app()->pdo->insert('categories', $this->cat_new_data)->execute();
// TODO Add site log
}

// TODO flush Redis Cache
app()->redis->del('site:enabled_torrent_category');
}
}
23 changes: 19 additions & 4 deletions apps/models/form/Manage/Categories/RemoveForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class RemoveForm extends Validator
public $cat_id;

private $category_data;
private $child_count;

public static function inputRules()
{
Expand All @@ -31,7 +32,7 @@ public static function callbackRules()

protected function getExistCategoryData()
{
$this->category_data = app()->pdo->createCommand('SELECT * FROM `torrents_categories` WHERE id = :id')->bindParams([
$this->category_data = app()->pdo->createCommand('SELECT * FROM `categories` WHERE id = :id')->bindParams([
'id' => $this->cat_id
])->queryScalar();
if ($this->category_data === false) {
Expand All @@ -41,10 +42,10 @@ protected function getExistCategoryData()

protected function checkChildNode()
{
$child_chount = app()->pdo->createCommand('SELECT COUNT(`id`) FROM `torrents_categories` WHERE `parent_id` = :pid')->bindParams([
$this->child_count = app()->pdo->createCommand('SELECT COUNT(`id`) FROM `categories` WHERE `parent_id` = :pid')->bindParams([
'pid' => $this->cat_id
])->queryScalar();
if ($child_chount !== 0) {
if ($this->child_count !== 0) {
$this->buildCallbackFailMsg('Categories;child', 'This category has sub category exist, Please clean subcategory first.');
}
}
Expand All @@ -57,9 +58,23 @@ public function flush()
])->execute();

// Delete it~
app()->pdo->createCommand('DELETE FROM `torrents_categories` WHERE id = :id')->bindParams([
app()->pdo->createCommand('DELETE FROM `categories` WHERE id = :id')->bindParams([
'id' => $this->cat_id
])->execute();

// Enabled parent category if no siblings
$siblings_count = app()->pdo->createCommand('SELECT COUNT(`id`) FROM `categories` WHERE `parent_id` = :pid')->bindParams([
'pid' => $this->category_data['parent_id']
])->queryScalar();

if ($siblings_count == 0) {
app()->pdo->createCommand('UPDATE `categories` SET `enabled` = 1 WHERE `id` = :id')->bindParams([
'id' => $this->category_data['parent_id']
])->execute();
}


// TODO flush Redis cache
app()->redis->del('site:enabled_torrent_category');
}
}
4 changes: 2 additions & 2 deletions apps/models/form/TorrentUploadForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static function ruleCategory()
{
$categories = app()->redis->get('site:enabled_torrent_category');
if (false === $categories) {
$categories = app()->pdo->createCommand('SELECT * FROM `torrents_categories` WHERE `enabled` = 1 ORDER BY `parent_id`,`sort_index`,`id`')->queryAll();
$categories = app()->pdo->createCommand('SELECT * FROM `categories` WHERE `id` > 0 ORDER BY `full_path`')->queryAll();
app()->redis->set('site:enabled_torrent_category', $categories, 86400);
}
return $categories;
Expand All @@ -88,7 +88,7 @@ public static function rulePinnedTags()
{
$tags = app()->redis->get('site:pinned_tags');
if (false === $tags) {
$tags = app()->pdo->createCommand('SELECT * FROM `tags` WHERE `pinned` = 1')->queryAll();
$tags = app()->pdo->createCommand('SELECT * FROM `tags` WHERE `pinned` = 1 LIMIT 10;')->queryAll();
app()->redis->set('site:pinned_tags', $tags, 86400);
}
return $tags;
Expand Down
49 changes: 19 additions & 30 deletions apps/views/manage/categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* Date: 7/15/2019
* Time: 10:40 PM
* @var League\Plates\Template\Template $this
* @var int $parent_id
* @var array $parent_category
* @var array $categories
*/

Expand All @@ -20,13 +18,7 @@
<?php $this->start('container') ?>
<div class="row">
<div class="col-md-12">
<h2>
<?php if ($parent_id == 0) : ?>
Manage Category
<?php else : ?>
Manage Sub Category of <kbd><?= $parent_category['name'] ?></kbd>
<?php endif; ?>
</h2>
<h2>Manage Category</h2>
</div>
<div class="col-md-12">
<div class="pull-right">
Expand All @@ -35,37 +27,30 @@
<table class="table table-hover" id="cat_manager_table">
<thead>
<tr>
<td>Id</td>
<td>Sort Index</td>
<td>Name</td>
<td>Image</td>
<td>Class Name</td>
<td>Sub Category</td>
<td>Enabled</td>
<td>Action</td>
<th width="15px"></th>
<th>Id</th>
<th>Name</th>
<th>Image</th>
<th>Class Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php if (count($categories) > 0): ?>
<?php foreach ($categories as $category): ?>
<tr class="<?= $category['enabled'] ? '':'warning' ?>"
data-id="<?= $category['id'] ?>"
<tr data-id="<?= $category['id'] ?>"
data-parent_id="<?= $category['parent_id'] ?>"
data-enabled="<?= $category['enabled'] ?>"
data-sort_index="<?= $category['sort_index'] ?>"
data-name="<?= $category['name'] ?>"
data-full_name="<?= $category['full_path'] ?>"
data-image="<?= $category['image'] ?>"
data-class_name="<?= $category['class_name'] ?>"
>
<td><?= $category['enabled'] ? '<i class="far fa-fw fa-check-square"></i>':'<i class="far fa-fw fa-square"></i>' ?></td>
<td><?= $category['id'] ?></td>
<td><?= $category['sort_index'] ?></td>
<td><?= $category['name'] ?></td>
<td><?= str_repeat('&nbsp;', 4 * ($category['level'] + 1)) ?><b><?= $category['name'] ?></b></td>
<td><?= $category['image'] ?></td>
<td><?= $category['class_name'] ?></td>
<td>
<a href="<?= '?parent_id=' . $category['id'] ?>"><?= $category['child_count'] > 0 ? "${category['child_count']} SubCategories" : 'No SubCategory' ?></a>
</td>
<td><?= $category['enabled'] ? '<i class="far fa-fw fa-check-square"></i>':'<i class="far fa-fw fa-square"></i>' ?></td>
<td><a href="javascript:" class="cat-edit" data-id="<?= $category['id'] ?>">Edit</a> |
<a href="javascript:" class="cat-remove" data-id="<?= $category['id'] ?>">Remove</a>
</td>
Expand Down Expand Up @@ -99,7 +84,6 @@
<div class="modal-body">
<label class="hidden"><input type="text" name="action" value="cat_edit"></label>
<label class="hidden"><input type="number" id="cat_id" name="cat_id" value="0"></label>
<label class="hidden"><input type="number" id="cat_parent_id" name="cat_parent_id" value="<?= $this->e($parent_id) ?>"></label>
<div class="form-group">
<label for="cat_name" class="required">Name</label>
<input type="text" class="form-control" id="cat_name" name="cat_name" required>
Expand All @@ -112,9 +96,14 @@
</div>
</div>
<div class="form-group">
<label for="cat_sort_index">Sort Index</label>
<input type="number" class="form-control" id="cat_sort_index" name="cat_sort_index" value="0">
<div class="help-block">Ascendantly, i.e. '0' comes first.</div>
<label for="cat_parent_id" class="required">Parent Category</label>
<select id="cat_parent_id" name="cat_parent_id" class="form-control">
<?php foreach ($categories as $category) : ?>
<?php $level = substr_count($category['full_path'],' - '); ?>
<option value="<?= $category['id'] ?>"><?= str_repeat('&nbsp;', 4 * ($category['level'] + 1)) . $category['name'] ?></option>
<?php endforeach; ?>
</select>
<div class="help-block">Recommended up to two levels of category.</div>
</div>
<div class="form-group">
<label for="cat_image">Image</label>
Expand Down
7 changes: 7 additions & 0 deletions apps/views/manage/quality.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
/** TODO
* Created by PhpStorm.
* User: Rhilip
* Date: 7/20/2019
* Time: 7:43 PM
*/
Loading

0 comments on commit f8c1475

Please sign in to comment.