BxEventsModule.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 profiles module.
  13. */
  14. class BxEventsModule extends BxBaseModGroupsModule
  15. {
  16. function __construct(&$aModule)
  17. {
  18. parent::__construct($aModule);
  19. }
  20. public function actionCalendarData()
  21. {
  22. // check permissions
  23. $iContentId = (int)bx_get('event');
  24. if ($iContentId) {
  25. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  26. if (CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedView($aContentInfo)) {
  27. $this->_oTemplate->displayAccessDenied();
  28. exit;
  29. }
  30. $aSQLPart = array();
  31. }
  32. else {
  33. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  34. $aSQLPart = $oPrivacy ? $oPrivacy->getContentPublicAsSQLPart(0, $oPrivacy->getPartiallyVisiblePrivacyGroups()) : array();
  35. }
  36. // get entries
  37. $aEntries = $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), bx_get('event'), $aSQLPart);
  38. header('Content-Type: application/json; charset=utf-8');
  39. echo json_encode($aEntries);
  40. }
  41. public function serviceCalendar($aData = array(), $sTemplate = 'calendar.html')
  42. {
  43. if (isset($aData['event'])) {
  44. $aContentInfo = $this->_oDb->getContentInfoById ((int)$aData['event']);
  45. if ('' == $aContentInfo['repeat_stop']) // don't display calendar for non repeating events
  46. return '';
  47. }
  48. $o = new BxTemplCalendar(array(
  49. 'eventSources' => array (
  50. 'url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'calendar_data',
  51. 'data' => $aData,
  52. ),
  53. ), $this->_oTemplate);
  54. return $o->display($sTemplate);
  55. }
  56. /**
  57. * Send remoiders to event's participants.
  58. * It must be processed once every hour for the proper processing.
  59. */
  60. public function serviceProcessReminders()
  61. {
  62. $iNow = time();
  63. // get all events for today and tomorrow, since the max reminder is 24 hours
  64. $aEntries = $this->_oDb->getEntriesByDate('@' . time(), '@' . (time() + 86400));
  65. foreach ($aEntries as $a) {
  66. if (!$a['reminder'])
  67. continue;
  68. $iTimestamp = $a['start_utc'] - (3600 * $a['reminder']);
  69. if ($iNow > ($iTimestamp - 3600) && $iNow < $iTimestamp)
  70. $this->sendReminders($a);
  71. }
  72. }
  73. public function actionIntervals()
  74. {
  75. $sAction = bx_get('a');
  76. $sMethodName = 'subaction' . ucfirst($sAction);
  77. if (!method_exists($this, $sMethodName)) {
  78. $this->_oTemplate->pageNotFound();
  79. return;
  80. }
  81. $this->$sMethodName();
  82. }
  83. public function subactionRestore($iContentId = 0)
  84. {
  85. if (!$iContentId)
  86. $iContentId = (int)bx_get('c');
  87. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  88. if (
  89. ($aContentInfo && CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedEdit ($aContentInfo))
  90. ||
  91. (!$aContentInfo && CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedAdd ())
  92. ) {
  93. $this->_oTemplate->displayAccessDenied();
  94. exit;
  95. }
  96. $a = $this->_oDb->getIntervals($iContentId);
  97. foreach ($a as $iIntervalId => $r) {
  98. $a[$iIntervalId]['file_id'] = $r['interval_id'];
  99. $a[$iIntervalId]['repeat_stop'] = bx_process_output ($r['repeat_stop'], BX_DATA_DATE_TS);
  100. }
  101. if ('json' == bx_get('f')) {
  102. header('Content-type: text/html; charset=utf-8');
  103. echo json_encode($a);
  104. }
  105. }
  106. public function subactionDelete()
  107. {
  108. header('Content-type: text/html; charset=utf-8');
  109. $iIntervalId = (int)bx_get('id');
  110. if (!($aContentInfo = $this->_oDb->getContentInfoByIntervalId($iIntervalId))) {
  111. echo _t('_sys_request_page_not_found_cpt');
  112. }
  113. elseif (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->checkAllowedEdit ($aContentInfo))) {
  114. echo $sMsg;
  115. }
  116. elseif (!$this->_oDb->deleteIntervalById($iIntervalId)) {
  117. echo _t('_sys_txt_error_occured');
  118. }
  119. else {
  120. echo 'ok';
  121. }
  122. }
  123. /**
  124. * Send reminder to event's paritcipants
  125. * @param $aContentInfo content info
  126. * @return number of sent reminders
  127. */
  128. protected function sendReminders($aContentInfo)
  129. {
  130. $CNF = $this->_oConfig->CNF;
  131. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  132. if (!$oConnection)
  133. return false;
  134. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aContentInfo[$CNF['FIELD_ID']], $this->getName());
  135. if (!$oGroupProfile)
  136. return false;
  137. $aParticipants = $oConnection->getConnectedInitiators($oGroupProfile->id(), true);
  138. if (!$aParticipants)
  139. return true;
  140. $oDateBegin = new DateTime();
  141. $oDateBegin->setTimestamp($aContentInfo['start_utc']);
  142. $oDateBegin->setTimezone(new DateTimeZone($aContentInfo['timezone']));
  143. $sEntryBegin = $oDateBegin->format('r');
  144. $sEntryBeginShort = $oDateBegin->format('j M');
  145. $oDateBegin->setTimezone(new DateTimeZone('UTC'));
  146. $sEntryBeginUTC = $oDateBegin->format('c');
  147. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  148. $sLocationString = $oMetatags->locationsString($aContentInfo[$CNF['FIELD_ID']], false);
  149. $iCounter = 0;
  150. foreach ($aParticipants as $iProfileId) {
  151. $b = sendMailTemplate($CNF['EMAIL_REMINDER'], 0, $iProfileId, array(
  152. 'EntryUrl' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]),
  153. 'EntryTitle' => $aContentInfo['title'],
  154. 'EntryBegin' => $sEntryBegin,
  155. 'EntryBeginShort' => $sEntryBeginShort,
  156. 'EntryBeginUTC' => $sEntryBeginUTC,
  157. 'EntryLocation' => $sLocationString,
  158. ), BX_EMAIL_NOTIFY);
  159. $iCounter += $b ? 1 : 0;
  160. }
  161. return $iCounter;
  162. }
  163. }
  164. /** @} */