Skip to content

Commit 6981f7b

Browse files
authored
Merge pull request #431 from topcoder-platform/issues-422
Issues-422: Fixed sql query for Flat Tree
2 parents 71485dc + 5679a12 commit 6981f7b

File tree

3 files changed

+129
-23
lines changed

3 files changed

+129
-23
lines changed

vanilla/applications/vanilla/controllers/class.categoriescontroller.php

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,16 @@ private function getCategoryTree($category = null, $displayAs = null, $recent =
140140
$perPage = c('Vanilla.Categories.PerPage', 30);
141141
$page = Gdn::request()->get('Page', Gdn::request()->get('page', null));
142142
list($offset, $limit) = offsetLimit($page, $perPage);
143-
$categoryTree = $this->CategoryModel->getTreeAsFlat($categoryIdentifier, $offset, $limit);
143+
144+
$filter = [];
145+
if(Gdn::session()->isValid()) {
146+
$filter['UserID'] = Gdn::session()->UserID;
147+
$filter['isAdmin'] = Gdn::session()->User->Admin;
148+
}
149+
$categoryTree = $this->CategoryModel->getTreeAsFlat($categoryIdentifier, $offset, $limit,$filter, 'c.DateInserted', 'desc');
150+
$countOfCategoryTree = $this->CategoryModel->countOfCategories($categoryIdentifier,$filter);
144151
$this->setData('_Limit', $perPage);
152+
$this->setData('_RecordCount', $countOfCategoryTree);
145153
$this->setData('_CurrentRecords', count($categoryTree));
146154
break;
147155
case 'Categories':
@@ -616,23 +624,25 @@ public function all($Category = '', $displayAs = '') {
616624
true
617625
);
618626
}
619-
620-
if($this->data('CategorySort')) {
621-
if( $this->data('CategorySort') == self::SORT_OLDEST_POST) {
622-
usort($categoryTree, function ($a, $b) {
623-
return Gdn_Format::toTimestamp($a['LastDiscussionCommentsDate']) - Gdn_Format::toTimestamp($b['LastDiscussionCommentsDate']);
624-
});
625-
626-
} else if( $this->data('CategorySort') == self::SORT_LAST_POST) {
627-
usort($categoryTree, function ($a, $b) {
628-
return Gdn_Format::toTimestamp($b['LastDiscussionCommentsDate']) - Gdn_Format::toTimestamp($a['LastDiscussionCommentsDate']);
629-
627+
// FIX: https://github.com/topcoder-platform/forums/issues/422
628+
// Sorting for Flat type in SQL
629+
if($displayAs != 'Flat') {
630+
if ($this->data('CategorySort')) {
631+
if ($this->data('CategorySort') == self::SORT_OLDEST_POST) {
632+
usort($categoryTree, function ($a, $b) {
633+
return Gdn_Format::toTimestamp($a['LastDiscussionCommentsDate']) - Gdn_Format::toTimestamp($b['LastDiscussionCommentsDate']);
634+
});
635+
636+
} else if ($this->data('CategorySort') == self::SORT_LAST_POST) {
637+
usort($categoryTree, function ($a, $b) {
638+
return Gdn_Format::toTimestamp($b['LastDiscussionCommentsDate']) - Gdn_Format::toTimestamp($a['LastDiscussionCommentsDate']);
639+
});
640+
}
641+
} else {
642+
usort($categoryTree, function ($a, $b) { // desc
643+
return Gdn_Format::toTimestamp($b['LastDiscussionCommentsDate']) - Gdn_Format::toTimestamp($a['LastDiscussionCommentsDate']);
630644
});
631645
}
632-
} else {
633-
usort($categoryTree, function ($a, $b) { // desc
634-
return Gdn_Format::toTimestamp($b['LastDiscussionCommentsDate']) - Gdn_Format::toTimestamp($a['LastDiscussionCommentsDate']);
635-
});
636646
}
637647

638648
$this->setData('CategoryTree', $categoryTree);

vanilla/applications/vanilla/models/class.categorymodel.php

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,15 +1064,32 @@ public function getTree($categoryID, array $options = []) {
10641064
* @param string $orderDirection
10651065
* @return array
10661066
*/
1067-
public function getTreeAsFlat($id, $offset = null, $limit = null, $filter = null, $orderFields = 'Name', $orderDirection = 'asc') {
1068-
$query = $this->SQL
1069-
->from('Category')
1070-
->where('DisplayAs <>', 'Heading')
1071-
->where('ParentCategoryID', $id)
1072-
->limit($limit, $offset)
1067+
public function getTreeAsFlat($id, $offset = null, $limit = null, $filter = null, $orderFields = 'Name', $orderDirection = 'asc')
1068+
{
1069+
$query = $this->SQL->from('Category c');
1070+
1071+
//FIX: https://github.com/topcoder-platform/forums/issues/422
1072+
if (!val('isAdmin', $filter, false)) {
1073+
if (val('UserID', $filter, false)) {
1074+
$userID = val('UserID', $filter);
1075+
$query->
1076+
leftJoin('UserGroup ug', 'c.GroupID = ug.GroupID')
1077+
->beginWhereGroup()
1078+
->where('c.GroupID is null')
1079+
->orWhere('ug.UserID', $userID)
1080+
->endWhereGroup();
1081+
} else {
1082+
$query->where('c.GroupID is null');
1083+
}
1084+
}
1085+
1086+
$query->where('DisplayAs <>', 'Heading')
1087+
->where('ParentCategoryID', $id);
1088+
1089+
$query->limit($limit, $offset)
10731090
->orderBy($orderFields, $orderDirection);
10741091

1075-
if ($filter) {
1092+
if ($filter && is_string($filter)) {
10761093
$query->like('Name', $filter);
10771094
}
10781095

@@ -1082,6 +1099,43 @@ public function getTreeAsFlat($id, $offset = null, $limit = null, $filter = null
10821099
return $categories;
10831100
}
10841101

1102+
/**
1103+
* FIX: https://github.com/topcoder-platform/forums/issues/422
1104+
* @param int|string $id The parent category ID or slug.
1105+
* @param string|null $filter Restrict results to only those with names matching this value, if provided.
1106+
* @return int
1107+
*
1108+
*/
1109+
public function countOfCategories($id, $filter = null) {
1110+
$query = $this->SQL
1111+
->select('c.CategoryID', 'count', 'Count')
1112+
->from('Category c');
1113+
1114+
if (!val('isAdmin', $filter, false)) {
1115+
if (val('UserID', $filter, false)) {
1116+
$userID = val('UserID', $filter);
1117+
$query->
1118+
leftJoin('UserGroup ug', 'c.GroupID = ug.GroupID')
1119+
->beginWhereGroup()
1120+
->where('c.GroupID is null')
1121+
->orWhere('ug.UserID', $userID)
1122+
->endWhereGroup();
1123+
} else {
1124+
$query->where('c.GroupID is null');
1125+
}
1126+
}
1127+
1128+
$query->where('DisplayAs <>', 'Heading')
1129+
->where('ParentCategoryID', $id);
1130+
1131+
if ($filter && is_string($filter)) {
1132+
$query->like('Name', $filter);
1133+
}
1134+
$count = $query->get()
1135+
->firstRow()->Count;
1136+
return $count;
1137+
}
1138+
10851139
/**
10861140
* Recursively remove children from categories configured to display as "Categories" or "Flat".
10871141
*
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php if (!defined('APPLICATION')) exit();
2+
if (!function_exists('GetOptions')) {
3+
include $this->fetchViewLocation('helper_functions', 'categories');
4+
}
5+
$userID = Gdn::session()->UserID;
6+
$categoryID = $this->Category->CategoryID;
7+
?>
8+
9+
<h1 class="H HomepageTitle"><?php echo $this->data('Title'); ?></h1>
10+
11+
<?php
12+
//if ($description = $this->description()) {
13+
// echo wrap($description, 'div', ['class' => 'P PageDescription']);
14+
//}
15+
$this->fireEvent('AfterPageTitle');
16+
17+
$categories = $this->data('CategoryTree');
18+
$this->EventArguments['NumRows'] = count($categories);
19+
?>
20+
21+
<h2 class="sr-only"><?php echo t('Category List'); ?></h2>
22+
<div class="PageControls Top">
23+
<?php
24+
$PagerOptions = ['Wrapper' => '<span class="PagerNub">&#160;</span><div %1$s>%2$s</div>', 'RecordCount' => $this->data('_RecordCount'), 'CurrentRecords' => $this->data('_CurrentRecords')];
25+
26+
PagerModule::write($PagerOptions);
27+
?>
28+
</div>
29+
<ul class="DataList CategoryList">
30+
<?php
31+
foreach ($categories as $category) {
32+
$this->EventArguments['Category'] = &$category;
33+
$this->fireEvent('BeforeCategoryItem');
34+
35+
writeListItem($category, 1);
36+
}
37+
?>
38+
</ul>
39+
40+
<div class="PageControls Bottom">
41+
<?php PagerModule::write($PagerOptions); ?>
42+
</div>

0 commit comments

Comments
 (0)