3 namespace Rapsys\AirBundle\Repository
; 
   5 use Symfony\Component\Translation\TranslatorInterface
; 
   6 use Doctrine\DBAL\Types\Type
; 
   7 use Doctrine\ORM\Query\ResultSetMapping
; 
   8 use Doctrine\ORM\Query
; 
  13 class SessionRepository 
extends \Doctrine\ORM\EntityRepository 
{ 
  14         ///Set accuweather max number of daily pages 
  15         const ACCUWEATHER_DAILY 
= 12; 
  17         ///Set accuweather max number of hourly pages 
  18         const ACCUWEATHER_HOURLY 
= 3; 
  21         const GUEST_DELAY 
= 2; 
  24         const REGULAR_DELAY 
= 3; 
  27         const SENIOR_DELAY 
= 4; 
  30         //TODO: document utf-8 codes ? 
  33                 'Morning' => 'π
', #0001f305 
  34                 'Afternoon' => 'βοΈ', #2600 
  35                 'Evening' => 'π', #0001f307 
  36                 'After' => 'β¨', #2728 
  38                 'Cleary' => 'β', #2600 
  39                 'Sunny' => 'β
', #26c5 
  40                 'Cloudy' => 'β', #2601 
  41                 'Winty' => 'βοΈ', #2744 
  42                 'Rainy' => 'π', #0001f302 
  43                 'Stormy' => 'β' #2614 
  47          * Find session by location, slot and date 
  49          * @param $location The location 
  50          * @param $slot The slot 
  51          * @param $date The datetime 
  53         public function findOneByLocationSlotDate($location, $slot, $date) { 
  55                 return $this->getEntityManager() 
  56                         ->createQuery('SELECT s FROM RapsysAirBundle:Session s WHERE (s.location = :location AND s.slot = :slot AND s.date = :date)') 
  57                         ->setParameter('location', $location) 
  58                         ->setParameter('slot', $slot) 
  59                         ->setParameter('date', $date) 
  64          * Find sessions by date period 
  66          * @param $period The date period 
  68         public function findAllByDatePeriod($period) { 
  70                 return $this->getEntityManager() 
  71                         ->createQuery('SELECT s FROM RapsysAirBundle:Session s WHERE s.date BETWEEN :begin AND :end') 
  72                         ->setParameter('begin', $period->getStartDate()) 
  73                         ->setParameter('end', $period->getEndDate()) 
  78          * Find sessions by location and date period 
  80          * @param $location The location 
  81          * @param $period The date period 
  83         public function findAllByLocationDatePeriod($location, $period) { 
  85                 return $this->getEntityManager() 
  86                         ->createQuery('SELECT s FROM RapsysAirBundle:Session s WHERE (s.location = :location AND s.date BETWEEN :begin AND :end)') 
  87                         ->setParameter('location', $location) 
  88                         ->setParameter('begin', $period->getStartDate()) 
  89                         ->setParameter('end', $period->getEndDate()) 
  94          * Find one session by location and user id within last month 
  96          * @param $location The location id 
  97          * @param $user The user id 
  99         public function findOneWithinLastMonthByLocationUser($location, $user) { 
 101                 $em = $this->getEntityManager(); 
 104                 $qs = $em->getConfiguration()->getQuoteStrategy(); 
 105                 $dp = $em->getConnection()->getDatabasePlatform(); 
 107                 //Get quoted table names 
 108                 //XXX: this allow to make this code table name independent 
 110                         'RapsysAirBundle:Session' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Session'), $dp), 
 111                         'RapsysAirBundle:Application' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Application'), $dp), 
 117                 //XXX: give the gooddelay to guest just in case 
 120 FROM RapsysAirBundle:Session s 
 121 JOIN RapsysAirBundle:Application a ON (a.id = s.application_id AND a.user_id = :uid AND (a.canceled IS NULL OR TIMESTAMPDIFF(DAY, a.canceled, ADDTIME(s.date, s.begin)) < 1)) 
 122 WHERE s.location_id = :lid AND s.date >= DATE_ADD(DATE_SUB(NOW(), INTERVAL 1 MONTH), INTERVAL :gooddelay DAY) 
 125                 //Replace bundle entity name by table name 
 126                 $req = str_replace(array_keys($tables), array_values($tables), $req); 
 128                 //Get result set mapping instance 
 129                 $rsm = new ResultSetMapping(); 
 132                 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php 
 133                 $rsm->addScalarResult('id', 'id', 'integer') 
 134                         ->addIndexByScalar('id'); 
 138                         ->createNativeQuery($req, $rsm) 
 139                         ->setParameter('lid', $location) 
 140                         ->setParameter('uid', $user) 
 141                         ->setParameter('gooddelay', self
::SENIOR_DELAY
) 
 142                         ->getOneOrNullResult(); 
 146          * Fetch sessions by date period 
 148          * @param $period The date period 
 149          * @param $locale The locale 
 151         public function fetchAllByDatePeriod($period, $locale = null) { 
 153                 $em = $this->getEntityManager(); 
 156                 $qs = $em->getConfiguration()->getQuoteStrategy(); 
 157                 $dp = $em->getConnection()->getDatabasePlatform(); 
 159                 //Get quoted table names 
 160                 //XXX: this allow to make this code table name independent 
 162                         'RapsysAirBundle:Application' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Application'), $dp), 
 163                         'RapsysAirBundle:Location' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Location'), $dp), 
 164                         'RapsysAirBundle:Session' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Session'), $dp), 
 165                         'RapsysAirBundle:Snippet' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Snippet'), $dp), 
 166                         'RapsysAirBundle:User' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:User'), $dp), 
 173                 //TODO: exclude opera and others ? 
 180         ADDDATE(ADDTIME(s.date, s.begin), INTERVAL IF(s.slot_id = :afterid, 1, 0) DAY) AS start, 
 181         ADDDATE(ADDTIME(ADDTIME(s.date, s.begin), s.length), INTERVAL IF(s.slot_id = :afterid, 1, 0) DAY) AS stop, 
 182         s.location_id AS l_id, 
 183         l.address AS l_address, 
 184         l.zipcode AS l_zipcode, 
 188         l.latitude AS l_latitude, 
 189         l.longitude AS l_longitude, 
 190         s.application_id AS a_id, 
 191         a.canceled AS a_canceled, 
 193         au.forename AS au_forename, 
 194         au.pseudonym AS au_pseudonym, 
 196         p.description AS p_description, 
 200         p.contact AS p_contact, 
 201         p.donate AS p_donate, 
 203         p.profile AS p_profile 
 204 FROM RapsysAirBundle:Session AS s 
 205 JOIN RapsysAirBundle:Location AS l ON (l.id = s.location_id) 
 206 JOIN RapsysAirBundle:Application AS a ON (a.id = s.application_id) 
 207 JOIN RapsysAirBundle:User AS au ON (au.id = a.user_id) 
 208 LEFT JOIN RapsysAirBundle:Snippet AS p ON (p.location_id = s.location_id AND p.user_id = a.user_id AND p.locale = :locale) 
 209 WHERE s.date BETWEEN :begin AND :end 
 213                 //Replace bundle entity name by table name 
 214                 $req = str_replace(array_keys($tables), array_values($tables), $req); 
 216                 //Get result set mapping instance 
 217                 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php 
 218                 $rsm = new ResultSetMapping(); 
 221                 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php 
 222                 //addScalarResult($sqlColName, $resColName, $type = 'string'); 
 223                 $rsm->addScalarResult('id', 'id', 'integer') 
 224                         ->addScalarResult('date', 'date', 'date') 
 225                         ->addScalarResult('locked', 'locked', 'datetime') 
 226                         ->addScalarResult('updated', 'updated', 'datetime') 
 227                         ->addScalarResult('start', 'start', 'datetime') 
 228                         ->addScalarResult('stop', 'stop', 'datetime') 
 229                         ->addScalarResult('l_id', 'l_id', 'integer') 
 230                         ->addScalarResult('l_address', 'l_address', 'string') 
 231                         ->addScalarResult('l_zipcode', 'l_zipcode', 'string') 
 232                         ->addScalarResult('l_city', 'l_city', 'string') 
 233                         ->addScalarResult('l_latitude', 'l_latitude', 'float') 
 234                         ->addScalarResult('l_longitude', 'l_longitude', 'float') 
 235                         ->addScalarResult('l_short', 'l_short', 'string') 
 236                         ->addScalarResult('l_title', 'l_title', 'string') 
 237                         ->addScalarResult('t_id', 't_id', 'integer') 
 238                         ->addScalarResult('t_title', 't_title', 'string') 
 239                         ->addScalarResult('a_id', 'a_id', 'integer') 
 240                         ->addScalarResult('a_canceled', 'a_canceled', 'datetime') 
 241                         ->addScalarResult('au_id', 'au_id', 'integer') 
 242                         ->addScalarResult('au_forename', 'au_forename', 'string') 
 243                         ->addScalarResult('au_pseudonym', 'au_pseudonym', 'string') 
 244                         ->addScalarResult('p_id', 'p_id', 'integer') 
 245                         ->addScalarResult('p_description', 'p_description', 'string') 
 246                         ->addScalarResult('p_class', 'p_class', 'string') 
 247                         ->addScalarResult('p_short', 'p_short', 'string') 
 248                         ->addScalarResult('p_rate', 'p_rate', 'integer') 
 249                         ->addScalarResult('p_contact', 'p_contact', 'string') 
 250                         ->addScalarResult('p_donate', 'p_donate', 'string') 
 251                         ->addScalarResult('p_link', 'p_link', 'string') 
 252                         ->addScalarResult('p_profile', 'p_profile', 'string') 
 253                         ->addIndexByScalar('id'); 
 257                         ->createNativeQuery($req, $rsm) 
 258                         ->setParameter('begin', $period->getStartDate()) 
 259                         ->setParameter('end', $period->getEndDate()) 
 260                         ->setParameter('locale', $locale); 
 263                 return $res->getResult(); 
 267          * Fetch session by id 
 269          * @param $id The session id 
 270          * @param $locale The locale 
 271          * @return array The session data 
 273         public function fetchOneById($id, $locale = null) { 
 275                 $em = $this->getEntityManager(); 
 278                 $qs = $em->getConfiguration()->getQuoteStrategy(); 
 279                 $dp = $em->getConnection()->getDatabasePlatform(); 
 281                 //Get quoted table names 
 282                 //XXX: this allow to make this code table name independent 
 284                         'RapsysAirBundle:Application' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Application'), $dp), 
 285                         'RapsysAirBundle:Group' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Group'), $dp), 
 286                         'RapsysAirBundle:GroupUser' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('groups'), $em->getClassMetadata('RapsysAirBundle:User'), $dp), 
 287                         'RapsysAirBundle:Location' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Location'), $dp), 
 288                         'RapsysAirBundle:Session' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Session'), $dp), 
 289                         'RapsysAirBundle:Snippet' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Snippet'), $dp), 
 290                         'RapsysAirBundle:Slot' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Slot'), $dp), 
 291                         'RapsysAirBundle:User' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:User'), $dp), 
 298                 //TODO: compute scores ? 
 299                 //TODO: compute delivery date ? (J-3/J-4 ?) 
 305         ADDDATE(ADDTIME(s.date, s.begin), INTERVAL IF(s.slot_id = :afterid, 1, 0) DAY) AS start, 
 307         ADDDATE(ADDTIME(ADDTIME(s.date, s.begin), s.length), INTERVAL IF(s.slot_id = :afterid, 1, 0) DAY) AS stop, 
 319         s.location_id AS l_id, 
 322         l.address AS l_address, 
 323         l.zipcode AS l_zipcode, 
 325         l.latitude AS l_latitude, 
 326         l.longitude AS l_longitude, 
 327         l.updated AS l_updated, 
 330         t.updated AS t_updated, 
 331         s.application_id AS a_id, 
 332         a.canceled AS a_canceled, 
 334         au.pseudonym AS au_pseudonym, 
 336         p.description AS p_description, 
 338         p.contact AS p_contact, 
 339         p.donate AS p_donate, 
 341         p.profile AS p_profile, 
 342         p.updated AS p_updated, 
 343         GROUP_CONCAT(sa.id ORDER BY sa.user_id SEPARATOR "\\n") AS sa_id, 
 344         GROUP_CONCAT(IFNULL(sa.score, 'NULL') ORDER BY sa.user_id SEPARATOR "\\n") AS sa_score, 
 345         GROUP_CONCAT(sa.created ORDER BY sa.user_id SEPARATOR "\\n") AS sa_created, 
 346         GROUP_CONCAT(sa.updated ORDER BY sa.user_id SEPARATOR "\\n") AS sa_updated, 
 347         GROUP_CONCAT(IFNULL(sa.canceled, 'NULL') ORDER BY sa.user_id SEPARATOR "\\n") AS sa_canceled, 
 348         GROUP_CONCAT(sa.user_id ORDER BY sa.user_id SEPARATOR "\\n") AS sau_id, 
 349         GROUP_CONCAT(sau.pseudonym ORDER BY sa.user_id SEPARATOR "\\n") AS sau_pseudonym 
 350 FROM RapsysAirBundle:Session AS s 
 351 JOIN RapsysAirBundle:Location AS l ON (l.id = s.location_id) 
 352 JOIN RapsysAirBundle:Slot AS t ON (t.id = s.slot_id) 
 353 LEFT JOIN RapsysAirBundle:Application AS a ON (a.id = s.application_id) 
 354 LEFT JOIN RapsysAirBundle:User AS au ON (au.id = a.user_id) 
 355 LEFT JOIN RapsysAirBundle:Snippet AS p ON (p.location_id = s.location_id AND p.user_id = a.user_id AND p.locale = :locale) 
 356 LEFT JOIN RapsysAirBundle:Application AS sa ON (sa.session_id = s.id) 
 357 LEFT JOIN RapsysAirBundle:User AS sau ON (sau.id = sa.user_id) 
 363                 //Replace bundle entity name by table name 
 364                 $req = str_replace(array_keys($tables), array_values($tables), $req); 
 366                 //Get result set mapping instance 
 367                 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php 
 368                 $rsm = new ResultSetMapping(); 
 371                 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php 
 372                 $rsm->addScalarResult('id', 'id', 'integer') 
 373                         ->addScalarResult('date', 'date', 'date') 
 374                         ->addScalarResult('begin', 'begin', 'time') 
 375                         ->addScalarResult('start', 'start', 'datetime') 
 376                         ->addScalarResult('length', 'length', 'time') 
 377                         ->addScalarResult('stop', 'stop', 'datetime') 
 378                         ->addScalarResult('rainfall', 'rainfall', 'float') 
 379                         ->addScalarResult('rainrisk', 'rainrisk', 'float') 
 380                         ->addScalarResult('realfeel', 'realfeel', 'float') 
 381                         ->addScalarResult('realfeelmin', 'realfeelmin', 'float') 
 382                         ->addScalarResult('realfeelmax', 'realfeelmax', 'float') 
 383                         ->addScalarResult('temperature', 'temperature', 'float') 
 384                         ->addScalarResult('temperaturemin', 'temperaturemin', 'float') 
 385                         ->addScalarResult('temperaturemax', 'temperaturemax', 'float') 
 386                         ->addScalarResult('locked', 'locked', 'datetime') 
 387                         ->addScalarResult('created', 'created', 'datetime') 
 388                         ->addScalarResult('updated', 'updated', 'datetime') 
 389                         ->addScalarResult('l_id', 'l_id', 'integer') 
 390                         ->addScalarResult('l_short', 'l_short', 'string') 
 391                         ->addScalarResult('l_title', 'l_title', 'string') 
 392                         ->addScalarResult('l_address', 'l_address', 'string') 
 393                         ->addScalarResult('l_zipcode', 'l_zipcode', 'string') 
 394                         ->addScalarResult('l_city', 'l_city', 'string') 
 395                         ->addScalarResult('l_latitude', 'l_latitude', 'float') 
 396                         ->addScalarResult('l_longitude', 'l_longitude', 'float') 
 397                         ->addScalarResult('l_updated', 'l_updated', 'datetime') 
 398                         ->addScalarResult('t_id', 't_id', 'integer') 
 399                         ->addScalarResult('t_title', 't_title', 'string') 
 400                         ->addScalarResult('t_updated', 't_updated', 'datetime') 
 401                         ->addScalarResult('a_id', 'a_id', 'integer') 
 402                         ->addScalarResult('a_canceled', 'a_canceled', 'datetime') 
 403                         ->addScalarResult('au_id', 'au_id', 'integer') 
 404                         ->addScalarResult('au_pseudonym', 'au_pseudonym', 'string') 
 405                         ->addScalarResult('p_id', 'p_id', 'integer') 
 406                         ->addScalarResult('p_description', 'p_description', 'text') 
 407                         ->addScalarResult('p_class', 'p_class', 'text') 
 408                         ->addScalarResult('p_contact', 'p_contact', 'text') 
 409                         ->addScalarResult('p_donate', 'p_donate', 'text') 
 410                         ->addScalarResult('p_link', 'p_link', 'text') 
 411                         ->addScalarResult('p_profile', 'p_profile', 'text') 
 412                         ->addScalarResult('p_updated', 'p_updated', 'datetime') 
 413                         //XXX: is a string because of \n separator 
 414                         ->addScalarResult('sa_id', 'sa_id', 'string') 
 415                         //XXX: is a string because of \n separator 
 416                         ->addScalarResult('sa_score', 'sa_score', 'string') 
 417                         //XXX: is a string because of \n separator 
 418                         ->addScalarResult('sa_created', 'sa_created', 'string') 
 419                         //XXX: is a string because of \n separator 
 420                         ->addScalarResult('sa_updated', 'sa_updated', 'string') 
 421                         //XXX: is a string because of \n separator 
 422                         ->addScalarResult('sa_canceled', 'sa_canceled', 'string') 
 423                         //XXX: is a string because of \n separator 
 424                         ->addScalarResult('sau_id', 'sau_id', 'string') 
 425                         //XXX: is a string because of \n separator 
 426                         ->addScalarResult('sau_pseudonym', 'sau_pseudonym', 'string') 
 427                         ->addIndexByScalar('id'); 
 431                         ->createNativeQuery($req, $rsm) 
 432                         ->setParameter('sid', $id) 
 433                         ->setParameter('locale', $locale) 
 434                         ->getOneOrNullResult(); 
 438          * Fetch sessions calendar with translated location by date period 
 440          * @param $translator The TranslatorInterface instance 
 441          * @param $period The date period 
 442          * @param $locationId The location id 
 443          * @param $sessionId The session id 
 444          * @param $granted The session is granted 
 446         public function fetchCalendarByDatePeriod(TranslatorInterface 
$translator, $period, $locationId = null, $sessionId = null, $granted = false) { 
 448                 $em = $this->getEntityManager(); 
 451                 $qs = $em->getConfiguration()->getQuoteStrategy(); 
 452                 $dp = $em->getConnection()->getDatabasePlatform(); 
 454                 //Get quoted table names 
 455                 //XXX: this allow to make this code table name independent 
 457                         'RapsysAirBundle:GroupUser' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('groups'), $em->getClassMetadata('RapsysAirBundle:User'), $dp), 
 458                         'RapsysAirBundle:Session' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Session'), $dp), 
 459                         'RapsysAirBundle:Application' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Application'), $dp), 
 460                         'RapsysAirBundle:Group' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Group'), $dp), 
 461                         'RapsysAirBundle:Location' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Location'), $dp), 
 462                         'RapsysAirBundle:Slot' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Slot'), $dp), 
 463                         'RapsysAirBundle:User' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:User'), $dp), 
 472                 //When granted is set 
 473                 if (empty($granted)) { 
 474                         //Set application and user as optional 
 481                 //When location id is set 
 482                 if (!empty($locationId)) { 
 483                         //Add location id clause 
 484                         $locationSql = "\n\t".'AND s.location_id = :lid'; 
 497         ADDDATE(ADDTIME(s.date, s.begin), INTERVAL IF(s.slot_id = :afterid, 1, 0) DAY) AS start, 
 498         ADDDATE(ADDTIME(ADDTIME(s.date, s.begin), s.length), INTERVAL IF(s.slot_id = :afterid, 1, 0) DAY) AS stop, 
 499         s.location_id AS l_id, 
 504         s.application_id AS a_id, 
 505         a.canceled AS a_canceled, 
 507         au.pseudonym AS au_pseudonym, 
 508         GROUP_CONCAT(sa.user_id ORDER BY sa.user_id SEPARATOR "\\n") AS sau_id, 
 509         GROUP_CONCAT(sau.pseudonym ORDER BY sa.user_id SEPARATOR "\\n") AS sau_pseudonym 
 510 FROM RapsysAirBundle:Session AS s 
 511 JOIN RapsysAirBundle:Location AS l ON (l.id = s.location_id) 
 512 JOIN RapsysAirBundle:Slot AS t ON (t.id = s.slot_id) 
 513 ${grantSql}JOIN RapsysAirBundle:Application AS a ON (a.id = s.application_id) 
 514 ${grantSql}JOIN RapsysAirBundle:User AS au ON (au.id = a.user_id) 
 515 LEFT JOIN RapsysAirBundle:Application AS sa ON (sa.session_id = s.id) 
 516 LEFT JOIN RapsysAirBundle:User AS sau ON (sau.id = sa.user_id) 
 517 WHERE s.date BETWEEN :begin AND :end${locationSql} 
 522                 //Replace bundle entity name by table name 
 523                 $req = str_replace(array_keys($tables), array_values($tables), $req); 
 525                 //Get result set mapping instance 
 526                 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php 
 527                 $rsm = new ResultSetMapping(); 
 530                 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php 
 531                 //addScalarResult($sqlColName, $resColName, $type = 'string'); 
 532                 $rsm->addScalarResult('id', 'id', 'integer') 
 533                         ->addScalarResult('date', 'date', 'date') 
 534                         ->addScalarResult('rainrisk', 'rainrisk', 'float') 
 535                         ->addScalarResult('rainfall', 'rainfall', 'float') 
 536                         ->addScalarResult('realfeel', 'realfeel', 'float') 
 537                         ->addScalarResult('temperature', 'temperature', 'float') 
 538                         ->addScalarResult('locked', 'locked', 'datetime') 
 539                         ->addScalarResult('start', 'start', 'datetime') 
 540                         ->addScalarResult('stop', 'stop', 'datetime') 
 541                         ->addScalarResult('t_id', 't_id', 'integer') 
 542                         ->addScalarResult('t_title', 't_title', 'string') 
 543                         ->addScalarResult('l_id', 'l_id', 'integer') 
 544                         ->addScalarResult('l_short', 'l_short', 'string') 
 545                         ->addScalarResult('l_title', 'l_title', 'string') 
 546                         ->addScalarResult('a_id', 'a_id', 'integer') 
 547                         ->addScalarResult('a_canceled', 'a_canceled', 'datetime') 
 548                         ->addScalarResult('au_id', 'au_id', 'integer') 
 549                         ->addScalarResult('au_pseudonym', 'au_pseudonym', 'string') 
 550                         //XXX: is a string because of \n separator 
 551                         ->addScalarResult('sau_id', 'sau_id', 'string') 
 552                         //XXX: is a string because of \n separator 
 553                         ->addScalarResult('sau_pseudonym', 'sau_pseudonym', 'string') 
 554                         ->addIndexByScalar('id'); 
 558                         ->createNativeQuery($req, $rsm) 
 559                         ->setParameter('begin', $period->getStartDate()) 
 560                         ->setParameter('end', $period->getEndDate()); 
 562                 //Add optional location id 
 563                 if (!empty($locationId)) { 
 564                         $res->setParameter('lid', $locationId); 
 568                 $res = $res->getResult(); 
 576                 //Iterate on each day 
 577                 foreach($period as $date) { 
 578                         //Init day in calendar 
 579                         $calendar[$Ymd = $date->format('Ymd')] = [ 
 580                                 'title' => $translator->trans($date->format('l')).' '.$date->format('d'), 
 585                         //Detect month change 
 586                         if ($month != $date->format('m')) { 
 587                                 $month = $date->format('m'); 
 588                                 //Append month for first day of month 
 589                                 //XXX: except if today to avoid double add 
 590                                 if ($date->format('U') != strtotime('today')) { 
 591                                         $calendar[$Ymd]['title'] .= '/'.$month; 
 595                         if ($date->format('U') == ($today = strtotime('today'))) { 
 596                                 $calendar[$Ymd]['title'] .= '/'.$month; 
 597                                 $calendar[$Ymd]['current'] = true; 
 598                                 $calendar[$Ymd]['class'][] = 'current'; 
 600                         //Disable passed days 
 601                         if ($date->format('U') < $today) { 
 602                                 $calendar[$Ymd]['disabled'] = true; 
 603                                 $calendar[$Ymd]['class'][] = 'disabled'; 
 605                         //Set next month days 
 606                         if ($date->format('m') > date('m')) { 
 607                                 $calendar[$Ymd]['next'] = true; 
 608                                 #$calendar[$Ymd]['class'][] = 'next'; 
 612                         if ($date->format('w') == 0) { 
 613                                 $calendar[$Ymd]['class'][] = 'sunday'; 
 616                         //Iterate on each session to find the one of the day 
 617                         foreach($res as $session) { 
 618                                 if (($sessionYmd = $session['date']->format('Ymd')) == $Ymd) { 
 619                                         //Count number of application 
 620                                         $count = count(explode("\n", $session['sau_id'])); 
 624                                         if (!empty($session['a_id'])) { 
 625                                                 $applications = [ $session['au_id'] => $session['au_pseudonym'] ]; 
 626                                                 if (!empty($session['a_canceled'])) { 
 627                                                         $class[] = 'canceled'; 
 629                                                         $class[] = 'granted'; 
 631                                         } elseif ($count > 1) { 
 632                                                 $class[] = 'disputed'; 
 633                                         } elseif (!empty($session['locked'])) { 
 636                                                 $class[] = 'pending'; 
 639                                         if ($sessionId == $session['id']) { 
 640                                                 $class[] = 'highlight'; 
 644                                         //XXX: realfeel may be null, temperature should not 
 645                                         $temperature = $session['realfeel'] !== null ? $session['realfeel'] : $session['temperature']; 
 648                                         //XXX: rainfall may be null 
 649                                         if ($session['rainrisk'] > 0.50 || $session['rainfall'] > 2) { 
 650                                                 $weather = self
::GLYPHS
['Stormy']; 
 651                                         } elseif ($session['rainrisk'] > 0.40 || $session['rainfall'] > 1) { 
 652                                                 $weather = self
::GLYPHS
['Rainy']; 
 653                                         } elseif ($temperature > 24) { 
 654                                                 $weather = self
::GLYPHS
['Cleary']; 
 655                                         } elseif ($temperature > 17) { 
 656                                                 $weather = self
::GLYPHS
['Sunny']; 
 657                                         } elseif ($temperature > 10) { 
 658                                                 $weather = self
::GLYPHS
['Cloudy']; 
 659                                         } elseif ($temperature !== null) { 
 660                                                 $weather = self
::GLYPHS
['Winty']; 
 668                                         //Check if realfeel is available 
 669                                         if ($session['realfeel'] !== null) { 
 670                                                 $weathertitle[] = $session['realfeel'].'Β°R'; 
 673                                         //Check if temperature is available 
 674                                         if ($session['temperature'] !== null) { 
 675                                                 $weathertitle[] = $session['temperature'].'Β°C'; 
 678                                         //Check if rainrisk is available 
 679                                         if ($session['rainrisk'] !== null) { 
 680                                                 $weathertitle[] = ($session['rainrisk']*100).'%'; 
 683                                         //Check if rainfall is available 
 684                                         if ($session['rainfall'] !== null) { 
 685                                                 $weathertitle[] = $session['rainfall'].'mm'; 
 690                                                 0 => $translator->trans($session['t_title']).' '.$translator->trans('at '.$session['l_title']).$translator->trans(':') 
 693                                         //Fetch pseudonyms from session applications 
 694                                         $applications +
= array_combine(explode("\n", $session['sau_id']), array_map(function ($v) {return '- '.$v
;}, explode("\n", $session['sau_pseudonym']))); 
 699                                         //Check that session is not granted 
 700                                         if (empty($session['a_id'])) { 
 701                                                 //With location id and unique application 
 703                                                         //Set unique application pseudonym 
 704                                                         $pseudonym = $session['sau_pseudonym']; 
 708                                                 //Replace granted application 
 709                                                 $applications[$session['au_id']] = '* '.$session['au_pseudonym']; 
 712                                                 $pseudonym = $session['au_pseudonym'].($count > 1 ? ' ['.$count.']':''); 
 716                                         $calendar[$Ymd]['sessions'][$session['t_id'].sprintf('%05d', $session['id'])] = [ 
 717                                                 'id' => $session['id'], 
 718                                                 'start' => $session['start'], 
 719                                                 'stop' => $session['stop'], 
 720                                                 'location' => $translator->trans($session['l_short']), 
 721                                                 'pseudonym' => $pseudonym, 
 723                                                 'slot' => self
::GLYPHS
[$session['t_title']], 
 724                                                 'slottitle' => $translator->trans($session['t_title']), 
 725                                                 'weather' => $weather, 
 726                                                 'weathertitle' => implode(' ', $weathertitle), 
 727                                                 'applications' => $applications 
 733                         ksort($calendar[$Ymd]['sessions']); 
 741          * Fetch sessions calendar with translated location by date period and user 
 743          * @param $translator The TranslatorInterface instance 
 744          * @param $period The date period 
 745          * @param $userId The user id 
 746          * @param $sessionId The session id 
 748         public function fetchUserCalendarByDatePeriod(TranslatorInterface 
$translator, $period, $userId = null, $sessionId = null) { 
 750                 $em = $this->getEntityManager(); 
 753                 $qs = $em->getConfiguration()->getQuoteStrategy(); 
 754                 $dp = $em->getConnection()->getDatabasePlatform(); 
 756                 //Get quoted table names 
 757                 //XXX: this allow to make this code table name independent 
 759                         'RapsysAirBundle:GroupUser' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('groups'), $em->getClassMetadata('RapsysAirBundle:User'), $dp), 
 760                         'RapsysAirBundle:Session' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Session'), $dp), 
 761                         'RapsysAirBundle:Application' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Application'), $dp), 
 762                         'RapsysAirBundle:Group' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Group'), $dp), 
 763                         'RapsysAirBundle:Location' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Location'), $dp), 
 764                         'RapsysAirBundle:Slot' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Slot'), $dp), 
 765                         'RapsysAirBundle:User' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:User'), $dp), 
 772                 $userJoinSql = $userWhereSql = ''; 
 774                 //When user id is set 
 775                 if (!empty($userId)) { 
 777                         $userJoinSql = 'JOIN RapsysAirBundle:Application AS sua ON (sua.session_id = s.id)'."\n"; 
 779                         $userWhereSql = "\n\t".'AND sua.user_id = :uid'; 
 783                 //TODO: change as_u_* in sau_*, a_u_* in au_*, etc, see request up 
 793         ADDDATE(ADDTIME(s.date, s.begin), INTERVAL IF(s.slot_id = :afterid, 1, 0) DAY) AS start, 
 794         ADDDATE(ADDTIME(ADDTIME(s.date, s.begin), s.length), INTERVAL IF(s.slot_id = :afterid, 1, 0) DAY) AS stop, 
 795         s.location_id AS l_id, 
 800         s.application_id AS a_id, 
 802         au.pseudonym AS au_pseudonym, 
 803         GROUP_CONCAT(sa.user_id ORDER BY sa.user_id SEPARATOR "\\n") AS sau_id, 
 804         GROUP_CONCAT(CONCAT("- ", sau.pseudonym) ORDER BY sa.user_id SEPARATOR "\\n") AS sau_pseudonym 
 805 FROM RapsysAirBundle:Session AS s 
 806 JOIN RapsysAirBundle:Location AS l ON (l.id = s.location_id) 
 807 JOIN RapsysAirBundle:Slot AS t ON (t.id = s.slot_id) 
 808 ${userJoinSql}LEFT JOIN RapsysAirBundle:Application AS a ON (a.id = s.application_id) 
 809 LEFT JOIN RapsysAirBundle:User AS au ON (au.id = a.user_id) 
 810 LEFT JOIN RapsysAirBundle:Application AS sa ON (sa.session_id = s.id) 
 811 LEFT JOIN RapsysAirBundle:User AS sau ON (sau.id = sa.user_id) 
 812 WHERE s.date BETWEEN :begin AND :end${userWhereSql} 
 817                 //Replace bundle entity name by table name 
 818                 $req = str_replace(array_keys($tables), array_values($tables), $req); 
 820                 //Get result set mapping instance 
 821                 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php 
 822                 $rsm = new ResultSetMapping(); 
 825                 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php 
 826                 //addScalarResult($sqlColName, $resColName, $type = 'string'); 
 827                 $rsm->addScalarResult('id', 'id', 'integer') 
 828                         ->addScalarResult('date', 'date', 'date') 
 829                         ->addScalarResult('rainrisk', 'rainrisk', 'float') 
 830                         ->addScalarResult('rainfall', 'rainfall', 'float') 
 831                         ->addScalarResult('realfeel', 'realfeel', 'float') 
 832                         ->addScalarResult('temperature', 'temperature', 'float') 
 833                         ->addScalarResult('locked', 'locked', 'datetime') 
 834                         ->addScalarResult('start', 'start', 'datetime') 
 835                         ->addScalarResult('stop', 'stop', 'datetime') 
 836                         ->addScalarResult('t_id', 't_id', 'integer') 
 837                         ->addScalarResult('t_title', 't_title', 'string') 
 838                         ->addScalarResult('l_id', 'l_id', 'integer') 
 839                         ->addScalarResult('l_short', 'l_short', 'string') 
 840                         ->addScalarResult('l_title', 'l_title', 'string') 
 841                         ->addScalarResult('a_id', 'a_id', 'integer') 
 842                         ->addScalarResult('au_id', 'au_id', 'integer') 
 843                         ->addScalarResult('au_pseudonym', 'au_pseudonym', 'string') 
 844                         //XXX: is a string because of \n separator 
 845                         ->addScalarResult('sau_id', 'sau_id', 'string') 
 846                         //XXX: is a string because of \n separator 
 847                         ->addScalarResult('sau_pseudonym', 'sau_pseudonym', 'string') 
 848                         ->addIndexByScalar('id'); 
 852                         ->createNativeQuery($req, $rsm) 
 853                         ->setParameter('begin', $period->getStartDate()) 
 854                         ->setParameter('end', $period->getEndDate()) 
 855                         ->setParameter('uid', $userId) 
 864                 //Iterate on each day 
 865                 foreach($period as $date) { 
 866                         //Init day in calendar 
 867                         $calendar[$Ymd = $date->format('Ymd')] = [ 
 868                                 'title' => $translator->trans($date->format('l')).' '.$date->format('d'), 
 873                         //Detect month change 
 874                         if ($month != $date->format('m')) { 
 875                                 $month = $date->format('m'); 
 876                                 //Append month for first day of month 
 877                                 //XXX: except if today to avoid double add 
 878                                 if ($date->format('U') != strtotime('today')) { 
 879                                         $calendar[$Ymd]['title'] .= '/'.$month; 
 883                         if ($date->format('U') == ($today = strtotime('today'))) { 
 884                                 $calendar[$Ymd]['title'] .= '/'.$month; 
 885                                 $calendar[$Ymd]['current'] = true; 
 886                                 $calendar[$Ymd]['class'][] = 'current'; 
 888                         //Disable passed days 
 889                         if ($date->format('U') < $today) { 
 890                                 $calendar[$Ymd]['disabled'] = true; 
 891                                 $calendar[$Ymd]['class'][] = 'disabled'; 
 893                         //Set next month days 
 894                         if ($date->format('m') > date('m')) { 
 895                                 $calendar[$Ymd]['next'] = true; 
 896                                 #$calendar[$Ymd]['class'][] = 'next'; 
 900                         if ($date->format('w') == 0) { 
 901                                 $calendar[$Ymd]['class'][] = 'sunday'; 
 904                         //Iterate on each session to find the one of the day 
 905                         foreach($res as $session) { 
 906                                 if (($sessionYmd = $session['date']->format('Ymd')) == $Ymd) { 
 907                                         //Count number of application 
 908                                         $count = count(explode("\n", $session['sau_id'])); 
 912                                         if (!empty($session['a_id'])) { 
 913                                                 $applications = [ $session['au_id'] => $session['au_pseudonym'] ]; 
 914                                                 if ($session['au_id'] == $userId) { 
 915                                                         $class[] = 'granted'; 
 917                                                         $class[] = 'disputed'; 
 919                                         } elseif ($count > 1) { 
 920                                                 $class[] = 'disputed'; 
 921                                         } elseif (!empty($session['locked'])) { 
 924                                                 $class[] = 'pending'; 
 927                                         if ($sessionId == $session['id']) { 
 928                                                 $class[] = 'highlight'; 
 932                                         //XXX: realfeel may be null, temperature should not 
 933                                         $temperature = $session['realfeel'] !== null ? $session['realfeel'] : $session['temperature']; 
 936                                         //XXX: rainfall may be null 
 937                                         if ($session['rainrisk'] > 0.50 || $session['rainfall'] > 2) { 
 938                                                 $weather = self
::GLYPHS
['Stormy']; 
 939                                         } elseif ($session['rainrisk'] > 0.40 || $session['rainfall'] > 1) { 
 940                                                 $weather = self
::GLYPHS
['Rainy']; 
 941                                         } elseif ($temperature > 24) { 
 942                                                 $weather = self
::GLYPHS
['Cleary']; 
 943                                         } elseif ($temperature > 17) { 
 944                                                 $weather = self
::GLYPHS
['Sunny']; 
 945                                         } elseif ($temperature > 10) { 
 946                                                 $weather = self
::GLYPHS
['Cloudy']; 
 947                                         } elseif ($temperature !== null) { 
 948                                                 $weather = self
::GLYPHS
['Winty']; 
 956                                         //Check if realfeel is available 
 957                                         if ($session['realfeel'] !== null) { 
 958                                                 $weathertitle[] = $session['realfeel'].'Β°R'; 
 961                                         //Check if temperature is available 
 962                                         if ($session['temperature'] !== null) { 
 963                                                 $weathertitle[] = $session['temperature'].'Β°C'; 
 966                                         //Check if rainrisk is available 
 967                                         if ($session['rainrisk'] !== null) { 
 968                                                 $weathertitle[] = ($session['rainrisk']*100).'%'; 
 971                                         //Check if rainfall is available 
 972                                         if ($session['rainfall'] !== null) { 
 973                                                 $weathertitle[] = $session['rainfall'].'mm'; 
 978                                                 0 => $translator->trans($session['t_title']).' '.$translator->trans('at '.$session['l_title']).$translator->trans(':') 
 981                                         //Fetch pseudonyms from session applications 
 982                                         $applications +
= array_combine(explode("\n", $session['sau_id']), array_map(function ($v) {return '- '.$v
;}, explode("\n", $session['sau_pseudonym']))); 
 987                                         //Check that session is not granted 
 988                                         if (empty($session['a_id'])) { 
 989                                                 //With location id and unique application 
 991                                                         //Set unique application pseudonym 
 992                                                         $pseudonym = $session['sau_pseudonym']; 
 996                                                 //Replace granted application 
 997                                                 $applications[$session['au_id']] = '* '.$session['au_pseudonym']; 
1000                                                 $pseudonym = $session['au_pseudonym'].($count > 1 ? ' ['.$count.']':''); 
1004                                         $title = $translator->trans($session['l_title']).($count > 1 ? ' ['.$count.']':''); 
1007                                         $calendar[$Ymd]['sessions'][$session['t_id'].sprintf('%02d', $session['l_id'])] = [ 
1008                                                 'id' => $session['id'], 
1009                                                 'start' => $session['start'], 
1010                                                 'stop' => $session['stop'], 
1011                                                 'location' => $translator->trans($session['l_short']), 
1012                                                 'pseudonym' => $pseudonym, 
1014                                                 'slot' => self
::GLYPHS
[$session['t_title']], 
1015                                                 'slottitle' => $translator->trans($session['t_title']), 
1016                                                 'weather' => $weather, 
1017                                                 'weathertitle' => implode(' ', $weathertitle), 
1018                                                 'applications' => $applications 
1024                         ksort($calendar[$Ymd]['sessions']); 
1032          * Find all session pending hourly weather 
1034          * @return array<Session> The sessions to update 
1036         public function findAllPendingHourlyWeather() { 
1037                 //Get entity manager 
1038                 $em = $this->getEntityManager(); 
1040                 //Get quote strategy 
1041                 $qs = $em->getConfiguration()->getQuoteStrategy(); 
1042                 $dp = $em->getConnection()->getDatabasePlatform(); 
1044                 //Get quoted table names 
1045                 //XXX: this allow to make this code table name independent 
1047                         'RapsysAirBundle:Session' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Session'), $dp), 
1048                         'RapsysAirBundle:Location' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Location'), $dp), 
1050                         ':accuhourly' => self
::ACCUWEATHER_HOURLY
, 
1057                 //Select all sessions starting and stopping in the next 3 days 
1058                 //XXX: select session starting after now and stopping before date(now)+3d as accuweather only provide hourly data for the next 3 days (INTERVAL 3 DAY) 
1060 SELECT s.id, s.slot_id, s.location_id, s.date, s.begin, s.length, s.rainfall, s.rainrisk, s.realfeel, s.realfeelmin, s.realfeelmax, s.temperature, s.temperaturemin, s.temperaturemax, l.zipcode 
1061 FROM RapsysAirBundle:Session AS s 
1062 JOIN RapsysAirBundle:Location AS l ON (l.id = s.location_id) 
1063 WHERE ADDDATE(ADDTIME(s.date, s.begin), INTERVAL IF(s.slot_id = :afterid, 1, 0) DAY) >= NOW() AND ADDDATE(ADDTIME(ADDTIME(s.date, s.begin), s.length), INTERVAL IF(s.slot_id = :afterid, 1, 0) DAY) < DATE(ADDDATE(NOW(), INTERVAL :accuhourly DAY)) 
1066                 //Replace bundle entity name by table name 
1067                 $req = str_replace(array_keys($tables), array_values($tables), $req); 
1069                 //Get result set mapping instance 
1070                 $rsm = new ResultSetMapping(); 
1072                 //Declare all fields 
1074                         ->addEntityResult('RapsysAirBundle:Session', 's') 
1075                         ->addFieldResult('s', 'id', 'id') 
1076                         ->addFieldResult('s', 'date', 'date') 
1077                         ->addFieldResult('s', 'begin', 'begin') 
1078                         ->addFieldResult('s', 'length', 'length') 
1079                         ->addFieldResult('s', 'rainfall', 'rainfall') 
1080                         ->addFieldResult('s', 'rainrisk', 'rainrisk') 
1081                         ->addFieldResult('s', 'realfeel', 'realfeel') 
1082                         ->addFieldResult('s', 'realfeelmin', 'realfeelmin') 
1083                         ->addFieldResult('s', 'realfeelmax', 'realfeelmax') 
1084                         ->addFieldResult('s', 'temperature', 'temperature') 
1085                         ->addFieldResult('s', 'temperaturemin', 'temperaturemin') 
1086                         ->addFieldResult('s', 'temperaturemax', 'temperaturemax') 
1087                         ->addJoinedEntityResult('RapsysAirBundle:Slot', 'o', 's', 'slot') 
1088                         ->addFieldResult('o', 'slot_id', 'id') 
1089                         ->addJoinedEntityResult('RapsysAirBundle:Location', 'l', 's', 'location') 
1090                         ->addFieldResult('l', 'location_id', 'id') 
1091                         ->addFieldResult('l', 'zipcode', 'zipcode') 
1092                         ->addIndexBy('s', 'id'); 
1096                         ->createNativeQuery($req, $rsm) 
1101          * Find all session pending daily weather 
1103          * @return array<Session> The sessions to update 
1105         public function findAllPendingDailyWeather() { 
1106                 //Get entity manager 
1107                 $em = $this->getEntityManager(); 
1109                 //Get quote strategy 
1110                 $qs = $em->getConfiguration()->getQuoteStrategy(); 
1111                 $dp = $em->getConnection()->getDatabasePlatform(); 
1113                 //Get quoted table names 
1114                 //XXX: this allow to make this code table name independent 
1116                         'RapsysAirBundle:Session' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Session'), $dp), 
1117                         'RapsysAirBundle:Location' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Location'), $dp), 
1119                         ':accudaily' => self
::ACCUWEATHER_DAILY
, 
1120                         ':accuhourly' => self
::ACCUWEATHER_HOURLY
, 
1127                 //Select all sessions stopping after next 3 days 
1128                 //XXX: select session stopping after or equal date(now)+3d as accuweather only provide hourly data for the next 3 days (INTERVAL 3 DAY) 
1130 SELECT s.id, s.slot_id, s.location_id, s.date, s.begin, s.length, s.rainfall, s.rainrisk, s.realfeel, s.realfeelmin, s.realfeelmax, s.temperature, s.temperaturemin, s.temperaturemax, l.zipcode 
1131 FROM RapsysAirBundle:Session AS s 
1132 JOIN RapsysAirBundle:Location AS l ON (l.id = s.location_id) 
1133 WHERE ADDDATE(ADDTIME(ADDTIME(s.date, s.begin), s.length), INTERVAL IF(s.slot_id = :afterid, 1, 0) DAY) >= DATE(ADDDATE(NOW(), INTERVAL :accuhourly DAY)) AND ADDDATE(ADDTIME(ADDTIME(s.date, s.begin), s.length), INTERVAL IF(s.slot_id = :afterid, 1, 0) DAY) < DATE(ADDDATE(NOW(), INTERVAL :accudaily DAY)) 
1136                 //Replace bundle entity name by table name 
1137                 $req = str_replace(array_keys($tables), array_values($tables), $req); 
1139                 //Get result set mapping instance 
1140                 $rsm = new ResultSetMapping(); 
1142                 //Declare all fields 
1144                         ->addEntityResult('RapsysAirBundle:Session', 's') 
1145                         ->addFieldResult('s', 'id', 'id') 
1146                         ->addFieldResult('s', 'date', 'date') 
1147                         ->addFieldResult('s', 'begin', 'begin') 
1148                         ->addFieldResult('s', 'length', 'length') 
1149                         ->addFieldResult('s', 'rainfall', 'rainfall') 
1150                         ->addFieldResult('s', 'rainrisk', 'rainrisk') 
1151                         ->addFieldResult('s', 'realfeel', 'realfeel') 
1152                         ->addFieldResult('s', 'realfeelmin', 'realfeelmin') 
1153                         ->addFieldResult('s', 'realfeelmax', 'realfeelmax') 
1154                         ->addFieldResult('s', 'temperature', 'temperature') 
1155                         ->addFieldResult('s', 'temperaturemin', 'temperaturemin') 
1156                         ->addFieldResult('s', 'temperaturemax', 'temperaturemax') 
1157                         ->addJoinedEntityResult('RapsysAirBundle:Slot', 'o', 's', 'slot') 
1158                         ->addFieldResult('o', 'slot_id', 'id') 
1159                         ->addJoinedEntityResult('RapsysAirBundle:Location', 'l', 's', 'location') 
1160                         ->addFieldResult('l', 'location_id', 'id') 
1161                         ->addFieldResult('l', 'zipcode', 'zipcode') 
1162                         ->addIndexBy('s', 'id'); 
1166                         ->createNativeQuery($req, $rsm) 
1171          * Find every session pending application 
1173          * @return array<Session> The sessions to update 
1175         public function findAllPendingApplication() { 
1176                 //Get entity manager 
1177                 $em = $this->getEntityManager(); 
1179                 //Get quote strategy 
1180                 $qs = $em->getConfiguration()->getQuoteStrategy(); 
1181                 $dp = $em->getConnection()->getDatabasePlatform(); 
1183                 //Get quoted table names 
1184                 //XXX: this allow to make this code table name independent 
1186                         'RapsysAirBundle:Application' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Application'), $dp), 
1187                         'RapsysAirBundle:Session' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Session'), $dp), 
1189                         ':regulardelay' => self
::REGULAR_DELAY 
* 24 * 3600, 
1190                         ':seniordelay' => self
::SENIOR_DELAY 
* 24 * 3600, 
1197                 //Select all sessions not locked without application or canceled application within attribution period 
1198                 //XXX: DIFF(start, now) <= IF(DIFF(start, created) <= SENIOR_DELAY in DAY, DIFF(start, created) * 3 / 4, SENIOR_DELAY) 
1199                 //TODO: remonter les donnΓ©es pour le mail ? 
1202 FROM RapsysAirBundle:Session as s 
1203 LEFT JOIN RapsysAirBundle:Application AS a ON (a.id = s.application_id AND a.canceled IS NULL) 
1204 JOIN RapsysAirBundle:Application AS a2 ON (a2.session_id = s.id AND a2.canceled IS NULL) 
1205 WHERE s.locked IS NULL AND a.id IS NULL AND 
1206 TIME_TO_SEC(TIMEDIFF(@dt_start := ADDDATE(ADDTIME(s.date, s.begin), INTERVAL IF(s.slot_id = :afterid, 1, 0) DAY), NOW())) <= IF( 
1207         TIME_TO_SEC(@td_sc := TIMEDIFF(@dt_start, s.created)) <= :seniordelay, 
1208         ROUND(TIME_TO_SEC(@td_sc) * :regulardelay / :seniordelay), 
1212 ORDER BY @dt_start ASC, s.created ASC 
1216                 //Replace bundle entity name by table name 
1217                 $req = str_replace(array_keys($tables), array_values($tables), $req); 
1219                 //Get result set mapping instance 
1220                 $rsm = new ResultSetMapping(); 
1222                 //Declare all fields 
1224                         ->addEntityResult('RapsysAirBundle:Session', 's') 
1225                         ->addFieldResult('s', 'id', 'id') 
1226                         ->addIndexBy('s', 'id'); 
1230                         ->createNativeQuery($req, $rsm) 
1235          * Fetch session best application by session id 
1237          * @param int $id The session id 
1238          * @return Application|null The application or null 
1240         public function findBestApplicationById($id) { 
1241                 //Get entity manager 
1242                 $em = $this->getEntityManager(); 
1244                 //Get quote strategy 
1245                 $qs = $em->getConfiguration()->getQuoteStrategy(); 
1246                 $dp = $em->getConnection()->getDatabasePlatform(); 
1248                 //Get quoted table names 
1249                 //XXX: this allow to make this code table name independent 
1251                         'RapsysAirBundle:Application' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Application'), $dp), 
1252                         'RapsysAirBundle:GroupUser' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('groups'), $em->getClassMetadata('RapsysAirBundle:User'), $dp), 
1253                         'RapsysAirBundle:Location' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Location'), $dp), 
1254                         'RapsysAirBundle:Session' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Session'), $dp), 
1255                         //XXX: Set limit used to workaround mariadb subselect optimization 
1256                         ':limit' => PHP_INT_MAX
, 
1258                         ':guestdelay' => self
::GUEST_DELAY 
* 24 * 3600, 
1259                         ':regulardelay' => self
::REGULAR_DELAY 
* 24 * 3600, 
1260                         ':seniordelay' => self
::SENIOR_DELAY 
* 24 * 3600, 
1266                         ':afternoonid' => 2, 
1269                         //XXX: days since last session after which guest regain normal priority 
1271                         //XXX: session count until considered at regular delay 
1273                         //XXX: pn_ratio over which considered at regular delay 
1275                         //XXX: tr_ratio diff over which considered at regular delay 
1282                  * Query session applications ranked by location score, global score, created and user_id 
1284                  * @xxx guest (or less) with application on location within 30 day are only considered within guestdelay 
1286                  * @xxx regular (or less) premium application on hotspot are only considered within regulardelay 
1288                  * @xxx senior (or less) with 5 or less session on location are only considered within seniordelay 
1290                  * @xxx senior (or less) with l_pn_ratio >= 1 are only considered within seniordelay 
1292                  * @xxx senior (or less) with l_tr_ratio >= (o_tr_ratio + 5) are only considered within seniordelay 
1294                  * @xxx only consider session within one year (may be unaccurate by the day with after session) 
1296                  * @xxx rainfall may not be accessible for previous session and other session at d-4 (only at d-2) 
1298                  * @todo ??? feedback the data to inform the rejected users ??? 
1301 SELECT e.id, e.l_score AS score 
1313                 MAX(gu.group_id) AS group_id, 
1328                         AVG(IF(a4.id IS NOT NULL AND s4.temperature IS NOT NULL AND s4.rainfall IS NOT NULL, s4.temperature/(1+s4.rainfall), NULL)) AS o_tr_ratio, 
1345                                 SUM(IF(a3.id IS NOT NULL, 1/ABS(DATEDIFF(ADDDATE(b.date, INTERVAL IF(b.slot_id = :afterid, 1, 0) DAY), ADDDATE(s3.date, INTERVAL IF(s3.slot_id = :afterid, 1, 0) DAY))), 0)) AS g_score, 
1358                                         COUNT(a2.id) AS l_count, 
1359                                         SUM(IF(a2.id IS NOT NULL, 1/ABS(DATEDIFF(ADDDATE(s.date, INTERVAL IF(s.slot_id = :afterid, 1, 0) DAY), ADDDATE(s2.date, INTERVAL IF(s2.slot_id = :afterid, 1, 0) DAY))), 0)) AS l_score, 
1360                                         AVG(IF(a2.id IS NOT NULL AND s2.temperature IS NOT NULL AND s2.rainfall IS NOT NULL, s2.temperature/(1+s2.rainfall), NULL)) AS l_tr_ratio, 
1361                                         (SUM(IF(a2.id IS NOT NULL AND s2.premium = 1, 1, 0))+1)/(SUM(IF(a2.id IS NOT NULL AND s2.premium = 0, 1, 0))+1) AS l_pn_ratio, 
1362                                         MIN(IF(a2.id IS NOT NULL, DATEDIFF(ADDDATE(s.date, INTERVAL IF(s.slot_id = :afterid, 1, 0) DAY), ADDDATE(s2.date, INTERVAL IF(s2.slot_id = :afterid, 1, 0) DAY)), NULL)) AS l_previous, 
1363                                         TIME_TO_SEC(TIMEDIFF(ADDDATE(ADDTIME(s.date, s.begin), INTERVAL IF(s.slot_id = :afterid, 1, 0) DAY), NOW())) AS remaining, 
1367                                 FROM RapsysAirBundle:Session AS s 
1368                                 JOIN RapsysAirBundle:Location AS l ON (l.id = s.location_id) 
1369                                 JOIN RapsysAirBundle:Application AS a ON (a.session_id = s.id AND a.canceled IS NULL) 
1370                                 LEFT JOIN RapsysAirBundle:Session AS s2 ON (s2.id != s.id AND s2.location_id = s.location_id AND s2.slot_id IN (:afternoonid, :eveningid) AND s2.application_id IS NOT NULL AND s2.locked IS NULL AND s2.date > s.date - INTERVAL 1 YEAR) 
1371                                 LEFT JOIN RapsysAirBundle:Application AS a2 ON (a2.id = s2.application_id AND a2.user_id = a.user_id AND (a2.canceled IS NULL OR TIMESTAMPDIFF(DAY, a2.canceled, ADDDATE(ADDTIME(s2.date, s2.begin), INTERVAL IF(s2.slot_id = :afterid, 1, 0) DAY)) < 1)) 
1377                         LEFT JOIN RapsysAirBundle:Session AS s3 ON (s3.id != b.session_id AND s3.application_id IS NOT NULL AND s3.locked IS NULL AND s3.date > b.date - INTERVAL 1 YEAR) 
1378                         LEFT JOIN RapsysAirBundle:Application AS a3 ON (a3.id = s3.application_id AND a3.user_id = b.user_id AND (a3.canceled IS NULL OR TIMESTAMPDIFF(DAY, a3.canceled, ADDDATE(ADDTIME(s3.date, s3.begin), INTERVAL IF(s3.slot_id = :afterid, 1, 0) DAY)) < 1)) 
1383                 LEFT JOIN RapsysAirBundle:Session AS s4 ON (s4.id != c.session_id AND s4.location_id = c.location_id AND s4.application_id IS NOT NULL AND s4.locked IS NULL AND s4.date > c.date - INTERVAL 1 YEAR) 
1384                 LEFT JOIN RapsysAirBundle:Application AS a4 ON (a4.id = s4.application_id AND a4.user_id != c.user_id AND (a4.canceled IS NULL OR TIMESTAMPDIFF(DAY, a4.canceled, ADDDATE(ADDTIME(s4.date, s4.begin), INTERVAL IF(s4.slot_id = :afterid, 1, 0) DAY)) < 1)) 
1389         LEFT JOIN RapsysAirBundle:GroupUser AS gu ON (gu.user_id = d.user_id) 
1394         IF(e.group_id <= :guestid AND e.l_previous <= :guestwait, e.remaining <= :guestdelay, 1) AND 
1395         IF(e.group_id <= :regularid AND e.premium = 1 AND e.hotspot = 1, e.remaining <= :regulardelay, 1) AND 
1396         IF(e.group_id <= :seniorid AND e.l_count <= :scount, e.remaining <= :regulardelay, 1) AND 
1397         IF(e.group_id <= :seniorid AND e.l_pn_ratio >= :pnratio, e.remaining <= :regulardelay, 1) AND 
1398         IF(e.group_id <= :seniorid AND e.l_tr_ratio >= (e.o_tr_ratio + :trdiff), e.remaining <= :regulardelay, 1) 
1399 ORDER BY e.l_score ASC, e.g_score ASC, e.created ASC, e.user_id ASC 
1402                 //Replace bundle entity name by table name 
1403                 $req = str_replace(array_keys($tables), array_values($tables), $req); 
1405                 //Set update request 
1406                 $upreq = 'UPDATE RapsysAirBundle:Application SET score = :score, updated = NOW() WHERE id = :id'; 
1408                 //Replace bundle entity name by table name 
1409                 $upreq = str_replace(array_keys($tables), array_values($tables), $upreq); 
1411                 //Get result set mapping instance 
1412                 $rsm = new ResultSetMapping(); 
1414                 //Declare all fields 
1416                         ->addEntityResult('RapsysAirBundle:Application', 'a') 
1417                         ->addFieldResult('a', 'id', 'id') 
1418                         ->addFieldResult('a', 'score', 'score') 
1419                         ->addIndexBy('a', 'id'); 
1422                 //XXX: setting limit in subqueries is required to prevent mariadb optimisation 
1424                         ->createNativeQuery($req, $rsm) 
1425                         ->setParameter('sid', $id) 
1426                         //XXX: removed, we update score before returning best candidate 
1427                         //->getOneOrNullResult(Query::HYDRATE_SINGLE_SCALAR); 
1434                 foreach($applications as $application) { 
1435                         //Check if we already saved best candidate 
1436                         if ($ret === null) { 
1437                                 //Return first application 
1438                                 $ret = $application; 
1441                         //Update application updated field 
1442                         //XXX: updated field is not modified for user with bad behaviour as application is not retrieved until delay is reached 
1443                         $em->getConnection()->executeUpdate($upreq, ['id' => $application->getId(), 'score' => $application->getScore()], ['id' => Type
::INTEGER, 'score' => Type
::FLOAT]); 
1446                 //Return best ranked application 
1452          * Rekey sessions and applications by chronological session id 
1454          * @return bool The rekey success or failure 
1456         function rekey(): bool { 
1457                 //Get entity manager 
1458                 $em = $this->getEntityManager(); 
1461                 $cnx = $em->getConnection(); 
1463                 //Get quote strategy 
1464                 $qs = $em->getConfiguration()->getQuoteStrategy(); 
1465                 $dp = $em->getConnection()->getDatabasePlatform(); 
1467                 //Get quoted table names 
1468                 //XXX: this allow to make this code table name independent 
1470                         'RapsysAirBundle:Application' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Application'), $dp), 
1471                         'RapsysAirBundle:Session' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Session'), $dp), 
1488                 GROUP_CONCAT(sa.id ORDER BY sa.id SEPARATOR "\\n") AS sa_id 
1489         FROM RapsysAirBundle:Session AS s 
1490         LEFT JOIN RapsysAirBundle:Application AS sa ON (sa.session_id = s.id) 
1494 ORDER BY ADDDATE(ADDTIME(a.date, a.begin), INTERVAL IF(a.slot_id = :afterid, 1, 0) DAY) ASC 
1497                 //Replace bundle entity name by table name 
1498                 $req = str_replace(array_keys($tables), array_values($tables), $req); 
1500                 //Get result set mapping instance 
1501                 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php 
1502                 $rsm = new ResultSetMapping(); 
1504                 //Declare all fields 
1505                 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php 
1506                 //addScalarResult($sqlColName, $resColName, $type = 'string'); 
1507                 $rsm->addScalarResult('id', 'id', 'integer') 
1508                         ->addScalarResult('sa_id', 'sa_id', 'string'); 
1509                         #->addIndexByScalar('id'); 
1512                 $rnq = $em->createNativeQuery($req, $rsm); 
1515                 $res = $rnq->getResult(); 
1518                 $cnx->beginTransaction(); 
1520                 //Set update session request 
1522 UPDATE RapsysAirBundle:Session 
1523 SET id = :nid, updated = NOW() 
1527                 //Replace bundle entity name by table name 
1528                 $sreq = str_replace(array_keys($tables), array_values($tables), $sreq); 
1530                 //Set update application request 
1532 UPDATE RapsysAirBundle:Application 
1533 SET session_id = :nid, updated = NOW() 
1534 WHERE session_id = :id 
1537                 //Replace bundle entity name by table name 
1538                 $areq = str_replace(array_keys($tables), array_values($tables), $areq); 
1541                 $max = max(array_keys($res)); 
1544                         //Prepare session to update 
1545                         foreach($res as $id => $data) { 
1547                                 $res[$id]['t_id'] = $max + 
$id + 
1; 
1550                                 $res[$id]['n_id'] = $id + 
1; 
1552                                 //Explode application ids 
1553                                 $res[$id]['sa_id'] = explode("\n", $data['sa_id']); 
1556                                 if ($res[$id]['n_id'] == $res[$id]['id']) { 
1557                                         //Remove unchanged session 
1564                                 //Disable foreign key checks 
1565                                 $cnx->prepare('SET foreign_key_checks = 0')->execute(); 
1568                                 foreach($res as $id => $data) { 
1569                                         //Run session update 
1570                                         $cnx->executeUpdate($sreq, ['nid' => $res[$id]['t_id'], 'id' => $res[$id]['id']]); 
1572                                         //Run applications update 
1573                                         $cnx->executeUpdate($areq, ['nid' => $res[$id]['t_id'], 'id' => $res[$id]['id']]); 
1577                                 foreach($res as $id => $data) { 
1578                                         //Run session update 
1579                                         $cnx->executeUpdate($sreq, ['nid' => $res[$id]['n_id'], 'id' => $res[$id]['t_id']]); 
1581                                         //Run applications update 
1582                                         $cnx->executeUpdate($areq, ['nid' => $res[$id]['n_id'], 'id' => $res[$id]['t_id']]); 
1585                                 //Restore foreign key checks 
1586                                 $cnx->prepare('SET foreign_key_checks = 1')->execute(); 
1588                                 //Commit transaction 
1591                                 //Set update auto_increment request 
1593 ALTER TABLE RapsysAirBundle:Session 
1597                                 //Replace bundle entity name by table name 
1598                                 $ireq = str_replace(array_keys($tables), array_values($tables), $ireq); 
1600                                 //Reset auto_increment 
1604                                 //Rollback transaction 
1607                 } catch(\Exception 
$e) { 
1608                         //Rollback transaction