BxEventsDb.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Events Events
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Groups module database queries
  13. */
  14. class BxEventsDb extends BxBaseModGroupsDb
  15. {
  16. public function __construct(&$oConfig)
  17. {
  18. parent::__construct($oConfig);
  19. }
  20. public function getContentInfoByIntervalId ($iIntervalId)
  21. {
  22. $iContentId = $this->getOne("SELECT `event_id` FROM `bx_events_intervals` WHERE `interval_id` = :interval", array(
  23. 'interval' => $iIntervalId,
  24. ));
  25. if (!$iContentId)
  26. return 0;
  27. return $this->getContentInfoById ($iContentId);
  28. }
  29. public function getContentInfoById ($iContentId)
  30. {
  31. $sQuery = $this->prepare ("SELECT `c`.*, `p`.`account_id`, `p`.`id` AS `profile_id`, `p`.`status` AS `profile_status`, MAX(`i`.`repeat_stop`) AS `repeat_stop` FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` AS `c`
  32. INNER JOIN `sys_profiles` AS `p` ON (`p`.`content_id` = `c`.`id` AND `p`.`type` = ?)
  33. LEFT JOIN `bx_events_intervals` as `i` ON (`c`.`id` = `i`.`event_id`)
  34. WHERE `c`.`id` = ?
  35. GROUP BY `c`.`id`", $this->_oConfig->getName(), $iContentId);
  36. return $this->getRow($sQuery);
  37. }
  38. public function deleteIntervalById ($iIntervalId)
  39. {
  40. return $this->query("DELETE FROM `bx_events_intervals` WHERE `interval_id` = :interval", array(
  41. 'interval' => $iIntervalId,
  42. ));
  43. }
  44. public function getIntervals ($iContentId)
  45. {
  46. return $this->getAllWithKey("SELECT * FROM `bx_events_intervals` WHERE `event_id` = :event_id", 'interval_id', array(
  47. 'event_id' => $iContentId,
  48. ));
  49. }
  50. public function getEntriesByDate($sDateFrom, $sDateTo, $iEventId = 0, $aSQLPart = array())
  51. {
  52. // validate input data
  53. if (false === ($oDateFrom = date_create($sDateFrom, new DateTimeZone('UTC'))))
  54. return array();
  55. if (false === ($oDateTo = date_create($sDateTo, new DateTimeZone('UTC'))))
  56. return array();
  57. if ($oDateFrom > $oDateTo)
  58. return array();
  59. // increase start and end date to cover timezones
  60. $oDateFrom = $oDateFrom->sub(new DateInterval("P1D"));
  61. $oDateTo = $oDateTo->add(new DateInterval("P1D"));
  62. // look throught all days in the interval
  63. $oDateIter = clone($oDateFrom);
  64. $aEntries = array();
  65. while ($oDateIter->format('Y-m-d') != $oDateTo->format('Y-m-d')) {
  66. $oDateMin = date_create($oDateIter->format('Y-m-d') . '00:00:00', new DateTimeZone('UTC'));
  67. $oDateMax = date_create($oDateIter->format('Y-m-d') . '23:59:59', new DateTimeZone('UTC'));
  68. // get all events for the specific day
  69. $oDateMonthBegin = date_create($oDateIter->format('Y-m-01'), new DateTimeZone('UTC'));
  70. $iWeekOfMonth = $oDateIter->format('W') - $oDateMonthBegin->format('W') + 1;
  71. $aBindings = array(
  72. 'timestamp_min' => $oDateMin->getTimestamp(),
  73. 'timestamp_max' => $oDateMax->getTimestamp(),
  74. 'timestamp_from' => $oDateFrom->getTimestamp(),
  75. 'timestamp_to' => $oDateTo->getTimestamp(),
  76. 'year_start' => $oDateFrom->format('Y'),
  77. 'year' => $oDateIter->format('Y'),
  78. 'month' => $oDateIter->format('n'),
  79. 'week_of_month' => $iWeekOfMonth > 0 ? $iWeekOfMonth : $oDateIter->format('W') + 1,
  80. 'day_of_month' => $oDateIter->format('j'),
  81. 'day_of_week' => $oDateIter->format('N'),
  82. );
  83. $sWhere = isset($aSQLPart['where']) ? $aSQLPart['where'] : '';
  84. if ((int)$iEventId) {
  85. $aBindings['event'] = (int)$iEventId;
  86. $sWhere .= " AND `bx_events_data`.`id` = :event ";
  87. }
  88. $a = $this->getAll("SELECT DISTINCT `bx_events_data`.`id`, `bx_events_data`.`event_name` AS `title`, `bx_events_data`.`date_start`, `bx_events_data`.`date_end`, `bx_events_data`.`timezone`, `bx_events_data`.`reminder`
  89. FROM `bx_events_data`
  90. LEFT JOIN `bx_events_intervals` AS `i` ON (
  91. `bx_events_data`.`id` = `i`.`event_id`
  92. AND
  93. `bx_events_data`.`date_start` < :timestamp_max
  94. AND
  95. (0 = `i`.`repeat_stop` OR `i`.`repeat_stop` >= :timestamp_min)
  96. AND (
  97. (0 = `i`.`repeat_year` OR 0 = (:year - :year_start) % `i`.`repeat_year`)
  98. AND
  99. (0 = `i`.`repeat_month` OR :month = `i`.`repeat_month`)
  100. AND
  101. (0 = `i`.`repeat_week_of_month` OR :week_of_month = `i`.`repeat_week_of_month`)
  102. AND
  103. (0 = `i`.`repeat_day_of_month` OR :day_of_month = `i`.`repeat_day_of_month`)
  104. AND
  105. (0 = `i`.`repeat_day_of_week` OR :day_of_week = `i`.`repeat_day_of_week`)
  106. )
  107. )
  108. WHERE ((`bx_events_data`.`date_start` >= :timestamp_min AND `bx_events_data`.`date_start` <= :timestamp_max AND `i`.`interval_id` IS NULL) OR (`i`.`interval_id` IS NOT NULL)) $sWhere
  109. ", $aBindings);
  110. // prepare variables for each event
  111. $sCurrentDay = $oDateIter->format('Y-m-d');
  112. foreach ($a as $k => $r) {
  113. $oDateStart = new DateTime();
  114. $oDateStart->setTimestamp($r['date_start']);
  115. $oDateStart->setTimezone(new DateTimeZone('UTC'));
  116. $oDateEnd = new DateTime();
  117. $oDateEnd->setTimestamp($r['date_end']);
  118. $oDateEnd->setTimezone(new DateTimeZone('UTC'));
  119. $oDuration = $oDateStart->diff($oDateEnd);
  120. $sHoursStart = $oDateStart->format('H:i:s');
  121. $oStart = date_create($sCurrentDay . ' ' . $sHoursStart, new DateTimeZone($r['timezone']));
  122. $oEnd = $oStart ? clone($oStart) : null;
  123. $oEnd = $oEnd ? $oEnd->add($oDuration) : null;
  124. $a[$k]['start'] = $oStart ? $oStart->format('c') : 0;
  125. $a[$k]['end'] = $oEnd ? $oEnd->format('c') : 0;
  126. $a[$k]['start_utc'] = $oStart ? $oStart->getTimestamp() : 0;
  127. $a[$k]['end_utc'] = $oEnd ? $oEnd->getTimestamp() : 0;
  128. $a[$k]['url'] = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $this->_oConfig->CNF['URI_VIEW_ENTRY'] . '&id=' . $r['id']);
  129. }
  130. // merge with all other events
  131. $aEntries = array_merge($aEntries, $a);
  132. // go to the next day
  133. $oDateIter = $oDateIter->add(new DateInterval("P1D"));
  134. }
  135. return $aEntries;
  136. }
  137. }
  138. /** @} */