Skip to content

Commit 37edabf

Browse files
committed
fix(wizard): form categories may show when they are empty
rewrite of the selection of the category for display based on KB categories
1 parent a41bbe4 commit 37edabf

File tree

1 file changed

+59
-65
lines changed

1 file changed

+59
-65
lines changed

inc/category.class.php

Lines changed: 59 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -95,80 +95,74 @@ public static function getCategoryTree($rootId = 0, $helpdeskHome = false) {
9595
'contains' => ''
9696
]);
9797

98-
// Selects categories containing forms or sub-categories
99-
$where = "(SELECT COUNT($form_table.id)
100-
FROM $form_table
101-
WHERE $form_table.`plugin_formcreator_categories_id` = $cat_table.`id`
102-
AND $form_table.`is_active` = 1
103-
AND $form_table.`is_deleted` = 0
104-
$helpdesk
105-
AND $form_table.`language` IN ('".$_SESSION['glpilanguage']."', '', NULL, '0')
106-
AND ".getEntitiesRestrictRequest("", $form_table, "", "", true, false)."
107-
AND ($form_table.`access_rights` != ".PluginFormcreatorForm::ACCESS_RESTRICTED." OR $form_table.`id` IN (
108-
SELECT plugin_formcreator_forms_id
109-
FROM $table_fp
110-
WHERE profiles_id = ".$_SESSION['glpiactiveprofile']['id']."))
111-
) > 0
112-
OR (SELECT COUNT(*)
113-
FROM `$cat_table` AS `cat2`
114-
WHERE `cat2`.`plugin_formcreator_categories_id`=`$cat_table`.`id`
115-
) > 0
116-
OR (SELECT COUNT(*)
117-
FROM ($query_faqs) AS `faqs`
118-
WHERE `faqs`.`knowbaseitemcategories_id` = `$cat_table`.`knowbaseitemcategories_id`
119-
AND `faqs`.`knowbaseitemcategories_id` <> '0'
120-
) > 0";
121-
122-
if ($rootId == 0) {
123-
$query = "SELECT *
124-
FROM $cat_table
125-
WHERE `level` = '1'
126-
AND ($where)
127-
ORDER BY `name`";
128-
$name = '';
129-
$parent = 0;
130-
} else {
131-
$query = "SELECT *
132-
FROM $cat_table
133-
WHERE `plugin_formcreator_categories_id` = '$rootId'
134-
AND ($where)
135-
ORDER BY `name`";
136-
$formCategory = new self();
137-
$formCategory->getFromDB($rootId);
138-
$name = $formCategory->getField('name');
139-
$parent = $formCategory->getField('plugin_formcreator_categories_id');
140-
}
98+
$categoryFk = self::getForeignKeyField();
99+
$query = "SELECT `id`, `name`, `$categoryFk` as `parent`, `level`, (
100+
(
101+
SELECT COUNT($form_table.id)
102+
FROM $form_table
103+
WHERE $form_table.`plugin_formcreator_categories_id` = $cat_table.`id`
104+
AND $form_table.`is_active` = 1
105+
AND $form_table.`is_deleted` = 0
106+
$helpdesk
107+
AND $form_table.`language` IN ('".$_SESSION['glpilanguage']."', '', NULL, '0')
108+
AND ".getEntitiesRestrictRequest("", $form_table, "", "", true, false)."
109+
AND ($form_table.`access_rights` != ".PluginFormcreatorForm::ACCESS_RESTRICTED." OR $form_table.`id` IN (
110+
SELECT plugin_formcreator_forms_id
111+
FROM $table_fp
112+
WHERE profiles_id = ".$_SESSION['glpiactiveprofile']['id']."))
113+
)
114+
+ (
115+
SELECT COUNT(*)
116+
FROM ($query_faqs) AS `faqs`
117+
WHERE `faqs`.`knowbaseitemcategories_id` = `$cat_table`.`knowbaseitemcategories_id`
118+
AND `faqs`.`knowbaseitemcategories_id` <> '0'
119+
)
120+
) as `items_count`
121+
FROM $cat_table
122+
ORDER BY `level` DESC, `name` DESC";
141123

142-
$items = [];
124+
$categories = [];
143125
if ($result = $DB->query($query)) {
144-
if ($DB->numrows($result)) {
145-
while ($item = $DB->fetch_assoc($result)) {
146-
$items[$item['id']] = $item;
147-
}
126+
while ($category = $DB->fetch_assoc($result)) {
127+
$categories[$category['id']] = $category;
148128
}
149129
}
150130

151-
// No sub-categories, then return
152-
if (count($items) == 0) {
153-
return [
154-
'name' => $name,
155-
'parent' => $parent,
156-
'id' => $rootId,
157-
'subcategories' => new stdClass()
158-
];
131+
// Remove categories that have no items and no children
132+
// Requires category list to be sorted by level DESC
133+
foreach ($categories as $index => $category) {
134+
$children = array_filter(
135+
$categories,
136+
function ($element) use ($category) {
137+
return $category['id'] == $element['parent'];
138+
}
139+
);
140+
141+
if (empty($children) && 0 == $category['items_count']) {
142+
unset($categories[$index]);
143+
continue;
144+
}
145+
$categories[$index]['subcategories'] = [];
159146
}
160147

161-
// Generate sub categories
162-
$children = [
163-
'name' => $name,
164-
'parent' => $parent,
165-
'id' => $rootId,
166-
'subcategories' => []
148+
// Create root node
149+
$nodes = [
150+
'name' => '',
151+
'id' => 0,
152+
'parent' => 0,
153+
'subcategories' => [],
154+
];
155+
$flat = [
156+
0 => &$nodes,
167157
];
168-
foreach ($items as $categoryItem) {
169-
$children['subcategories'][] = self::getCategoryTree($categoryItem['id']);
158+
159+
// Build from root node to leaves
160+
$categories = array_reverse($categories);
161+
foreach ($categories as $category) {
162+
$flat[$category['id']] = $category;
163+
$flat[$category['parent']]['subcategories'][] = &$flat[$category['id']];
170164
}
171165

172-
return $children;
166+
return $nodes;
173167
}
174168
}

0 commit comments

Comments
 (0)