]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - modules/private/websites/nicecoop/gestion-compte/AmbassadorShiftTimeLogCommand.php
To merge in nicecoop-installation
[perso/Immae/Config/Nix.git] / modules / private / websites / nicecoop / gestion-compte / AmbassadorShiftTimeLogCommand.php
1 <?php
2 // src/AppBundle/Command/AmbassadorShiftTimeLogCommand.php
3 namespace AppBundle\Command;
4
5 use Swift_Message;
6 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7 use Symfony\Component\Console\Input\InputInterface;
8 use Symfony\Component\Console\Input\InputOption;
9 use Symfony\Component\Console\Output\OutputInterface;
10 use Doctrine\ORM\Query\Expr\Join;
11
12 class AmbassadorShiftTimeLogCommand extends ContainerAwareCommand
13 {
14 protected function configure()
15 {
16 $this
17 ->setName('app:shift:send_late_shifters')
18 ->setDescription('Send shifters which are too late')
19 ->setHelp('This command allows you to send alerts for shifters that are too late')
20 ->addOption('emails', null, InputOption::VALUE_OPTIONAL, 'Email recipients (comma separated)')
21 ->addOption('emailTemplate', null, InputOption::VALUE_OPTIONAL, 'Template used in email alerts', 'SHIFT_LATE_ALERT_EMAIL');
22 }
23
24 protected function execute(InputInterface $input, OutputInterface $output)
25 {
26 $email_template = $input->getOption('emailTemplate');
27
28 $alerts = $this->computeAlerts();
29 $nbAlerts = count($alerts);
30 if ($nbAlerts > 0) {
31 $output->writeln('<fg=cyan;>Found ' . $nbAlerts . ' alerts to send</>');
32 $this->sendAlertsByEmail($input, $output, $alerts, $email_template);
33 } else {
34 $output->writeln('<fg=cyan;>No shift alert to send</>');
35 }
36 }
37
38 private function computeAlerts() {
39 $time_after_which_members_are_late_with_shifts = $this->getContainer()->getParameter('time_after_which_members_are_late_with_shifts');
40
41 $em = $this->getContainer()->get('doctrine')->getManager();
42 $qb = $em->getRepository("AppBundle:Membership")->createQueryBuilder('o');
43 $qb = $qb->leftJoin("o.beneficiaries", "b")
44 ->leftJoin("b.user", "u")
45 ->leftJoin("o.registrations", "r")->addSelect("r");
46 $qb = $qb->andWhere('o.member_number > 0'); //do not include admin user
47 $qb = $qb->leftJoin("o.registrations", "lr", Join::WITH,'lr.date > r.date')->addSelect("lr")
48 ->where('lr.id IS NULL') //registration is the last one registere
49 ->leftJoin("o.timeLogs", "c")->addSelect("c")
50 ->addSelect("(SELECT SUM(ti.time) FROM AppBundle\Entity\TimeLog ti WHERE ti.membership = o.id) AS HIDDEN time");
51 $qb = $qb->andWhere('o.withdrawn = 0');
52 $qb = $qb->andWhere('o.frozen = 0');
53 $qb = $qb->andWhere('b.membership IN (SELECT IDENTITY(t.membership) FROM AppBundle\Entity\TimeLog t GROUP BY t.membership HAVING SUM(t.time) < :compteurlt * 60)')
54 ->setParameter('compteurlt', $time_after_which_members_are_late_with_shifts);
55 $alerts = $qb->getQuery()->getResult();
56 return $alerts;
57 }
58
59 private function sendAlertsByEmail(InputInterface $input, OutputInterface $output, $alerts, $template) {
60 $mailer = $this->getContainer()->get('mailer');
61 $recipients = $input->getOption('emails') ? explode(',', $input->getOption('emails')) : null;
62 if ($recipients) {
63 setlocale(LC_TIME, 'fr_FR.UTF8');
64 $subject = '[ALERTE RETARDS] Membres en retard de shifts';
65
66 $shiftEmail = $this->getContainer()->getParameter('emails.shift');
67
68 $em = $this->getContainer()->get('doctrine')->getManager();
69 $dynamicContent = null;
70 //$dynamicContent = $em->getRepository('AppBundle:DynamicContent')->findOneByCode($template);
71 $template = null;
72 if ($dynamicContent) {
73 $template = $this->getContainer()->get('twig')->createTemplate($dynamicContent->getContent());
74 } else {
75 $template = 'emails/shift_late_alerts_default.html.twig';
76 }
77
78 $email = (new Swift_Message($subject))
79 ->setFrom($shiftEmail['address'], $shiftEmail['from_name'])
80 ->setTo($recipients)
81 ->setBody(
82 $this->getContainer()->get('twig')->render(
83 $template,
84 array('alerts' => $alerts)
85 ),
86 'text/html'
87 );
88 $mailer->send($email);
89 $output->writeln('<fg=cyan;>Email(s) sent</>');
90 }
91 }
92 }