BxEventsModule.php 33 KB

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