Skip to content

Commit

Permalink
refactor: moved autocomplete code to controller
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Jul 29, 2023
1 parent 538e596 commit a264ca6
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 116 deletions.
5 changes: 4 additions & 1 deletion phpmyfaq/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ RewriteRule tags/([0-9]+)/([^\/]+)\.htm(l?)$ index.php?ac
RewriteRule admin/api/updates admin/api/updates.php
RewriteRule admin/api/update-check admin/api/updates.php

# Autocomplete API
RewriteRule api/autocomplete api/index.php

# REST API v2.0
# * http://[...]/api/v2.0/<ACTION>
RewriteRule api/v2.0/version api/index.php
Expand Down Expand Up @@ -213,6 +216,6 @@ RewriteRule api/v2.2/register api.php?action=register [L,QS

# REST API v2.3
# * http://[...]/api/v2.3/<ACTION>
RewriteRule api/v2.3/version api/index.php
RewriteRule api/v2.3/language api/index.php
RewriteRule api/v2.3/title api/index.php
RewriteRule api/v2.3/version api/index.php
114 changes: 0 additions & 114 deletions phpmyfaq/api.autocomplete.php

This file was deleted.

7 changes: 7 additions & 0 deletions phpmyfaq/api/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

use phpMyFAQ\Configuration;
use phpMyFAQ\Language;
use phpMyFAQ\Strings;
use phpMyFAQ\Translation;
use phpMyFAQ\User\CurrentUser;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -66,6 +68,11 @@
echo '<strong>Error:</strong> ' . $e->getMessage();
}

//
// Initializing static string wrapper
//
Strings::init($currentLanguage);

$request = Request::createFromGlobals();

$routes = include __DIR__ . '/../src/api-routes.php';
Expand Down
2 changes: 1 addition & 1 deletion phpmyfaq/assets/src/api/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/

export const fetchAutoCompleteData = async (searchString) => {
return await fetch(`api.autocomplete.php?search=${searchString}`, {
return await fetch(`api/autocomplete?search=${searchString}`, {
method: 'GET',
cache: 'no-cache',
headers: {
Expand Down
7 changes: 7 additions & 0 deletions phpmyfaq/src/api-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* @since 2023-07-29
*/

use phpMyFAQ\Api\Controller\AutoCompleteController;
use phpMyFAQ\Api\Controller\LanguageController;
use phpMyFAQ\Api\Controller\TitleController;
use phpMyFAQ\Api\Controller\VersionController;
Expand All @@ -38,4 +39,10 @@
new Route("v{$apiVersion}/title", ['_controller' => [TitleController::class, 'index']])
);


$routes->add(
'api.autocomplete',
new Route('autocomplete', ['_controller' => [AutoCompleteController::class, 'search']])
);

return $routes;
58 changes: 58 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Api/Controller/AutoCompleteController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace phpMyFAQ\Api\Controller;

use phpMyFAQ\Category;
use phpMyFAQ\Configuration;
use phpMyFAQ\Core\Exception;
use phpMyFAQ\Faq\FaqPermission;
use phpMyFAQ\Filter;
use phpMyFAQ\Helper\SearchHelper;
use phpMyFAQ\Language\Plurals;
use phpMyFAQ\Search;
use phpMyFAQ\Search\SearchResultSet;
use phpMyFAQ\User\CurrentUser;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class AutoCompleteController
{
public function search(Request $request): JsonResponse
{
$response = new JsonResponse();
$faqConfig = Configuration::getConfigurationInstance();

$searchString = Filter::filterVar($request->query->get('search'), FILTER_SANITIZE_SPECIAL_CHARS);

$user = CurrentUser::getCurrentUser($faqConfig);
[ $currentUser, $currentGroups ] = CurrentUser::getCurrentUserGroupId($user);

$category = new Category($faqConfig, $currentGroups);
$category->setUser($currentUser);
$category->setGroups($currentGroups);
$category->transform(0);
$category->buildCategoryTree();

$faqPermission = new FaqPermission($faqConfig);
$faqSearch = new Search($faqConfig);
$faqSearchResult = new SearchResultSet($user, $faqPermission, $faqConfig);

if (!is_null($searchString)) {
$faqSearch->setCategory($category);

$searchResult = $faqSearch->autoComplete($searchString);

$faqSearchResult->reviewResultSet($searchResult);

$faqSearchHelper = new SearchHelper($faqConfig);
$faqSearchHelper->setSearchTerm($searchString);
$faqSearchHelper->setCategory($category);
$faqSearchHelper->setPlurals(new Plurals());
$response->setData(Response::HTTP_OK);
$response->setData($faqSearchHelper->createAutoCompleteResult($faqSearchResult));
}

return $response;
}
}

0 comments on commit a264ca6

Please sign in to comment.