Skip to content

Issues-422: Fixed sql query for Flat Tree #431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,16 @@ private function getCategoryTree($category = null, $displayAs = null, $recent =
$perPage = c('Vanilla.Categories.PerPage', 30);
$page = Gdn::request()->get('Page', Gdn::request()->get('page', null));
list($offset, $limit) = offsetLimit($page, $perPage);
$categoryTree = $this->CategoryModel->getTreeAsFlat($categoryIdentifier, $offset, $limit);

$filter = [];
if(Gdn::session()->isValid()) {
$filter['UserID'] = Gdn::session()->UserID;
$filter['isAdmin'] = Gdn::session()->User->Admin;
}
$categoryTree = $this->CategoryModel->getTreeAsFlat($categoryIdentifier, $offset, $limit,$filter, 'c.DateInserted', 'desc');
$countOfCategoryTree = $this->CategoryModel->countOfCategories($categoryIdentifier,$filter);
$this->setData('_Limit', $perPage);
$this->setData('_RecordCount', $countOfCategoryTree);
$this->setData('_CurrentRecords', count($categoryTree));
break;
case 'Categories':
Expand Down Expand Up @@ -616,23 +624,25 @@ public function all($Category = '', $displayAs = '') {
true
);
}

if($this->data('CategorySort')) {
if( $this->data('CategorySort') == self::SORT_OLDEST_POST) {
usort($categoryTree, function ($a, $b) {
return Gdn_Format::toTimestamp($a['LastDiscussionCommentsDate']) - Gdn_Format::toTimestamp($b['LastDiscussionCommentsDate']);
});

} else if( $this->data('CategorySort') == self::SORT_LAST_POST) {
usort($categoryTree, function ($a, $b) {
return Gdn_Format::toTimestamp($b['LastDiscussionCommentsDate']) - Gdn_Format::toTimestamp($a['LastDiscussionCommentsDate']);

// FIX: https://github.com/topcoder-platform/forums/issues/422
// Sorting for Flat type in SQL
if($displayAs != 'Flat') {
if ($this->data('CategorySort')) {
if ($this->data('CategorySort') == self::SORT_OLDEST_POST) {
usort($categoryTree, function ($a, $b) {
return Gdn_Format::toTimestamp($a['LastDiscussionCommentsDate']) - Gdn_Format::toTimestamp($b['LastDiscussionCommentsDate']);
});

} else if ($this->data('CategorySort') == self::SORT_LAST_POST) {
usort($categoryTree, function ($a, $b) {
return Gdn_Format::toTimestamp($b['LastDiscussionCommentsDate']) - Gdn_Format::toTimestamp($a['LastDiscussionCommentsDate']);
});
}
} else {
usort($categoryTree, function ($a, $b) { // desc
return Gdn_Format::toTimestamp($b['LastDiscussionCommentsDate']) - Gdn_Format::toTimestamp($a['LastDiscussionCommentsDate']);
});
}
} else {
usort($categoryTree, function ($a, $b) { // desc
return Gdn_Format::toTimestamp($b['LastDiscussionCommentsDate']) - Gdn_Format::toTimestamp($a['LastDiscussionCommentsDate']);
});
}

$this->setData('CategoryTree', $categoryTree);
Expand Down
68 changes: 61 additions & 7 deletions vanilla/applications/vanilla/models/class.categorymodel.php
Original file line number Diff line number Diff line change
Expand Up @@ -1064,15 +1064,32 @@ public function getTree($categoryID, array $options = []) {
* @param string $orderDirection
* @return array
*/
public function getTreeAsFlat($id, $offset = null, $limit = null, $filter = null, $orderFields = 'Name', $orderDirection = 'asc') {
$query = $this->SQL
->from('Category')
->where('DisplayAs <>', 'Heading')
->where('ParentCategoryID', $id)
->limit($limit, $offset)
public function getTreeAsFlat($id, $offset = null, $limit = null, $filter = null, $orderFields = 'Name', $orderDirection = 'asc')
{
$query = $this->SQL->from('Category c');

//FIX: https://github.com/topcoder-platform/forums/issues/422
if (!val('isAdmin', $filter, false)) {
if (val('UserID', $filter, false)) {
$userID = val('UserID', $filter);
$query->
leftJoin('UserGroup ug', 'c.GroupID = ug.GroupID')
->beginWhereGroup()
->where('c.GroupID is null')
->orWhere('ug.UserID', $userID)
->endWhereGroup();
} else {
$query->where('c.GroupID is null');
}
}

$query->where('DisplayAs <>', 'Heading')
->where('ParentCategoryID', $id);

$query->limit($limit, $offset)
->orderBy($orderFields, $orderDirection);

if ($filter) {
if ($filter && is_string($filter)) {
$query->like('Name', $filter);
}

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

/**
* FIX: https://github.com/topcoder-platform/forums/issues/422
* @param int|string $id The parent category ID or slug.
* @param string|null $filter Restrict results to only those with names matching this value, if provided.
* @return int
*
*/
public function countOfCategories($id, $filter = null) {
$query = $this->SQL
->select('c.CategoryID', 'count', 'Count')
->from('Category c');

if (!val('isAdmin', $filter, false)) {
if (val('UserID', $filter, false)) {
$userID = val('UserID', $filter);
$query->
leftJoin('UserGroup ug', 'c.GroupID = ug.GroupID')
->beginWhereGroup()
->where('c.GroupID is null')
->orWhere('ug.UserID', $userID)
->endWhereGroup();
} else {
$query->where('c.GroupID is null');
}
}

$query->where('DisplayAs <>', 'Heading')
->where('ParentCategoryID', $id);

if ($filter && is_string($filter)) {
$query->like('Name', $filter);
}
$count = $query->get()
->firstRow()->Count;
return $count;
}

/**
* Recursively remove children from categories configured to display as "Categories" or "Flat".
*
Expand Down
42 changes: 42 additions & 0 deletions vanilla/applications/vanilla/views/categories/flat_all.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php if (!defined('APPLICATION')) exit();
if (!function_exists('GetOptions')) {
include $this->fetchViewLocation('helper_functions', 'categories');
}
$userID = Gdn::session()->UserID;
$categoryID = $this->Category->CategoryID;
?>

<h1 class="H HomepageTitle"><?php echo $this->data('Title'); ?></h1>

<?php
//if ($description = $this->description()) {
// echo wrap($description, 'div', ['class' => 'P PageDescription']);
//}
$this->fireEvent('AfterPageTitle');

$categories = $this->data('CategoryTree');
$this->EventArguments['NumRows'] = count($categories);
?>

<h2 class="sr-only"><?php echo t('Category List'); ?></h2>
<div class="PageControls Top">
<?php
$PagerOptions = ['Wrapper' => '<span class="PagerNub">&#160;</span><div %1$s>%2$s</div>', 'RecordCount' => $this->data('_RecordCount'), 'CurrentRecords' => $this->data('_CurrentRecords')];

PagerModule::write($PagerOptions);
?>
</div>
<ul class="DataList CategoryList">
<?php
foreach ($categories as $category) {
$this->EventArguments['Category'] = &$category;
$this->fireEvent('BeforeCategoryItem');

writeListItem($category, 1);
}
?>
</ul>

<div class="PageControls Bottom">
<?php PagerModule::write($PagerOptions); ?>
</div>