BxEventsModule.php 29 KB

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