Commit 30ce98f3 authored by Jean-Philippe Metivier's avatar Jean-Philippe Metivier
Browse files

début de gestion et de notification des abonnements

parent fd0fe72e
Loading
Loading
Loading
Loading
+57 −1
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ namespace Application;

use Indicateur\Controller\Abonnement\AbonnementController;
use Indicateur\Controller\Abonnement\AbonnementControllerFactory;
use Indicateur\Provider\Privilege\AbonnementPrivileges;
use Indicateur\Service\Abonnement\AbonnementService;
use Indicateur\Service\Abonnement\AbonnementServiceFactory;
use UnicaenAuth\Guard\PrivilegeController;
@@ -14,13 +15,68 @@ return [
    'bjyauthorize' => [
        'guards' => [
            PrivilegeController::class => [

                [
                    'controller' => AbonnementController::class,
                    'action' => [
                        'souscrire',
                        'resilier',
                        'notifier'
                    ],
                    'privileges' => [
                        AbonnementPrivileges::EDITER,
                    ],
                ],
            ],
        ],
    ],

    'router'          => [
        'routes' => [
            'abonnement' => [
                'type'  => Literal::class,
                'options' => [
                    'route'    => '/abonnement',
                    'defaults' => [
                        'controller' => AbonnementController::class,
                    ],
                ],
                'may_terminate' => false,
                'child_routes' => [
                    'souscrire' => [
                        'type'  => Segment::class,
                        'options' => [
                            'route'    => '/souscrire/:indicateur',
                            'defaults' => [
                                'controller' => AbonnementController::class,
                                'action' => 'souscrire'
                            ],
                        ],
                        'may_terminate' => true,
                    ],
                    'resilier' => [
                        'type'  => Segment::class,
                        'options' => [
                            'route'    => '/resilier/:abonnement',
                            'defaults' => [
                                'controller' => AbonnementController::class,
                                'action' => 'resilier'
                            ],
                        ],
                        'may_terminate' => true,
                    ],
                    'notifier' => [
                        'type'  => Literal::class,
                        'options' => [
                            'route'    => '/notifier',
                            'defaults' => [
                                'controller' => AbonnementController::class,
                                'action' => 'notifier'
                            ],
                        ],
                        'may_terminate' => true,
                    ],
                ],
            ],
        ],
    ],

+67 −0
Original line number Diff line number Diff line
@@ -2,10 +2,19 @@

namespace Indicateur\Controller\Abonnement;

use DateTime;
use Indicateur\Entity\Db\Abonnement;
use Indicateur\Service\Abonnement\AbonnementService;
use Indicateur\Service\Abonnement\AbonnementServiceAwareTrait;
use Indicateur\Service\Indicateur\IndicateurServiceAwareTrait;
use Utilisateur\Service\User\UserServiceAwareTrait;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class AbonnementController extends AbstractActionController {
    use AbonnementServiceAwareTrait;
    use IndicateurServiceAwareTrait;
    use UserServiceAwareTrait;

    public function indexAction()
    {
@@ -15,4 +24,62 @@ class AbonnementController extends AbstractActionController {
            'abonnements' => $abonnements,
        ]);
    }

    public function souscrireAction()
    {
        $indicateur = $this->getIndicateurService()->getRequestedIndicateur($this);
        $user = $this->getUserService()->getConnectedUser();

        $abonnement = new Abonnement();
        $abonnement->setIndicateur($indicateur);
        $abonnement->setUser($user);
        $abonnement->setFrequence('P1D');
        $this->getAbonnementService()->create($abonnement);

        return $this->redirect()->toRoute('indicateurs', [], [], true);
    }

    public function desabonnementAction()
    {
        $abonnement = $this->getAbonnementService()->getRequestedAbonnement($this);
        $this->getAbonnementService()->delete($abonnement);

        return $this->redirect()->toRoute('indicateurs', [], [], true);
    }

    public function notifierAction()
    {
        $abonnements = $this->getAbonnementService()->getAbonnements();
        $now = new DateTime();
        $listing = [];
        foreach ($abonnements as $abonnement) {
            if ($now - $abonnement->getFrequence() > $abonnement->getDernierEnvoi()) {
                $listing[$abonnement->getIndicateur()->getId()][] = $abonnement;
            }
        }
        foreach ($listing as $indicateurId => $list) {
            $indicateur = $this->getIndicateurService()->getIndicateur($indicateurId);
            $titre = "Publication de l'indicateur [".$indicateur->getTitre()."] (". $now->format("d/m/Y à H:i:s").")";
            $result = $this->getIndicateurService()->getIndicateurData($indicateur);
            $texte  = "<table>";
            $texte .= "<thead>";
            $texte .= "<tr>";
            foreach ($result[0] as $rubrique) $texte .= "<th>" . $rubrique . "</th>";
            $texte .= "</tr>";
            $texte .= "</thead>";
            $texte .= "<tbody>";
            foreach ($result[1] as $item) {
                $texte .="<tr>";
                foreach ($item as $value) $texte .="<td>". $value ."</td>";
                $texte .="</tr>";
            }
            $texte .= "</tbody>";
            $texte .= "</table>";

            foreach ($list as $abonnement) {
                $this->getAbonnementService()->notify($abonnement, $titre, $texte, $now);
            }
        }
        exit();
    }
}
 No newline at end of file
+15 −0
Original line number Diff line number Diff line
@@ -2,14 +2,29 @@

namespace Indicateur\Controller\Abonnement;

use Indicateur\Service\Abonnement\AbonnementService;
use Indicateur\Service\Indicateur\IndicateurService;
use Utilisateur\Service\User\UserService;
use Zend\Mvc\Controller\ControllerManager;

class AbonnementControllerFactory {

    public function __invoke(ControllerManager $manager)
    {
        /**
         * @var AbonnementService $abonnementService
         * @var IndicateurService $indicateurService
         * @var UserService $userService
         */
        $abonnementService = $manager->getServiceLocator()->get(AbonnementService::class);
        $indicateurService = $manager->getServiceLocator()->get(IndicateurService::class);
        $userService = $manager->getServiceLocator()->get(UserService::class);

        /** @var AbonnementController $controller */
        $controller = new AbonnementController();
        $controller->setAbonnementService($abonnementService);
        $controller->setIndicateurService($indicateurService);
        $controller->setUserService($userService);
        return $controller;
    }
}
 No newline at end of file
+13 −0
Original line number Diff line number Diff line
@@ -5,22 +5,35 @@ namespace Indicateur\Controller\Indicateur;
use DateTime;
use Indicateur\Entity\Db\Indicateur;
use Indicateur\Form\Indicateur\IndicateurFormAwareTrait;
use Indicateur\Service\Abonnement\AbonnementServiceAwareTrait;
use Indicateur\Service\Indicateur\IndicateurServiceAwareTrait;
use UnicaenApp\View\Model\CsvModel;
use Utilisateur\Service\User\UserServiceAwareTrait;
use Zend\Http\Request;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndicateurController extends AbstractActionController {
    use IndicateurServiceAwareTrait;
    use UserServiceAwareTrait;
    use AbonnementServiceAwareTrait;

    use IndicateurFormAwareTrait;

    public function indexAction()
    {
        $indicateurs = $this->getIndicateurService()->getIndicateurs();
        $user = $this->getUserService()->getConnectedUser();
        $abonnements = $this->getAbonnementService()->getAbonnementsByUser($user);

        $array = [];
        foreach ($abonnements as $abonnement) {
            $array [$abonnement->getIndicateur()->getId()] = $abonnement;
        }

        return new ViewModel([
            'indicateurs' => $indicateurs,
            'abonnements' => $array,
        ]);
    }

+8 −0
Original line number Diff line number Diff line
@@ -3,7 +3,9 @@
namespace Indicateur\Controller\Indicateur;

use Indicateur\Form\Indicateur\IndicateurForm;
use Indicateur\Service\Abonnement\AbonnementService;
use Indicateur\Service\Indicateur\IndicateurService;
use Utilisateur\Service\User\UserService;
use Zend\Mvc\Controller\ControllerManager;

class IndicateurControllerFactory {
@@ -11,9 +13,13 @@ class IndicateurControllerFactory {
    public function __invoke(ControllerManager $manager)
    {
        /**
         * @var AbonnementService $abonnementService
         * @var IndicateurService $indicateurService
         * @var UserService $userService
         */
        $abonnementService = $manager->getServiceLocator()->get(AbonnementService::class);
        $indicateurService = $manager->getServiceLocator()->get(IndicateurService::class);
        $userService = $manager->getServiceLocator()->get(UserService::class);

        /**
         * @var IndicateurForm $indicateurForm
@@ -22,7 +28,9 @@ class IndicateurControllerFactory {

        /** @var IndicateurController $controller */
        $controller = new IndicateurController();
        $controller->setAbonnementService($abonnementService);
        $controller->setIndicateurService($indicateurService);
        $controller->setUserService($userService);
        $controller->setIndicateurForm($indicateurForm);
        return $controller;
    }
Loading