BxEventsModule.php 33 KB

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