BxEventsModule.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. /**
  12. * Events profiles module.
  13. */
  14. class BxEventsModule extends BxBaseModGroupsModule implements iBxDolCalendarService
  15. {
  16. function __construct(&$aModule)
  17. {
  18. parent::__construct($aModule);
  19. $CNF = &$this->_oConfig->CNF;
  20. $this->_aSearchableNamesExcept = array_merge($this->_aSearchableNamesExcept, array(
  21. $CNF['FIELD_TIMEZONE'],
  22. $CNF['FIELD_JOIN_CONFIRMATION'],
  23. $CNF['FIELD_REMINDER']
  24. ));
  25. }
  26. public function actionCalendarData()
  27. {
  28. // check permissions
  29. $aSQLPart = array();
  30. $iContentId = (int)bx_get('event');
  31. $iContextId = (int)bx_get('context_id');
  32. if ($iContentId) {
  33. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  34. if (CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedView($aContentInfo)) {
  35. $this->_oTemplate->displayAccessDenied();
  36. exit;
  37. }
  38. $aSQLPart = array();
  39. }
  40. else {
  41. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  42. if($iContextId){
  43. if (!$this->serviceIsEnableForContext($iContextId)){
  44. exit;
  45. }
  46. else{
  47. $aSQLPart = $oPrivacy ? $oPrivacy->getContentByGroupAsSQLPart(- $iContextId) : array();
  48. }
  49. }
  50. else{
  51. $aSQLPart = $oPrivacy ? $oPrivacy->getContentPublicAsSQLPart(0, $oPrivacy->getPartiallyVisiblePrivacyGroups()) : array();
  52. }
  53. }
  54. // get entries
  55. $aEntries = $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), bx_get('event'), $aSQLPart);
  56. bx_alert($this->getName(), 'calendar_data', 0, false, array(
  57. 'event' => $iContentId,
  58. 'context_id' => $iContextId,
  59. 'start' => bx_get('start'),
  60. 'end' => bx_get('end'),
  61. 'sql_part' => &$aSQLPart,
  62. 'data' => &$aEntries,
  63. ));
  64. header('Content-Type: application/json; charset=utf-8');
  65. echo json_encode($aEntries);
  66. }
  67. public function serviceGetCalendarEntries($iProfileId)
  68. {
  69. $CNF = &$this->_oConfig->CNF;
  70. $oConn = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  71. $aData = $oConn->getConnectedContent($iProfileId);
  72. $aData2 = array(0);
  73. foreach($aData as $iProfileId2){
  74. $oProfile = BxDolProfile::getInstance($iProfileId2);
  75. array_push($aData2, $oProfile->getContentId());
  76. }
  77. $aSQLPart['where'] = " AND `bx_events_data`.`id` IN(" . implode(',', $aData2) . ")";
  78. return $this->_oDb->getEntriesByDate(bx_get('start'), bx_get('end'), null, $aSQLPart);
  79. }
  80. public function serviceGetSafeServices()
  81. {
  82. $a = parent::serviceGetSafeServices();
  83. return array_merge($a, array (
  84. 'BrowsePastProfiles' => '',
  85. 'Calendar' => '',
  86. ));
  87. }
  88. /**
  89. * @page service Service Calls
  90. * @section bx_events Events
  91. * @subsection bx_events-browse Browse
  92. * @subsubsection bx_events-browse_upcoming_profiles browse_upcoming_profiles
  93. *
  94. * @code bx_srv('bx_events', 'browse_upcoming_profiles', [...]); @endcode
  95. *
  96. * Browse upcoming events
  97. *
  98. * @param $bDisplayEmptyMsg show "empty" message or not if nothing found
  99. * @param $bAjaxPaginate use AJAX paginate or not
  100. *
  101. * @see BxEventsModule::serviceBrowseUpcomingProfiles
  102. */
  103. /**
  104. * @ref bx_events-browse_upcoming_profiles "browse_upcoming_profiles"
  105. */
  106. public function serviceBrowseUpcomingProfiles ($aParams = false)
  107. {
  108. $bDisplayEmptyMsg = false;
  109. if(isset($aParams['empty_message'])) {
  110. $bDisplayEmptyMsg = (bool)$aParams['empty_message'];
  111. unset($aParams['empty_message']);
  112. }
  113. $bAjaxPaginate = true;
  114. if(isset($aParams['ajax_paginate'])) {
  115. $bAjaxPaginate = (bool)$aParams['ajax_paginate'];
  116. unset($aParams['ajax_paginate']);
  117. }
  118. return $this->_serviceBrowse ('upcoming', $aParams, BX_DB_PADDING_DEF, $bDisplayEmptyMsg, $bAjaxPaginate);
  119. }
  120. /**
  121. * @page service Service Calls
  122. * @section bx_events Events
  123. * @subsection bx_events-browse Browse
  124. * @subsubsection bx_events-browse_upcoming_connected_profiles browse_upcoming_connected_profiles
  125. *
  126. * @code bx_srv('bx_events', 'browse_upcoming_connected_profiles', [...]); @endcode
  127. *
  128. * Browse upcoming connected (followed) events
  129. *
  130. * @param $bDisplayEmptyMsg show "empty" message or not if nothing found
  131. * @param $bAjaxPaginate use AJAX paginate or not
  132. *
  133. * @see BxEventsModule::serviceBrowseUpcomingConnectedProfiles
  134. */
  135. /**
  136. * @ref bx_events-browse_upcoming_connected_profiles "browse_upcoming_connected_profiles"
  137. */
  138. public function serviceBrowseUpcomingConnectedProfiles ($aParams = false)
  139. {
  140. if(!is_array($aParams) || empty($aParams['object']) || empty($aParams['profile']))
  141. return '';
  142. $bDisplayEmptyMsg = false;
  143. if(isset($aParams['empty_message'])) {
  144. $bDisplayEmptyMsg = (bool)$aParams['empty_message'];
  145. unset($aParams['empty_message']);
  146. }
  147. $bAjaxPaginate = true;
  148. if(isset($aParams['ajax_paginate'])) {
  149. $bAjaxPaginate = (bool)$aParams['ajax_paginate'];
  150. unset($aParams['ajax_paginate']);
  151. }
  152. $aDefaults = array(
  153. 'type' => 'content',
  154. 'mutual' => false,
  155. 'profile2' => 0
  156. );
  157. return $this->_serviceBrowse ('upcoming_connected', array_merge($aDefaults, $aParams), BX_DB_PADDING_DEF, $bDisplayEmptyMsg, $bAjaxPaginate);
  158. }
  159. /**
  160. * @page service Service Calls
  161. * @section bx_events Events
  162. * @subsection bx_events-browse Browse
  163. * @subsubsection bx_events-browse_past_profiles browse_past_profiles
  164. *
  165. * @code bx_srv('bx_events', 'browse_past_profiles', [...]); @endcode
  166. *
  167. * Browse past events
  168. *
  169. * @param $bDisplayEmptyMsg show "empty" message or not if nothing found
  170. * @param $bAjaxPaginate use AJAX paginate or not
  171. *
  172. * @see BxEventsModule::serviceBrowsePastProfiles
  173. */
  174. /**
  175. * @ref bx_events-browse_past_profiles "browse_past_profiles"
  176. */
  177. public function serviceBrowsePastProfiles ($bDisplayEmptyMsg = false, $bAjaxPaginate = true)
  178. {
  179. return $this->_serviceBrowse ('past', false, BX_DB_PADDING_DEF, $bDisplayEmptyMsg, $bAjaxPaginate);
  180. }
  181. /**
  182. * @page service Service Calls
  183. * @section bx_events Events
  184. * @subsection bx_events-browse Browse
  185. * @subsubsection bx_events-browse_past_profiles browse_past_profiles_by_params
  186. *
  187. * @code bx_srv('bx_events', 'browse_past_profiles_by_params', [...]); @endcode
  188. *
  189. * Browse past events
  190. *
  191. * @param $aParams array with all parameters
  192. *
  193. * @see BxEventsModule::serviceBrowsePastProfilesByParams
  194. */
  195. /**
  196. * @ref bx_events-browse_past_profiles_by_params "browse_past_profiles_by_params"
  197. */
  198. public function serviceBrowsePastProfilesByParams ($aParams)
  199. {
  200. $bDisplayEmptyMsg = false;
  201. if(isset($aParams['empty_message'])) {
  202. $bDisplayEmptyMsg = (bool)$aParams['empty_message'];
  203. unset($aParams['empty_message']);
  204. }
  205. $bAjaxPaginate = true;
  206. if(isset($aParams['ajax_paginate'])) {
  207. $bAjaxPaginate = (bool)$aParams['ajax_paginate'];
  208. unset($aParams['ajax_paginate']);
  209. }
  210. return $this->_serviceBrowse ('past', $aParams, BX_DB_PADDING_DEF, $bDisplayEmptyMsg, $bAjaxPaginate);
  211. }
  212. /**
  213. * @page service Service Calls
  214. * @section bx_events Events
  215. * @subsection bx_events-page_blocks Page Blocks
  216. * @subsubsection bx_events-calendar calendar
  217. *
  218. * @code bx_srv('bx_events', 'calendar', [...]); @endcode
  219. *
  220. * Shows event or events calendar
  221. *
  222. * @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.
  223. * @param $sTemplate template to use to show calendar, or leave empty for default template, possible options: calendar.html, calendar_compact.html
  224. * @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.
  225. *
  226. * @see BxEventsModule::serviceCalendar
  227. */
  228. /**
  229. * @ref bx_events-calendar "calendar"
  230. */
  231. public function serviceCalendar($aData = array(), $sTemplate = 'calendar.html')
  232. {
  233. if (isset($aData['event'])) {
  234. $aContentInfo = $this->_oDb->getContentInfoById ((int)$aData['event']);
  235. if ('' == $aContentInfo['repeat_stop']) // don't display calendar for non repeating events
  236. return '';
  237. }
  238. if (isset($aData['context_id'])) {
  239. if (!$this->serviceIsEnableForContext($aData['context_id'])){
  240. return '';
  241. }
  242. }
  243. $oCalendar = new BxTemplCalendar(array(
  244. 'eventSources' => array (
  245. bx_append_url_params(BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'calendar_data', $aData),
  246. ),
  247. ), $this->_oTemplate);
  248. $this->_oTemplate->addCss(array('main.css'));
  249. return $oCalendar->display($sTemplate);
  250. }
  251. /**
  252. * @page service Service Calls
  253. * @section bx_events Events
  254. * @subsection bx_events-other Other
  255. * @subsubsection bx_events-process_reminders process_reminders
  256. *
  257. * @code bx_srv('bx_events', 'process_reminders'); @endcode
  258. *
  259. * Send remiders to event's participants.
  260. *
  261. * It must be processed once every hour for the proper processing.
  262. * @return nothing
  263. *
  264. * @see BxEventsModule::serviceProcessReminders
  265. */
  266. /**
  267. * @ref bx_events-process_reminders "process_reminders"
  268. */
  269. public function serviceProcessReminders()
  270. {
  271. $iNow = time();
  272. // get all events for today and tomorrow, since the max reminder is 24 hours
  273. $aEntries = $this->_oDb->getEntriesByDate('@' . time(), '@' . (time() + 86400));
  274. foreach ($aEntries as $a) {
  275. if (!$a['reminder'])
  276. continue;
  277. $iTimestamp = $a['start_utc'] - (3600 * $a['reminder']);
  278. if ($iNow > ($iTimestamp - 3600) && $iNow < $iTimestamp)
  279. $this->sendReminders($a);
  280. }
  281. }
  282. /**
  283. * @page service Service Calls
  284. * @section bx_events Events
  285. * @subsection bx_events-internal Internal
  286. * @subsubsection bx_events-get_timeline_post get_timeline_post
  287. *
  288. * @code bx_srv('bx_events', 'get_timeline_post', [...]); @endcode
  289. *
  290. * Get Timeline post. It's needed for Timeline module.
  291. *
  292. * @param $aEvent timeline event array from Timeline module
  293. * @return array in special format which is needed specifically for Timeline module to display the data.
  294. *
  295. * @see BxEventsModule::serviceGetTimelinePost
  296. */
  297. /**
  298. * @ref bx_events-get_timeline_post "get_timeline_post"
  299. */
  300. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  301. {
  302. $a = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  303. if($a === false)
  304. return false;
  305. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  306. if(empty($aContentInfo) || !is_array($aContentInfo))
  307. return $a;
  308. $CNF = $this->_oConfig->CNF;
  309. $oDateStart = date_create('@' . $aContentInfo['date_start']);
  310. $oDateStart->setTimezone(new DateTimeZone($aContentInfo['timezone'] ? $aContentInfo['timezone'] : 'UTC'));
  311. $oDateEnd = date_create('@' . ($aContentInfo['date_start'] > $aContentInfo['repeat_stop'] ? $aContentInfo['date_start'] : $aContentInfo['repeat_stop']));
  312. $oDateEnd->setTimezone(new DateTimeZone($aContentInfo['timezone'] ? $aContentInfo['timezone'] : 'UTC'));
  313. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  314. $sLocationString = $oMetatags ? $oMetatags->locationsString($aContentInfo[$CNF['FIELD_ID']], false) : false;
  315. $a['content']['raw'] = $this->_oTemplate->parseHtmlByName('timeline_post.html', array(
  316. 'title' => $a['content']['title'],
  317. 'title_attr' => bx_html_attribute($a['content']['title']),
  318. 'url' => $a['content']['url'],
  319. 'date' => strftime(getParam('bx_events_short_date_format'), $oDateStart->getTimestamp()) . ($oDateStart->format('ymd') == $oDateEnd->format('ymd') ? '' : ' - ' . strftime(getParam('bx_events_short_date_format'), $oDateEnd->getTimestamp())),
  320. 'date_c' => $oDateStart->format('c'),
  321. 'bx_if:location' => array(
  322. 'condition' => !!$sLocationString,
  323. 'content' => array('location' => $sLocationString),
  324. ),
  325. ));
  326. $a['content']['title'] = '';
  327. $a['content']['text'] = '';
  328. return $a;
  329. }
  330. public function actionIntervals()
  331. {
  332. $sAction = bx_get('a');
  333. $sMethodName = 'subaction' . ucfirst($sAction);
  334. if (!method_exists($this, $sMethodName)) {
  335. $this->_oTemplate->pageNotFound();
  336. return;
  337. }
  338. $this->$sMethodName();
  339. }
  340. public function subactionRestore($iContentId = 0)
  341. {
  342. if (!$iContentId)
  343. $iContentId = (int)bx_get('c');
  344. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  345. if (
  346. ($aContentInfo && CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedEdit ($aContentInfo))
  347. ||
  348. (!$aContentInfo && CHECK_ACTION_RESULT_ALLOWED !== $this->checkAllowedAdd ())
  349. ) {
  350. $this->_oTemplate->displayAccessDenied();
  351. exit;
  352. }
  353. $a = $this->_oDb->getIntervals($iContentId);
  354. foreach ($a as $iIntervalId => $r) {
  355. $a[$iIntervalId]['file_id'] = $r['interval_id'];
  356. $a[$iIntervalId]['repeat_stop'] = bx_process_output ($r['repeat_stop'], BX_DATA_DATE_TS);
  357. }
  358. if ('json' == bx_get('f')) {
  359. header('Content-type: text/html; charset=utf-8');
  360. echo json_encode($a);
  361. }
  362. }
  363. public function subactionDelete()
  364. {
  365. header('Content-type: text/html; charset=utf-8');
  366. $iIntervalId = (int)bx_get('id');
  367. if (!($aContentInfo = $this->_oDb->getContentInfoByIntervalId($iIntervalId))) {
  368. echo _t('_sys_request_page_not_found_cpt');
  369. }
  370. elseif (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->checkAllowedEdit ($aContentInfo))) {
  371. echo $sMsg;
  372. }
  373. elseif (!$this->_oDb->deleteIntervalById($iIntervalId)) {
  374. echo _t('_sys_txt_error_occured');
  375. }
  376. else {
  377. echo 'ok';
  378. }
  379. }
  380. /**
  381. * Send reminder to event's paritcipants
  382. * @param $aContentInfo content info
  383. * @return number of sent reminders
  384. */
  385. protected function sendReminders($aContentInfo)
  386. {
  387. $CNF = $this->_oConfig->CNF;
  388. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  389. if (!$oConnection)
  390. return false;
  391. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aContentInfo[$CNF['FIELD_ID']], $this->getName());
  392. if (!$oGroupProfile)
  393. return false;
  394. $aParticipants = $oConnection->getConnectedInitiators($oGroupProfile->id(), true);
  395. if (!$aParticipants)
  396. return true;
  397. $oDateBegin = new DateTime();
  398. $oDateBegin->setTimestamp($aContentInfo['start_utc']);
  399. $oDateBegin->setTimezone(new DateTimeZone($aContentInfo['timezone'] ? $aContentInfo['timezone'] : 'UTC'));
  400. $sEntryBegin = $oDateBegin->format('r');
  401. $sEntryBeginShort = strftime(getParam('bx_events_short_date_format'), $oDateBegin->getTimestamp());
  402. $oDateBegin->setTimezone(new DateTimeZone('UTC'));
  403. $sEntryBeginUTC = $oDateBegin->format('c');
  404. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  405. $sLocationString = $oMetatags->locationsString($aContentInfo[$CNF['FIELD_ID']], false);
  406. $iCounter = 0;
  407. foreach ($aParticipants as $iProfileId) {
  408. $b = sendMailTemplate($CNF['EMAIL_REMINDER'], 0, $iProfileId, array(
  409. 'EntryUrl' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]),
  410. 'EntryTitle' => $aContentInfo['title'],
  411. 'EntryBegin' => $sEntryBegin,
  412. 'EntryBeginShort' => $sEntryBeginShort,
  413. 'EntryBeginUTC' => $sEntryBeginUTC,
  414. 'EntryLocation' => $sLocationString ? _t('_bx_events_email_reminder_location', $sLocationString) : '',
  415. ), BX_EMAIL_NOTIFY);
  416. $iCounter += $b ? 1 : 0;
  417. }
  418. return $iCounter;
  419. }
  420. }
  421. /** @} */