BxEventsModule.php 9.7 KB

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