BxEventsSearchResult.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. class BxEventsSearchResult extends BxBaseModGroupsSearchResult
  12. {
  13. function __construct($sMode = '', $aParams = false)
  14. {
  15. parent::__construct($sMode, $aParams);
  16. $this->aCurrent = array(
  17. 'name' => 'bx_events',
  18. 'module_name' => 'bx_events',
  19. 'object_metatags' => 'bx_events',
  20. 'title' => _t('_bx_events_page_title_browse'),
  21. 'table' => 'sys_profiles',
  22. 'tableSearch' => 'bx_events_data',
  23. 'ownFields' => array(),
  24. 'searchFields' => array(),
  25. 'restriction' => array(
  26. 'account_id' => array('value' => '', 'field' => 'account_id', 'operator' => '='),
  27. 'perofileStatus' => array('value' => 'active', 'field' => 'status', 'operator' => '='),
  28. 'perofileType' => array('value' => 'bx_events', 'field' => 'type', 'operator' => '='),
  29. 'owner' => array('value' => '', 'field' => 'author', 'operator' => '=', 'table' => 'bx_events_data'),
  30. 'featured' => array('value' => '', 'field' => 'featured', 'operator' => '<>', 'table' => 'bx_events_data'),
  31. 'past' => array('value' => '', 'field' => 'date_end', 'operator' => '<', 'table' => 'bx_events_data'),
  32. 'date_start_from' => array('value' => '', 'field' => 'date_start', 'operator' => '>=', 'table' => 'bx_events_data'),
  33. 'date_start_to' => array('value' => '', 'field' => 'date_start', 'operator' => '<=', 'table' => 'bx_events_data'),
  34. 'status' => array('value' => 'active', 'field' => 'status', 'operator' => '=', 'table' => 'bx_events_data'),
  35. 'statusAdmin' => array('value' => 'active', 'field' => 'status_admin', 'operator' => '=', 'table' => 'bx_events_data'),
  36. ),
  37. 'join' => array (
  38. 'profile' => array(
  39. 'type' => 'INNER',
  40. 'table' => 'bx_events_data',
  41. 'mainField' => 'content_id',
  42. 'onField' => 'id',
  43. 'joinFields' => array('id', 'event_name', 'event_desc', 'picture', 'cover', 'cover_data', 'added', 'author', 'allow_view_to', 'date_start', 'date_end', 'threshold'),
  44. ),
  45. 'account' => array(
  46. 'type' => 'INNER',
  47. 'table' => 'sys_accounts',
  48. 'mainField' => 'account_id',
  49. 'onField' => 'id',
  50. 'joinFields' => array(),
  51. ),
  52. ),
  53. 'paginate' => array('perPage' => getParam('bx_events_per_page_browse'), 'start' => 0),
  54. 'sorting' => 'last',
  55. 'rss' => array(
  56. 'title' => '',
  57. 'link' => '',
  58. 'image' => '',
  59. 'profile' => 0,
  60. 'fields' => array (
  61. 'Guid' => 'link',
  62. 'Link' => 'link',
  63. 'Title' => 'event_name',
  64. 'DateTimeUTS' => 'added',
  65. 'Desc' => 'event_name',
  66. 'Image' => 'picture',
  67. ),
  68. ),
  69. 'ident' => 'id'
  70. );
  71. $this->sFilterName = 'bx_events_data_filter';
  72. $this->oModule = $this->getMain();
  73. $CNF = &$this->oModule->_oConfig->CNF;
  74. $sSearchFields = getParam($CNF['PARAM_SEARCHABLE_FIELDS']);
  75. $this->aCurrent['searchFields'] = !empty($sSearchFields) ? explode(',', $sSearchFields) : '';
  76. $oJoinedProfile = null;
  77. $bProcessConditionsForPrivateContent = true;
  78. if(isset($this->_aParams['by_location_country'], $this->_aParams['by_location_state'], $this->_aParams['by_location_city'])) {
  79. $aConditions = [];
  80. if(!empty($this->_aParams['by_location_country']))
  81. $aConditions['keyword'] = $this->_aParams['by_location_country'];
  82. if(!empty($this->_aParams['by_location_state']))
  83. $aConditions['state'] = $this->_aParams['by_location_state'];
  84. if(!empty($this->_aParams['by_location_city']))
  85. $aConditions['city'] = $this->_aParams['by_location_city'];
  86. if(!empty($aConditions)) {
  87. $this->setMetaType('location_country_city');
  88. $this->setCustomSearchCondition($aConditions);
  89. }
  90. }
  91. $iDateStart = $iDateEnd = 0;
  92. if(isset($this->_aParams['by_date'])) {
  93. $iTimezoneOffset = 0;
  94. if(!empty($this->_aParams['timezone']) && in_array($this->_aParams['timezone'], timezone_identifiers_list()))
  95. $iTimezoneOffset = date_offset_get(date_create('now', timezone_open($this->_aParams['timezone'])));
  96. switch($this->_aParams['by_date']) {
  97. case 'today':
  98. $iDateStart = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
  99. $iDateEnd = $iDateStart + 86399;
  100. break;
  101. case 'tomorrow':
  102. $iDateStart = mktime(0, 0, 0, date("m"), date("d") + 1, date("Y"));
  103. $iDateEnd = $iDateStart + 86399;
  104. break;
  105. case 'weekend':
  106. $iDayOfWeek = date("w");
  107. $iDateStart = mktime(0, 0, 0, date("m"), date("d") + ($iDayOfWeek > 0 ? 6 - $iDayOfWeek : -1), date("Y"));
  108. $iDateEnd = $iDateStart + 2 * 86400 - 1;
  109. break;
  110. case 'this_week':
  111. $iDayOfWeek = date("w");
  112. $iDateStart = mktime(0, 0, 0, date("m"), date("d") - ($iDayOfWeek > 0 ? $iDayOfWeek - 1 : 6), date("Y"));
  113. $iDateEnd = $iDateStart + 7 * 86400 - 1;
  114. break;
  115. case 'next_week':
  116. $iDayOfWeek = date("w");
  117. $iDateStart = mktime(0, 0, 0, date("m"), date("d") + ($iDayOfWeek > 0 ? 8 - $iDayOfWeek : 1), date("Y"));
  118. $iDateEnd = $iDateStart + 7 * 86400 - 1;
  119. break;
  120. case 'this_month':
  121. $iDateStart = mktime(0, 0, 0, date("m"), 1, date("Y"));
  122. $iDateEnd = mktime(23, 59, 59, date("m") + 1, 0, date("Y"));
  123. break;
  124. case 'date_range':
  125. if(!empty($this->_aParams['date_start'])) {
  126. list($iDsy, $iDsm, $iDsd,) = explode('-', $this->_aParams['date_start']);
  127. $iDateStart = mktime(0, 0, 0, $iDsm, $iDsd, $iDsy);
  128. }
  129. else
  130. $iDateStart = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
  131. if(!empty($this->_aParams['date_end'])) {
  132. list($iDey, $iDem, $iDed,) = explode('-', $this->_aParams['date_end']);
  133. $iDateEnd = mktime(23, 59, 59, $iDem, $iDed, $iDey);
  134. }
  135. else
  136. $iDateEnd = mktime(23, 59, 59, date("m"), date("d"), date("Y"));
  137. break;
  138. }
  139. if($iDateStart != 0 && $iDateEnd != 0) {
  140. $iDateStart -= $iTimezoneOffset;
  141. $iDateEnd -= $iTimezoneOffset;
  142. //--- 1. already started or will start in selected date interval
  143. $this->aCurrent['restriction']['filter_date_start'] = ['value' => $iDateEnd, 'field' => 'date_start', 'operator' => '<', 'table' => 'bx_events_data'];
  144. //--- 2. and didn't finished to the beginning of selected date interval
  145. $this->aCurrent['restriction']['filter_date_end'] = ['value' => $iDateStart, 'field' => 'date_end', 'operator' => '>', 'table' => 'bx_events_data'];
  146. }
  147. }
  148. switch ($sMode) {
  149. case 'created_entries':
  150. $this->_setAuthorConditions($sMode, $aParams, $oJoinedProfile);
  151. break;
  152. case 'context':
  153. $oProfileAuthor = null;
  154. if(!$this->_updateCurrentForContext($sMode, $aParams, $oProfileAuthor))
  155. break;
  156. if(!$this->aCurrent['title'])
  157. $this->aCurrent['title'] = _t('_bx_events_page_title_browse_by_context');
  158. break;
  159. case 'joined_entries':
  160. $oJoinedProfile = null;
  161. if(!$this->_updateCurrentForJoinedEntries($sMode, $aParams, $oJoinedProfile))
  162. break;
  163. $bProcessConditionsForPrivateContent = false;
  164. if(!$this->aCurrent['title'])
  165. $this->aCurrent['title'] = _t('_bx_events_page_title_joined_entries');
  166. break;
  167. case 'followed_entries':
  168. $oJoinedProfile = null;
  169. if(!$this->_updateCurrentForFollowedEntries($sMode, $aParams, $oJoinedProfile))
  170. break;
  171. $bProcessConditionsForPrivateContent = false;
  172. if(!$this->aCurrent['title'])
  173. $this->aCurrent['title'] = _t('_bx_events_page_title_followed_entries');
  174. break;
  175. case 'connections':
  176. if ($this->_setConnectionsConditions($aParams)) {
  177. $bProcessConditionsForPrivateContent = false;
  178. $oProfile = BxDolProfile::getInstance($aParams['profile']);
  179. $oProfile2 = isset($aParams['profile2']) ? BxDolProfile::getInstance($aParams['profile2']) : null;
  180. if (isset($aParams['type']) && $aParams['type'] == 'common' && $oProfile && $oProfile2)
  181. $this->aCurrent['title'] = _t('_bx_events_page_title_browse_connections_mutual', $oProfile->getDisplayName(), $oProfile2->getDisplayName());
  182. elseif ((!isset($aParams['type']) || $aParams['type'] != 'common') && $oProfile)
  183. $this->aCurrent['title'] = _t('_bx_events_page_title_browse_connections', $oProfile->getDisplayName());
  184. $this->aCurrent['rss']['link'] = 'modules/?r=events/rss/' . $sMode . '/' . $aParams['object'] . '/' . $aParams['type'] . '/' . (int)$aParams['profile'] . '/' . (int)$aParams['profile2'] . '/' . (int)$aParams['mutual'];
  185. }
  186. break;
  187. case 'favorite':
  188. $this->_setFavoriteConditions($sMode, $aParams, $oJoinedProfile);
  189. break;
  190. case 'recent':
  191. $this->aCurrent['rss']['link'] = 'modules/?r=events/rss/' . $sMode;
  192. $this->aCurrent['title'] = _t('_bx_events_page_title_browse_recent');
  193. $this->aCurrent['sorting'] = 'last';
  194. $this->sBrowseUrl = 'page.php?i=events-home';
  195. break;
  196. case 'featured':
  197. $this->sBrowseUrl = BxDolPermalinks::getInstance()->permalink($CNF['URL_HOME']);
  198. $this->aCurrent['title'] = _t('_bx_events_page_title_browse_featured');
  199. $this->aCurrent['restriction']['featured']['value'] = '0';
  200. $this->aCurrent['rss']['link'] = 'modules/?r=events/rss/' . $sMode;
  201. $this->aCurrent['sorting'] = 'featured';
  202. break;
  203. case 'recommended':
  204. $this->sBrowseUrl = BxDolPermalinks::getInstance()->permalink($CNF['URL_HOME']);
  205. $this->aCurrent['title'] = _t('_bx_events_page_title_browse_recommended');
  206. $this->aCurrent['restriction']['featured']['value'] = '0';
  207. $this->aCurrent['rss']['link'] = 'modules/?r=events/rss/' . $sMode;
  208. $this->aCurrent['sorting'] = 'recommended';
  209. $this->_setConditionsForRecommended();
  210. break;
  211. case 'top':
  212. $this->aCurrent['rss']['link'] = 'modules/?r=events/rss/' . $sMode;
  213. $this->aCurrent['title'] = _t('_bx_events_page_title_browse_top');
  214. $this->aCurrent['sorting'] = 'top';
  215. $this->sBrowseUrl = 'page.php?i=events-top';
  216. break;
  217. case 'upcoming':
  218. $this->aCurrent['rss']['link'] = 'modules/?r=events/rss/' . $sMode;
  219. $this->aCurrent['title'] = _t('_bx_events_page_title_browse_upcoming');
  220. // $this->aCurrent['restriction_sql'] = BxDolDb::getInstance()->prepareAsString(" AND `date_max` IS NOT NULL AND (`date_max` = 0 OR `date_max` > ?) ", time());
  221. // TODO: remake to use date_max (commented line above) in the future
  222. $this->aCurrent['join']['intervals'] = array (
  223. 'type' => 'LEFT',
  224. 'table' => 'bx_events_intervals',
  225. 'table_alias' => 'ei',
  226. 'mainField' => 'id',
  227. 'mainTable' => 'bx_events_data',
  228. 'onField' => 'event_id',
  229. 'groupTable' => 'bx_events_data',
  230. 'groupField' => 'id',
  231. 'groupHaving' => BxDolDb::getInstance()->prepareAsString(" (`date_end` > ? OR MIN(`repeat_stop`) = 0 OR MAX(`repeat_stop`) > ?) ", time(), time()),
  232. 'joinFields' => ['MIN(`repeat_stop`) AS `repeat_min`, ', 'MAX(`repeat_stop`) AS `repeat_max`, '],
  233. );
  234. if(!empty($aParams['date_start_from']))
  235. $this->aCurrent['restriction']['date_start_from']['value'] = $aParams['date_start_from'];
  236. if(!empty($aParams['date_start_to']))
  237. $this->aCurrent['restriction']['date_start_to']['value'] = $aParams['date_start_to'];
  238. $this->aCurrent['sorting'] = 'upcoming';
  239. if(!empty($aParams['per_page']))
  240. $this->aCurrent['paginate']['perPage'] = is_numeric($aParams['per_page']) ? (int)$aParams['per_page'] : (int)getParam($aParams['per_page']);
  241. $this->sBrowseUrl = 'page.php?i=events-upcoming';
  242. break;
  243. case 'upcoming_connected':
  244. if(!$this->_setConnectionsConditions($aParams))
  245. break;
  246. $bProcessConditionsForPrivateContent = false;
  247. $this->aCurrent['rss']['link'] = 'modules/?r=events/rss/upcoming';
  248. $this->aCurrent['title'] = _t('_bx_events_page_title_browse_upcoming');
  249. // $this->aCurrent['restriction_sql'] = BxDolDb::getInstance()->prepareAsString(" AND `date_max` IS NOT NULL AND (`date_max` = 0 OR `date_max` > ?) ", time());
  250. // TODO: remake to use date_max (commented line above) in the future
  251. $this->aCurrent['join']['intervals'] = array (
  252. 'type' => 'LEFT',
  253. 'table' => 'bx_events_intervals',
  254. 'table_alias' => 'ei',
  255. 'mainField' => 'id',
  256. 'mainTable' => 'bx_events_data',
  257. 'onField' => 'event_id',
  258. 'groupTable' => 'bx_events_data',
  259. 'groupField' => 'id',
  260. 'groupHaving' => BxDolDb::getInstance()->prepareAsString(" (`date_end` > ? OR MIN(`repeat_stop`) = 0 OR MAX(`repeat_stop`) > ?) ", time(), time()),
  261. 'joinFields' => ['MIN(`repeat_stop`) AS `repeat_min`, ', 'MAX(`repeat_stop`) AS `repeat_max`, '],
  262. );
  263. if(!empty($aParams['date_start_from']))
  264. $this->aCurrent['restriction']['date_start_from']['value'] = $aParams['date_start_from'];
  265. if(!empty($aParams['date_start_to']))
  266. $this->aCurrent['restriction']['date_start_to']['value'] = $aParams['date_start_to'];
  267. $this->aCurrent['sorting'] = 'upcoming';
  268. if(!empty($aParams['per_page']))
  269. $this->aCurrent['paginate']['perPage'] = is_numeric($aParams['per_page']) ? (int)$aParams['per_page'] : (int)getParam($aParams['per_page']);
  270. $this->sBrowseUrl = 'page.php?i=events-upcoming';
  271. break;
  272. case 'past':
  273. $this->aCurrent['rss']['link'] = 'modules/?r=events/rss/' . $sMode;
  274. $this->aCurrent['title'] = _t('_bx_events_page_title_browse_past');
  275. $this->aCurrent['restriction']['past']['value'] = time();
  276. $this->aCurrent['sorting'] = 'past';
  277. $this->sBrowseUrl = 'page.php?i=events-past';
  278. break;
  279. case '': // search results
  280. $this->sBrowseUrl = BX_DOL_SEARCH_KEYWORD_PAGE;
  281. $this->aCurrent['title'] = _t('_bx_events');
  282. unset($this->aCurrent['paginate']['perPage'], $this->aCurrent['rss']);
  283. break;
  284. default:
  285. $this->isError = true;
  286. }
  287. if ($bProcessConditionsForPrivateContent) {
  288. $this->addConditionsForPrivateContent($CNF, $oJoinedProfile);
  289. $this->addCustomConditions($CNF, $oJoinedProfile, $sMode, $aParams);
  290. }
  291. $this->sCenterContentUnitSelector = false;
  292. }
  293. function getAlterOrder()
  294. {
  295. switch ($this->aCurrent['sorting']) {
  296. case 'featured':
  297. return array('order' => ' ORDER BY `bx_events_data`.`featured` DESC ');
  298. case 'recommended':
  299. return array('order' => ' ORDER BY RAND() ');
  300. case 'none':
  301. return array();
  302. case 'top':
  303. return array('order' => ' ORDER BY `bx_events_data`.`views` DESC ');
  304. case 'upcoming':
  305. return array('order' => ' ORDER BY `bx_events_data`.`date_start` ASC, `bx_events_data`.`date_end` ASC ');
  306. case 'past':
  307. return array('order' => ' ORDER BY `bx_events_data`.`date_start` DESC, `bx_events_data`.`date_end` DESC ');
  308. case 'last':
  309. default:
  310. return array('order' => ' ORDER BY `bx_events_data`.`added` DESC ');
  311. }
  312. }
  313. function _getPseud ()
  314. {
  315. return array(
  316. 'id' => 'id',
  317. 'event_name' => 'event_name',
  318. 'added' => 'added',
  319. 'author' => 'author',
  320. 'picture' => 'picture',
  321. );
  322. }
  323. function setFieldUnit ($sFieldName, $sTableName, $sOperator = '', $bRenameMode = true)
  324. {
  325. if (0 === mb_strpos($sFieldName, 'MIN') || 0 === mb_strpos($sFieldName, 'MAX'))
  326. return $sFieldName;
  327. return parent::setFieldUnit ($sFieldName, $sTableName, $sOperator, $bRenameMode);
  328. }
  329. }
  330. /** @} */