@@ -94,119 +94,74 @@ public static function getCategoryTree($rootId = 0, $helpdeskHome = false) {
9494 ]);
9595
9696 // Selects categories containing forms or sub-categories
97- // $subQuery = [];
98- // $subQuery[] = new QuerySubQuery([
99- // 'COUNT' => 'count',
100- // 'FROM' => $form_table,
101- // 'WHERE' => [
102- // "AND" => [
103- // "$form_table.is_active" => '1',
104- // "$form_table.is_deleted" => '0',
105- // "$form_table.plugin_formcreator_categories_id" => $cat_table."id",
106- // "$form_table.language" => [$_SESSION['glpilanguage'], '', '0', null],
107- // 'OR' => [
108- // 'access_rights' => ['!=', PluginFormcreatorForm::ACCESS_RESTRICTED],
109- // "$form_table.id" => new QuerySubQuery([
110- // 'SELECT' => 'plugin_formcreator_forms_id',
111- // 'FROM' => $table_fp,
112- // 'WHERE' => [
113- // 'profiles_id' => $_SESSION['glpiactiveprofile']['id']
114- // ]
115- // ])
116- // ]
117- // ]
118- // + $dbUtils->getEntitiesRestrictCriteria($form_table, "", "", true, false)
119- // + ($helpdeskHome ? ["$form_table.helpdesk_home" => '1'] : [])
120- // ],
121- // ]);
122- // $subQuery[] = new QuerySubQuery([
123- // 'COUNT' => 'count',
124- // 'FROM' => "$cat_table AS cat2",
125- // 'WHERE' => [
126- // new QueryExpression("`cat2`.`plugin_formcreator_categories_id`=`$cat_table`.`id`"),
127- // ],
128- // ]);
129- // $subQuery[] = new QuerySubQuery([
130- // 'COUNT' => 'count',
131- // 'FROM' => "$cat_table AS cat2",
132- // 'WHERE' => [
133- // new QueryExpression("`cat2`.`plugin_formcreator_categories_id`=`$cat_table`.`id`"),
134- // ],
135- // ]);
13697 // TODO: To solve the 3rd OR for faq count
137- $ where = "(SELECT COUNT( $ form_table.id)
138- FROM $ form_table
139- WHERE $ form_table.`plugin_formcreator_categories_id` = $ cat_table.`id`
140- AND $ form_table.`is_active` = 1
141- AND $ form_table.`is_deleted` = 0
142- $ helpdesk
143- AND $ form_table.`language` IN (' " .$ _SESSION ['glpilanguage ' ]."', '', NULL, '0')
144- AND " .getEntitiesRestrictRequest ("" , $ form_table , "" , "" , true , false )."
145- AND ( $ form_table.`access_rights` != " .PluginFormcreatorForm::ACCESS_RESTRICTED ." OR $ form_table.`id` IN (
146- SELECT plugin_formcreator_forms_id
147- FROM $ table_fp
148- WHERE profiles_id = " .$ _SESSION ['glpiactiveprofile ' ]['id ' ]."))
149- ) > 0
150- OR (SELECT COUNT(*)
151- FROM ` $ cat_table` AS `cat2`
152- WHERE `cat2`.`plugin_formcreator_categories_id`=` $ cat_table`.`id`
153- ) > 0
154- OR (SELECT COUNT(*)
155- FROM ( $ query_faqs) AS `faqs`
156- WHERE `faqs`.`knowbaseitemcategories_id` = ` $ cat_table`.`knowbaseitemcategories_id`
157- AND `faqs`.`knowbaseitemcategories_id` <> '0'
158- ) > 0 " ;
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 " ;
159123
160- if ($ rootId == 0 ) {
161- $ query = "SELECT *
162- FROM $ cat_table
163- WHERE `level` = '1'
164- AND ( $ where)
165- ORDER BY `name` " ;
166- $ name = '' ;
167- $ parent = 0 ;
168- } else {
169- $ query = "SELECT *
170- FROM $ cat_table
171- WHERE `plugin_formcreator_categories_id` = ' $ rootId'
172- AND ( $ where)
173- ORDER BY `name` " ;
174- $ formCategory = new self ();
175- $ formCategory ->getFromDB ($ rootId );
176- $ name = $ formCategory ->getField ('name ' );
177- $ parent = $ formCategory ->getField ('plugin_formcreator_categories_id ' );
178- }
179-
180- $ items = [];
124+ $ categories = [];
181125 if ($ result = $ DB ->query ($ query )) {
182- if ($ DB ->numrows ($ result )) {
183- while ($ item = $ DB ->fetch_assoc ($ result )) {
184- $ items [$ item ['id ' ]] = $ item ;
185- }
126+ while ($ category = $ DB ->fetch_assoc ($ result )) {
127+ $ categories [$ category ['id ' ]] = $ category ;
186128 }
187129 }
188130
189- // No sub-categories, then return
190- if (count ($ items ) == 0 ) {
191- return [
192- 'name ' => $ name ,
193- 'parent ' => $ parent ,
194- 'id ' => $ rootId ,
195- 'subcategories ' => new stdClass ()
196- ];
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+ if (empty ($ children ) && 0 == $ category ['items_count ' ]) {
141+ unset($ categories [$ index ]);
142+ continue ;
143+ }
144+ $ categories [$ index ]['subcategories ' ] = [];
197145 }
198146
199- // Generate sub categories
200- $ children = [
201- 'name ' => $ name ,
202- 'parent ' => $ parent ,
203- 'id ' => $ rootId ,
204- 'subcategories ' => []
147+ // Create root node
148+ $ nodes = [
149+ 'name ' => '' ,
150+ 'id ' => 0 ,
151+ 'parent ' => 0 ,
152+ 'subcategories ' => [],
153+ ];
154+ $ flat = [
155+ 0 => &$ nodes ,
205156 ];
206- foreach ($ items as $ categoryItem ) {
207- $ children ['subcategories ' ][] = self ::getCategoryTree ($ categoryItem ['id ' ]);
157+
158+ // Build from root node to leaves
159+ $ categories = array_reverse ($ categories );
160+ foreach ($ categories as $ item ) {
161+ $ flat [$ item ['id ' ]] = $ item ;
162+ $ flat [$ item ['parent ' ]]['subcategories ' ][] = &$ flat [$ item ['id ' ]];
208163 }
209164
210- return $ children ;
165+ return $ nodes ;
211166 }
212167}
0 commit comments