2012-07-31 09:09:52 +00:00
< ? php
namespace KekRozsak\FrontBundle\Twig ;
use Symfony\Bridge\Doctrine\RegistryInterface ;
use Symfony\Component\Security\Core\SecurityContextInterface ;
2012-08-16 17:06:00 +00:00
use JMS\DiExtraBundle\Annotation as DI ;
2012-07-31 09:09:52 +00:00
2012-08-16 17:06:00 +00:00
/**
* @ DI\Service
* @ DI\Tag ( " twig.extension " )
*/
2012-07-31 09:09:52 +00:00
class EventsExtension extends \Twig_Extension
{
2012-08-16 17:06:00 +00:00
protected $_doctrine ;
protected $_securityContext ;
2012-07-31 09:09:52 +00:00
2012-08-16 17:06:00 +00:00
/**
* @ DI\InjectParams ({
* " doctrine " = @ DI\Inject ( " doctrine " ),
* " securityContext " = @ DI\Inject ( " security.context " )
* })
*
2012-08-17 14:18:43 +00:00
* @ param \Symfony\Bridge\Doctrine\RegistryInterface $doctrine
2012-08-16 17:06:00 +00:00
* @ param \Symfony\Component\Security\Core\SecurityContextInterface $securityContext
*/
public function __construct ( RegistryInterface $doctrine , SecurityContextInterface $securityContext )
{
$this -> _doctrine = $doctrine ;
$this -> _securityContext = $securityContext ;
}
2012-07-31 09:09:52 +00:00
2012-08-16 17:06:00 +00:00
public function getGlobals ()
{
$today = new \DateTime ( 'now' );
$firstDay = \DateTime :: createFromFormat ( 'Y-m-d' , $today -> format ( 'Y-m-01' ));
$firstDayWeekday = $firstDay -> format ( 'N' );
$numDays = $firstDay -> format ( 't' );
$lastDay = \DateTime :: createFromFormat ( 'Y-m-d' , $today -> format ( 'Y-m-' . sprintf ( " %02d " , $numDays )));
2012-07-31 09:09:52 +00:00
2012-08-16 17:06:00 +00:00
/*
* Get all events in today ' s month . Iterate through this
* collection , adding each element to $monthEvents array ' s
* 'day' th element array .
*/
$query = $this -> _doctrine -> getEntityManager () -> createQuery ( 'SELECT e FROM KekRozsakFrontBundle:Event e WHERE e.cancelled = FALSE AND ((e.startDate < :firstDay AND e.endDate >= :firstDay) OR e.startDate BETWEEN :firstDay AND :lastDay)' );
$query -> setParameter ( 'firstDay' , $firstDay , \Doctrine\DBAL\Types\Type :: DATE );
$query -> setParameter ( 'lastDay' , $lastDay , \Doctrine\DBAL\Types\Type :: DATE );
$events = $query -> getResult ();
2012-07-31 09:09:52 +00:00
2012-08-16 17:06:00 +00:00
$eventList = array ();
for ( $i = 1 ; $i <= $numDays ; $i ++ ) {
$date = \DateTime :: createFromFormat (
'Y-m-d' ,
$today -> format ( 'Y-m-' . sprintf ( '%02d' , $i ))
);
$eventList [ $i ][ 'date' ] = $date ;
$eventList [ $i ][ 'events' ] = array ();
2012-08-17 14:18:43 +00:00
foreach ( $events as $event ) {
2012-08-16 17:06:00 +00:00
if ( $event -> isOnDate ( $date )) {
$eventList [ $i ][ 'events' ][] = $event ;
}
}
}
2012-07-31 09:09:52 +00:00
2012-08-16 17:06:00 +00:00
return array (
'events' => $events ,
'eventList' => $eventList ,
'today' => $today ,
'firstDay' => $firstDay ,
'lastDay' => $lastDay ,
2012-09-01 16:45:06 +00:00
'firstDayWeekday' => ( $firstDayWeekday == 0 ) ? 7 : $firstDayWeekday ,
2012-08-16 17:06:00 +00:00
'numDays' => $numDays ,
);
}
2012-07-31 09:09:52 +00:00
2012-08-16 17:06:00 +00:00
public function getName ()
{
return 'Events' ;
}
2012-07-31 09:09:52 +00:00
}