Commit c11c68ac authored by Antony Le Courtes's avatar Antony Le Courtes
Browse files

Amélioration bouton next etape feuille de route

parent 3c64691d
Loading
Loading
Loading
Loading
+209 −209
Changes for module/Application/src/Service/WorkflowService.php: 209 added lines, 209 removed lines.
Original line number Diff line number Diff line
@@ -30,6 +30,33 @@ class WorkflowService extends AbstractService



    /**
     * @param WfEtapeService|WorkflowEtape|TblWorkflow|string $etape
     * @param Intervenant|null                                $intervenant
     * @param Structure|null                                  $structure
     *
     * @return WorkflowEtape
     */
    public function getPreviousAccessibleEtape ($etape, Intervenant $intervenant = null, Structure $structure = null)
    {
        [$etapeCode, $intervenant, $structure] = $this->prepareEtapeParams($etape, $intervenant, $structure);

        $fdr       = $this->getFeuilleDeRoute($intervenant, $structure);
        $isCurrent = false;
        foreach ($fdr as $etape) {
            if ($isCurrent && $etape->isAtteignable() && $etape->getUrl() && $this->isAllowed($etape)) {
                return $etape;
            }
            if ($etape->getEtape()->getCode() == $etapeCode) {
                $isCurrent = true;
            }
        }

        return null;
    }



    protected function prepareEtapeParams ($etape, Intervenant $intervenant = null, Structure $structure = null)
    {
        switch (true) {
@@ -74,128 +101,6 @@ class WorkflowService extends AbstractService



    /**
     * @param WfEtapeService|WorkflowEtape|TblWorkflow|string $etape
     * @param Intervenant|null                                $intervenant
     * @param Structure|null                                  $structure
     *
     * @return WorkflowEtape
     */
    public function getEtape($etape, Intervenant $intervenant = null, Structure $structure = null)
    {
        [$etapeCode, $intervenant, $structure] = $this->prepareEtapeParams($etape, $intervenant, $structure);

        $fdr = $this->getFeuilleDeRoute($intervenant, $structure);
        if ($fdr) {
            foreach ($fdr as $etape) {
                if ($etape->getEtape()->getCode() == $etapeCode) {
                    return $etape;
                }
            }
        }

        return null;
    }



    /**
     * @param WfEtapeService|WorkflowEtape|TblWorkflow|string $etape
     * @param Intervenant|null                                $intervenant
     * @param Structure|null                                  $structure
     *
     * @return WorkflowEtape
     */
    public function getNextEtape($etape, Intervenant $intervenant = null, Structure $structure = null)
    {
        [$etapeCode, $intervenant, $structure] = $this->prepareEtapeParams($etape, $intervenant, $structure);

        $fdr       = $this->getFeuilleDeRoute($intervenant, $structure);
        $isCurrent = false;
        foreach ($fdr as $etape) {
            if ($isCurrent) {
                return $etape;
            }
            if ($etape->getEtape()->getCode() == $etapeCode) {
                $isCurrent = true;
            }
        }

        return null;
    }



    /**
     * @param WfEtapeService|WorkflowEtape|TblWorkflow|string $etape
     * @param Intervenant|null                                $intervenant
     * @param Structure|null                                  $structure
     *
     * @return WorkflowEtape
     */
    public function getPreviousAccessibleEtape($etape, Intervenant $intervenant = null, Structure $structure = null)
    {
        [$etapeCode, $intervenant, $structure] = $this->prepareEtapeParams($etape, $intervenant, $structure);

        $fdr       = $this->getFeuilleDeRoute($intervenant, $structure);
        $isCurrent = false;
        foreach ($fdr as $etape) {
            if ($isCurrent && $etape->isAtteignable() && $etape->getUrl() && $this->isAllowed($etape)) {
                return $etape;
            }
            if ($etape->getEtape()->getCode() == $etapeCode) {
                $isCurrent = true;
            }
        }

        return null;
    }



    /**
     * @param WfEtapeService|WorkflowEtape|TblWorkflow|string $etape
     * @param Intervenant|null                                $intervenant
     * @param Structure|null                                  $structure
     *
     * @return WorkflowEtape
     */
    public function getNextAccessibleEtape($etape, Intervenant $intervenant = null, Structure $structure = null)
    {
        [$etapeCode, $intervenant, $structure] = $this->prepareEtapeParams($etape, $intervenant, $structure);

        $fdr       = $this->getFeuilleDeRoute($intervenant, $structure);
        $isCurrent = false;
        foreach ($fdr as $etape) {
            if ($isCurrent && $etape->isAtteignable() && $etape->getUrl() && $this->isAllowed($etape)) {
                return $etape;
            }
            if ($etape->getEtape()->getCode() == $etapeCode) {
                $isCurrent = true;
            }
        }

        return $this->getEtapeCourante($intervenant, $structure);
    }



    public function isAllowed($etape)
    {
        if ($etape instanceof WorkflowEtape) {
            $etape = $etape->getEtape();
        }
        if (!$etape instanceof WfEtape) {
            throw new \Exception('L\'étape fournie n\'est pas de classe WfEtape');
        }

        $resource = \Application\Util::routeToActionResource($etape->getRoute());

        return $this->getServiceAuthorize()->isAllowed($resource);
    }



    /**
     * @param Intervenant|null $intervenant
     * @param Structure|null   $structure
@@ -273,59 +178,51 @@ class WorkflowService extends AbstractService


    /**
     * @return $this
     * @throws \Doctrine\DBAL\DBALException
     * @param Intervenant|null $intervenant
     *
     * @return TblWorkflow[]
     */
    public function calculerTout()
    protected function getEtapes (Intervenant $intervenant, ?Structure $structure = null, bool $calcIfEmpty = true)
    {
        $this->getServiceTableauBord()->calculer('workflow');

        return $this;
    }
        $dql = "
        SELECT
          we, tw, str, dblo, dep
        FROM
          Application\Entity\Db\TblWorkflow tw
          JOIN tw.etape we
          LEFT JOIN tw.structure str
          LEFT JOIN tw.etapeDeps dblo
          LEFT JOIN dblo.wfEtapeDep dep
        WHERE
          tw.intervenant = :intervenant
          " . ($structure ? "AND (tw.structure IS NULL OR tw.structure = :structure)" : '') . "
        ORDER BY
          we.ordre, str.libelleCourt
        ";

        $query = $this->getEntityManager()->createQuery($dql);
        $query->setParameter('intervenant', $intervenant);
        if ($structure) $query->setParameter('structure', $structure);
        $etapes = $query->getResult();

        if (empty($etapes) && $calcIfEmpty) {
            $this->calculerTableauxBord([], $intervenant);

    /**
     * @return int
     * @throws \Doctrine\DBAL\DBALException
     */
    public function calculerTousTableauxBord($beforeTrigger = null, $afterTrigger = null)
    {
        $sql    = "SELECT tbl_name FROM tbl WHERE tbl_name <> 'formule' ORDER BY ordre";
        $tbls   = $this->getEntityManager()->getConnection()->fetchAllAssociative($sql);
        $result = true;
        foreach ($tbls as $tbl) {
            $begin = microtime(true);
            $tbl   = $tbl['TBL_NAME'];
            $sql   = 'BEGIN UNICAEN_TBL.CALCULER(\'' . $tbl . '\'); END;';
            if ($beforeTrigger instanceof \Closure) {
                $beforeTrigger([
                    'tableau-bord' => $tbl,
                ]);
            }
            try {
                $this->getEntityManager()->getConnection()->executeStatement($sql);
                if ($afterTrigger instanceof \Closure) {
                    $afterTrigger([
                        'tableau-bord' => $tbl,
                        'result'       => true,
                        'duree'        => microtime(true) - $begin,
                    ]);
            return $this->getEtapes($intervenant, $structure, false);
        }
            } catch (\Exception $e) {
                if ($afterTrigger instanceof \Closure) {
                    $afterTrigger([
                        'tableau-bord' => $tbl,
                        'result'       => false,
                        'exception'    => $e,
                        'duree'        => microtime(true) - $begin,
                    ]);

        /* @var $etapes TblWorkflow[] */
        if ($this->getServiceContext()->getSelectedIdentityRole()->getIntervenant()) {
            foreach ($etapes as $etape) {
                $we = $etape->getEtape();
                if ($we->getRouteIntervenant()) {
                    $we->setRoute($we->getRouteIntervenant());
                }
                $result = false;
            }
        }

        return $result;
        return $etapes;
    }


@@ -417,6 +314,53 @@ class WorkflowService extends AbstractService



    /**
     * @param WfEtapeService|WorkflowEtape|TblWorkflow|string $etape
     * @param Intervenant|null                                $intervenant
     * @param Structure|null                                  $structure
     *
     * @return WorkflowEtape
     */
    public function getEtape ($etape, Intervenant $intervenant = null, Structure $structure = null)
    {
        [$etapeCode, $intervenant, $structure] = $this->prepareEtapeParams($etape, $intervenant, $structure);

        $fdr = $this->getFeuilleDeRoute($intervenant, $structure);
        if ($fdr) {
            foreach ($fdr as $etape) {
                if ($etape->getEtape()->getCode() == $etapeCode) {
                    return $etape;
                }
            }
        }

        return null;
    }



    /**
     * Generates a url given the name of a route.
     *
     * @param string            $name               Name of the route
     * @param array             $params             Parameters for the link
     * @param array|Traversable $options            Options for the route
     * @param bool              $reuseMatchedParams Whether to reuse matched parameters
     *
     * @return string Url                         For the link href attribute
     * @see    \Laminas\Mvc\Router\RouteInterface::assemble()
     *
     */
    protected function getUrl ($name = null, $params = [], $options = [], $reuseMatchedParams = false)
    {
        $url = \Application::$container->get('ViewHelperManager')->get('url');

        /* @var $url \Laminas\View\Helper\Url */
        return $url->__invoke($name, $params, $options, $reuseMatchedParams);
    }



    /**
     * @param $data WorkflowEtape[]
     */
@@ -455,72 +399,128 @@ class WorkflowService extends AbstractService


    /**
     * @param WfEtapeService|WorkflowEtape|TblWorkflow|string $etape
     * @param Intervenant|null                                $intervenant
     * @param Structure|null                                  $structure
     *
     * @return TblWorkflow[]
     * @return WorkflowEtape
     */
    protected function getEtapes(Intervenant $intervenant, ?Structure $structure = null, bool $calcIfEmpty = true)
    public function getNextEtape ($etape, Intervenant $intervenant = null, Structure $structure = null)
    {
        [$etapeCode, $intervenant, $structure] = $this->prepareEtapeParams($etape, $intervenant, $structure);

        $dql = "
        SELECT
          we, tw, str, dblo, dep
        FROM
          Application\Entity\Db\TblWorkflow tw
          JOIN tw.etape we
          LEFT JOIN tw.structure str
          LEFT JOIN tw.etapeDeps dblo
          LEFT JOIN dblo.wfEtapeDep dep
        WHERE
          tw.intervenant = :intervenant
          " . ($structure ? "AND (tw.structure IS NULL OR tw.structure = :structure)" : '') . "
        ORDER BY
          we.ordre, str.libelleCourt
        ";
        $fdr       = $this->getFeuilleDeRoute($intervenant, $structure);
        $isCurrent = false;
        foreach ($fdr as $etape) {
            if ($isCurrent) {
                return $etape;
            }
            if ($etape->getEtape()->getCode() == $etapeCode) {
                $isCurrent = true;
            }
        }

        $query = $this->getEntityManager()->createQuery($dql);
        $query->setParameter('intervenant', $intervenant);
        if ($structure) $query->setParameter('structure', $structure);
        $etapes = $query->getResult();
        return null;
    }

        if (empty($etapes) && $calcIfEmpty) {
            $this->calculerTableauxBord([], $intervenant);

            return $this->getEtapes($intervenant, $structure, false);

    public function isAllowed ($etape)
    {
        if ($etape instanceof WorkflowEtape) {
            $etape = $etape->getEtape();
        }
        if (!$etape instanceof WfEtape) {
            throw new \Exception('L\'étape fournie n\'est pas de classe WfEtape');
        }

        /* @var $etapes TblWorkflow[] */
        if ($this->getServiceContext()->getSelectedIdentityRole()->getIntervenant()) {
            foreach ($etapes as $etape) {
                $we = $etape->getEtape();
                if ($we->getRouteIntervenant()) {
                    $we->setRoute($we->getRouteIntervenant());
        $resource = \Application\Util::routeToActionResource($etape->getRoute());

        return $this->getServiceAuthorize()->isAllowed($resource);
    }



    /**
     * @param WfEtapeService|WorkflowEtape|TblWorkflow|string $etape
     * @param Intervenant|null                                $intervenant
     * @param Structure|null                                  $structure
     *
     * @return WorkflowEtape
     */
    public function getNextAccessibleEtape ($etape, Intervenant $intervenant = null, Structure $structure = null)
    {
        [$etapeCode, $intervenant, $structure] = $this->prepareEtapeParams($etape, $intervenant, $structure);

        $fdr       = $this->getFeuilleDeRoute($intervenant, $structure);
        $isCurrent = false;
        foreach ($fdr as $etape) {
            if ($isCurrent && $etape->isAtteignable() && $etape->getFranchie() < 1 && $etape->getUrl() && $this->isAllowed($etape)) {
                return $etape;
            }
            if ($etape->getEtape()->getCode() == $etapeCode) {
                $isCurrent = true;
            }
        }

        return $etapes;
        return $this->getEtapeCourante($intervenant, $structure);
    }



    /**
     * Generates a url given the name of a route.
     *
     * @param string            $name               Name of the route
     * @param array             $params             Parameters for the link
     * @param array|Traversable $options            Options for the route
     * @param bool              $reuseMatchedParams Whether to reuse matched parameters
     *
     * @return string Url                         For the link href attribute
     * @see    \Laminas\Mvc\Router\RouteInterface::assemble()
     *
     * @return $this
     * @throws \Doctrine\DBAL\DBALException
     */
    protected function getUrl($name = null, $params = [], $options = [], $reuseMatchedParams = false)
    public function calculerTout ()
    {
        $url = \Application::$container->get('ViewHelperManager')->get('url');
        $this->getServiceTableauBord()->calculer('workflow');

        /* @var $url \Laminas\View\Helper\Url */
        return $url->__invoke($name, $params, $options, $reuseMatchedParams);
        return $this;
    }



    /**
     * @return int
     * @throws \Doctrine\DBAL\DBALException
     */
    public function calculerTousTableauxBord ($beforeTrigger = null, $afterTrigger = null)
    {
        $sql    = "SELECT tbl_name FROM tbl WHERE tbl_name <> 'formule' ORDER BY ordre";
        $tbls   = $this->getEntityManager()->getConnection()->fetchAllAssociative($sql);
        $result = true;
        foreach ($tbls as $tbl) {
            $begin = microtime(true);
            $tbl   = $tbl['TBL_NAME'];
            $sql   = 'BEGIN UNICAEN_TBL.CALCULER(\'' . $tbl . '\'); END;';
            if ($beforeTrigger instanceof \Closure) {
                $beforeTrigger([
                    'tableau-bord' => $tbl,
                ]);
            }
            try {
                $this->getEntityManager()->getConnection()->executeStatement($sql);
                if ($afterTrigger instanceof \Closure) {
                    $afterTrigger([
                        'tableau-bord' => $tbl,
                        'result'       => true,
                        'duree'        => microtime(true) - $begin,
                    ]);
                }
            } catch (\Exception $e) {
                if ($afterTrigger instanceof \Closure) {
                    $afterTrigger([
                        'tableau-bord' => $tbl,
                        'result'       => false,
                        'exception'    => $e,
                        'duree'        => microtime(true) - $begin,
                    ]);
                }
                $result = false;
            }
        }

        return $result;
    }
}
 No newline at end of file