2019-10-28 16:55:36 +01:00
< ? php
namespace Cadoles\WebsocketBundle\Command ;
use Symfony\Component\Console\Command\Command ;
use Symfony\Component\Console\Input\InputInterface ;
use Symfony\Component\Console\Input\InputArgument ;
use Symfony\Component\Console\Output\OutputInterface ;
use Symfony\Component\Finder\Finder ;
use Symfony\Component\Filesystem\Filesystem ;
use Symfony\Component\HttpFoundation\File\File ;
use Symfony\Component\HttpKernel\KernelInterface ;
use Doctrine\DBAL\Connection as DBALConnection ;
use Doctrine\ORM\EntityManager ;
use Symfony\Component\Validator\Constraints\DateTime ;
class CountMessageCommand extends Command
{ protected function configure ()
{
$this
-> 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 ();
2019-11-08 16:21:08 +01:00
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 ();
2019-10-28 16:55:36 +01:00
2019-11-08 16:21:08 +01:00
$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 ();
2019-10-28 16:55:36 +01:00
2019-11-08 16:21:08 +01:00
if ( $tm - $tr > 0 ) $cptnotread += ( $tm - $tr );
}
if ( $cptnotread > 0 ) {
$this -> writeln ( $user -> getUsername () . " notifié de " . $cptnotread . " messages non lus " );
2019-10-28 16:55:36 +01:00
2019-11-08 16:21:08 +01:00
$template = " template " ;
2019-10-28 16:55:36 +01:00
2019-11-08 16:21:08 +01:00
$mail_params = array (
" subject " => $this -> appname . " : Messages non lus " ,
" body_html " => " <p>Vous avez " . $cptnotread . " messages non lus sur " . $this -> appname . " </p><p>Vous pouvez les consulter sur <a href=' " . $this -> url . " '> " . $this -> url . " </a></p> " ,
" body_text " => " Vous avez " . $cptnotread . " messages non lus sur " . $this -> appname . " \n Vous pouvez les consulter sur " . $this -> url ,
);
2019-10-28 16:55:36 +01:00
2019-11-08 16:21:08 +01:00
$message = $this -> container -> get ( 'cadoles.core.service.mail' );
$message -> sendEmail ( $template , $mail_params , $user -> getEmail (), $this -> noreply , $this -> appname );
}
2019-10-28 16:55:36 +01:00
}
}
$this -> writeln ( '' );
return 1 ;
}
private function writelnred ( $string ) {
$this -> output -> writeln ( '<fg=red>' . $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 " );
}
}