BxEventsModule.php 32 KB

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