BxEventsModule.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 bx_events Events
  50. * @subsection bx_events-page_blocks Page Blocks
  51. * @subsubsection bx_events-calendar calendar
  52. *
  53. * @code bx_srv('bx_events', 'calendar', [...]); @endcode
  54. *
  55. * Shows event or events calendar
  56. *
  57. * @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.
  58. * @param $sTemplate template to use to show calendar, or leave empty for default template, possible options: calendar.html, calendar_compact.html
  59. * @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.
  60. *
  61. * @see BxEventsModule::serviceCalendar
  62. */
  63. /**
  64. * @ref bx_events-calendar "calendar"
  65. */
  66. public function serviceCalendar($aData = array(), $sTemplate = 'calendar.html')
  67. {
  68. if (isset($aData['event'])) {
  69. $aContentInfo = $this->_oDb->getContentInfoById ((int)$aData['event']);
  70. if ('' == $aContentInfo['repeat_stop']) // don't display calendar for non repeating events
  71. return '';
  72. }
  73. $o = new BxTemplCalendar(array(
  74. 'eventSources' => array (
  75. BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'calendar_data',
  76. $aData,
  77. ),
  78. ), $this->_oTemplate);
  79. return $o->display($sTemplate);
  80. }
  81. /**
  82. * @page service Service Calls
  83. * @section bx_events Events
  84. * @subsection bx_events-other Other
  85. * @subsubsection bx_events-process_reminders process_reminders
  86. *
  87. * @code bx_srv('bx_events', 'process_reminders'); @endcode
  88. *
  89. * Send remiders to event's participants.
  90. *
  91. * It must be processed once every hour for the proper processing.
  92. * @return nothing
  93. *
  94. * @see BxEventsModule::serviceProcessReminders
  95. */
  96. /**
  97. * @ref bx_events-process_reminders "process_reminders"
  98. */
  99. public function serviceProcessReminders()
  100. {
  101. $iNow = time();
  102. // get all events for today and tomorrow, since the max reminder is 24 hours
  103. $aEntries = $this->_oDb->getEntriesByDate('@' . time(), '@' . (time() + 86400));
  104. foreach ($aEntries as $a) {
  105. if (!$a['reminder'])
  106. continue;
  107. $iTimestamp = $a['start_utc'] - (3600 * $a['reminder']);
  108. if ($iNow > ($iTimestamp - 3600) && $iNow < $iTimestamp)
  109. $this->sendReminders($a);
  110. }
  111. }
  112. /**
  113. * @page service Service Calls
  114. * @section bx_events Events
  115. * @subsection bx_events-internal Internal
  116. * @subsubsection bx_events-get_timeline_post get_timeline_post
  117. *
  118. * @code bx_srv('bx_events', 'get_timeline_post', [...]); @endcode
  119. *
  120. * Get Timeline post. It's needed for Timeline module.
  121. *
  122. * @param $aEvent timeline event array from Timeline module
  123. * @return array in special format which is needed specifically for Timeline module to display the data.
  124. *
  125. * @see BxEventsModule::serviceGetTimelinePost
  126. */
  127. /**
  128. * @ref bx_events-get_timeline_post "get_timeline_post"
  129. */
  130. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  131. {
  132. $a = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  133. if($a === false)
  134. return false;
  135. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  136. if(empty($aContentInfo) || !is_array($aContentInfo))
  137. return $a;
  138. $CNF = $this->_oConfig->CNF;
  139. $oDateStart = date_create('@' . $aContentInfo['date_start'], new DateTimeZone($aContentInfo['timezone'] ? $aContentInfo['timezone'] : 'UTC'));
  140. $oDateEnd = date_create('@' . ($aContentInfo['date_start'] > $aContentInfo['repeat_stop'] ? $aContentInfo['date_start'] : $aContentInfo['repeat_stop']), new DateTimeZone($aContentInfo['timezone'] ? $aContentInfo['timezone'] : 'UTC'));
  141. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  142. $sLocationString = $oMetatags ? $oMetatags->locationsString($aContentInfo[$CNF['FIELD_ID']], false) : false;
  143. $a['content']['raw'] = $this->_oTemplate->parseHtmlByName('timeline_post.html', array(
  144. 'title' => $a['content']['title'],
  145. 'title_attr' => bx_html_attribute($a['content']['title']),
  146. 'url' => $a['content']['url'],
  147. 'date' => $oDateStart->format(getParam('bx_events_short_date_format')) . ($oDateStart->format('ymd') == $oDateEnd->format('ymd') ? '' : ' - ' . $oDateEnd->format(getParam('bx_events_short_date_format'))),
  148. 'date_c' => $oDateStart->format('c'),
  149. 'bx_if:location' => array(
  150. 'condition' => !!$sLocationString,
  151. 'content' => array('location' => $sLocationString),
  152. ),
  153. ));
  154. $a['content']['title'] = '';
  155. $a['content']['text'] = '';
  156. return $a;
  157. }
  158. public function actionIntervals()
  159. {
  160. $sAction = bx_get('a');
  161. $sMethodName = 'subaction' . ucfirst($sAction);
  162. if (!method_exists($this, $sMethodName)) {
  163. $this->_oTemplate->pageNotFound();
  164. return;
  165. }
  166. $this->$sMethodName();
  167. }
  168. public function subactionRestore($iContentId = 0)
  169. {
  170. if (!$iContentId)
  171. $iContentId = (int)bx_get('c');
  172. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  173. if (
  174. ($aContentInfo && CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedEdit ($aContentInfo))
  175. ||
  176. (!$aContentInfo && CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedAdd ())
  177. ) {
  178. $this->_oTemplate->displayAccessDenied();
  179. exit;
  180. }
  181. $a = $this->_oDb->getIntervals($iContentId);
  182. foreach ($a as $iIntervalId => $r) {
  183. $a[$iIntervalId]['file_id'] = $r['interval_id'];
  184. $a[$iIntervalId]['repeat_stop'] = bx_process_output ($r['repeat_stop'], BX_DATA_DATE_TS);
  185. }
  186. if ('json' == bx_get('f')) {
  187. header('Content-type: text/html; charset=utf-8');
  188. echo json_encode($a);
  189. }
  190. }
  191. public function subactionDelete()
  192. {
  193. header('Content-type: text/html; charset=utf-8');
  194. $iIntervalId = (int)bx_get('id');
  195. if (!($aContentInfo = $this->_oDb->getContentInfoByIntervalId($iIntervalId))) {
  196. echo _t('_sys_request_page_not_found_cpt');
  197. }
  198. elseif (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->checkAllowedEdit ($aContentInfo))) {
  199. echo $sMsg;
  200. }
  201. elseif (!$this->_oDb->deleteIntervalById($iIntervalId)) {
  202. echo _t('_sys_txt_error_occured');
  203. }
  204. else {
  205. echo 'ok';
  206. }
  207. }
  208. /**
  209. * Send reminder to event's paritcipants
  210. * @param $aContentInfo content info
  211. * @return number of sent reminders
  212. */
  213. protected function sendReminders($aContentInfo)
  214. {
  215. $CNF = $this->_oConfig->CNF;
  216. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  217. if (!$oConnection)
  218. return false;
  219. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aContentInfo[$CNF['FIELD_ID']], $this->getName());
  220. if (!$oGroupProfile)
  221. return false;
  222. $aParticipants = $oConnection->getConnectedInitiators($oGroupProfile->id(), true);
  223. if (!$aParticipants)
  224. return true;
  225. $oDateBegin = new DateTime();
  226. $oDateBegin->setTimestamp($aContentInfo['start_utc']);
  227. $oDateBegin->setTimezone(new DateTimeZone($aContentInfo['timezone'] ? $aContentInfo['timezone'] : 'UTC'));
  228. $sEntryBegin = $oDateBegin->format('r');
  229. $sEntryBeginShort = $oDateBegin->format(getParam('bx_events_short_date_format'));
  230. $oDateBegin->setTimezone(new DateTimeZone('UTC'));
  231. $sEntryBeginUTC = $oDateBegin->format('c');
  232. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  233. $sLocationString = $oMetatags->locationsString($aContentInfo[$CNF['FIELD_ID']], false);
  234. $iCounter = 0;
  235. foreach ($aParticipants as $iProfileId) {
  236. $b = sendMailTemplate($CNF['EMAIL_REMINDER'], 0, $iProfileId, array(
  237. 'EntryUrl' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]),
  238. 'EntryTitle' => $aContentInfo['title'],
  239. 'EntryBegin' => $sEntryBegin,
  240. 'EntryBeginShort' => $sEntryBeginShort,
  241. 'EntryBeginUTC' => $sEntryBeginUTC,
  242. 'EntryLocation' => $sLocationString,
  243. ), BX_EMAIL_NOTIFY);
  244. $iCounter += $b ? 1 : 0;
  245. }
  246. return $iCounter;
  247. }
  248. }
  249. /** @} */