Skip to content

Commit

Permalink
refactor: added controller to delete search terms, removed old code
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Oct 26, 2023
1 parent 55bffca commit 4c62539
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 77 deletions.
1 change: 1 addition & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ server {
rewrite admin/api/elasticsearch/drop /admin/api/index.php last;
rewrite admin/api/elasticsearch/import /admin/api/index.php last;
rewrite admin/api/elasticsearch/statistics /admin/api/index.php last;
rewrite admin/api/search/term /admin/api/index.php last;
rewrite admin/api/health-check /admin/api/index.php last;
rewrite admin/api/updates /admin/api/index.php last;
rewrite admin/api/update-check /admin/api/index.php last;
Expand Down
5 changes: 3 additions & 2 deletions phpmyfaq/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,13 @@ RewriteRule admin/api/content/attachments/upload admin/api/index.php
RewriteRule admin/api/content/comments admin/api/index.php
RewriteRule admin/api/content/images admin/api/index.php
RewriteRule admin/api/content/markdown admin/api/index.php
RewriteRule admin/api/dashboard/versions admin/api/index.php
RewriteRule admin/api/dashboard/visits admin/api/index.php
RewriteRule admin/api/elasticsearch/create admin/api/index.php
RewriteRule admin/api/elasticsearch/drop admin/api/index.php
RewriteRule admin/api/elasticsearch/import admin/api/index.php
RewriteRule admin/api/elasticsearch/statistics admin/api/index.php
RewriteRule admin/api/dashboard/versions admin/api/index.php
RewriteRule admin/api/dashboard/visits admin/api/index.php
RewriteRule admin/api/search/term admin/api/index.php
RewriteRule admin/api/health-check admin/api/index.php
RewriteRule admin/api/versions admin/api/index.php
RewriteRule admin/api/update-check admin/api/index.php
Expand Down
61 changes: 0 additions & 61 deletions phpmyfaq/admin/api/search.php

This file was deleted.

15 changes: 6 additions & 9 deletions phpmyfaq/admin/assets/src/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const handleStatistics = () => {
const csrf = event.target.getAttribute('data-csrf-token');

if (confirm('Are you sure?')) {
fetch('index.php?action=ajax&ajax=search&ajaxaction=delete_searchterm', {
fetch('./api/search/term', {
method: 'DELETE',
headers: {
Accept: 'application/json, text/plain, */*',
Expand All @@ -39,22 +39,19 @@ export const handleStatistics = () => {
}),
})
.then(async (response) => {
if (response.status === 200) {
if (response.ok) {
return response.json();
}
throw new Error('Network response was not ok.');
throw new Error('Network response was not ok: ', { cause: { response } });
})
.then((response) => {
const row = document.getElementById(`row-search-id-${response.deleted}`);
row.addEventListener('click', () => (row.style.opacity = '0'));
row.addEventListener('transitionend', () => row.remove());
})
.catch((error) => {
const table = document.querySelector('.table');
table.insertAdjacentElement(
'afterend',
addElement('div', { classList: 'alert alert-danger', innerText: error })
);
.catch(async (error) => {
const errorMessage = await error.cause.response.json();
console.error(errorMessage.error);
});
}
});
Expand Down
4 changes: 0 additions & 4 deletions phpmyfaq/admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,6 @@
case 'records':
require 'api/faqs.php';
break;
// Search
case 'search':
require 'api/search.php';
break;
// Users
case 'user':
require 'api/user.php';
Expand Down
2 changes: 1 addition & 1 deletion phpmyfaq/admin/stat.search.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
++$displayedCounter;

$num = round(($searchItem['number'] * 100 / $searchesCount), 2);
$csrfToken = Token::getInstance()->getTokenString('delete-searchterms');
$csrfToken = Token::getInstance()->getTokenString('delete-searchterm');
?>
<tr id="row-search-id-<?= $searchItem['id'] ?>">
<td><?= Strings::htmlspecialchars($searchItem['searchterm']) ?></td>
Expand Down
9 changes: 9 additions & 0 deletions phpmyfaq/src/admin-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use phpMyFAQ\Controller\Administration\ElasticsearchController;
use phpMyFAQ\Controller\Administration\ImageController;
use phpMyFAQ\Controller\Administration\MarkdownController;
use phpMyFAQ\Controller\Administration\SearchController;
use phpMyFAQ\Controller\Administration\UpdateController;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
Expand Down Expand Up @@ -111,6 +112,14 @@
new Route('/elasticsearch/statistics', ['_controller' => [ElasticsearchController::class, 'statistics']])
);

//
// Search API
//
$routes->add(
'admin.api.search.term',
new Route('/search/term', ['_controller' => [SearchController::class, 'deleteTerm'], '_methods' => 'DELETE'])
);

//
// Update API
//
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace phpMyFAQ\Controller\Administration;

use phpMyFAQ\Configuration;
use phpMyFAQ\Controller;
use phpMyFAQ\Filter;
use phpMyFAQ\Search;
use phpMyFAQ\Session\Token;
use phpMyFAQ\Translation;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class SearchController extends Controller
{
#[Route('admin/api/search/term')]
public function deleteTerm(Request $request): JsonResponse
{
$response = new JsonResponse();
$deleteData = json_decode($request->getContent());

$search = new Search(Configuration::getConfigurationInstance());

if (!Token::getInstance()->verifyToken('delete-searchterm', $deleteData->csrf)) {
$response->setStatusCode(Response::HTTP_BAD_REQUEST);
$response->setData(['error' => Translation::get('err_NotAuth')]);
return $response;
}

$searchId = Filter::filterVar($deleteData->searchTermId, FILTER_VALIDATE_INT);

if ($search->deleteSearchTermById($searchId)) {
$response->setStatusCode(Response::HTTP_OK);
$response->setData(['deleted' => $searchId]);
} else {
$response->setStatusCode(Response::HTTP_BAD_REQUEST);
$response->setData(['error' => $searchId]);
}

return $response;
}
}

0 comments on commit 4c62539

Please sign in to comment.