Skip to content
Snippets Groups Projects
Commit fe4a0db0 authored by Kevin von Spiczak's avatar Kevin von Spiczak
Browse files

[FEATURE] automatic related jobs

parent 526207dc
No related branches found
No related tags found
1 merge request!38Feature 4256 related jobs
......@@ -310,6 +310,11 @@ class JoblistController extends ActionController {
$headTagService->execute();
}
$this->view->assign('job', $job);
$enableAutomaticRelatedJobs = (bool) $this->settings['enableAutomaticRelatedJobs'];
if ($enableAutomaticRelatedJobs) {
$relatedJobs = $this->jobRepository->findRelated($job);
$this->view->assign('relatedJobs', $relatedJobs);
}
} else {
$storagePid = (int) $this->configurationManager->getConfiguration(
ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK
......
......@@ -26,12 +26,16 @@ namespace SGalinski\SgJobs\Domain\Repository;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use SGalinski\SgJobs\Domain\Model\Job;
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
use TYPO3\CMS\Extbase\Persistence\Generic\Query;
use TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser;
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface as ExtbaseQueryResultInterface;
use TYPO3\CMS\Extbase\Persistence\Repository;
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
/**
* Job Repository
......@@ -259,6 +263,94 @@ class JobRepository extends Repository {
return $query->execute();
}
/**
* Finds related jobs by company & department.
* Tries to find at least $minRelatedJobs by first combining all constraints via AND.
* If we cannot find enough related jobs with these constraints, we repeat the query but with fewer constraints.
* E.g. we can only find 1 record which has the same country AND department -> fill the remaining space with
* jobs, which have either the same country OR department.
*
* @param Job $job
* @param int $limit
* @return array
*/
public function findRelated(Job $job, int $limit = 0): array {
// the minimum amount of related jobs, we should try to find before removing constraints
$minRelatedJobs = 2;
$relatedJobs = [];
$query = $this->prepareRelatedJobsQuery($job, 0);
if ($limit > 0) {
$query->setLimit($limit);
}
$resultCount = $query->count();
// fill $relatedJobs with more related jobs, by repeating the query, but with fewer constraints
if ($resultCount < $minRelatedJobs) {
$queryResult = $query->execute()->toArray();
array_push($relatedJobs, ...$queryResult);
$query = $this->prepareRelatedJobsQuery($job, 1);
$queryResult = $query->execute()->toArray();
array_push($relatedJobs, ...$queryResult);
$relatedJobs = array_unique($relatedJobs);
$relatedJobsCount = count($relatedJobs);
if ($relatedJobsCount < $minRelatedJobs) {
$query = $this->prepareRelatedJobsQuery($job, 2);
$queryResult = $query->execute()->toArray();
array_push($relatedJobs, ...$queryResult);
$relatedJobs = array_unique($relatedJobs);
}
} else {
$relatedJobs = $query->execute()->toArray();
}
return $relatedJobs;
}
/**
* Helper function, to avoid duplicate code.
* Returns a query, to be used within findRelated()
*
* @param Job $job
* @param int $iteration
* @return QueryInterface
*/
protected function prepareRelatedJobsQuery(Job $job, int $iteration): QueryInterface {
$query = $this->createQuery();
$constraints = [];
$storagePageIds = $query->getQuerySettings()->getStoragePageIds();
if (empty($storagePageIds)) {
// if no record storage page has been selected in the plugin, ignore it
$query->getQuerySettings()->setRespectStoragePage(FALSE);
}
$company = $job->getCompany();
$department = $job->getDepartment();
// look for jobs, which have the same company AND department
if ($iteration === 0) {
if ($company !== NULL) {
$constraints[] = $query->equals('company', $company->getUid());
}
if ($department !== NULL) {
$constraints[] = $query->equals('department', $department->getUid());
}
} else if (($iteration === 1) && $company !== NULL) {
// look for jobs, which have the same company
$constraints[] = $query->equals('company', $company->getUid());
} else if (($iteration === 2) && $department !== NULL) {
// look for jobs, which have the same department
$constraints[] = $query->equals('department', $department->getUid());
}
if (\count($constraints)) {
$query->matching($query->logicalAnd($constraints));
}
return $query;
}
/**
* Gets the amount of jobs filtered by companies
*
......
......@@ -25,5 +25,7 @@ plugin.tx_sgjobs {
offersPage =
# cat=plugin.tx_sgjobs/other; type=int+; If set to 1, the application-form page will redirect to the overview if no jobId is passed
disallowUnsolicitedApplication = 0
# cat=plugin.tx_sgjobs/other; type=int+; If set to 1, the (automatic) output of related jobs (in regards to the location/department) is enabled
enableAutomaticRelatedJobs = 1
}
}
......@@ -25,6 +25,7 @@ plugin.tx_sgjobs {
privacyPolicyPage = {$plugin.tx_sgjobs.settings.privacyPolicyPage}
offersPage = {$plugin.tx_sgjobs.settings.offersPage}
disallowUnsolicitedApplication = {$plugin.tx_sgjobs.settings.disallowUnsolicitedApplication}
enableAutomaticRelatedJobs = {$plugin.tx_sgjobs.settings.enableAutomaticRelatedJobs}
}
features {
......
......@@ -649,6 +649,20 @@
</div>
</div>
</f:then>
<f:else>
<f:if condition="{settings.enableAutomaticRelatedJobs}">
<div class="row">
<div class="container">
<h3><f:translate key="frontend.apply.relatedJobs"/></h3>
<div class="row default-content-element equal-height-columns stretch-first-child">
<f:for each="{relatedJobs}" as="relatedJob">
<f:render partial="Teaser" arguments="{job: relatedJob, applyPageUid: settings.applyPage}"/>
</f:for>
</div>
</div>
</div>
</f:if>
</f:else>
</f:if>
</f:section>
......
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