BxEventsModule.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 implements iBxDolCalendarService
  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. $aSQLPart = array();
  30. $iContentId = (int)bx_get('event');
  31. $iContextId = (int)bx_get('context_id');
  32. if ($iContentId) {
  33. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  34. if (CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedView($aContentInfo)) {
  35. $this->_oTemplate->displayAccessDenied();
  36. exit;
  37. }
  38. $aSQLPart = array();
  39. }
  40. else {
  41. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  42. if($iContextId){
  43. if (!$this->serviceIsEnableForContext($iContextId)){
  44. exit;
  45. }
  46. else{
  47. $aSQLPart = $oPrivacy ? $oPrivacy->getContentByGroupAsSQLPart(- $iContextId) : array();
  48. }
  49. }
  50. else{
  51. $aSQLPart = $oPrivacy ? $oPrivacy->getContentPublicAsSQLPart(0, $oPrivacy->getPartiallyVisiblePrivacyGroups()) : array();
  52. }
  53. }
  54. // get entries
  55. $aEntries = $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), bx_get('event'), $aSQLPart);
  56. header('Content-Type: application/json; charset=utf-8');
  57. echo json_encode($aEntries);
  58. }
  59. public function serviceGetCalendarEntries($iProfileId)
  60. {
  61. $CNF = &$this->_oConfig->CNF;
  62. $oConn = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  63. $aData = $oConn->getConnectedContent($iProfileId);
  64. $aData2 = array();
  65. foreach($aData as $iProfileId2){
  66. $oProfile = BxDolProfile::getInstance($iProfileId2);
  67. array_push($aData2, $oProfile->getContentId());
  68. }
  69. $aSQLPart['where'] = " AND `bx_events_data`.`id` IN(" . implode(',', $aData2) . ")";
  70. return $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), null, $aSQLPart);
  71. }
  72. public function serviceGetSafeServices()
  73. {
  74. $a = parent::serviceGetSafeServices();
  75. return array_merge($a, array (
  76. 'BrowsePastProfiles' => '',
  77. 'Calendar' => '',
  78. ));
  79. }
  80. /**
  81. * @page service Service Calls
  82. * @section bx_events Events
  83. * @subsection bx_events-browse Browse
  84. * @subsubsection bx_events-browse_upcoming_profiles browse_upcoming_profiles
  85. *
  86. * @code bx_srv('bx_events', 'browse_upcoming_profiles', [...]); @endcode
  87. *
  88. * Browse upcoming events
  89. *
  90. * @param $bDisplayEmptyMsg show "empty" message or not if nothing found
  91. * @param $bAjaxPaginate use AJAX paginate or not
  92. *
  93. * @see BxEventsModule::serviceBrowseUpcomingProfiles
  94. */
  95. /**
  96. * @ref bx_events-browse_upcoming_profiles "browse_upcoming_profiles"
  97. */
  98. public function serviceBrowseUpcomingProfiles ($aParams = false)
  99. {
  100. $bDisplayEmptyMsg = false;
  101. if(isset($aParams['empty_message'])) {
  102. $bDisplayEmptyMsg = (bool)$aParams['empty_message'];
  103. unset($aParams['empty_message']);
  104. }
  105. $bAjaxPaginate = true;
  106. if(isset($aParams['ajax_paginate'])) {
  107. $bAjaxPaginate = (bool)$aParams['ajax_paginate'];
  108. unset($aParams['ajax_paginate']);
  109. }
  110. return $this->_serviceBrowse ('upcoming', $aParams, BX_DB_PADDING_DEF, $bDisplayEmptyMsg, $bAjaxPaginate);
  111. }
  112. /**
  113. * @page service Service Calls
  114. * @section bx_events Events
  115. * @subsection bx_events-browse Browse
  116. * @subsubsection bx_events-browse_upcoming_connected_profiles browse_upcoming_connected_profiles
  117. *
  118. * @code bx_srv('bx_events', 'browse_upcoming_connected_profiles', [...]); @endcode
  119. *
  120. * Browse upcoming connected (followed) events
  121. *
  122. * @param $bDisplayEmptyMsg show "empty" message or not if nothing found
  123. * @param $bAjaxPaginate use AJAX paginate or not
  124. *
  125. * @see BxEventsModule::serviceBrowseUpcomingConnectedProfiles
  126. */
  127. /**
  128. * @ref bx_events-browse_upcoming_connected_profiles "browse_upcoming_connected_profiles"
  129. */
  130. public function serviceBrowseUpcomingConnectedProfiles ($aParams = false)
  131. {
  132. if(!is_array($aParams) || empty($aParams['object']) || empty($aParams['profile']))
  133. return '';
  134. $bDisplayEmptyMsg = false;
  135. if(isset($aParams['empty_message'])) {
  136. $bDisplayEmptyMsg = (bool)$aParams['empty_message'];
  137. unset($aParams['empty_message']);
  138. }
  139. $bAjaxPaginate = true;
  140. if(isset($aParams['ajax_paginate'])) {
  141. $bAjaxPaginate = (bool)$aParams['ajax_paginate'];
  142. unset($aParams['ajax_paginate']);
  143. }
  144. $aDefaults = array(
  145. 'type' => 'content',
  146. 'mutual' => false,
  147. 'profile2' => 0
  148. );
  149. return $this->_serviceBrowse ('upcoming_connected', array_merge($aDefaults, $aParams), BX_DB_PADDING_DEF, $bDisplayEmptyMsg, $bAjaxPaginate);
  150. }
  151. /**
  152. * @page service Service Calls
  153. * @section bx_events Events
  154. * @subsection bx_events-browse Browse
  155. * @subsubsection bx_events-browse_past_profiles browse_past_profiles
  156. *
  157. * @code bx_srv('bx_events', 'browse_past_profiles', [...]); @endcode
  158. *
  159. * Browse past events
  160. *
  161. * @param $bDisplayEmptyMsg show "empty" message or not if nothing found
  162. * @param $bAjaxPaginate use AJAX paginate or not
  163. *
  164. * @see BxEventsModule::serviceBrowsePastProfiles
  165. */
  166. /**
  167. * @ref bx_events-browse_past_profiles "browse_past_profiles"
  168. */
  169. public function serviceBrowsePastProfiles ($bDisplayEmptyMsg = false, $bAjaxPaginate = true)
  170. {
  171. return $this->_serviceBrowse ('past', false, BX_DB_PADDING_DEF, $bDisplayEmptyMsg, $bAjaxPaginate);
  172. }
  173. public function serviceIsEnableForContext($iProfileId = 0)
  174. {
  175. if ($iProfileId == 0)
  176. return false;
  177. $CNF = &$this->_oConfig->CNF;
  178. if (in_array(BxDolProfile::getInstance($iProfileId)->getModule(), $CNF['ENABLE_FOR_CONTEXT_IN_MODULES']))
  179. return true;
  180. return false;
  181. }
  182. /**
  183. * @page service Service Calls
  184. * @section bx_events Events
  185. * @subsection bx_events-page_blocks Page Blocks
  186. * @subsubsection bx_events-calendar calendar
  187. *
  188. * @code bx_srv('bx_events', 'calendar', [...]); @endcode
  189. *
  190. * Shows event or events calendar
  191. *
  192. * @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, specify context's ID in 'context_id' array key to show calendar for one context events only. If only one event is specified then it will show calendar only if it's repeating event.
  193. * @param $sTemplate template to use to show calendar, or leave empty for default template, possible options: calendar.html, calendar_compact.html
  194. * @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.
  195. *
  196. * @see BxEventsModule::serviceCalendar
  197. */
  198. /**
  199. * @ref bx_events-calendar "calendar"
  200. */
  201. public function serviceCalendar($aData = array(), $sTemplate = 'calendar.html')
  202. {
  203. if (isset($aData['event'])) {
  204. $aContentInfo = $this->_oDb->getContentInfoById ((int)$aData['event']);
  205. if ('' == $aContentInfo['repeat_stop']) // don't display calendar for non repeating events
  206. return '';
  207. }
  208. if (isset($aData['context_id'])) {
  209. if (!$this->serviceIsEnableForContext($aData['context_id'])){
  210. return '';
  211. }
  212. }
  213. $o = new BxTemplCalendar(array(
  214. 'eventSources' => array (
  215. bx_append_url_params(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'calendar_data', $aData),
  216. ),
  217. ), $this->_oTemplate);
  218. return $o->display($sTemplate);
  219. }
  220. /**
  221. * @page service Service Calls
  222. * @section bx_events Events
  223. * @subsection bx_events-other Other
  224. * @subsubsection bx_events-process_reminders process_reminders
  225. *
  226. * @code bx_srv('bx_events', 'process_reminders'); @endcode
  227. *
  228. * Send remiders to event's participants.
  229. *
  230. * It must be processed once every hour for the proper processing.
  231. * @return nothing
  232. *
  233. * @see BxEventsModule::serviceProcessReminders
  234. */
  235. /**
  236. * @ref bx_events-process_reminders "process_reminders"
  237. */
  238. public function serviceProcessReminders()
  239. {
  240. $iNow = time();
  241. // get all events for today and tomorrow, since the max reminder is 24 hours
  242. $aEntries = $this->_oDb->getEntriesByDate('@' . time(), '@' . (time() + 86400));
  243. foreach ($aEntries as $a) {
  244. if (!$a['reminder'])
  245. continue;
  246. $iTimestamp = $a['start_utc'] - (3600 * $a['reminder']);
  247. if ($iNow > ($iTimestamp - 3600) && $iNow < $iTimestamp)
  248. $this->sendReminders($a);
  249. }
  250. }
  251. /**
  252. * @page service Service Calls
  253. * @section bx_events Events
  254. * @subsection bx_events-internal Internal
  255. * @subsubsection bx_events-get_timeline_post get_timeline_post
  256. *
  257. * @code bx_srv('bx_events', 'get_timeline_post', [...]); @endcode
  258. *
  259. * Get Timeline post. It's needed for Timeline module.
  260. *
  261. * @param $aEvent timeline event array from Timeline module
  262. * @return array in special format which is needed specifically for Timeline module to display the data.
  263. *
  264. * @see BxEventsModule::serviceGetTimelinePost
  265. */
  266. /**
  267. * @ref bx_events-get_timeline_post "get_timeline_post"
  268. */
  269. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  270. {
  271. $a = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  272. if($a === false)
  273. return false;
  274. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  275. if(empty($aContentInfo) || !is_array($aContentInfo))
  276. return $a;
  277. $CNF = $this->_oConfig->CNF;
  278. $oDateStart = date_create('@' . $aContentInfo['date_start'], new DateTimeZone($aContentInfo['timezone'] ? $aContentInfo['timezone'] : 'UTC'));
  279. $oDateEnd = date_create('@' . ($aContentInfo['date_start'] > $aContentInfo['repeat_stop'] ? $aContentInfo['date_start'] : $aContentInfo['repeat_stop']), new DateTimeZone($aContentInfo['timezone'] ? $aContentInfo['timezone'] : 'UTC'));
  280. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  281. $sLocationString = $oMetatags ? $oMetatags->locationsString($aContentInfo[$CNF['FIELD_ID']], false) : false;
  282. $a['content']['raw'] = $this->_oTemplate->parseHtmlByName('timeline_post.html', array(
  283. 'title' => $a['content']['title'],
  284. 'title_attr' => bx_html_attribute($a['content']['title']),
  285. 'url' => $a['content']['url'],
  286. 'date' => $oDateStart->format(getParam('bx_events_short_date_format')) . ($oDateStart->format('ymd') == $oDateEnd->format('ymd') ? '' : ' - ' . $oDateEnd->format(getParam('bx_events_short_date_format'))),
  287. 'date_c' => $oDateStart->format('c'),
  288. 'bx_if:location' => array(
  289. 'condition' => !!$sLocationString,
  290. 'content' => array('location' => $sLocationString),
  291. ),
  292. ));
  293. $a['content']['title'] = '';
  294. $a['content']['text'] = '';
  295. return $a;
  296. }
  297. public function actionIntervals()
  298. {
  299. $sAction = bx_get('a');
  300. $sMethodName = 'subaction' . ucfirst($sAction);
  301. if (!method_exists($this, $sMethodName)) {
  302. $this->_oTemplate->pageNotFound();
  303. return;
  304. }
  305. $this->$sMethodName();
  306. }
  307. public function subactionRestore($iContentId = 0)
  308. {
  309. if (!$iContentId)
  310. $iContentId = (int)bx_get('c');
  311. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  312. if (
  313. ($aContentInfo && CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedEdit ($aContentInfo))
  314. ||
  315. (!$aContentInfo && CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedAdd ())
  316. ) {
  317. $this->_oTemplate->displayAccessDenied();
  318. exit;
  319. }
  320. $a = $this->_oDb->getIntervals($iContentId);
  321. foreach ($a as $iIntervalId => $r) {
  322. $a[$iIntervalId]['file_id'] = $r['interval_id'];
  323. $a[$iIntervalId]['repeat_stop'] = bx_process_output ($r['repeat_stop'], BX_DATA_DATE_TS);
  324. }
  325. if ('json' == bx_get('f')) {
  326. header('Content-type: text/html; charset=utf-8');
  327. echo json_encode($a);
  328. }
  329. }
  330. public function subactionDelete()
  331. {
  332. header('Content-type: text/html; charset=utf-8');
  333. $iIntervalId = (int)bx_get('id');
  334. if (!($aContentInfo = $this->_oDb->getContentInfoByIntervalId($iIntervalId))) {
  335. echo _t('_sys_request_page_not_found_cpt');
  336. }
  337. elseif (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->checkAllowedEdit ($aContentInfo))) {
  338. echo $sMsg;
  339. }
  340. elseif (!$this->_oDb->deleteIntervalById($iIntervalId)) {
  341. echo _t('_sys_txt_error_occured');
  342. }
  343. else {
  344. echo 'ok';
  345. }
  346. }
  347. /**
  348. * Send reminder to event's paritcipants
  349. * @param $aContentInfo content info
  350. * @return number of sent reminders
  351. */
  352. protected function sendReminders($aContentInfo)
  353. {
  354. $CNF = $this->_oConfig->CNF;
  355. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  356. if (!$oConnection)
  357. return false;
  358. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aContentInfo[$CNF['FIELD_ID']], $this->getName());
  359. if (!$oGroupProfile)
  360. return false;
  361. $aParticipants = $oConnection->getConnectedInitiators($oGroupProfile->id(), true);
  362. if (!$aParticipants)
  363. return true;
  364. $oDateBegin = new DateTime();
  365. $oDateBegin->setTimestamp($aContentInfo['start_utc']);
  366. $oDateBegin->setTimezone(new DateTimeZone($aContentInfo['timezone'] ? $aContentInfo['timezone'] : 'UTC'));
  367. $sEntryBegin = $oDateBegin->format('r');
  368. $sEntryBeginShort = $oDateBegin->format(getParam('bx_events_short_date_format'));
  369. $oDateBegin->setTimezone(new DateTimeZone('UTC'));
  370. $sEntryBeginUTC = $oDateBegin->format('c');
  371. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  372. $sLocationString = $oMetatags->locationsString($aContentInfo[$CNF['FIELD_ID']], false);
  373. $iCounter = 0;
  374. foreach ($aParticipants as $iProfileId) {
  375. $b = sendMailTemplate($CNF['EMAIL_REMINDER'], 0, $iProfileId, array(
  376. 'EntryUrl' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]),
  377. 'EntryTitle' => $aContentInfo['title'],
  378. 'EntryBegin' => $sEntryBegin,
  379. 'EntryBeginShort' => $sEntryBeginShort,
  380. 'EntryBeginUTC' => $sEntryBeginUTC,
  381. 'EntryLocation' => $sLocationString,
  382. ), BX_EMAIL_NOTIFY);
  383. $iCounter += $b ? 1 : 0;
  384. }
  385. return $iCounter;
  386. }
  387. }
  388. /** @} */