@@ -183,23 +183,33 @@ public function findCategoriesPaginated(
183183 int $ offset = 0 ,
184184 string $ sortField = 'id ' ,
185185 string $ sortOrder = 'ASC ' ,
186+ bool $ activeOnly = false ,
186187 ): array {
187188 $ categories = [];
188- $ prefix = Database::getTablePrefix ();
189189
190- // Whitelist validation for sort field
190+ // Whitelist validation for the sort field
191191 $ allowedSortFields = ['id ' , 'name ' , 'parent_id ' , 'active ' ];
192192 if (!in_array ($ sortField , $ allowedSortFields , strict: true )) {
193193 $ sortField = 'id ' ;
194194 }
195195
196196 $ query = sprintf (
197197 'SELECT id, lang, parent_id, name, description, user_id, group_id, active, show_home, image FROM %sfaqcategories ' ,
198- $ prefix ,
198+ Database:: getTablePrefix () ,
199199 );
200200
201+ $ whereConditions = [];
202+
201203 if ($ language !== null && preg_match (pattern: '/^[a-z\-]{2,}$/ ' , subject: $ language )) {
202- $ query .= " WHERE lang = ' " . $ this ->configuration ->getDb ()->escape ($ language ) . "' " ;
204+ $ whereConditions [] = "lang = ' " . $ this ->configuration ->getDb ()->escape ($ language ) . "' " ;
205+ }
206+
207+ if ($ activeOnly ) {
208+ $ whereConditions [] = 'active = 1 ' ;
209+ }
210+
211+ if (!empty ($ whereConditions )) {
212+ $ query .= ' WHERE ' . implode (' AND ' , $ whereConditions );
203213 }
204214
205215 $ query .= sprintf (' ORDER BY %s %s LIMIT %d OFFSET %d ' , $ sortField , $ sortOrder , $ limit , $ offset );
@@ -220,14 +230,25 @@ public function findCategoriesPaginated(
220230 * Count total categories for a language.
221231 *
222232 * @param string|null $language Language code filter
233+ * @param bool $activeOnly Only count active categories
223234 * @return int Total count
224235 */
225- public function countCategories (?string $ language = null ): int
236+ public function countCategories (?string $ language = null , bool $ activeOnly = false ): int
226237 {
227238 $ query = sprintf ('SELECT COUNT(*) as total FROM %sfaqcategories ' , Database::getTablePrefix ());
228239
240+ $ whereConditions = [];
241+
229242 if ($ language !== null && preg_match (pattern: '/^[a-z\-]{2,}$/ ' , subject: $ language )) {
230- $ query .= " WHERE lang = ' " . $ this ->configuration ->getDb ()->escape ($ language ) . "' " ;
243+ $ whereConditions [] = "lang = ' " . $ this ->configuration ->getDb ()->escape ($ language ) . "' " ;
244+ }
245+
246+ if ($ activeOnly ) {
247+ $ whereConditions [] = 'active = 1 ' ;
248+ }
249+
250+ if (!empty ($ whereConditions )) {
251+ $ query .= ' WHERE ' . implode (' AND ' , $ whereConditions );
231252 }
232253
233254 $ result = $ this ->configuration ->getDb ()->query ($ query );
0 commit comments