Skip to content
Snippets Groups Projects
Commit 95de8ab8 authored by Kevin Ditscheid's avatar Kevin Ditscheid
Browse files

[TASK] Provide a mapper for the new RouteEnhancers in 9LTS

parent 429b1969
No related branches found
No related tags found
1 merge request!12Feature upgrade to9 lts
<?php
namespace SGalinski\SgJobs\Routing\Aspect;
/**
*
* Copyright notice
*
* (c) sgalinski Internet Services (https://www.sgalinski.de)
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
*/
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Routing\Aspect\PersistedMappableAspectInterface;
use TYPO3\CMS\Core\Routing\Aspect\StaticMappableAspectInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Class JobAspect
*
* @package SGalinski\SgJobs\Routing\Aspect
* @author Kevin Ditscheid <kevin.ditscheid@sgalinski.de>
*/
class JobTitleMapper implements PersistedMappableAspectInterface, StaticMappableAspectInterface{
/**
* @param string $value
* @return string|null
* @throws \Exception
*/
public function generate(string $value): ?string {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_sgjobs_domain_model_job');
$job = $queryBuilder->select('job.uid', 'job.title', 'company.name', 'contact.street', 'contact.city', 'contact.zip', 'job.start_date', 'job.alternative_start_date')
->from('tx_sgjobs_domain_model_job', 'job')
->leftJoin('job', 'tx_sgjobs_domain_model_company', 'company', 'job.company=company.uid')
->leftJoin('job', 'tx_sgjobs_domain_model_contact', 'contact', 'job.contact=contact.uid')
->where(
$queryBuilder->expr()->eq('job.uid', $queryBuilder->createNamedParameter($value, \PDO::PARAM_INT))
)
->setMaxResults(1)
->execute()->fetch();
$jobtitle = $job['title'];
if ($job['name']) {
$jobtitle .= '-' . $job['name'];
} elseif ($job['street'] || $job['city'] || $job['zip']) {
$jobtitle .= '-' . $job['street'] . '_' . $job['city'] . '_' . $job['zip'];
}
if ($job['start_date']) {
/*
* because the DataHandler alters the timestamp by substracting the timezone bit from the timestamp,
* we need to add that part here to be consistent with it
* @see \TYPO3\CMS\Core\DataHandling\DataHandler->checkValue_input_Eval
*/
$date = $job['start_date'] + date('Z', $job['start_date']);
$jobtitle .= '-' . (new \DateTime('@' . $date))->format('dmY');
} elseif($job['alternative_start_date']) {
$jobtitle .= '-' . $job['alternative_start_date'];
}
return $jobtitle;
}
/**
* @param string $value
* @return string|null
*/
public function resolve(string $value): ?string {
if (!preg_match('/^(?P<title>.+)-(?P<company>.+)-(?P<startdate>.+)/', $value, $matches)) {
return null;
}
$values = $this->filterNamesKeys($matches);
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_sgjobs_domain_model_job');
$queryBuilder->select('job.uid')
->from('tx_sgjobs_domain_model_job', 'job')
->leftJoin('job', 'tx_sgjobs_domain_model_company', 'company', 'job.company=company.uid')
->leftJoin('job', 'tx_sgjobs_domain_model_contact', 'contact', 'job.contact=contact.uid')
->where(
$queryBuilder->expr()->like('job.title', $queryBuilder->createNamedParameter($values['title']))
)
->setMaxResults(1);
if ($values['startdate']) {
$date = \DateTime::createFromFormat('dmY', $values['startdate']);
$date->setTime(0,0);
$queryBuilder->andWhere(
$queryBuilder->expr()->eq('start_date', $queryBuilder->createNamedParameter($date->getTimestamp()))
);
}
if ($values['company']) {
$companyNameConstraint = $queryBuilder->expr()->like('company.name', $queryBuilder->createNamedParameter($values['company']));
$contact = explode('_', $values['company']);
if (\count($contact) === 3) {
$companyNameConstraint = $queryBuilder->expr()->andX(
$queryBuilder->expr()->like('contact.street', $queryBuilder->createNamedParameter($contact['street'])),
$queryBuilder->expr()->like('contact.city', $queryBuilder->createNamedParameter($contact['city'])),
$queryBuilder->expr()->like('contact.zip', $queryBuilder->createNamedParameter($contact['zip']))
);
}
$queryBuilder->andWhere($companyNameConstraint);
}
$result = $queryBuilder->execute()->fetch();
if (isset($result['uid'])) {
return (string)$result['uid'];
}
return null;
}
/**
* @param array $array
* @return array
*/
protected function filterNamesKeys(array $array): array
{
return array_filter(
$array,
function ($key) {
return !is_numeric($key);
},
ARRAY_FILTER_USE_KEY
);
}
}
......@@ -101,5 +101,8 @@ call_user_func(
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\SGalinski\SgJobs\Updates\DepartmentUpdateWizard::class]
= \SGalinski\SgJobs\Updates\DepartmentUpdateWizard::class;
if (version_compare(\TYPO3\CMS\Core\Utility\VersionNumberUtility::getCurrentTypo3Version(), '9.0.0', '>')) {
$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['aspects']['JobTitleMapper'] = \SGalinski\SgJobs\Routing\Aspect\JobTitleMapper::class;
}
}, 'sg_jobs'
);
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