Commit b7c11596 authored by Stephane Bouvry's avatar Stephane Bouvry
Browse files

Merge branch 'activity-mots-cles-admin2' into starling

parents a477983e a67b2b4c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -64,6 +64,7 @@ return array(
                        'index',
                        'logs',
                        'messages',
                        'motscles',
                        'organizationRole',
                        'organizationRoleApi',
                        'organizationType',
+9 −0
Original line number Diff line number Diff line
@@ -2005,6 +2005,15 @@ administration:
        route: /discipline
        defaults:
          action: discipline

    motscles:
      type: segment
      may_terminate: true
      options:
        route: /motscles
        defaults:
          action: motscles

    tva:
      type: segment
      may_terminate: true
+127 −2
Original line number Diff line number Diff line
@@ -18,7 +18,10 @@ class ActivityMotsClesController extends AbstractOscarController implements UseL
        try {

            if ($this->getRequest()->getMethod() == "GET") {
                return $this->getMotsCles();
                $includeActivityCountQueryParam = $this->params()->fromQuery('include_activity_count');
                $includeActivityCount = $includeActivityCountQueryParam && $includeActivityCountQueryParam === "true";

                return $this->getMotsCles($includeActivityCount);
            }

            if ($this->getRequest()->getMethod() == "POST") {
@@ -27,6 +30,18 @@ class ActivityMotsClesController extends AbstractOscarController implements UseL
                if ($action_mot_cle_json->action == "create") {
                    return $this->createMotCle($action_mot_cle_json);
                }

                if ($action_mot_cle_json->action == "delete") {
                    return $this->deleteMotCle($action_mot_cle_json);
                }

                if ($action_mot_cle_json->action == "update") {
                    return $this->updateMotCle($action_mot_cle_json);
                }

                if ($action_mot_cle_json->action == "fusion") {
                    return $this->fusionnerMotsCles($action_mot_cle_json);
                }
            }

            $response = new Response();
@@ -42,18 +57,30 @@ class ActivityMotsClesController extends AbstractOscarController implements UseL
        }
    }

    private function getMotsCles() {
    private function getMotsCles($includeCount) {

        /** @var ActivityMotCleRepository $activityMotCleRepository */
        $activityMotCleRepository = $this->getEntityManager()->getRepository(ActivityMotCle::class);

        $motsCles = [];

        $tousLesMotsCles = [];
        if ($includeCount) {
            foreach ($activityMotCleRepository->getMotsClesCounted() as $motCle) {
                $motsCles[] = [
                    'id'    => $motCle['id'],
                    'label' => $motCle['label'],
                    'activity_count' => $motCle['activity_count'],
                ];
            }
        } else {
            foreach ($activityMotCleRepository->getAll() as $motCle) {
                $motsCles[] = [
                    'id'    => $motCle->getId(),
                    'label' => $motCle->getLabel(),
                ];
            }
        }

        $response = new JsonModel();
        $response->setVariables(['motscles' => $motsCles]);
@@ -75,4 +102,102 @@ class ActivityMotsClesController extends AbstractOscarController implements UseL
        return $response;
    }

    private function deleteMotCle($action_mot_cle_json) {

        $this->getLoggerService()->info("deleteMotCle");
        $this->getLoggerService()->info("by: " . $this->getCurrentPerson());

        $motCle = $this->getEntityManager()
            ->getRepository(ActivityMotCle::class)
            ->findOneBy(array('id' => $action_mot_cle_json->id));
        if (!$motCle) {
            throw new \Exception("Le mot clé n'existe pas. Veuillez actualiser la page.");
        }

        $this->getEntityManager()->remove($motCle);
        $this->getEntityManager()->flush();

        $response = new JsonModel();
        $response->setVariables(['id' => $action_mot_cle_json->id]);
        return $response;
    }

    private function updateMotCle($action_mot_cle_json) {

        $this->getLoggerService()->info("updateMotCle");
        $this->getLoggerService()->info("by: " . $this->getCurrentPerson());

        $motCle = $this->getEntityManager()
            ->getRepository(ActivityMotCle::class)
            ->findOneBy(array('id' => $action_mot_cle_json->id));
        if (!$motCle) {
            throw new \Exception("Le mot clé n'existe pas. Veuillez actualiser la page.");
        }

        $motCle->setLabel($action_mot_cle_json->label);
        $this->getEntityManager()->persist($motCle);
        $this->getEntityManager()->flush();

        $response = new JsonModel();
        $response->setVariables(['id' => $motCle->getId(), 'label' => $motCle->getLabel()]);
        return $response;
    }

    private function fusionnerMotsCles($action_mot_cle_json) {

        $this->getLoggerService()->info("fusionMotCle");
        $this->getLoggerService()->info("by: " . $this->getCurrentPerson());

        $targetId = $action_mot_cle_json->targetId;
        if (!$targetId || !is_numeric($targetId)) {
            throw new \Exception("Le paramètre targetId doit être un identifiant numérique.");
        }
        $motCleCible = $this->getEntityManager()
            ->getRepository(ActivityMotCle::class)
            ->findOneBy(array('id' => $targetId));
        if (!$motCleCible) {
            throw new \Exception("Le mot clé cible n'existe pas. Veuillez actualiser la page.");
        }

        $idsToMerge = $action_mot_cle_json->idsToMerge;
        if (!$idsToMerge || !is_array($idsToMerge) || count($idsToMerge) < 2) {
            throw new \Exception("Le paramètre idsToMerge doit être un tableau d'au moins deux identifiants numériques de mots clés.");
        }

        $motsClesASupprimer = [];
        foreach ($idsToMerge as $idToMerge) {
            if (!is_numeric($idToMerge)) {
                throw new \Exception("Le paramètre idsToMerge doit contenir des identifiants numériques de mots clés.");
            }
            if ($idToMerge == $targetId) {
                continue;
            }
            $motCleASupprimer = $this->getEntityManager()
                ->getRepository(ActivityMotCle::class)
                ->findOneBy(array('id' => $idToMerge));
            if (!$motCleASupprimer) {
                throw new \Exception("Le mot clé à supprimer n'existe pas. Veuillez actualiser la page.");
            }
            $motsClesASupprimer[] = $motCleASupprimer;
        }

        foreach ($motsClesASupprimer as $motCleASupprimer) {
            foreach ($motCleASupprimer->getActivities() as $activity) {
                if (!$activity->hasMotcle($motCleCible)) {
                    $activity->addMotCle($motCleCible);
                    $this->getEntityManager()->persist($activity);
                    $this->getEntityManager()->flush();
                }
            }
        }

        foreach ($motsClesASupprimer as $motCleASupprimer) {
            $this->getEntityManager()->remove($motCleASupprimer);
            $this->getEntityManager()->flush();
        }

        $response = new JsonModel();
        $response->setVariables([]);
        return $response;
    }
}
+4 −0
Original line number Diff line number Diff line
@@ -764,6 +764,10 @@ class AdministrationController extends AbstractOscarController implements UsePro
        return $datas;
    }

    public function motsclesAction()
    {
        return [];
    }

    /**
     * Reconstruction de l'index de recherche.
+10 −0
Original line number Diff line number Diff line
@@ -30,4 +30,14 @@ class ActivityMotCleRepository extends EntityRepository
        }
        return $out;
    }

    /**
     * Retourne la liste des mots clés avec le comptage des projets.
     */
    public function getMotsClesCounted() {
        $dql = "SELECT m.id, m.label, count(a.id) as activity_count FROM Oscar\Entity\ActivityMotCle m LEFT JOIN m.activities a GROUP BY m.id ORDER BY m.label";
        $query = $this->getEntityManager()->createQuery($dql);
        return $query->getArrayResult();
    }

}
Loading