BxEventsModule.php 16 KB

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