2 // src/AppBundle/Command/AmbassadorShiftTimeLogCommand.php
3 namespace AppBundle\Command
;
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
;
12 class AmbassadorShiftTimeLogCommand
extends ContainerAwareCommand
14 protected function configure()
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');
24 protected function execute(InputInterface
$input, OutputInterface
$output)
26 $email_template = $input->getOption('emailTemplate');
28 $alerts = $this->computeAlerts();
29 $nbAlerts = count($alerts);
31 $output->writeln('<fg=cyan;>Found ' . $nbAlerts . ' alerts to send</>');
32 $this->sendAlertsByEmail($input, $output, $alerts, $email_template);
34 $output->writeln('<fg=cyan;>No shift alert to send</>');
38 private function computeAlerts() {
39 $time_after_which_members_are_late_with_shifts = $this->getContainer()->getParameter('time_after_which_members_are_late_with_shifts');
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();
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;
63 setlocale(LC_TIME
, 'fr_FR.UTF8');
64 $subject = '[ALERTE RETARDS] Membres en retard de shifts';
66 $shiftEmail = $this->getContainer()->getParameter('emails.shift');
68 $em = $this->getContainer()->get('doctrine')->getManager();
69 $dynamicContent = null;
70 //$dynamicContent = $em->getRepository('AppBundle:DynamicContent')->findOneByCode($template);
72 if ($dynamicContent) {
73 $template = $this->getContainer()->get('twig')->createTemplate($dynamicContent->getContent());
75 $template = 'emails/shift_late_alerts_default.html.twig';
78 $email = (new Swift_Message($subject))
79 ->setFrom($shiftEmail['address'], $shiftEmail['from_name'])
82 $this->getContainer()->get('twig')->render(
84 array('alerts' => $alerts)
88 $mailer->send($email);
89 $output->writeln('<fg=cyan;>Email(s) sent</>');