BxEventsModule.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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. public function serviceEntityEditQuestionnaire($iProfileId = 0)
  226. {
  227. $CNF = &$this->_oConfig->CNF;
  228. if(!$iProfileId)
  229. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  230. if(!$iProfileId)
  231. return '';
  232. $aProfileInfo = BxDolProfileQuery::getInstance()->getInfoById($iProfileId);
  233. if(empty($aProfileInfo) || !is_array($aProfileInfo))
  234. return '';
  235. $aContentInfo = $this->_oDb->getContentInfoById($aProfileInfo['content_id']);
  236. if(empty($aContentInfo) || !is_array($aContentInfo))
  237. return '';
  238. if($this->checkAllowedEdit($aContentInfo) !== CHECK_ACTION_RESULT_ALLOWED)
  239. return MsgBox(_t('_Access denied'));
  240. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_QUESTIONS_MANAGE']);
  241. if(!$oGrid)
  242. return '';
  243. return $oGrid->getCode();
  244. }
  245. public function serviceEntitySessions($iProfileId = 0)
  246. {
  247. $CNF = &$this->_oConfig->CNF;
  248. if(!$iProfileId)
  249. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  250. if(!$iProfileId)
  251. return '';
  252. $aProfileInfo = BxDolProfileQuery::getInstance()->getInfoById($iProfileId);
  253. if(empty($aProfileInfo) || !is_array($aProfileInfo))
  254. return '';
  255. $aContentInfo = $this->_oDb->getContentInfoById($aProfileInfo['content_id']);
  256. if(empty($aContentInfo) || !is_array($aContentInfo))
  257. return '';
  258. if($this->checkAllowedEdit($aContentInfo) !== CHECK_ACTION_RESULT_ALLOWED)
  259. return MsgBox(_t('_Access denied'));
  260. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_SESSIONS_MANAGE']);
  261. if(!$oGrid)
  262. return '';
  263. return $oGrid->getCode();
  264. }
  265. /**
  266. * @page service Service Calls
  267. * @section bx_events Events
  268. * @subsection bx_events-browse Browse
  269. * @subsubsection bx_events-browse_upcoming_profiles browse_upcoming_profiles
  270. *
  271. * @code bx_srv('bx_events', 'browse_upcoming_profiles', [...]); @endcode
  272. *
  273. * Browse upcoming events
  274. *
  275. * @param $bDisplayEmptyMsg show "empty" message or not if nothing found
  276. * @param $bAjaxPaginate use AJAX paginate or not
  277. *
  278. * @see BxEventsModule::serviceBrowseUpcomingProfiles
  279. */
  280. /**
  281. * @ref bx_events-browse_upcoming_profiles "browse_upcoming_profiles"
  282. */
  283. public function serviceBrowseUpcomingProfiles ($aParams = false)
  284. {
  285. $bDisplayEmptyMsg = false;
  286. if(isset($aParams['empty_message'])) {
  287. $bDisplayEmptyMsg = (bool)$aParams['empty_message'];
  288. unset($aParams['empty_message']);
  289. }
  290. $bAjaxPaginate = true;
  291. if(isset($aParams['ajax_paginate'])) {
  292. $bAjaxPaginate = (bool)$aParams['ajax_paginate'];
  293. unset($aParams['ajax_paginate']);
  294. }
  295. return $this->_serviceBrowse ('upcoming', $aParams, BX_DB_PADDING_DEF, $bDisplayEmptyMsg, $bAjaxPaginate);
  296. }
  297. /**
  298. * @page service Service Calls
  299. * @section bx_events Events
  300. * @subsection bx_events-browse Browse
  301. * @subsubsection bx_events-browse_upcoming_connected_profiles browse_upcoming_connected_profiles
  302. *
  303. * @code bx_srv('bx_events', 'browse_upcoming_connected_profiles', [...]); @endcode
  304. *
  305. * Browse upcoming connected (followed) events
  306. *
  307. * @param $bDisplayEmptyMsg show "empty" message or not if nothing found
  308. * @param $bAjaxPaginate use AJAX paginate or not
  309. *
  310. * @see BxEventsModule::serviceBrowseUpcomingConnectedProfiles
  311. */
  312. /**
  313. * @ref bx_events-browse_upcoming_connected_profiles "browse_upcoming_connected_profiles"
  314. */
  315. public function serviceBrowseUpcomingConnectedProfiles ($aParams = false)
  316. {
  317. if(!is_array($aParams) || empty($aParams['object']) || empty($aParams['profile']))
  318. return '';
  319. $bDisplayEmptyMsg = false;
  320. if(isset($aParams['empty_message'])) {
  321. $bDisplayEmptyMsg = (bool)$aParams['empty_message'];
  322. unset($aParams['empty_message']);
  323. }
  324. $bAjaxPaginate = true;
  325. if(isset($aParams['ajax_paginate'])) {
  326. $bAjaxPaginate = (bool)$aParams['ajax_paginate'];
  327. unset($aParams['ajax_paginate']);
  328. }
  329. $aDefaults = array(
  330. 'type' => 'content',
  331. 'mutual' => false,
  332. 'profile2' => 0
  333. );
  334. return $this->_serviceBrowse ('upcoming_connected', array_merge($aDefaults, $aParams), BX_DB_PADDING_DEF, $bDisplayEmptyMsg, $bAjaxPaginate);
  335. }
  336. /**
  337. * @page service Service Calls
  338. * @section bx_events Events
  339. * @subsection bx_events-browse Browse
  340. * @subsubsection bx_events-browse_past_profiles browse_past_profiles
  341. *
  342. * @code bx_srv('bx_events', 'browse_past_profiles', [...]); @endcode
  343. *
  344. * Browse past events
  345. *
  346. * @param $bDisplayEmptyMsg show "empty" message or not if nothing found
  347. * @param $bAjaxPaginate use AJAX paginate or not
  348. *
  349. * @see BxEventsModule::serviceBrowsePastProfiles
  350. */
  351. /**
  352. * @ref bx_events-browse_past_profiles "browse_past_profiles"
  353. */
  354. public function serviceBrowsePastProfiles ($bDisplayEmptyMsg = false, $bAjaxPaginate = true)
  355. {
  356. return $this->_serviceBrowse ('past', false, BX_DB_PADDING_DEF, $bDisplayEmptyMsg, $bAjaxPaginate);
  357. }
  358. /**
  359. * @page service Service Calls
  360. * @section bx_events Events
  361. * @subsection bx_events-browse Browse
  362. * @subsubsection bx_events-browse_past_profiles browse_past_profiles_by_params
  363. *
  364. * @code bx_srv('bx_events', 'browse_past_profiles_by_params', [...]); @endcode
  365. *
  366. * Browse past events
  367. *
  368. * @param $aParams array with all parameters
  369. *
  370. * @see BxEventsModule::serviceBrowsePastProfilesByParams
  371. */
  372. /**
  373. * @ref bx_events-browse_past_profiles_by_params "browse_past_profiles_by_params"
  374. */
  375. public function serviceBrowsePastProfilesByParams ($aParams)
  376. {
  377. $bDisplayEmptyMsg = false;
  378. if(isset($aParams['empty_message'])) {
  379. $bDisplayEmptyMsg = (bool)$aParams['empty_message'];
  380. unset($aParams['empty_message']);
  381. }
  382. $bAjaxPaginate = true;
  383. if(isset($aParams['ajax_paginate'])) {
  384. $bAjaxPaginate = (bool)$aParams['ajax_paginate'];
  385. unset($aParams['ajax_paginate']);
  386. }
  387. return $this->_serviceBrowse ('past', $aParams, BX_DB_PADDING_DEF, $bDisplayEmptyMsg, $bAjaxPaginate);
  388. }
  389. /**
  390. * @page service Service Calls
  391. * @section bx_events Events
  392. * @subsection bx_events-page_blocks Page Blocks
  393. * @subsubsection bx_events-calendar calendar
  394. *
  395. * @code bx_srv('bx_events', 'calendar', [...]); @endcode
  396. *
  397. * Shows event or events calendar
  398. *
  399. * @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.
  400. * @param $sTemplate template to use to show calendar, or leave empty for default template, possible options: calendar.html, calendar_compact.html
  401. * @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.
  402. *
  403. * @see BxEventsModule::serviceCalendar
  404. */
  405. /**
  406. * @ref bx_events-calendar "calendar"
  407. */
  408. public function serviceCalendar($aData = [], $sTemplate = 'calendar.html')
  409. {
  410. if(isset($aData['event']) && !isset($aData['type']))
  411. $aData['type'] = 'event';
  412. if(isset($aData['profile_id']) && !isset($aData['type']))
  413. $aData['type'] = 'joined';
  414. if(isset($aData['context_id'])) {
  415. if(!$this->serviceIsEnableForContext($aData['context_id']))
  416. return '';
  417. $aData['type'] = 'context';
  418. }
  419. if(!isset($aData['type']))
  420. $aData['type'] = 'browse';
  421. $oCalendar = new BxTemplCalendar([
  422. 'eventSources' => [
  423. bx_append_url_params(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'calendar_data', $aData),
  424. ],
  425. ], $this->_oTemplate);
  426. if (bx_is_api())
  427. return [bx_api_get_block('calendar', ['request_url' => $this->getName() . '/calendar_data&params[]=' . json_encode(['params' => $aData])])];
  428. $this->_oTemplate->addCss(['main.css']);
  429. return $oCalendar->display($sTemplate);
  430. }
  431. public function serviceSessions($iContentId = 0)
  432. {
  433. if(!$iContentId)
  434. $iContentId = (int)bx_get('id');
  435. if(!$iContentId)
  436. return '';
  437. return $this->_oTemplate->getBlockSessions($iContentId);
  438. }
  439. /**
  440. * @page service Service Calls
  441. * @section bx_events Events
  442. * @subsection bx_events-other Other
  443. * @subsubsection bx_events-process_reminders process_reminders
  444. *
  445. * @code bx_srv('bx_events', 'process_reminders'); @endcode
  446. *
  447. * Send remiders to event's participants.
  448. *
  449. * It must be processed once every hour for the proper processing.
  450. * @return nothing
  451. *
  452. * @see BxEventsModule::serviceProcessReminders
  453. */
  454. /**
  455. * @ref bx_events-process_reminders "process_reminders"
  456. */
  457. public function serviceProcessReminders()
  458. {
  459. $CNF = &$this->_oConfig->CNF;
  460. $iNow = time();
  461. $bUseIn = $this->_oConfig->isInternalNotifications();
  462. $sModule = $this->getName();
  463. // get all events for today and tomorrow, since the max reminder is 24 hours
  464. $aEntries = $this->_oDb->getEntriesByDate('@' . time(), '@' . (time() + 3600 * getParam('bx_events_reminder_interval')));
  465. foreach($aEntries as $aEntry) {
  466. $aReminders = $bUseIn ? [$aEntry['reminder']] : [24, 1];
  467. if(!$aReminders)
  468. continue;
  469. $oEventProfile = BxDolProfile::getInstanceByContentAndType($aEntry[$CNF['FIELD_ID']], $sModule);
  470. if(!$oEventProfile)
  471. continue;
  472. $iEventProfileId = $oEventProfile->id();
  473. foreach($aReminders as $iReminder) {
  474. $iTimestamp = $aEntry['start_utc'] - (3600 * $iReminder);
  475. if($iNow <= ($iTimestamp - 3600) || $iNow >= $iTimestamp)
  476. continue;
  477. if($bUseIn)
  478. $this->sendReminders($aEntry);
  479. bx_alert($sModule, 'reminder', $aEntry[$CNF['FIELD_ID']], $iEventProfileId, array(
  480. 'object_author_id' => $iEventProfileId,
  481. 'reminder' => $iReminder
  482. ));
  483. }
  484. }
  485. }
  486. public function serviceGetNotificationsData()
  487. {
  488. $CNF = &$this->_oConfig->CNF;
  489. $aResults = parent::serviceGetNotificationsData();
  490. $sModule = $this->_aModule['name'];
  491. $aSettingsTypes = ['follow_context'];
  492. $aResults['handlers'] = array_merge($aResults['handlers'], [
  493. ['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']],
  494. ]);
  495. $aResults['settings'] = array_merge($aResults['settings'], [
  496. ['group' => 'reminder', 'unit' => $sModule, 'action' => 'reminder', 'types' => $aSettingsTypes],
  497. ]);
  498. $aResults['alerts'] = array_merge($aResults['alerts'], [
  499. ['unit' => $sModule, 'action' => 'reminder'],
  500. ]);
  501. return $aResults;
  502. }
  503. public function serviceGetNotificationsInsertData($oAlert, $aHandler, $aDataItems)
  504. {
  505. if($oAlert->sAction != 'reminder' || empty($aDataItems) || !is_array($aDataItems) || empty($oAlert->aExtras['reminder']))
  506. return $aDataItems;
  507. foreach($aDataItems as $iIndex => $aDataItem) {
  508. $aContent = [];
  509. if(!empty($aDataItem['content']))
  510. $aContent = unserialize($aDataItem['content']);
  511. $aContent['reminder'] = (int)$oAlert->aExtras['reminder'];
  512. $aDataItems[$iIndex]['content'] = serialize($aContent);
  513. }
  514. return $aDataItems;
  515. }
  516. public function serviceGetNotificationsReminder($aEvent)
  517. {
  518. $CNF = &$this->_oConfig->CNF;
  519. if(!isset($aEvent['content']['reminder']))
  520. return [];
  521. $iContentId = (int)$aEvent['object_id'];
  522. $oEventProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  523. if(!$oEventProfile)
  524. return [];
  525. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  526. if(empty($aContentInfo) || !is_array($aContentInfo))
  527. return [];
  528. return [
  529. 'entry_sample' => $CNF['T']['txt_sample_single'],
  530. 'entry_url' => bx_absolute_url(str_replace(BX_DOL_URL_ROOT, '', $oEventProfile->getUrl()), '{bx_url_root}'),
  531. 'entry_caption' => $oEventProfile->getDisplayName(),
  532. 'entry_author' => $oEventProfile->id(),
  533. 'lang_key' => '_bx_events_txt_ntfs_reminder_' . $aEvent['content']['reminder']
  534. ];
  535. }
  536. /**
  537. * @page service Service Calls
  538. * @section bx_events Events
  539. * @subsection bx_events-internal Internal
  540. * @subsubsection bx_events-get_timeline_post get_timeline_post
  541. *
  542. * @code bx_srv('bx_events', 'get_timeline_post', [...]); @endcode
  543. *
  544. * Get Timeline post. It's needed for Timeline module.
  545. *
  546. * @param $aEvent timeline event array from Timeline module
  547. * @return array in special format which is needed specifically for Timeline module to display the data.
  548. *
  549. * @see BxEventsModule::serviceGetTimelinePost
  550. */
  551. /**
  552. * @ref bx_events-get_timeline_post "get_timeline_post"
  553. */
  554. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  555. {
  556. $a = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  557. if($a === false)
  558. return false;
  559. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  560. if(empty($aContentInfo) || !is_array($aContentInfo))
  561. return $a;
  562. $CNF = $this->_oConfig->CNF;
  563. $oDateStart = date_create('@' . $aContentInfo['date_start']);
  564. if ($oDateStart){
  565. $oDateStart->setTimezone(new DateTimeZone($aContentInfo['timezone'] ? $aContentInfo['timezone'] : 'UTC'));
  566. $a['content']['date_start'] = $aContentInfo['date_start'];
  567. }
  568. $oDateEnd = date_create('@' . $aContentInfo['date_end']);
  569. if ($oDateEnd){
  570. $oDateEnd->setTimezone(new DateTimeZone($aContentInfo['timezone'] ? $aContentInfo['timezone'] : 'UTC'));
  571. $a['content']['date_end'] = $aContentInfo['date_end'];
  572. }
  573. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  574. $sLocationString = $oMetatags ? $oMetatags->locationsString($aContentInfo[$CNF['FIELD_ID']], false) : false;
  575. $a['content']['raw'] = $this->_oTemplate->parseHtmlByName('timeline_post.html', [
  576. 'title' => $a['content']['title'],
  577. 'title_attr' => bx_html_attribute($a['content']['title']),
  578. 'url' => $a['content']['url'],
  579. 'bx_if:date' => [
  580. 'condition' => $oDateStart,
  581. 'content' => [
  582. 'date' => $oDateStart ? bx_time_js($aContentInfo['date_start'], BX_FORMAT_DATE_TIME, true) : '',
  583. 'date_c' => $oDateStart->format('c'),
  584. ]],
  585. 'bx_if:location' => array(
  586. 'condition' => !!$sLocationString,
  587. 'content' => array('location' => $sLocationString),
  588. ),
  589. ]);
  590. return $a;
  591. }
  592. public function actionIntervals()
  593. {
  594. $sAction = bx_get('a');
  595. $sMethodName = 'subaction' . ucfirst($sAction);
  596. if (!method_exists($this, $sMethodName)) {
  597. $this->_oTemplate->pageNotFound();
  598. return;
  599. }
  600. $this->$sMethodName();
  601. }
  602. public function subactionRestore($iContentId = 0)
  603. {
  604. if (!$iContentId)
  605. $iContentId = (int)bx_get('c');
  606. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  607. if (
  608. ($aContentInfo && CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedEdit ($aContentInfo))
  609. ||
  610. (!$aContentInfo && CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedAdd ())
  611. ) {
  612. $this->_oTemplate->displayAccessDenied();
  613. exit;
  614. }
  615. $a = $this->_oDb->getIntervals($iContentId);
  616. foreach ($a as $iIntervalId => $r) {
  617. $a[$iIntervalId]['file_id'] = $r['interval_id'];
  618. $a[$iIntervalId]['repeat_stop'] = bx_process_output ($r['repeat_stop'], BX_DATA_DATE_TS);
  619. }
  620. if ('json' == bx_get('f')) {
  621. header('Content-type: text/html; charset=utf-8');
  622. echo json_encode($a);
  623. }
  624. }
  625. public function subactionDelete()
  626. {
  627. header('Content-type: text/html; charset=utf-8');
  628. $iIntervalId = (int)bx_get('id');
  629. if (!($aContentInfo = $this->_oDb->getContentInfoByIntervalId($iIntervalId))) {
  630. echo _t('_sys_request_page_not_found_cpt');
  631. }
  632. elseif (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->checkAllowedEdit ($aContentInfo))) {
  633. echo $sMsg;
  634. }
  635. elseif (!$this->_oDb->deleteIntervalById($iIntervalId)) {
  636. echo _t('_sys_txt_error_occured');
  637. }
  638. else {
  639. echo 'ok';
  640. }
  641. }
  642. public function checkAllowedCheckIn($aDataEntry, $isPerformAction = false)
  643. {
  644. $CNF = &$this->_oConfig->CNF;
  645. $sError = _t('_sys_txt_access_denied');
  646. if(!$this->_iProfileId || !$this->isFan($aDataEntry[$CNF['FIELD_ID']], $this->_iProfileId))
  647. return $sError;
  648. if(!$this->isOngoing($aDataEntry))
  649. return $sError;
  650. if($this->_oDb->isCheckedIn($this->_iProfileId, $aDataEntry[$CNF['FIELD_ID']]))
  651. return $sError;
  652. return CHECK_ACTION_RESULT_ALLOWED;
  653. }
  654. public function checkAllowedIcalExport ($aDataEntry, $isPerformAction = false)
  655. {
  656. return $aDataEntry['date_start'] && $aDataEntry['date_end'] ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_access_denied');
  657. }
  658. /**
  659. * Send reminder to event's paritcipants
  660. * @param $aContentInfo content info
  661. * @return number of sent reminders
  662. */
  663. protected function sendReminders($aContentInfo)
  664. {
  665. $CNF = $this->_oConfig->CNF;
  666. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  667. if (!$oConnection)
  668. return false;
  669. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aContentInfo[$CNF['FIELD_ID']], $this->getName());
  670. if (!$oGroupProfile)
  671. return false;
  672. $aParticipants = $oConnection->getConnectedInitiators($oGroupProfile->id(), true);
  673. if (!$aParticipants)
  674. return true;
  675. $oDateBegin = new DateTime();
  676. $oDateBegin->setTimestamp($aContentInfo['start_utc']);
  677. $oDateBegin->setTimezone(new DateTimeZone($aContentInfo['timezone'] ? $aContentInfo['timezone'] : 'UTC'));
  678. $sEntryBegin = $oDateBegin->format('r');
  679. $sEntryBeginShort = date(getParam($CNF['PARAM_FORMAT_DATE']), $oDateBegin->getTimestamp());
  680. $oDateBegin->setTimezone(new DateTimeZone('UTC'));
  681. $sEntryBeginUTC = $oDateBegin->format('c');
  682. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  683. $sLocationString = $oMetatags->locationsString($aContentInfo[$CNF['FIELD_ID']], false);
  684. $iCounter = 0;
  685. foreach ($aParticipants as $iProfileId) {
  686. $b = sendMailTemplate($CNF['EMAIL_REMINDER'], 0, $iProfileId, array(
  687. 'EntryUrl' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']])),
  688. 'EntryTitle' => $aContentInfo['title'],
  689. 'EntryBegin' => $sEntryBegin,
  690. 'EntryBeginShort' => $sEntryBeginShort,
  691. 'EntryBeginUTC' => $sEntryBeginUTC,
  692. 'EntryLocation' => $sLocationString ? _t('_bx_events_email_reminder_location', $sLocationString) : '',
  693. ), BX_EMAIL_NOTIFY);
  694. $iCounter += $b ? 1 : 0;
  695. }
  696. return $iCounter;
  697. }
  698. public function isOngoing($aDataEntry)
  699. {
  700. $CNF = &$this->_oConfig->CNF;
  701. if(empty($aDataEntry[$CNF['FIELD_DATE_START']]) || empty($aDataEntry[$CNF['FIELD_DATE_END']]))
  702. return false;
  703. $iTime = time();
  704. return $iTime >= $aDataEntry[$CNF['FIELD_DATE_START']] && $iTime <= $aDataEntry[$CNF['FIELD_DATE_END']];
  705. }
  706. }
  707. /** @} */