Skip to content

Commit

Permalink
fix(wizard): form categories may show when they are empty
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Aug 22, 2019
1 parent 9a00c27 commit d3182ac
Showing 1 changed file with 58 additions and 103 deletions.
161 changes: 58 additions & 103 deletions inc/category.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,119 +94,74 @@ public static function getCategoryTree($rootId = 0, $helpdeskHome = false) {
]);

// Selects categories containing forms or sub-categories
// $subQuery = [];
// $subQuery[] = new QuerySubQuery([
// 'COUNT' => 'count',
// 'FROM' => $form_table,
// 'WHERE' => [
// "AND" => [
// "$form_table.is_active" => '1',
// "$form_table.is_deleted" => '0',
// "$form_table.plugin_formcreator_categories_id" => $cat_table."id",
// "$form_table.language" => [$_SESSION['glpilanguage'], '', '0', null],
// 'OR' => [
// 'access_rights' => ['!=', PluginFormcreatorForm::ACCESS_RESTRICTED],
// "$form_table.id" => new QuerySubQuery([
// 'SELECT' => 'plugin_formcreator_forms_id',
// 'FROM' => $table_fp,
// 'WHERE' => [
// 'profiles_id' => $_SESSION['glpiactiveprofile']['id']
// ]
// ])
// ]
// ]
// + $dbUtils->getEntitiesRestrictCriteria($form_table, "", "", true, false)
// + ($helpdeskHome ? ["$form_table.helpdesk_home" => '1'] : [])
// ],
// ]);
// $subQuery[] = new QuerySubQuery([
// 'COUNT' => 'count',
// 'FROM' => "$cat_table AS cat2",
// 'WHERE' => [
// new QueryExpression("`cat2`.`plugin_formcreator_categories_id`=`$cat_table`.`id`"),
// ],
// ]);
// $subQuery[] = new QuerySubQuery([
// 'COUNT' => 'count',
// 'FROM' => "$cat_table AS cat2",
// 'WHERE' => [
// new QueryExpression("`cat2`.`plugin_formcreator_categories_id`=`$cat_table`.`id`"),
// ],
// ]);
// TODO: To solve the 3rd OR for faq count
$where = "(SELECT COUNT($form_table.id)
FROM $form_table
WHERE $form_table.`plugin_formcreator_categories_id` = $cat_table.`id`
AND $form_table.`is_active` = 1
AND $form_table.`is_deleted` = 0
$helpdesk
AND $form_table.`language` IN ('".$_SESSION['glpilanguage']."', '', NULL, '0')
AND ".getEntitiesRestrictRequest("", $form_table, "", "", true, false)."
AND ($form_table.`access_rights` != ".PluginFormcreatorForm::ACCESS_RESTRICTED." OR $form_table.`id` IN (
SELECT plugin_formcreator_forms_id
FROM $table_fp
WHERE profiles_id = ".$_SESSION['glpiactiveprofile']['id']."))
) > 0
OR (SELECT COUNT(*)
FROM `$cat_table` AS `cat2`
WHERE `cat2`.`plugin_formcreator_categories_id`=`$cat_table`.`id`
) > 0
OR (SELECT COUNT(*)
FROM ($query_faqs) AS `faqs`
WHERE `faqs`.`knowbaseitemcategories_id` = `$cat_table`.`knowbaseitemcategories_id`
AND `faqs`.`knowbaseitemcategories_id` <> '0'
) > 0";
$categoryFk = self::getForeignKeyField();
$query = "SELECT `id`, `name`, `$categoryFk` as `parent`, `level`, (
(
SELECT COUNT($form_table.id)
FROM $form_table
WHERE $form_table.`plugin_formcreator_categories_id` = $cat_table.`id`
AND $form_table.`is_active` = 1
AND $form_table.`is_deleted` = 0
$helpdesk
AND $form_table.`language` IN ('".$_SESSION['glpilanguage']."', '', NULL, '0')
AND ".getEntitiesRestrictRequest("", $form_table, "", "", true, false)."
AND ($form_table.`access_rights` != ".PluginFormcreatorForm::ACCESS_RESTRICTED." OR $form_table.`id` IN (
SELECT plugin_formcreator_forms_id
FROM $table_fp
WHERE profiles_id = ".$_SESSION['glpiactiveprofile']['id']."))
)
+ (
SELECT COUNT(*)
FROM ($query_faqs) AS `faqs`
WHERE `faqs`.`knowbaseitemcategories_id` = `$cat_table`.`knowbaseitemcategories_id`
AND `faqs`.`knowbaseitemcategories_id` <> '0'
)
) as `items_count`
FROM $cat_table
ORDER BY `level` DESC, `name` DESC";

if ($rootId == 0) {
$query = "SELECT *
FROM $cat_table
WHERE `level` = '1'
AND ($where)
ORDER BY `name`";
$name = '';
$parent = 0;
} else {
$query = "SELECT *
FROM $cat_table
WHERE `plugin_formcreator_categories_id` = '$rootId'
AND ($where)
ORDER BY `name`";
$formCategory = new self();
$formCategory->getFromDB($rootId);
$name = $formCategory->getField('name');
$parent = $formCategory->getField('plugin_formcreator_categories_id');
}

$items = [];
$categories = [];
if ($result = $DB->query($query)) {
if ($DB->numrows($result)) {
while ($item = $DB->fetch_assoc($result)) {
$items[$item['id']] = $item;
}
while ($category = $DB->fetch_assoc($result)) {
$categories[$category['id']] = $category;
}
}

// No sub-categories, then return
if (count($items) == 0) {
return [
'name' => $name,
'parent' => $parent,
'id' => $rootId,
'subcategories' => new stdClass()
];
// Remove categories that have no items and no children
// Requires category list to be sorted by level DESC
foreach ($categories as $index => $category) {
$children = array_filter(
$categories,
function ($element) use ($category) {
return $category['id'] == $element['parent'];
}
);
if (empty($children) && 0 == $category['items_count']) {
unset($categories[$index]);
continue;
}
$categories[$index]['subcategories'] = [];
}

// Generate sub categories
$children = [
'name' => $name,
'parent' => $parent,
'id' => $rootId,
'subcategories' => []
// Create root node
$nodes = [
'name' => '',
'id' => 0,
'parent' => 0,
'subcategories' => [],
];
$flat = [
0 => &$nodes,
];
foreach ($items as $categoryItem) {
$children['subcategories'][] = self::getCategoryTree($categoryItem['id']);

// Build from root node to leaves
$categories = array_reverse($categories);
foreach ($categories as $item) {
$flat[$item['id']] = $item;
$flat[$item['parent']]['subcategories'][] = &$flat[$item['id']];
}

return $children;
return $nodes;
}
}

0 comments on commit d3182ac

Please sign in to comment.