1
0

BxEventsModule.php 35 KB

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