BxEventsModule.php 27 KB

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