BxEventsModule.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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. use Spatie\CalendarLinks\Link;
  12. /**
  13. * Events profiles module.
  14. */
  15. class BxEventsModule extends BxBaseModGroupsModule implements iBxDolCalendarService
  16. {
  17. function __construct(&$aModule)
  18. {
  19. parent::__construct($aModule);
  20. $CNF = &$this->_oConfig->CNF;
  21. $this->_aSearchableNamesExcept = array_merge($this->_aSearchableNamesExcept, array(
  22. $CNF['FIELD_TIMEZONE'],
  23. $CNF['FIELD_JOIN_CONFIRMATION'],
  24. $CNF['FIELD_REMINDER']
  25. ));
  26. }
  27. public function actionCalendarSync($iContentId = 0)
  28. {
  29. $aContentInfo = (int)$iContentId ? $this->_oDb->getContentInfoById((int)$iContentId) : null;
  30. if (!$aContentInfo) {
  31. $this->_oTemplate->displayPageNotFound();
  32. exit;
  33. }
  34. if (CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedView($aContentInfo)) {
  35. $this->_oTemplate->displayAccessDenied();
  36. exit;
  37. }
  38. $CNF = $this->_oConfig->CNF;
  39. if (!isset($aContentInfo['date_start']) || !$aContentInfo['date_start'] || !isset($aContentInfo['date_end']) || !$aContentInfo['date_end'] || !$aContentInfo[$CNF['FIELD_TIMEZONE']] || empty($CNF['OBJECT_METATAGS'])) {
  40. $this->_oTemplate->displayPageNotFound();
  41. exit;
  42. }
  43. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  44. $sLocation = $oMetatags && $oMetatags->locationsIsEnabled() ? $oMetatags->locationsString($aContentInfo[$CNF['FIELD_ID']], false) : '';
  45. $oDateStart = new DateTime('@' . $aContentInfo['date_start']);
  46. $oDateEnd = new DateTime('@' . ($aContentInfo['date_end'] > $aContentInfo['repeat_stop'] ? $aContentInfo['date_end'] : $aContentInfo['repeat_stop']));
  47. $oTz = new DateTimeZone($aContentInfo[$CNF['FIELD_TIMEZONE']]);
  48. $oDateStart->setTimezone($oTz);
  49. $oDateEnd->setTimezone($oTz);
  50. $oICalLink = $oDateStart && $oDateEnd ? Link::create(
  51. $aContentInfo[$CNF['FIELD_TITLE']],
  52. $oDateStart,
  53. $oDateEnd
  54. ) : null;
  55. if ($sLocation)
  56. $oICalLink->address($sLocation);
  57. $s = $oICalLink->ics();
  58. if (!preg_match('/^data:([a-zA-Z0-9\/]+);charset=[a-zA-Z0-9]+;base64,(.*)$/', $s, $m)) {
  59. $this->_oTemplate->displayErrorOccured();
  60. exit;
  61. }
  62. header('Content-type: ' . $m[1] . '; charset=utf-8');
  63. header('Content-Disposition: inline; filename="event.ics"');
  64. echo base64_decode($m[2]);
  65. }
  66. public function actionCalendarData()
  67. {
  68. $aParams = [
  69. 'type' => ($sType = bx_get('type')) !== false ? $sType : 'browse',
  70. 'start' => bx_get('start'),
  71. 'end' => bx_get('end')
  72. ];
  73. if(($iEventId = bx_get('event')) !== false)
  74. $aParams['event_id'] = (int)$iEventId;
  75. if(($iProfileId = bx_get('profile_id')) !== false)
  76. $aParams['profile_id'] = (int)$iProfileId;
  77. if(($iContextId = bx_get('context_id')) !== false)
  78. $aParams['context_id'] = (int)$iContextId;
  79. header('Content-Type: application/json; charset=utf-8');
  80. echo json_encode($this->serviceCalendarData($aParams));
  81. }
  82. public function serviceCalendarData($aParams = [])
  83. {
  84. $CNF = &$this->_oConfig->CNF;
  85. if($this->_bIsApi && is_string($aParams)) {
  86. $aParams = json_decode($aParams, true);
  87. if(!empty($aParams['params']))
  88. $aParams = $aParams['params'];
  89. }
  90. $sModule = $this->getName();
  91. $oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW']);
  92. $iNow = time();
  93. $sStart = !empty($aParams['start']) ? $aParams['start'] : date('d.m.Y', $iNow);
  94. $sEnd = !empty($aParams['end']) ? $aParams['end'] : date('d.m.Y', $iNow + 3600 * 24 * 365);
  95. $iContentId = 0;
  96. $iContextId = 0;
  97. $aSQLPart = [];
  98. switch($aParams['type']) {
  99. case 'event':
  100. $iContentId = !empty($aParams['event_id']) ? (int)$aParams['event_id'] : 0;
  101. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  102. if(CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedView($aContentInfo))
  103. $this->_oTemplate->displayAccessDenied();
  104. break;
  105. case 'author':
  106. $iProfileId = !empty($aParams['profile_id']) ? (int)$aParams['profile_id'] : 0;
  107. $aSQLPart = [
  108. 'where' => $this->_oDb->prepareAsString("AND `{$CNF['TABLE_ENTRIES']}`.`{$CNF['FIELD_AUTHOR']}`=?", $iProfileId)
  109. ];
  110. break;
  111. case 'joined':
  112. $iProfileId = !empty($aParams['profile_id']) ? (int)$aParams['profile_id'] : 0;
  113. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  114. if(!$oConnection)
  115. return [];
  116. $aEventsJoined = $oConnection->getConnectedContent($iProfileId, true);
  117. if(empty($aEventsJoined) || !is_array($aEventsJoined))
  118. return [];
  119. $aSQLPart = [
  120. 'join' => "INNER JOIN `sys_profiles` ON `{$CNF['TABLE_ENTRIES']}`.`{$CNF['FIELD_ID']}`=`sys_profiles`.`content_id` AND `sys_profiles`.`type`='{$sModule}'",
  121. 'where' => "AND `sys_profiles`.`id` IN (" . $this->_oDb->implode_escape($aEventsJoined) . ")"
  122. ];
  123. break;
  124. case 'context':
  125. $iContextId = !empty($aParams['context_id']) ? (int)$aParams['context_id'] : 0;
  126. if(!$this->serviceIsEnableForContext($iContextId))
  127. return [];
  128. $aSQLPart = $oPrivacy->getContentByGroupAsSQLPart(-$iContextId);
  129. break;
  130. default:
  131. $aSQLPart = $oPrivacy->getContentPublicAsSQLPart(0, $oPrivacy->getPartiallyVisiblePrivacyGroups());
  132. }
  133. // get entries
  134. $aEntries = $this->_oDb->getEntriesByDate($sStart, $sEnd, $iContentId, $aSQLPart);
  135. bx_alert($this->getName(), 'calendar_data', 0, false, array(
  136. 'event' => $iContentId,
  137. 'context_id' => $iContextId,
  138. 'start' => $sStart,
  139. 'end' => $sEnd,
  140. 'sql_part' => &$aSQLPart,
  141. 'data' => &$aEntries,
  142. ));
  143. return $aEntries;
  144. }
  145. public function serviceIsIcalExportAvaliable($iContentId)
  146. {
  147. return true;
  148. $aContentInfo = $iContentId ? $this->_oDb->getContentInfoById($iContentId) : false;
  149. if ($aContentInfo)
  150. return false;
  151. return $aContentInfo['date_start'] && $aContentInfo['date_end'];
  152. }
  153. public function decodeDataAPI($aData, $aParams = [])
  154. {
  155. $CNF = $this->_oConfig->CNF;
  156. $aResult = parent::decodeDataAPI($aData, $aParams);
  157. $aResult = array_merge($aResult, [
  158. 'date_start' => $aData[$CNF['FIELD_DATE_START']],
  159. 'date_end' => $aData[$CNF['FIELD_DATE_END']]
  160. ]);
  161. return $aResult;
  162. }
  163. public function serviceGetCalendarEntries($iProfileId)
  164. {
  165. $CNF = &$this->_oConfig->CNF;
  166. $oConn = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  167. $aData = $oConn->getConnectedContent($iProfileId);
  168. $aData2 = array(0);
  169. foreach($aData as $iProfileId2){
  170. $oProfile = BxDolProfile::getInstance($iProfileId2);
  171. array_push($aData2, $oProfile->getContentId());
  172. }
  173. $aSQLPart['where'] = " AND `bx_events_data`.`id` IN(" . implode(',', $aData2) . ")";
  174. return $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), null, $aSQLPart);
  175. }
  176. public function serviceGetSafeServices()
  177. {
  178. $a = parent::serviceGetSafeServices();
  179. return array_merge($a, array (
  180. 'BrowsePastProfiles' => '',
  181. 'Calendar' => '',
  182. 'CalendarData' => ''
  183. ));
  184. }
  185. /**
  186. * @page service Service Calls
  187. * @section bx_events Events
  188. * @subsection bx_events-browse Browse
  189. * @subsubsection bx_events-browse_upcoming_profiles browse_upcoming_profiles
  190. *
  191. * @code bx_srv('bx_events', 'browse_upcoming_profiles', [...]); @endcode
  192. *
  193. * Browse upcoming events
  194. *
  195. * @param $bDisplayEmptyMsg show "empty" message or not if nothing found
  196. * @param $bAjaxPaginate use AJAX paginate or not
  197. *
  198. * @see BxEventsModule::serviceBrowseUpcomingProfiles
  199. */
  200. /**
  201. * @ref bx_events-browse_upcoming_profiles "browse_upcoming_profiles"
  202. */
  203. public function serviceBrowseUpcomingProfiles ($aParams = false)
  204. {
  205. $bDisplayEmptyMsg = false;
  206. if(isset($aParams['empty_message'])) {
  207. $bDisplayEmptyMsg = (bool)$aParams['empty_message'];
  208. unset($aParams['empty_message']);
  209. }
  210. $bAjaxPaginate = true;
  211. if(isset($aParams['ajax_paginate'])) {
  212. $bAjaxPaginate = (bool)$aParams['ajax_paginate'];
  213. unset($aParams['ajax_paginate']);
  214. }
  215. return $this->_serviceBrowse ('upcoming', $aParams, BX_DB_PADDING_DEF, $bDisplayEmptyMsg, $bAjaxPaginate);
  216. }
  217. /**
  218. * @page service Service Calls
  219. * @section bx_events Events
  220. * @subsection bx_events-browse Browse
  221. * @subsubsection bx_events-browse_upcoming_connected_profiles browse_upcoming_connected_profiles
  222. *
  223. * @code bx_srv('bx_events', 'browse_upcoming_connected_profiles', [...]); @endcode
  224. *
  225. * Browse upcoming connected (followed) events
  226. *
  227. * @param $bDisplayEmptyMsg show "empty" message or not if nothing found
  228. * @param $bAjaxPaginate use AJAX paginate or not
  229. *
  230. * @see BxEventsModule::serviceBrowseUpcomingConnectedProfiles
  231. */
  232. /**
  233. * @ref bx_events-browse_upcoming_connected_profiles "browse_upcoming_connected_profiles"
  234. */
  235. public function serviceBrowseUpcomingConnectedProfiles ($aParams = false)
  236. {
  237. if(!is_array($aParams) || empty($aParams['object']) || empty($aParams['profile']))
  238. return '';
  239. $bDisplayEmptyMsg = false;
  240. if(isset($aParams['empty_message'])) {
  241. $bDisplayEmptyMsg = (bool)$aParams['empty_message'];
  242. unset($aParams['empty_message']);
  243. }
  244. $bAjaxPaginate = true;
  245. if(isset($aParams['ajax_paginate'])) {
  246. $bAjaxPaginate = (bool)$aParams['ajax_paginate'];
  247. unset($aParams['ajax_paginate']);
  248. }
  249. $aDefaults = array(
  250. 'type' => 'content',
  251. 'mutual' => false,
  252. 'profile2' => 0
  253. );
  254. return $this->_serviceBrowse ('upcoming_connected', array_merge($aDefaults, $aParams), BX_DB_PADDING_DEF, $bDisplayEmptyMsg, $bAjaxPaginate);
  255. }
  256. /**
  257. * @page service Service Calls
  258. * @section bx_events Events
  259. * @subsection bx_events-browse Browse
  260. * @subsubsection bx_events-browse_past_profiles browse_past_profiles
  261. *
  262. * @code bx_srv('bx_events', 'browse_past_profiles', [...]); @endcode
  263. *
  264. * Browse past events
  265. *
  266. * @param $bDisplayEmptyMsg show "empty" message or not if nothing found
  267. * @param $bAjaxPaginate use AJAX paginate or not
  268. *
  269. * @see BxEventsModule::serviceBrowsePastProfiles
  270. */
  271. /**
  272. * @ref bx_events-browse_past_profiles "browse_past_profiles"
  273. */
  274. public function serviceBrowsePastProfiles ($bDisplayEmptyMsg = false, $bAjaxPaginate = true)
  275. {
  276. return $this->_serviceBrowse ('past', false, BX_DB_PADDING_DEF, $bDisplayEmptyMsg, $bAjaxPaginate);
  277. }
  278. /**
  279. * @page service Service Calls
  280. * @section bx_events Events
  281. * @subsection bx_events-browse Browse
  282. * @subsubsection bx_events-browse_past_profiles browse_past_profiles_by_params
  283. *
  284. * @code bx_srv('bx_events', 'browse_past_profiles_by_params', [...]); @endcode
  285. *
  286. * Browse past events
  287. *
  288. * @param $aParams array with all parameters
  289. *
  290. * @see BxEventsModule::serviceBrowsePastProfilesByParams
  291. */
  292. /**
  293. * @ref bx_events-browse_past_profiles_by_params "browse_past_profiles_by_params"
  294. */
  295. public function serviceBrowsePastProfilesByParams ($aParams)
  296. {
  297. $bDisplayEmptyMsg = false;
  298. if(isset($aParams['empty_message'])) {
  299. $bDisplayEmptyMsg = (bool)$aParams['empty_message'];
  300. unset($aParams['empty_message']);
  301. }
  302. $bAjaxPaginate = true;
  303. if(isset($aParams['ajax_paginate'])) {
  304. $bAjaxPaginate = (bool)$aParams['ajax_paginate'];
  305. unset($aParams['ajax_paginate']);
  306. }
  307. return $this->_serviceBrowse ('past', $aParams, BX_DB_PADDING_DEF, $bDisplayEmptyMsg, $bAjaxPaginate);
  308. }
  309. /**
  310. * @page service Service Calls
  311. * @section bx_events Events
  312. * @subsection bx_events-page_blocks Page Blocks
  313. * @subsubsection bx_events-calendar calendar
  314. *
  315. * @code bx_srv('bx_events', 'calendar', [...]); @endcode
  316. *
  317. * Shows event or events calendar
  318. *
  319. * @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.
  320. * @param $sTemplate template to use to show calendar, or leave empty for default template, possible options: calendar.html, calendar_compact.html
  321. * @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.
  322. *
  323. * @see BxEventsModule::serviceCalendar
  324. */
  325. /**
  326. * @ref bx_events-calendar "calendar"
  327. */
  328. public function serviceCalendar($aData = [], $sTemplate = 'calendar.html')
  329. {
  330. if(isset($aData['event']) && !isset($aData['type']))
  331. $aData['type'] = 'event';
  332. if(isset($aData['profile_id']) && !isset($aData['type']))
  333. $aData['type'] = 'joined';
  334. if(isset($aData['context_id'])) {
  335. if(!$this->serviceIsEnableForContext($aData['context_id']))
  336. return '';
  337. $aData['type'] = 'context';
  338. }
  339. if(!isset($aData['type']))
  340. $aData['type'] = 'browse';
  341. $oCalendar = new BxTemplCalendar([
  342. 'eventSources' => [
  343. bx_append_url_params(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'calendar_data', $aData),
  344. ],
  345. ], $this->_oTemplate);
  346. if (bx_is_api())
  347. return [bx_api_get_block('calendar', ['request_url' => $this->getName() . '/calendar_data&params[]=' . json_encode(['params' => $aData])])];
  348. $this->_oTemplate->addCss(['main.css']);
  349. return $oCalendar->display($sTemplate);
  350. }
  351. /**
  352. * @page service Service Calls
  353. * @section bx_events Events
  354. * @subsection bx_events-other Other
  355. * @subsubsection bx_events-process_reminders process_reminders
  356. *
  357. * @code bx_srv('bx_events', 'process_reminders'); @endcode
  358. *
  359. * Send remiders to event's participants.
  360. *
  361. * It must be processed once every hour for the proper processing.
  362. * @return nothing
  363. *
  364. * @see BxEventsModule::serviceProcessReminders
  365. */
  366. /**
  367. * @ref bx_events-process_reminders "process_reminders"
  368. */
  369. public function serviceProcessReminders()
  370. {
  371. $CNF = &$this->_oConfig->CNF;
  372. $iNow = time();
  373. $bUseIn = $this->_oConfig->isInternalNotifications();
  374. $sModule = $this->getName();
  375. // get all events for today and tomorrow, since the max reminder is 24 hours
  376. $aEntries = $this->_oDb->getEntriesByDate('@' . time(), '@' . (time() + 3600 * getParam('bx_events_reminder_interval')));
  377. foreach($aEntries as $aEntry) {
  378. $aReminders = $bUseIn ? [$aEntry['reminder']] : [24, 1];
  379. if(!$aReminders)
  380. continue;
  381. $oEventProfile = BxDolProfile::getInstanceByContentAndType($aEntry[$CNF['FIELD_ID']], $sModule);
  382. if(!$oEventProfile)
  383. continue;
  384. $iEventProfileId = $oEventProfile->id();
  385. foreach($aReminders as $iReminder) {
  386. $iTimestamp = $aEntry['start_utc'] - (3600 * $iReminder);
  387. if($iNow <= ($iTimestamp - 3600) || $iNow >= $iTimestamp)
  388. continue;
  389. if($bUseIn)
  390. $this->sendReminders($aEntry);
  391. bx_alert($sModule, 'reminder', $aEntry[$CNF['FIELD_ID']], $iEventProfileId, array(
  392. 'object_author_id' => $iEventProfileId,
  393. 'reminder' => $iReminder
  394. ));
  395. }
  396. }
  397. }
  398. public function serviceGetNotificationsData()
  399. {
  400. $CNF = &$this->_oConfig->CNF;
  401. $aResults = parent::serviceGetNotificationsData();
  402. $sModule = $this->_aModule['name'];
  403. $aSettingsTypes = ['follow_context'];
  404. $aResults['handlers'] = array_merge($aResults['handlers'], [
  405. ['group' => $sModule . '_reminder', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'reminder', 'module_name' => $sModule, 'module_method' => 'get_notifications_reminder', 'module_class' => 'Module', 'module_event_privacy' => $CNF['OBJECT_PRIVACY_VIEW_NOTIFICATION_EVENT']],
  406. ]);
  407. $aResults['settings'] = array_merge($aResults['settings'], [
  408. ['group' => 'reminder', 'unit' => $sModule, 'action' => 'reminder', 'types' => $aSettingsTypes],
  409. ]);
  410. $aResults['alerts'] = array_merge($aResults['alerts'], [
  411. ['unit' => $sModule, 'action' => 'reminder'],
  412. ]);
  413. return $aResults;
  414. }
  415. public function serviceGetNotificationsInsertData($oAlert, $aHandler, $aDataItems)
  416. {
  417. if($oAlert->sAction != 'reminder' || empty($aDataItems) || !is_array($aDataItems) || empty($oAlert->aExtras['reminder']))
  418. return $aDataItems;
  419. foreach($aDataItems as $iIndex => $aDataItem) {
  420. $aContent = [];
  421. if(!empty($aDataItem['content']))
  422. $aContent = unserialize($aDataItem['content']);
  423. $aContent['reminder'] = (int)$oAlert->aExtras['reminder'];
  424. $aDataItems[$iIndex]['content'] = serialize($aContent);
  425. }
  426. return $aDataItems;
  427. }
  428. public function serviceGetNotificationsReminder($aEvent)
  429. {
  430. $CNF = &$this->_oConfig->CNF;
  431. if(!isset($aEvent['content']['reminder']))
  432. return [];
  433. $iContentId = (int)$aEvent['object_id'];
  434. $oEventProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  435. if(!$oEventProfile)
  436. return [];
  437. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  438. if(empty($aContentInfo) || !is_array($aContentInfo))
  439. return [];
  440. return [
  441. 'entry_sample' => $CNF['T']['txt_sample_single'],
  442. 'entry_url' => bx_absolute_url(str_replace(BX_DOL_URL_ROOT, '', $oEventProfile->getUrl()), '{bx_url_root}'),
  443. 'entry_caption' => $oEventProfile->getDisplayName(),
  444. 'entry_author' => $oEventProfile->id(),
  445. 'lang_key' => '_bx_events_txt_ntfs_reminder_' . $aEvent['content']['reminder']
  446. ];
  447. }
  448. /**
  449. * @page service Service Calls
  450. * @section bx_events Events
  451. * @subsection bx_events-internal Internal
  452. * @subsubsection bx_events-get_timeline_post get_timeline_post
  453. *
  454. * @code bx_srv('bx_events', 'get_timeline_post', [...]); @endcode
  455. *
  456. * Get Timeline post. It's needed for Timeline module.
  457. *
  458. * @param $aEvent timeline event array from Timeline module
  459. * @return array in special format which is needed specifically for Timeline module to display the data.
  460. *
  461. * @see BxEventsModule::serviceGetTimelinePost
  462. */
  463. /**
  464. * @ref bx_events-get_timeline_post "get_timeline_post"
  465. */
  466. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  467. {
  468. $a = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  469. if($a === false)
  470. return false;
  471. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  472. if(empty($aContentInfo) || !is_array($aContentInfo))
  473. return $a;
  474. $CNF = $this->_oConfig->CNF;
  475. $oDateStart = date_create('@' . $aContentInfo['date_start']);
  476. if ($oDateStart)
  477. $oDateStart->setTimezone(new DateTimeZone($aContentInfo['timezone'] ? $aContentInfo['timezone'] : 'UTC'));
  478. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  479. $sLocationString = $oMetatags ? $oMetatags->locationsString($aContentInfo[$CNF['FIELD_ID']], false) : false;
  480. $a['content']['raw'] = $this->_oTemplate->parseHtmlByName('timeline_post.html', [
  481. 'title' => $a['content']['title'],
  482. 'title_attr' => bx_html_attribute($a['content']['title']),
  483. 'url' => $a['content']['url'],
  484. 'bx_if:date' => [
  485. 'condition' => $oDateStart,
  486. 'content' => [
  487. 'date' => $oDateStart ? bx_time_js($aContentInfo['date_start'], BX_FORMAT_DATE_TIME, true) : '',
  488. 'date_c' => $oDateStart->format('c'),
  489. ]],
  490. 'bx_if:location' => array(
  491. 'condition' => !!$sLocationString,
  492. 'content' => array('location' => $sLocationString),
  493. ),
  494. ]);
  495. return $a;
  496. }
  497. public function actionIntervals()
  498. {
  499. $sAction = bx_get('a');
  500. $sMethodName = 'subaction' . ucfirst($sAction);
  501. if (!method_exists($this, $sMethodName)) {
  502. $this->_oTemplate->pageNotFound();
  503. return;
  504. }
  505. $this->$sMethodName();
  506. }
  507. public function subactionRestore($iContentId = 0)
  508. {
  509. if (!$iContentId)
  510. $iContentId = (int)bx_get('c');
  511. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  512. if (
  513. ($aContentInfo && CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedEdit ($aContentInfo))
  514. ||
  515. (!$aContentInfo && CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedAdd ())
  516. ) {
  517. $this->_oTemplate->displayAccessDenied();
  518. exit;
  519. }
  520. $a = $this->_oDb->getIntervals($iContentId);
  521. foreach ($a as $iIntervalId => $r) {
  522. $a[$iIntervalId]['file_id'] = $r['interval_id'];
  523. $a[$iIntervalId]['repeat_stop'] = bx_process_output ($r['repeat_stop'], BX_DATA_DATE_TS);
  524. }
  525. if ('json' == bx_get('f')) {
  526. header('Content-type: text/html; charset=utf-8');
  527. echo json_encode($a);
  528. }
  529. }
  530. public function subactionDelete()
  531. {
  532. header('Content-type: text/html; charset=utf-8');
  533. $iIntervalId = (int)bx_get('id');
  534. if (!($aContentInfo = $this->_oDb->getContentInfoByIntervalId($iIntervalId))) {
  535. echo _t('_sys_request_page_not_found_cpt');
  536. }
  537. elseif (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->checkAllowedEdit ($aContentInfo))) {
  538. echo $sMsg;
  539. }
  540. elseif (!$this->_oDb->deleteIntervalById($iIntervalId)) {
  541. echo _t('_sys_txt_error_occured');
  542. }
  543. else {
  544. echo 'ok';
  545. }
  546. }
  547. public function checkAllowedIcalExport ($aDataEntry, $isPerformAction = false)
  548. {
  549. return $aDataEntry['date_start'] && $aDataEntry['date_end'] ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_access_denied');
  550. }
  551. /**
  552. * Send reminder to event's paritcipants
  553. * @param $aContentInfo content info
  554. * @return number of sent reminders
  555. */
  556. protected function sendReminders($aContentInfo)
  557. {
  558. $CNF = $this->_oConfig->CNF;
  559. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  560. if (!$oConnection)
  561. return false;
  562. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aContentInfo[$CNF['FIELD_ID']], $this->getName());
  563. if (!$oGroupProfile)
  564. return false;
  565. $aParticipants = $oConnection->getConnectedInitiators($oGroupProfile->id(), true);
  566. if (!$aParticipants)
  567. return true;
  568. $oDateBegin = new DateTime();
  569. $oDateBegin->setTimestamp($aContentInfo['start_utc']);
  570. $oDateBegin->setTimezone(new DateTimeZone($aContentInfo['timezone'] ? $aContentInfo['timezone'] : 'UTC'));
  571. $sEntryBegin = $oDateBegin->format('r');
  572. $sEntryBeginShort = date(getParam('bx_events_short_date_format'), $oDateBegin->getTimestamp());
  573. $oDateBegin->setTimezone(new DateTimeZone('UTC'));
  574. $sEntryBeginUTC = $oDateBegin->format('c');
  575. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  576. $sLocationString = $oMetatags->locationsString($aContentInfo[$CNF['FIELD_ID']], false);
  577. $iCounter = 0;
  578. foreach ($aParticipants as $iProfileId) {
  579. $b = sendMailTemplate($CNF['EMAIL_REMINDER'], 0, $iProfileId, array(
  580. 'EntryUrl' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']])),
  581. 'EntryTitle' => $aContentInfo['title'],
  582. 'EntryBegin' => $sEntryBegin,
  583. 'EntryBeginShort' => $sEntryBeginShort,
  584. 'EntryBeginUTC' => $sEntryBeginUTC,
  585. 'EntryLocation' => $sLocationString ? _t('_bx_events_email_reminder_location', $sLocationString) : '',
  586. ), BX_EMAIL_NOTIFY);
  587. $iCounter += $b ? 1 : 0;
  588. }
  589. return $iCounter;
  590. }
  591. }
  592. /** @} */