BxEventsModule.php 33 KB

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