BxEventsModule.php 9.5 KB

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