Select Git revision
StatutService.php

Antony Le Courtes authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
StatutService.php 4.09 KiB
<?php
namespace Intervenant\Service;
use Application\Service\AbstractEntityService;
use Intervenant\Entity\Db\Statut;
use Doctrine\ORM\QueryBuilder;
use UnicaenApp\Traits\SessionContainerTrait;
/**
* Description of Statut
*
* @author Laurent LÉCLUSE <laurent.lecluse at unicaen.fr>
*/
class StatutService extends AbstractEntityService
{
use SessionContainerTrait;
private array $statuts = [];
/**
* retourne la classe des entités
*
* @return string
* @throws RuntimeException
*/
public function getEntityClass()
{
return Statut::class;
}
/**
* Retourne l'alias d'entité courante
*
* @return string
*/
public function getAlias()
{
return 'statut';
}
/**
* @param string $code
*
* @return ?Statut
*/
public function getByCode(string $code): ?Statut
{
return $this->getRepo()->findOneBy(['code' => $code, 'annee' => $this->getServiceContext()->getAnnee()]);
}
/**
* @return Statut
*/
public function getAutres()
{
return $this->getByCode(Statut::CODE_AUTRES);
}
/**
* @return Statut[]
*/
public function getStatuts(): array
{
if (empty($this->statuts)) {
$dql = "SELECT s FROM " . Statut::class . " s WHERE s.annee = :annee AND s.histoDestruction IS NULL ORDER BY s.ordre";
$query = $this->getEntityManager()->createQuery($dql)->setParameter('annee', $this->getServiceContext()->getAnnee());
$this->statuts = $query->getResult();
}
return $this->statuts;
}
public function getStatutSelectable(Statut $statut, QueryBuilder $qb = null, $alias = null)
{
[$qb, $alias] = $this->initQuery($qb, $alias);
$qb->andWhere("$alias.dossierSelectionnable = 1");
$qb->andWhere("$alias.annee = " . $this->getServiceContext()->getAnnee()->getId());
$entities = $qb->getQuery()->execute();
$qb->addOrderBy("$alias.code");
var_dump($qb->getQuery()->getSql());
$result = [];
$entityClass = $this->getEntityClass();
foreach ($entities as $entity) {
if ($entity instanceof $entityClass) {
/**
* @var Statut $entity
*/
//Je prends le statut si il n'est pas détruit ou si l'intervenant a ce statut
if (is_null($entity->getHistoDestruction()) ||
$statut->getCode() == $entity->getCode()) {
$result[] = $entity;
}
}
}
return $result;
}
/**
* @param QueryBuilder|null $qb
* @param null $alias
*
* @return QueryBuilder
*/
public function orderBy(QueryBuilder $qb = null, $alias = null)
{
[$qb, $alias] = $this->initQuery($qb, $alias);
$qb->addOrderBy("$alias.ordre");
return $qb;
}
public function fetchMaxOrdre(): int
{
$sql = 'SELECT MAX(ordre) max_ordre FROM statut WHERE histo_destruction IS NULL';
return (int)$this->getEntityManager()->getConnection()->fetchOne($sql);
}
/**
* Retourne une nouvelle entité, initialisée avec les bons paramètres
*
*/
public function newEntity(): Statut
{
/** @var Statut $entity */
$statut = parent::newEntity();
$statut->setOrdre($this->fetchMaxOrdre() + 1);
return $statut;
}
public function delete($entity, $softDelete = true)
{
$this->getSessionContainer()->offsetUnset('privileges' . $entity->getAnnee()->getId());
return parent::delete($entity, $softDelete); // TODO: Change the autogenerated stub
}
/**
* @param Statut $entity
*
* @return mixed
*/
public function save($entity)
{
if (!$entity->getAnnee()) {
$entity->setAnnee($this->getServiceContext()->getAnnee());
}
$this->getSessionContainer()->offsetUnset('privileges' . $entity->getAnnee()->getId());
return parent::save($entity); // TODO: Change the autogenerated stub
}
}