Commit 94526a27 authored by Jean-Philippe Metivier's avatar Jean-Philippe Metivier
Browse files

Ajout de fonctions dans le service pour simplifier le controller

parent 619b7c2e
Loading
Loading
Loading
Loading
+6 −47
Changes for module/Application/src/Application/Controller/SubstitutionController.php: 6 added lines, 47 removed lines.
Original line number Diff line number Diff line
@@ -67,26 +67,7 @@ class SubstitutionController extends AbstractController
    public function creerAction()
    {
        $type = $this->params()->fromRoute('type');
        $structures = $this->structureService->getStructuresConcretes($type);

        /** Retrait des structures soient substituées soient substitutantes */
        $toRemove = [];
        /** @var StructureConcreteInterface $structure */
        foreach($structures as $structure) {
            if (count($structure->getStructure()->getStructuresSubstituees()) != 0) {
                $toRemove[] = $structure->getStructure();
                foreach ($structure->getStructure()->getStructuresSubstituees() as $sub) {
                    $toRemove[] = $sub;
                }

            }
        }
        /** @var Structure $remove */
        foreach($toRemove as $remove) {
            $structures = array_filter($structures, function (StructureConcreteInterface $structure) use ($remove) {
                return  $structure->getStructure()->getId() !== $remove->getId();
            });
        }
        $structures = $this->getStructureService()->getStructuresSubstituableByType($type);

        $request = $this->getRequest();
        if ($request->isPost()) {
@@ -104,16 +85,12 @@ class SubstitutionController extends AbstractController

            $sources = [];
            foreach($data['sourceIds'] as $sourceId) {
                $structure = $this->structureService->findStructureById($sourceId);
                $structureConcrete = $this->structureService->findStructureConcreteFromStructure($structure);
                if ($structureConcrete === null) {
                    throw new RuntimeException("Aucune structure concrète cible trouvée avec id=$sourceId.");
                }
                $structureConcrete = $this->getStructureService()->getStructuresConcreteByTypeAndStructureId($type, $sourceId);
                $sources[] = $structureConcrete;
            }

            //creation de la structureCible adequate
            $structureCibleDataObject = $this->structureService->createStructureConcrete($type);
            $structureCibleDataObject = $this->getStructureService()->createStructureConcrete($type);
            $this->structureService->updateFromPostData($structureCibleDataObject, $data['cible']);

            $structureCible = $this->structureService->createStructureSubstitutions($sources, $structureCibleDataObject);
@@ -136,7 +113,7 @@ class SubstitutionController extends AbstractController
        }

        $vm = new ViewModel([
            'title' => "Création d'une substitution",
            'title' => "Création d'une substitution (".$type.")",
            'cible' => $cible,
            'structuresConcretesSubstituees' => $structuresConcretesSubstituees,
            'structuresConcretes' => $structures,
@@ -159,26 +136,8 @@ class SubstitutionController extends AbstractController
            $structuresConcretesSubstituees[] = $structureConcreteSubstituee;
        }

        $structures = $this->structureService->getStructuresConcretes($structureCible->getTypeStructure()->getCode());

        /** Retrait des structures soient substituées soient substitutantes */
        $toRemove = [];
        /** @var StructureConcreteInterface $structure */
        foreach($structures as $structure) {
            if (count($structure->getStructure()->getStructuresSubstituees()) != 0) {
                $toRemove[] = $structure->getStructure();
                foreach ($structure->getStructure()->getStructuresSubstituees() as $sub) {
                    $toRemove[] = $sub;
                }

            }
        }
        /** @var Structure $remove */
        foreach($toRemove as $remove) {
            $structures = array_filter($structures, function (StructureConcreteInterface $structure) use ($remove) {
                return  $structure->getStructure()->getId() !== $remove->getId();
            });
        }
        $type=$structureCible->getTypeStructure();
        $structures = $this->getStructureService()->getStructuresSubstituableByType($type);

        $request = $this->getRequest();
        if ($request->isPost()) {
+11
Changes for module/Application/src/Application/Entity/Db/Mapping/Application.Entity.Db.Structure.dcm.xml: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -34,6 +34,17 @@
            </join-table>
        </many-to-many>

        <many-to-many field="structureSubstituante" target-entity="Application\Entity\Db\Structure">
            <join-table name="STRUCTURE_SUBSTIT">
                <join-columns>
                    <join-column name="FROM_STRUCTURE_ID" referenced-column-name="ID" />
                </join-columns>
                <inverse-join-columns>
                    <join-column name="TO_STRUCTURE_ID" referenced-column-name="ID" />
                </inverse-join-columns>
            </join-table>
        </many-to-many>

        <field name="sourceCode"    type="string" column="SOURCE_CODE" length="64" nullable="true"/>
        <field name="code"          type="string" column="CODE" length="64" nullable="true"/>

+62
Changes for module/Application/src/Application/Service/Structure/StructureService.php: 62 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -522,4 +522,66 @@ class StructureService extends BaseService
        return $result;
    }

    public function getEntityByType($type) {
        $entity = null;
        switch($type) {
            case TypeStructure::CODE_ECOLE_DOCTORALE :
            case 'École doctorale':
                $entity = EcoleDoctorale::class;
                break;
            case TypeStructure::CODE_UNITE_RECHERCHE :
            case 'Unité de recherche':
                $entity = UniteRecherche::class;
                break;
            case TypeStructure::CODE_ETABLISSEMENT :
            case 'Établissement':
                $entity = Etablissement::class;
                break;
            default :
                throw new RuntimeException('Type de structure inconnu ['.$type.']');
        }
        return $entity;
    }

    /**
     * @param TypeStructure $type
     * @return StructureConcreteInterface[]
     */
    public function getStructuresSubstituableByType($type)
    {
        $qb = $this->getEntityManager()->getRepository($this->getEntityByType($type))->createQueryBuilder('structureConcrete')
            ->join('structureConcrete.structure', 'structure')
            ->leftJoin('structure.structuresSubstituees', 'substitutionFrom')
            ->leftJoin('structure.structureSubstituante', 'substitutionTo')
            ->andWhere('substitutionFrom.id IS NULL')
            ->andWhere('substitutionTo.id IS NULL')
        ;

        $result = $qb->getQuery()->getResult();
        return $result;
    }

    /**
     * @param TypeStructure $type
     * @param int $structureId
     * @return StructureConcreteInterface
     */
    public function getStructuresConcreteByTypeAndStructureId($type, $structureId)
    {
        $qb = $this->getEntityManager()->getRepository($this->getEntityByType($type))->createQueryBuilder('structureConcrete')
            ->join('structureConcrete.structure', 'structure')
            ->andWhere('structure.id = :structureId')
            ->setParameter('structureId', $structureId)
        ;

        try {
            $result = $qb->getQuery()->getOneOrNullResult();
        } catch (NonUniqueResultException $e) {
            throw new RuntimeException("Plusieurs ".$type." partagent le même identifiant de structure [".$structureId."]");
        }

        if (!$result) throw new RuntimeException("Aucun(e) ".$type." de trouvé(e).");
        return $result;
    }

}
 No newline at end of file