BxEventsModule.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 serviceGetTimelinePost($aEvent)
  74. {
  75. $a = parent::serviceGetTimelinePost($aEvent);
  76. if (!($aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id'])))
  77. return $a;
  78. $CNF = $this->_oConfig->CNF;
  79. $oDateStart = date_create('@' . $aContentInfo['date_start'], new DateTimeZone($aContentInfo['timezone']));
  80. $oDateEnd = date_create('@' . ($aContentInfo['date_start'] > $aContentInfo['repeat_stop'] ? $aContentInfo['date_start'] : $aContentInfo['repeat_stop']), new DateTimeZone($aContentInfo['timezone']));
  81. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  82. $sLocationString = $oMetatags ? $oMetatags->locationsString($aContentInfo[$CNF['FIELD_ID']], false) : false;
  83. $a['content']['raw'] = $this->_oTemplate->parseHtmlByName('timeline_post.html', array(
  84. 'title' => $a['content']['title'],
  85. 'title_attr' => bx_html_attribute($a['content']['title']),
  86. 'url' => $a['content']['url'],
  87. 'date' => $oDateStart->format(getParam('bx_events_short_date_format')) . ($oDateStart->format('ymd') == $oDateEnd->format('ymd') ? '' : ' - ' . $oDateEnd->format(getParam('bx_events_short_date_format'))),
  88. 'date_c' => $oDateStart->format('c'),
  89. 'bx_if:location' => array(
  90. 'condition' => !!$sLocationString,
  91. 'content' => array('location' => $sLocationString),
  92. ),
  93. ));
  94. $a['content']['title'] = '';
  95. $a['content']['text'] = '';
  96. return $a;
  97. }
  98. public function actionIntervals()
  99. {
  100. $sAction = bx_get('a');
  101. $sMethodName = 'subaction' . ucfirst($sAction);
  102. if (!method_exists($this, $sMethodName)) {
  103. $this->_oTemplate->pageNotFound();
  104. return;
  105. }
  106. $this->$sMethodName();
  107. }
  108. public function subactionRestore($iContentId = 0)
  109. {
  110. if (!$iContentId)
  111. $iContentId = (int)bx_get('c');
  112. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  113. if (
  114. ($aContentInfo && CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedEdit ($aContentInfo))
  115. ||
  116. (!$aContentInfo && CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedAdd ())
  117. ) {
  118. $this->_oTemplate->displayAccessDenied();
  119. exit;
  120. }
  121. $a = $this->_oDb->getIntervals($iContentId);
  122. foreach ($a as $iIntervalId => $r) {
  123. $a[$iIntervalId]['file_id'] = $r['interval_id'];
  124. $a[$iIntervalId]['repeat_stop'] = bx_process_output ($r['repeat_stop'], BX_DATA_DATE_TS);
  125. }
  126. if ('json' == bx_get('f')) {
  127. header('Content-type: text/html; charset=utf-8');
  128. echo json_encode($a);
  129. }
  130. }
  131. public function subactionDelete()
  132. {
  133. header('Content-type: text/html; charset=utf-8');
  134. $iIntervalId = (int)bx_get('id');
  135. if (!($aContentInfo = $this->_oDb->getContentInfoByIntervalId($iIntervalId))) {
  136. echo _t('_sys_request_page_not_found_cpt');
  137. }
  138. elseif (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->checkAllowedEdit ($aContentInfo))) {
  139. echo $sMsg;
  140. }
  141. elseif (!$this->_oDb->deleteIntervalById($iIntervalId)) {
  142. echo _t('_sys_txt_error_occured');
  143. }
  144. else {
  145. echo 'ok';
  146. }
  147. }
  148. /**
  149. * Send reminder to event's paritcipants
  150. * @param $aContentInfo content info
  151. * @return number of sent reminders
  152. */
  153. protected function sendReminders($aContentInfo)
  154. {
  155. $CNF = $this->_oConfig->CNF;
  156. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  157. if (!$oConnection)
  158. return false;
  159. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aContentInfo[$CNF['FIELD_ID']], $this->getName());
  160. if (!$oGroupProfile)
  161. return false;
  162. $aParticipants = $oConnection->getConnectedInitiators($oGroupProfile->id(), true);
  163. if (!$aParticipants)
  164. return true;
  165. $oDateBegin = new DateTime();
  166. $oDateBegin->setTimestamp($aContentInfo['start_utc']);
  167. $oDateBegin->setTimezone(new DateTimeZone($aContentInfo['timezone']));
  168. $sEntryBegin = $oDateBegin->format('r');
  169. $sEntryBeginShort = $oDateBegin->format(getParam('bx_events_short_date_format'));
  170. $oDateBegin->setTimezone(new DateTimeZone('UTC'));
  171. $sEntryBeginUTC = $oDateBegin->format('c');
  172. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  173. $sLocationString = $oMetatags->locationsString($aContentInfo[$CNF['FIELD_ID']], false);
  174. $iCounter = 0;
  175. foreach ($aParticipants as $iProfileId) {
  176. $b = sendMailTemplate($CNF['EMAIL_REMINDER'], 0, $iProfileId, array(
  177. 'EntryUrl' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]),
  178. 'EntryTitle' => $aContentInfo['title'],
  179. 'EntryBegin' => $sEntryBegin,
  180. 'EntryBeginShort' => $sEntryBeginShort,
  181. 'EntryBeginUTC' => $sEntryBeginUTC,
  182. 'EntryLocation' => $sLocationString,
  183. ), BX_EMAIL_NOTIFY);
  184. $iCounter += $b ? 1 : 0;
  185. }
  186. return $iCounter;
  187. }
  188. }
  189. /** @} */