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