BxEventsDb.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. );
  75. $aBindingsRepeating = array(
  76. 'timestamp_min' => $oDateMin->getTimestamp(),
  77. 'timestamp_max' => $oDateMax->getTimestamp(),
  78. 'year_start' => $oDateFrom->format('Y'),
  79. 'year' => $oDateIter->format('Y'),
  80. 'month' => $oDateIter->format('n'),
  81. 'week_of_month' => $iWeekOfMonth > 0 ? $iWeekOfMonth : $oDateIter->format('W') + 1,
  82. 'day_of_month' => $oDateIter->format('j'),
  83. 'day_of_week' => $oDateIter->format('N'),
  84. );
  85. $sWhere = isset($aSQLPart['where']) ? $aSQLPart['where'] : '';
  86. if ((int)$iEventId) {
  87. $aBindings['event'] = (int)$iEventId;
  88. $aBindingsRepeating['event'] = (int)$iEventId;
  89. $sWhere .= " AND `bx_events_data`.`id` = :event ";
  90. }
  91. // search for regular events
  92. $a = $this->getAllWithKey("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`, 0 AS `repeating`
  93. FROM `bx_events_data`
  94. WHERE `bx_events_data`.`date_start` >= :timestamp_min AND `bx_events_data`.`date_start` <= :timestamp_max $sWhere
  95. ", 'id', $aBindings);
  96. if ($a)
  97. $sWhere .= " AND `bx_events_data`.`id` NOT IN(" . $this->implode_escape(array_keys($a)) . ") ";
  98. $aRepeating = $this->getAllWithKey("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`, 1 AS `repeating`
  99. FROM `bx_events_data`
  100. LEFT JOIN `bx_events_intervals` AS `i` ON (
  101. `bx_events_data`.`id` = `i`.`event_id`
  102. AND
  103. `bx_events_data`.`date_start` < :timestamp_max
  104. AND
  105. (0 = `i`.`repeat_stop` OR `i`.`repeat_stop` >= :timestamp_min)
  106. AND (
  107. (0 = `i`.`repeat_year` OR 0 = (:year - :year_start) % `i`.`repeat_year`)
  108. AND
  109. (0 = `i`.`repeat_month` OR :month = `i`.`repeat_month`)
  110. AND
  111. (0 = `i`.`repeat_week_of_month` OR :week_of_month = `i`.`repeat_week_of_month`)
  112. AND
  113. (0 = `i`.`repeat_day_of_month` OR :day_of_month = `i`.`repeat_day_of_month`)
  114. AND
  115. (0 = `i`.`repeat_day_of_week` OR :day_of_week = `i`.`repeat_day_of_week`)
  116. )
  117. )
  118. WHERE `i`.`interval_id` IS NOT NULL $sWhere
  119. ", 'id', $aBindingsRepeating);
  120. $a = array_merge($aRepeating ? $aRepeating : array(), $a ? $a : array());
  121. // prepare variables for each event
  122. foreach ($a as $k => $r) {
  123. $oDateStart = new DateTime('@' . $r['date_start']);
  124. $oDateEnd = new DateTime('@' . $r['date_end']);
  125. $oDuration = $oDateStart->diff($oDateEnd);
  126. if ($r['repeating']) {
  127. // since repeating interval values are stored in specific timezones then we need to convert it
  128. $oTz = new DateTimeZone($r['timezone'] ? $r['timezone'] : 'UTC');
  129. $sHoursStart = $oDateStart->format('H:i:s');
  130. $iSecondsStart = (clone($oDateStart))->setTimezone($oTz)->format('H') * 3600 + $oDateStart->format('i') * 60 + $oDateStart->format('s');
  131. $iSecondsOffset = $oTz->getOffset($oDateIter);
  132. $sCurrentDay = $oDateIter->format('Y-m-d');
  133. if ($iSecondsOffset > 0 && $iSecondsOffset > $iSecondsStart)
  134. $sCurrentDay = (clone($oDateIter))->sub(new DateInterval("P1D"))->format('Y-m-d');
  135. if ($iSecondsOffset < 0 && -$iSecondsOffset > (86400 - $iSecondsStart))
  136. $sCurrentDay = (clone($oDateIter))->add(new DateInterval("P1D"))->format('Y-m-d');
  137. $oStart = date_create($sCurrentDay . ' ' . $sHoursStart);
  138. $oEnd = $oStart ? clone($oStart) : null;
  139. $oEnd = $oEnd ? $oEnd->add($oDuration) : null;
  140. }
  141. else {
  142. $oStart = $oDateStart;
  143. $oEnd = $oDateEnd;
  144. }
  145. $a[$k]['start'] = $oStart ? $oStart->format('c') : 0;
  146. $a[$k]['end'] = $oEnd ? $oEnd->format('c') : 0;
  147. $a[$k]['start_utc'] = $oDateStart ? $oDateStart->getTimestamp() : 0;
  148. $a[$k]['end_utc'] = $oDateEnd ? $oDateEnd->getTimestamp() : 0;
  149. $a[$k]['url'] = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $this->_oConfig->CNF['URI_VIEW_ENTRY'] . '&id=' . $r['id']);
  150. }
  151. // merge with all other events
  152. $aEntries = array_merge($aEntries, $a);
  153. // go to the next day
  154. $oDateIter = $oDateIter->add(new DateInterval("P1D"));
  155. }
  156. return $aEntries;
  157. }
  158. }
  159. /** @} */