Commit 347b883c authored by Jean-Philippe Metivier's avatar Jean-Philippe Metivier
Browse files

abonnement notification limité par la fréquence

parent 30ce98f3
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -56,7 +56,7 @@ return [
                    'resilier' => [
                        'type'  => Segment::class,
                        'options' => [
                            'route'    => '/resilier/:abonnement',
                            'route'    => '/resilier/:indicateur',
                            'defaults' => [
                                'controller' => AbonnementController::class,
                                'action' => 'resilier'
+12 −5
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Indicateur\Controller\Abonnement;

use DateInterval;
use DateTime;
use Indicateur\Entity\Db\Abonnement;
use Indicateur\Service\Abonnement\AbonnementService;
@@ -39,10 +40,13 @@ class AbonnementController extends AbstractActionController {
        return $this->redirect()->toRoute('indicateurs', [], [], true);
    }

    public function desabonnementAction()
    public function resilierAction()
    {
        $abonnement = $this->getAbonnementService()->getRequestedAbonnement($this);
        $this->getAbonnementService()->delete($abonnement);
        $indicateur = $this->getIndicateurService()->getRequestedIndicateur($this);
        $user = $this->getUserService()->getConnectedUser();

        $abonnements = $this->getAbonnementService()->getAbonnementsByUserAndIndicateur($user, $indicateur);
        foreach ($abonnements as $abonnement) $this->getAbonnementService()->delete($abonnement);

        return $this->redirect()->toRoute('indicateurs', [], [], true);
    }
@@ -53,7 +57,7 @@ class AbonnementController extends AbstractActionController {
        $now = new DateTime();
        $listing = [];
        foreach ($abonnements as $abonnement) {
            if ($now - $abonnement->getFrequence() > $abonnement->getDernierEnvoi()) {
            if (date_diff($now, $abonnement->getDernierEnvoi()) > DateInterval::createFromDateString($abonnement->getFrequence())) {
                $listing[$abonnement->getIndicateur()->getId()][] = $abonnement;
            }
        }
@@ -78,8 +82,11 @@ class AbonnementController extends AbstractActionController {

            foreach ($list as $abonnement) {
                $this->getAbonnementService()->notify($abonnement, $titre, $texte, $now);
                $abonnement->setDernierEnvoi($now);
                $this->getAbonnementService()->update($abonnement);
            }
        }
        exit();

        return $this->redirect()->toRoute('indicateurs', [], [], true);
    }
}
 No newline at end of file
+19 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ use DateTime;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\ORMException;
use Indicateur\Entity\Db\Abonnement;
use Indicateur\Entity\Db\Indicateur;
use Mailing\Service\Mailing\MailingServiceAwareTrait;
use UnicaenApp\Exception\RuntimeException;
use UnicaenApp\Service\EntityManagerAwareTrait;
@@ -131,4 +132,22 @@ class AbonnementService {
        $this->getMailingService()->sendMail($mail, $titre, $texte);
        $abonnement->setDernierEnvoi($date);
    }

    /**
     * @param User $user
     * @param Indicateur $indicateur
     * @return Abonnement[]
     */
    public function getAbonnementsByUserAndIndicateur($user, $indicateur)
    {
        $qb = $this->getEntityManager()->getRepository(Abonnement::class)->createQueryBuilder('abonnement')
            ->andWhere('abonnement.user = :user')
            ->andWhere('abonnement.indicateur = :indicateur')
            ->setParameter('user', $user)
            ->setParameter('indicateur', $indicateur)
        ;

        $result = $qb->getQuery()->getResult();
        return $result;
    }
}
 No newline at end of file
+8 −2
Original line number Diff line number Diff line
@@ -59,9 +59,15 @@ $canDetruire = $this->isAllowed(IndicateurPrivileges::getResourceId(IndicateurPr
                    <td>
                        <?php echo count($indicateur->getAbonnements()); ?>
                        <?php if ($abonnements[$indicateur->getId()]) : ?>
                            <?php /** @see AbonnementController::resilierAction() */?>
                            <a href="<?php echo $this->url("abonnement/resilier", ['indicateur' => $indicateur->getId()], [], true); ?>">
                                <span class="icon ok" style="color:darkgreen;" title="Vous êtes abonné&middot;e à cet indicateur"></span>
                            </a>
                        <?php else : ?>
                            <?php /** @see AbonnementController::souscrireAction() */?>
                            <a href="<?php echo $this->url("abonnement/souscrire", ['indicateur' => $indicateur->getId()], [], true); ?>">
                                <span class="icon ko" style="color:darkred;" title="Vous n'êtes pas abonné&middot;e à cet indicateur"></span>
                            </a>
                        <?php endif;?>
                    </td>
                    <td>