Skip to content
Snippets Groups Projects
JobRepository.php 8.61 KiB
Newer Older
<?php

namespace SGalinski\SgJobs\Domain\Repository;

/***************************************************************
 *  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\Extbase\Object\ObjectManagerInterface;
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;

/**
 * Job Repository
 */
class JobRepository extends Repository {
	public const ORDER_BY_TITLE = 1;
	public const ORDER_BY_CRDATE = 2;
	public const ORDER_BY_SORTING = 0;
	/**
	 * @var bool $allowManualSorting
	 */
	public function __construct(ObjectManagerInterface $objectManager) {
		parent::__construct($objectManager);
		$querySettings = $objectManager->get(Typo3QuerySettings::class);
		$querySettings->setRespectStoragePage(TRUE);
		$this->setDefaultQuerySettings($querySettings);
	}

	/**
	 * @return bool
	 */
	public function isAllowManualSorting(): bool {
		return $this->allowManualSorting;
	}

	/**
	 * @param bool $allowManualSorting
	 */
	public function setAllowManualSorting(bool $allowManualSorting): void {
		$this->allowManualSorting = $allowManualSorting;
	}

	/**
	 * Queries the job records based on filters (for the backend)
	 *
	 * @param int $recordPageId
	 * @param array $filters
	 * @param int $limit
	 * @param int $offset
	 * @return mixed
	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
	 */
	public function findBackendJobs($recordPageId, array $filters = [], $limit = 0, $offset = 0) {
		$query = $this->createQuery();
		$query->setOrderings(
			[
				'sorting' => QueryInterface::ORDER_ASCENDING,
			]
		$query->getQuerySettings()->setStoragePageIds([$recordPageId]);
		$query->getQuerySettings()->setIgnoreEnableFields(TRUE);
		$constraints = [];
		if (isset($filters['locations'])) {
			if (\is_array($filters['locations'])) {
				$constraints[] = $query->in('company.city', $filters['locations']);
			} elseif ($filters['locations'] !== '') {
				$constraints[] = $query->equals('company.city', $filters['locations']);
			}
		if (isset($filters['search']) && $filters['search'] !== '') {
			$constraints[] = $query->like('title', '%' . $filters['search'] . '%');
		if ($limit > 0) {
			$query->setLimit($limit);
		}
		if ($offset > 0) {
			$query->setOffset($offset);
		}
		if (\count($constraints) > 1) {
			return $query->matching($query->logicalAnd($constraints))->execute();
			return $query->matching($constraints[0])->execute();
		return $query->execute();
	 * Returns a job filtered by company and page id
	 *
	 * @param array $jobIds
	 * @param int $limit
	 * @param int $offset
	 * @return QueryResultInterface
	 */
Stefan Galinski's avatar
Stefan Galinski committed
	public function findByJobIds(array $jobIds = [], $limit = 0, $offset = 0): ExtbaseQueryResultInterface {
		$query = $this->createQuery();
		$query->setOrderings(
			[
				'sorting' => QueryInterface::ORDER_ASCENDING,
			]
		);

		// Ignore enable fields in backend
		$querySettings = $query->getQuerySettings();
		$querySettings->setIgnoreEnableFields(TRUE);
		$storagePageIds = $query->getQuerySettings()->getStoragePageIds();
		if (empty($storagePageIds)) {
			// if no record storage page has been selected in the plugin, ignore it
			$querySettings->setRespectStoragePage(FALSE);
		}
		$this->setDefaultQuerySettings($querySettings);

			$companyConstraints = [];
			foreach ($jobIds as $jobId) {
				if ($jobId) {
					$companyConstraints[] = $query->equals('uid', $jobId);
				}
			}
			if (\count($companyConstraints)) {
				$constraints[] = $query->logicalOr($companyConstraints);
			}
		}

		if (\count($constraints)) {
			$query->matching($query->logicalAnd($constraints));
		}

		if ($limit > 0) {
			$query->setLimit($limit);
		}

		if ($offset > 0) {
			$query->setOffset($offset);
		}

		return $query->execute();
	}

	/**
	 * Find jobs matching the filter settings given
	 *
	 * @param array $filters
	 * @param int $limit
	 * @param int $offset
	 * @return ExtbaseQueryResultInterface
	 */
Stefan Galinski's avatar
Stefan Galinski committed
	public function findJobsByFilter(
Matthias Adrowski's avatar
Matthias Adrowski committed
		array $filters = [],
		$limit = 0,
		$offset = 0,
		$ordering = 0
	): ExtbaseQueryResultInterface {
		$query = $this->createQuery();
		$storagePageIds = $query->getQuerySettings()->getStoragePageIds();
		if (empty($storagePageIds)) {
			// if no record storage page has been selected in the plugin, ignore it
			$query->getQuerySettings()->setRespectStoragePage(FALSE);
		}

		if ($ordering === self::ORDER_BY_TITLE || (!$this->allowManualSorting && $ordering === self::ORDER_BY_SORTING)) {
			$query->setOrderings(
				[
					'title' => QueryInterface::ORDER_ASCENDING,
				]
			);
		}
		if ($ordering === self::ORDER_BY_CRDATE) {
			$query->setOrderings(
				[
					'crdate' => QueryInterface::ORDER_DESCENDING
		if ($ordering === self::ORDER_BY_SORTING && $this->allowManualSorting) {
			$query->setOrderings(
				[
					'sorting' => QueryInterface::ORDER_ASCENDING
				]
			);
		}

		$constraints = [];
		if ($filters['filterCountry'] !== '0' && $filters['filterCountry'] !== NULL) {
			$constraints[] = $query->equals('company.country', $filters['filterCountry']);
		}

		if ($filters['filterRemote'] !== '' && $filters['filterRemote'] !== NULL) {
			$constraints[] = $query->equals('telecommutePossible', TRUE);
		}

		if ($filters['filterLocation'] !== '0' && $filters['filterLocation'] !== NULL) {
			$constraints[] = $query->equals('company.city', $filters['filterLocation']);
		if ($filters['filterDepartment'] !== '0' && $filters['filterDepartment'] !== NULL) {
			$constraints[] = $query->equals('department', $filters['filterDepartment']);
		if ($filters['filterExperienceLevel'] !== '0' && $filters['filterExperienceLevel'] !== NULL) {
			$constraints[] = $query->equals('experienceLevel', $filters['filterExperienceLevel']);
		}

		if (\count($constraints)) {
			$query->matching($query->logicalAnd($constraints));
		}

		if ($limit > 0) {
			$query->setLimit($limit);
		}

		if ($offset > 0) {
			$query->setOffset($offset);
		}

		return $query->execute();
	}
	/**
	 * Gets the amount of jobs filtered by companies
	 *
	 * @param array $companies
	 * @return int
Stefan Galinski's avatar
Stefan Galinski committed
	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
	 */
	public function countAll(array $companies = []): int {
		$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);
		}

		if (\count($companies) !== 0) {
			$constraints[] = $query->in('company', $companies);
			$query->matching($query->logicalAnd($constraints));
		}
		$result = $query->execute();
		return $result->count();
	}

	/**
	 * Gets the featured jobs filtered by companies
	 *
	 * @param array $companies
	 * @param int $limit
	 * @return array|ExtbaseQueryResultInterface
Stefan Galinski's avatar
Stefan Galinski committed
	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
	public function findByFeaturedOffer(array $companies = [], int $limit = 3) {
		$query = $this->createQuery();

		$storagePageIds = $query->getQuerySettings()->getStoragePageIds();
			// if no record storage page has been selected in the plugin, ignore it
			$query->getQuerySettings()->setRespectStoragePage(FALSE);
		}

		if (\count($companies)) {
			$query->matching($query->in('company', $companies));
		$query->setOrderings(
			[
				'featured_offer' => QueryInterface::ORDER_DESCENDING
			]
		);

		$query->setLimit($limit);
		return $query->execute();