Skip to content

Commit 433b5e6

Browse files
committed
feat(api): added configuration for only active categories in API (#3876)
1 parent 77c3da2 commit 433b5e6

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

phpmyfaq/src/phpMyFAQ/Category.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ public function getCategoriesPaginated(
306306
int $offset = 0,
307307
string $sortField = 'id',
308308
string $sortOrder = 'ASC',
309+
bool $activeOnly = false,
309310
): array {
310311
$categories = [];
311312
$rows = $this->getCategoryRepository()->findCategoriesPaginated(
@@ -314,6 +315,7 @@ public function getCategoriesPaginated(
314315
$offset,
315316
$sortField,
316317
$sortOrder,
318+
$activeOnly,
317319
);
318320
foreach ($rows as $id => $row) {
319321
$categories[$id] = $row
@@ -328,11 +330,12 @@ public function getCategoriesPaginated(
328330
/**
329331
* Counts total categories for current language.
330332
*
333+
* @param bool $activeOnly Only count active categories
331334
* @return int Total count
332335
*/
333-
public function countCategories(): int
336+
public function countCategories(bool $activeOnly = false): int
334337
{
335-
return $this->getCategoryRepository()->countCategories($this->language);
338+
return $this->getCategoryRepository()->countCategories($this->language, $activeOnly);
336339
}
337340

338341
/**

phpmyfaq/src/phpMyFAQ/Category/CategoryRepository.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

phpmyfaq/src/phpMyFAQ/Controller/Api/CategoryController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ public function list(): JsonResponse
137137
$category->setGroups($currentGroups);
138138
$category->setLanguage($currentLanguage);
139139

140+
$onlyActive = (bool) $this->configuration->get('api.onlyActiveCategories');
141+
140142
// Get pagination and sorting parameters
141143
$pagination = $this->getPaginationRequest();
142144
$sort = $this->getSortRequest(
@@ -151,10 +153,11 @@ public function list(): JsonResponse
151153
offset: $pagination->offset,
152154
sortField: $sort->getField() ?? 'id',
153155
sortOrder: $sort->getOrderSql(),
156+
activeOnly: $onlyActive,
154157
);
155158

156159
// Get total count
157-
$total = $category->countCategories();
160+
$total = $category->countCategories(activeOnly: $onlyActive);
158161

159162
return $this->paginatedResponse(
160163
data: array_values($categories),

0 commit comments

Comments
 (0)