BxEventsModule.php 32 KB

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