setName('Websocket:CountMessage') ->setDescription('Count number of messages not read and send mail notification') ->setHelp('Count number of message') ->addArgument('cronid', InputArgument::OPTIONAL, 'ID Cron Job') ->addArgument('lastchance', InputArgument::OPTIONAL, 'Lastchance to run the cron') ; } protected function execute(InputInterface $input, OutputInterface $output) { $this->container = $this->getApplication()->getKernel()->getContainer(); $this->em = $this->container->get('doctrine')->getEntityManager(); $this->output = $output; $this->filesystem = new Filesystem(); $this->rootlog = $this->container->get('kernel')->getRootDir()."/../var/logs/"; $this->noreply = $this->container->getParameter('noreply'); $this->writelnred(''); $this->writelnred('== Websocket:Countmessage'); $this->writelnred('=========================================================================================================='); $now=new \DateTime('now'); $fgdebug = false; // Config $this->appname = $this->em->getRepository("CadolesCoreBundle:Config")->findOneBy(["id"=>"appname"])->getValue(); $this->url= "https://".$this->container->getParameter('weburl')."/".$this->container->getParameter('alias'); // Pour chaque utilisateur $users=$this->em->getRepository("CadolesCoreBundle:User")->findAll(); foreach($users as $user) { $cptnotread=0; // Pour chaque group de l'utilisateur foreach($user->getGroups() as $usergroup) { // On calcule le nombre de message non lu pour l'utilisateur' $group=$usergroup->getGroup(); if($group->getFgcanshare()) { $qb = $this->em->createQueryBuilder(); $tm = $qb ->select($qb->expr()->count('m.id')) ->from('CadolesWebsocketBundle:Message', 'm') ->where('m.group = :group') ->andWhere('m.user != :user') ->setParameter('group', $group) ->setParameter('user', $user) ->getQuery()->getSingleScalarResult(); $qb = $this->em->createQueryBuilder(); $tr = $qb ->select($qb->expr()->count('m.id')) ->from('CadolesWebsocketBundle:Message', 'm') ->where('m.group = :group') ->andWhere('m.user != :user') ->andWhere(':user MEMBER OF m.readers') ->setParameter('group', $group) ->setParameter('user', $user) ->getQuery()->getSingleScalarResult(); if($tm-$tr>0) $cptnotread+=($tm-$tr); } if($cptnotread>0) { $this->writeln($user->getUsername()." notifié de ".$cptnotread." messages non lus"); $template="template"; $mail_params=array( "subject" => $this->appname." : Messages non lus", "body_html"=>"

Vous avez ".$cptnotread." messages non lus sur ".$this->appname."

Vous pouvez les consulter sur ".$this->url."

", "body_text"=>"Vous avez ".$cptnotread." messages non lus sur ".$this->appname."\nVous pouvez les consulter sur ".$this->url, ); $message = $this->container->get('cadoles.core.service.mail'); $message->sendEmail($template, $mail_params, $user->getEmail(), $this->noreply, $this->appname); } } } $this->writeln(''); return 1; } private function writelnred($string) { $this->output->writeln(''.$string.''); $this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n"); } private function writeln($string) { $this->output->writeln($string); $this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n"); } }