BxEventsModule.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. * Events profiles module.
  13. */
  14. class BxEventsModule extends BxBaseModGroupsModule
  15. {
  16. function __construct(&$aModule)
  17. {
  18. parent::__construct($aModule);
  19. $CNF = &$this->_oConfig->CNF;
  20. $this->_aSearchableNamesExcept = array_merge($this->_aSearchableNamesExcept, array(
  21. $CNF['FIELD_TIMEZONE'],
  22. $CNF['FIELD_JOIN_CONFIRMATION'],
  23. $CNF['FIELD_REMINDER']
  24. ));
  25. }
  26. public function actionCalendarData()
  27. {
  28. // check permissions
  29. $iContentId = (int)bx_get('event');
  30. if ($iContentId) {
  31. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  32. if (CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedView($aContentInfo)) {
  33. $this->_oTemplate->displayAccessDenied();
  34. exit;
  35. }
  36. $aSQLPart = array();
  37. }
  38. else {
  39. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  40. $aSQLPart = $oPrivacy ? $oPrivacy->getContentPublicAsSQLPart(0, $oPrivacy->getPartiallyVisiblePrivacyGroups()) : array();
  41. }
  42. // get entries
  43. $aEntries = $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), bx_get('event'), $aSQLPart);
  44. header('Content-Type: application/json; charset=utf-8');
  45. echo json_encode($aEntries);
  46. }
  47. /**
  48. * @page service Service Calls
  49. * @section Events Events
  50. * @subsection bx_events-calendar calendar
  51. * @see BxEventsModule::serviceCalendar
  52. *
  53. * Shows event or events calendar
  54. *
  55. * @param $aData additional data to point which events to show, leave empty to show all events, specify event's ID in 'event' array key to show calendar for one event only. If only one event is specified then it will show calendar only if it's repeating event.
  56. * @param $sTemplate template to use to show calendar, or leave empty for default template, possible options: calendar.html, calendar_compact.html
  57. * @return HTML string with calendar to display on the site, all necessary CSS and JS files are automatically added to the HEAD section of the site HTML. On error empty string is returned.
  58. */
  59. /**
  60. * @ref bx_events-calendar "calendar"
  61. */
  62. public function serviceCalendar($aData = array(), $sTemplate = 'calendar.html')
  63. {
  64. if (isset($aData['event'])) {
  65. $aContentInfo = $this->_oDb->getContentInfoById ((int)$aData['event']);
  66. if ('' == $aContentInfo['repeat_stop']) // don't display calendar for non repeating events
  67. return '';
  68. }
  69. $o = new BxTemplCalendar(array(
  70. 'eventSources' => array (
  71. 'url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'calendar_data',
  72. 'data' => $aData,
  73. ),
  74. ), $this->_oTemplate);
  75. return $o->display($sTemplate);
  76. }
  77. /**
  78. * @page service Service Calls
  79. * @section Events Events
  80. * @subsection bx_events-process_reminders process_reminders
  81. * @see BxEventsModule::serviceProcessReminders
  82. *
  83. * Send remiders to event's participants.
  84. *
  85. * It must be processed once every hour for the proper processing.
  86. * @return nothing
  87. */
  88. /**
  89. * @ref bx_events-process_reminders "process_reminders"
  90. */
  91. public function serviceProcessReminders()
  92. {
  93. $iNow = time();
  94. // get all events for today and tomorrow, since the max reminder is 24 hours
  95. $aEntries = $this->_oDb->getEntriesByDate('@' . time(), '@' . (time() + 86400));
  96. foreach ($aEntries as $a) {
  97. if (!$a['reminder'])
  98. continue;
  99. $iTimestamp = $a['start_utc'] - (3600 * $a['reminder']);
  100. if ($iNow > ($iTimestamp - 3600) && $iNow < $iTimestamp)
  101. $this->sendReminders($a);
  102. }
  103. }
  104. /**
  105. * @page service Service Calls
  106. * @section Events Events
  107. * @subsection bx_events-get_timeline_post get_timeline_post
  108. * @see BxEventsModule::serviceGetTimelinePost
  109. *
  110. * Get Timeline post. It's needed for Timeline module.
  111. *
  112. * @param $aEvent timeline event array from Timeline module
  113. * @return array in special format which is needed specifically for Timeline module to display the data.
  114. */
  115. /**
  116. * @ref bx_events-get_timeline_post "get_timeline_post"
  117. */
  118. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  119. {
  120. $a = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  121. if (!($aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id'])))
  122. return $a;
  123. $CNF = $this->_oConfig->CNF;
  124. $oDateStart = date_create('@' . $aContentInfo['date_start'], new DateTimeZone($aContentInfo['timezone'] ? $aContentInfo['timezone'] : 'UTC'));
  125. $oDateEnd = date_create('@' . ($aContentInfo['date_start'] > $aContentInfo['repeat_stop'] ? $aContentInfo['date_start'] : $aContentInfo['repeat_stop']), new DateTimeZone($aContentInfo['timezone'] ? $aContentInfo['timezone'] : 'UTC'));
  126. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  127. $sLocationString = $oMetatags ? $oMetatags->locationsString($aContentInfo[$CNF['FIELD_ID']], false) : false;
  128. $a['content']['raw'] = $this->_oTemplate->parseHtmlByName('timeline_post.html', array(
  129. 'title' => $a['content']['title'],
  130. 'title_attr' => bx_html_attribute($a['content']['title']),
  131. 'url' => $a['content']['url'],
  132. 'date' => $oDateStart->format(getParam('bx_events_short_date_format')) . ($oDateStart->format('ymd') == $oDateEnd->format('ymd') ? '' : ' - ' . $oDateEnd->format(getParam('bx_events_short_date_format'))),
  133. 'date_c' => $oDateStart->format('c'),
  134. 'bx_if:location' => array(
  135. 'condition' => !!$sLocationString,
  136. 'content' => array('location' => $sLocationString),
  137. ),
  138. ));
  139. $a['content']['title'] = '';
  140. $a['content']['text'] = '';
  141. return $a;
  142. }
  143. public function actionIntervals()
  144. {
  145. $sAction = bx_get('a');
  146. $sMethodName = 'subaction' . ucfirst($sAction);
  147. if (!method_exists($this, $sMethodName)) {
  148. $this->_oTemplate->pageNotFound();
  149. return;
  150. }
  151. $this->$sMethodName();
  152. }
  153. public function subactionRestore($iContentId = 0)
  154. {
  155. if (!$iContentId)
  156. $iContentId = (int)bx_get('c');
  157. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  158. if (
  159. ($aContentInfo && CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedEdit ($aContentInfo))
  160. ||
  161. (!$aContentInfo && CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedAdd ())
  162. ) {
  163. $this->_oTemplate->displayAccessDenied();
  164. exit;
  165. }
  166. $a = $this->_oDb->getIntervals($iContentId);
  167. foreach ($a as $iIntervalId => $r) {
  168. $a[$iIntervalId]['file_id'] = $r['interval_id'];
  169. $a[$iIntervalId]['repeat_stop'] = bx_process_output ($r['repeat_stop'], BX_DATA_DATE_TS);
  170. }
  171. if ('json' == bx_get('f')) {
  172. header('Content-type: text/html; charset=utf-8');
  173. echo json_encode($a);
  174. }
  175. }
  176. public function subactionDelete()
  177. {
  178. header('Content-type: text/html; charset=utf-8');
  179. $iIntervalId = (int)bx_get('id');
  180. if (!($aContentInfo = $this->_oDb->getContentInfoByIntervalId($iIntervalId))) {
  181. echo _t('_sys_request_page_not_found_cpt');
  182. }
  183. elseif (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->checkAllowedEdit ($aContentInfo))) {
  184. echo $sMsg;
  185. }
  186. elseif (!$this->_oDb->deleteIntervalById($iIntervalId)) {
  187. echo _t('_sys_txt_error_occured');
  188. }
  189. else {
  190. echo 'ok';
  191. }
  192. }
  193. /**
  194. * Send reminder to event's paritcipants
  195. * @param $aContentInfo content info
  196. * @return number of sent reminders
  197. */
  198. protected function sendReminders($aContentInfo)
  199. {
  200. $CNF = $this->_oConfig->CNF;
  201. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  202. if (!$oConnection)
  203. return false;
  204. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aContentInfo[$CNF['FIELD_ID']], $this->getName());
  205. if (!$oGroupProfile)
  206. return false;
  207. $aParticipants = $oConnection->getConnectedInitiators($oGroupProfile->id(), true);
  208. if (!$aParticipants)
  209. return true;
  210. $oDateBegin = new DateTime();
  211. $oDateBegin->setTimestamp($aContentInfo['start_utc']);
  212. $oDateBegin->setTimezone(new DateTimeZone($aContentInfo['timezone'] ? $aContentInfo['timezone'] : 'UTC'));
  213. $sEntryBegin = $oDateBegin->format('r');
  214. $sEntryBeginShort = $oDateBegin->format(getParam('bx_events_short_date_format'));
  215. $oDateBegin->setTimezone(new DateTimeZone('UTC'));
  216. $sEntryBeginUTC = $oDateBegin->format('c');
  217. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  218. $sLocationString = $oMetatags->locationsString($aContentInfo[$CNF['FIELD_ID']], false);
  219. $iCounter = 0;
  220. foreach ($aParticipants as $iProfileId) {
  221. $b = sendMailTemplate($CNF['EMAIL_REMINDER'], 0, $iProfileId, array(
  222. 'EntryUrl' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]),
  223. 'EntryTitle' => $aContentInfo['title'],
  224. 'EntryBegin' => $sEntryBegin,
  225. 'EntryBeginShort' => $sEntryBeginShort,
  226. 'EntryBeginUTC' => $sEntryBeginUTC,
  227. 'EntryLocation' => $sLocationString,
  228. ), BX_EMAIL_NOTIFY);
  229. $iCounter += $b ? 1 : 0;
  230. }
  231. return $iCounter;
  232. }
  233. }
  234. /** @} */