Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filters and sorting on date of check in Report #106

Merged
merged 8 commits into from
Oct 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions Classes/BackendSession/BackendSession.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

/***
*
* This file is based on the "Backend Module" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* (c) 2016 Christian Fries <christian.fries@lst.team>
*
***/

namespace Sypets\Brofix\BackendSession;

use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;

class BackendSession
{
/**
* The backend session object
*
* @var BackendUserAuthentication
*/
protected $sessionObject;

/**
* Unique key to store data in the session.
* Overwrite this key in your initializeAction method.
*
* @var string
*/
protected $storageKey = 'filterKey';

public function __construct()
{
$this->sessionObject = $GLOBALS['BE_USER'];
}

public function setStorageKey(string $storageKey): void
{
$this->storageKey = $storageKey;
}

/**
* Store a value in the session
*
* @param string $key
* @param mixed $value
*/
public function store(string $key, $value): void
{
$sessionData = $this->sessionObject->getSessionData($this->storageKey);
$sessionData[$key] = $value;
$this->sessionObject->setAndSaveSessionData($this->storageKey, $sessionData);
}

/**
* Delete a value from the session
*
* @param string $key
*/
public function delete($key): void
{
$sessionData = $this->sessionObject->getSessionData($this->storageKey);
unset($sessionData[$key]);
$this->sessionObject->setAndSaveSessionData($this->storageKey, $sessionData);
}

/**
* Read a value from the session
*
* @param string $key
* @return mixed
*/
public function get(string $key)
{
$sessionData = $this->sessionObject->getSessionData($this->storageKey);
return isset($sessionData[$key]) ? $sessionData[$key] : null;
}
}
54 changes: 54 additions & 0 deletions Classes/Filter/Filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Sypets\Brofix\Filter;

class Filter
{
/**
* @var string
*/
protected $url_filtre = '';

/**
* @var string
*/
protected $uid_filtre = '';

/**
* @var string
*/
protected $title_filter = '';

// Getters and Setters

public function getUrlFilter(): string
{
return $this->url_filtre;
}
public function getUidFilter(): string
{
return $this->uid_filtre;
}

public function getTitleFilter(): string
{
return $this->title_filter;
}

public function setUrlFilter(string $url_filter): void
{
$this->url_filtre = $url_filter;
}

public function setUidFilter(string $uid_filter): void
{
$this->uid_filtre = $uid_filter;
}

public function setTitleFilter(string $title_filter): void
{
$this->title_filter = $title_filter;
}
}
23 changes: 22 additions & 1 deletion Classes/Repository/BrokenLinkRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Sypets\Brofix\CheckLinks\ExcludeLinkTarget;
use Sypets\Brofix\Filter\Filter;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Platform\PlatformInformation;
Expand Down Expand Up @@ -54,9 +55,10 @@ public function getMaxBindParameters(): int
* @param string[] $linkTypes Link types to validate
* @param array<string,array<string>> $searchFields
* @param array<array<string>> $orderBy
* @param Filter $filter
* @return mixed[]
*/
public function getBrokenLinks(array $pageList, array $linkTypes, array $searchFields, array $orderBy = []): array
public function getBrokenLinks(array $pageList, array $linkTypes, array $searchFields, array $orderBy = [], Filter $filter): array
{
$results = [];
$max = (int)($this->getMaxBindParameters() /2 - 4);
Expand Down Expand Up @@ -100,7 +102,26 @@ public function getBrokenLinks(array $pageList, array $linkTypes, array $searchF
$queryBuilder->expr()->neq('table_name', $queryBuilder->createNamedParameter('pages'))
)
)
)
// SQL Filter
->andWhere(
$queryBuilder->expr()->like(
self::TABLE . '.url',
$queryBuilder->createNamedParameter('%' . $queryBuilder->escapeLikeWildcards($filter->getUrlFilter()) . '%')
)
)
->andWhere(
$queryBuilder->expr()->like(
self::TABLE . '.headline',
$queryBuilder->createNamedParameter('%' . $queryBuilder->escapeLikeWildcards($filter->getTitleFilter()) . '%')
)
);
if ($filter->getUidFilter() != '') {
$queryBuilder->andWhere(
$queryBuilder->expr()->eq(self::TABLE . '.record_uid', $queryBuilder->createNamedParameter($filter->getUidFilter(), \PDO::PARAM_INT))
);
}

if ($orderBy !== []) {
$values = array_shift($orderBy);
if ($values && is_array($values) && count($values) === 2) {
Expand Down
62 changes: 57 additions & 5 deletions Classes/View/BrofixReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

namespace Sypets\Brofix\View;

use Sypets\Brofix\BackendSession\BackendSession;
use Sypets\Brofix\CheckLinks\ExcludeLinkTarget;
use Sypets\Brofix\Configuration\Configuration;
use Sypets\Brofix\Filter\Filter;
use Sypets\Brofix\LinkAnalyzer;
use Sypets\Brofix\Linktype\ErrorParams;
use Sypets\Brofix\Linktype\LinktypeInterface;
Expand Down Expand Up @@ -77,10 +79,17 @@ class BrofixReport
['field', 'DESC'],
],
'last_check' => [
['last_check', 'ASC']
['last_check', 'ASC'],
],
'last_check_reverse' => [
['last_check', 'DESC']
['last_check', 'DESC'],
],
// add by Mee
'last_check_url' => [
['last_check_url', 'ASC'],
],
'last_check_url_reverse' => [
['last_check_url', 'DESC'],
],
'url' => [
['link_type', 'ASC'],
Expand Down Expand Up @@ -148,6 +157,16 @@ class BrofixReport
*/
protected $linkAnalyzer;

/**
* @var BackendSession
*/
protected $backendSession;

/**
* @var Filter
*/
protected $filter;

/**
* @var array<string>
*/
Expand Down Expand Up @@ -266,14 +285,16 @@ public function __construct(
BrokenLinkRepository $brokenLinkRepository = null,
ExcludeLinkTarget $excludeLinkTarget = null,
Configuration $configuration = null,
FlashMessageService $flashMessageService = null
FlashMessageService $flashMessageService = null,
BackendSession $backendSession = null
) {
$this->brokenLinkRepository = $brokenLinkRepository ?: GeneralUtility::makeInstance(BrokenLinkRepository::class);
$this->pagesRepository = $pagesRepository ?: GeneralUtility::makeInstance(PagesRepository::class);
$this->excludeLinkTarget = $excludeLinkTarget ?: GeneralUtility::makeInstance(ExcludeLinkTarget::class);
$this->configuration = $configuration ?: GeneralUtility::makeInstance(Configuration::class);
$flashMessageService = $flashMessageService ?: GeneralUtility::makeInstance(FlashMessageService::class);
$this->defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
$this->backendSession = $backendSession ?: GeneralUtility::makeInstance(BackendSession::class);
}

/**
Expand Down Expand Up @@ -351,6 +372,26 @@ protected function getSettingsFromQueryParameters(): void

// get searchLevel (number of levels of pages to check / show results)
$depth = GeneralUtility::_GP('depth');

// store filter parameters in the Filter Object
$this->filter = new Filter();
$this->filter->setUidFilter(GeneralUtility::_GP('uid_searchFilter') ?? '');

$this->filter->setUrlFilter(GeneralUtility::_GP('url_searchFilter') ?? '');

$this->filter->setTitleFilter(GeneralUtility::_GP('title_searchFilter') ?? '');

// to prevent deleting session, when user sort the records
if (!is_null(GeneralUtility::_GP('url_searchFilter')) || !is_null(GeneralUtility::_GP('title_searchFilter')) || !is_null(GeneralUtility::_GP('uid_searchFilter'))) {
$this->backendSession->store('filterKey', $this->filter);
}

// create session, if it the first time
if (is_null($this->backendSession->get('filterKey'))) {
$this->backendSession->setStorageKey('filterKey');
$this->backendSession->store('filterKey', new Filter());
}

/**
* @var int $previousDepth
*/
Expand Down Expand Up @@ -586,11 +627,18 @@ protected function createViewForBrokenLinksTab(): StandaloneView
// @extensionScannerIgnoreLine problem with getRootLineIsHidden
$rootLineHidden = $this->pagesRepository->getRootLineIsHidden($this->pObj->pageinfo);
if ($this->id > 0 && (!$rootLineHidden || $this->configuration->isCheckHidden())) {
// build the search filter from the backendSession session
$searchFilter = new Filter();
$searchFilter->setUrlFilter($this->backendSession->get('filterKey')->getUrlFilter());
$searchFilter->setUidFilter($this->backendSession->get('filterKey')->getUidFilter());
$searchFilter->setTitleFilter($this->backendSession->get('filterKey')->getTitleFilter());

$brokenLinks = $this->brokenLinkRepository->getBrokenLinks(
$this->pageList,
$this->linkTypes,
$this->configuration->getSearchFields(),
self::ORDER_BY_VALUES[$this->orderBy] ?? []
self::ORDER_BY_VALUES[$this->orderBy] ?? [],
$searchFilter
);
if ($brokenLinks) {
$totalCount = count($brokenLinks);
Expand All @@ -607,6 +655,10 @@ protected function createViewForBrokenLinksTab(): StandaloneView
$this->pagination = null;
}
$view->assign('totalCount', $totalCount);
// send the search filters to the view
$view->assign('url_filter', $this->backendSession->get('filterKey')->getUrlFilter());
$view->assign('uid_filter', $this->backendSession->get('filterKey')->getUidFilter());
$view->assign('title_filter', $this->backendSession->get('filterKey')->getTitleFilter());
if ($this->id === 0) {
$this->createFlashMessagesForRootPage();
} elseif (empty($items)) {
Expand All @@ -626,6 +678,7 @@ protected function createViewForBrokenLinksTab(): StandaloneView

// Table header
$view->assign('tableHeader', $this->getVariablesForTableHeader($sortActions));

return $view;
}

Expand Down Expand Up @@ -933,7 +986,6 @@ protected function renderTableRow($table, array $row): array
$variables['freshness'] = 'fresh';
}
}

return $variables;
}

Expand Down
11 changes: 11 additions & 0 deletions Resources/Private/Language/Module/locallang.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<source>Check links done</source>
</trans-unit>

<!-- table header -->

<trans-unit id="list.tableHead.page" resname="list.tableHead.page">
<source>Page</source>
Expand Down Expand Up @@ -43,6 +44,16 @@
</trans-unit>


<!-- filter -->
<trans-unit id="list.filter.uid" resname="list.filter.uid">
<source>UID</source>
</trans-unit>
<trans-unit id="list.filter.url" resname="list.filter..url">
<source>URL</source>
</trans-unit>
<trans-unit id="list.filter.title" resname="list.filter.title">
<source>Title</source>
</trans-unit>

<trans-unit id="list.recheck.links.title" resname="list.recheck.links.title">
<source>Recheck links</source>
Expand Down
Loading