Skip to content
Snippets Groups Projects
Commit 3abc422b authored by Torsten Oppermann's avatar Torsten Oppermann
Browse files

[TASK] Refactored be filtering

parent 1ec49e8b
No related branches found
No related tags found
No related merge requests found
......@@ -77,11 +77,11 @@ class BackendController extends ActionController {
$this->docHeaderComponent->setMetaInformation($pageInfo);
BackendService::makeButtons($this->docHeaderComponent, $this->request);
$this->view->assign('docHeader', $this->docHeaderComponent->docHeaderContent());
// get all jobs
/** @var ObjectStorage $jobs */
$jobs = $this->jobRepository->findJobs($pageUid, $filters);
$totalJobCount = $jobs->count();
$totalJobCount = \count($this->jobRepository->findJobs($pageUid));
$this->view->assign('pages', BackendService::getPagesWithJobRecords());
$this->view->assign('pageUid', $pageUid);
......@@ -97,7 +97,8 @@ class BackendController extends ActionController {
/** @var Company $companies */
/** @var $companies Company[][] */
foreach ($companies as $company) {
$locationOptions[$company->getUid()] = $company->getCity();
$city = $company->getCity();
$locationOptions[$city] = $city;
}
$this->view->assign('locationOptions', $locationOptions);
$this->view->assign('filters', $filters);
......
......@@ -26,7 +26,10 @@ namespace SGalinski\SgJobs\Domain\Repository;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\DatabaseConnection;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface as ExtbaseQueryResultInterface;
......@@ -51,65 +54,74 @@ class JobRepository extends Repository {
/**
* Queries the job records based on filters (for the backend)
*
* @param int $pageUid
* @param int $recordPageId
* @param array $filters
* @param boolean $raw
* @param int $limit
* @param int $offset
* @return mixed
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
* @throws \InvalidArgumentException
*/
public function findJobs($pageUid, array $filters = [], $raw = FALSE, $limit = 0, $offset = 0) {
$query = $this->createQuery();
$querySettings = $query->getQuerySettings();
$querySettings->setStoragePageIds([$pageUid]);
$query->setQuerySettings($querySettings);
$query->setOrderings(
[
'sorting' => QueryInterface::ORDER_ASCENDING,
]
public function findJobs($recordPageId, array $filters = [], $limit = 0, $offset = 0) {
// get all company ids
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(
'tx_sgjobs_domain_model_company'
);
$constraints = [];
if (isset($filters['locations']) && \is_array($filters['locations']) && \count($filters['locations'])) {
$locationConstraints = [];
foreach ((array) $filters['locations'] as $location) {
if ((int) $location) {
$locationConstraints[] = $query->contains('location', $location);
}
}
if (\count($locationConstraints)) {
$constraints[] = $query->logicalOr($locationConstraints);
$statement = $queryBuilder->select('a.uid')
->from('tx_sgjobs_domain_model_job', 'a')
->join(
'a', 'tx_sgjobs_domain_model_company', 'b'
, $queryBuilder->expr()->eq('a.company', 'b.uid')
);
if ($filters['locations'] !== '' && $filters['locations'] !== NULL) {
foreach ($filters['locations'] as $location) {
$statement->andWhere(
$queryBuilder->expr()->eq(
'b.city', $queryBuilder->createNamedParameter($location)
)
);
}
}
if (isset($filters['search']) && $filters['search'] !== '') {
$searchConstraints = [];
$searchConstraints[] = $query->equals('uid', $filters['search']);
$searchConstraints[] = $query->like('title', '%' . $filters['search'] . '%');
$searchConstraints[] = $query->like('subtitle', '%' . $filters['search'] . '%');
$searchConstraints[] = $query->like('description', '%' . $filters['search'] . '%');
$constraints[] = $query->logicalOr($searchConstraints);
if ($filters['search'] !== '' && $filters['search'] !== NULL) {
$statement->andWhere(
$queryBuilder->expr()->eq(
'a.description', $queryBuilder->createNamedParameter($filters['search'])
)
);
}
if ($limit > 0) {
$query->setLimit($limit);
if ($filters['search'] !== '' && $filters['search'] !== NULL) {
$statement->andWhere(
$queryBuilder->expr()->eq(
'a.title', $queryBuilder->createNamedParameter($filters['search'])
)
);
}
if ($offset > 0) {
$query->setOffset($offset);
$statement->andWhere(
$queryBuilder->expr()->eq(
'b.pid', $queryBuilder->createNamedParameter($recordPageId)
)
);
$companies = $statement->execute()->fetchAll();
$result = [];
$result[0] = '';
foreach ($companies as $company) {
$result[$company['uid']] = $company['uid'];
}
if (\count($constraints) > 1) {
$query->matching($query->logicalAnd($constraints));
} elseif (\count($constraints)) {
$query->matching($constraints[0]);
// when filters are set, but there are no companies found, return an empty result
if (empty(!$filters) && empty($companies)) {
return $companies;
}
return $query->execute($raw);
// return the filtered jobs
return $this->findByJobIds($recordPageId, $result, $limit, $offset);
}
/**
......
......@@ -16,6 +16,13 @@
<f:then>
<f:if condition="{noRecords}">
<f:then>
<f:if condition="{locationOptions}">
<f:render partial="Filter" arguments="{filters: filters, locationOptions: locationOptions}" />
<f:render partial="CreateJob" arguments="{pageUid:pageUid}" />
<p>
<f:translate key="backend.noJobsMessage" />
</p>
</f:if>
<p>
</p>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment