BxBaseSearchResult.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. bx_import('BxDolSearch');
  10. class BxBaseSearchResult extends BxDolSearchResult
  11. {
  12. public $isError;
  13. protected $sBrowseUrl; ///< currect browse url, used for paginate and other links in browsing
  14. protected $sUnitTemplate = 'unit.html'; ///< common template to try to use for displaying one item
  15. protected $sUnitTemplateLiveSearch = 'unit_live_search.html'; ///< common template to try to use for displaying live search results
  16. protected $aGetParams = array(); ///< get params to keep in paginate and other browsing links
  17. protected $iDesignBoxTemplate = 11; ///< design box ID to wrap result in, or false to disable design box wrapping
  18. protected $aConstants;
  19. protected $sCenterContentUnitSelector = false;
  20. protected $aContainerAttrs = array();
  21. protected $aContainerClasses = array('bx-search-result-block', 'bx-clearfix');
  22. protected $aUnitParams = array(); ///< additional params array to pass to unit display function
  23. function __construct($oFunctions = false)
  24. {
  25. parent::__construct();
  26. if ($oFunctions)
  27. $this->oFunctions = $oFunctions;
  28. else
  29. $this->oFunctions = BxTemplFunctions::getInstance();
  30. }
  31. function getMain()
  32. {
  33. // override this to return main module class
  34. }
  35. function displayResultBlock ()
  36. {
  37. $sCode = '';
  38. bx_alert('simple_search', 'before_get_data', 0, false, array('object' => &$this->aCurrent, 'mode' => $this->_sMode));
  39. $aData = $this->getSearchData();
  40. bx_alert('simple_search', 'get_data', 0, false, array('object' => &$this->aCurrent, 'mode' => $this->_sMode, 'search_results' => &$aData));
  41. if ($this->aCurrent['paginate']['num'] > 0) {
  42. $sCode .= $this->addCustomParts();
  43. foreach ($aData as $aValue)
  44. $sCode .= $this->displaySearchUnit($aValue);
  45. $sSearchResultBlockId = 'bx-search-result-block-' . rand(0, PHP_INT_MAX);
  46. $sClasses = implode(' ', $this->aContainerClasses);
  47. $sAttributes = '';
  48. if(is_array($this->aContainerAttrs) && !empty($this->aContainerAttrs))
  49. foreach ($this->aContainerAttrs as $sName => $sValue)
  50. $sAttributes .= ' ' . $sName . '="' . $sValue . '"';
  51. $sCode = '<div id="' . $sSearchResultBlockId . '" class="' . $sClasses . '"' . $sAttributes . '>' . $sCode . '</div>';
  52. if (!$this->_bLiveSearch && $this->sCenterContentUnitSelector) {
  53. $sCode .= "
  54. <script>
  55. $(document).ready(function() {
  56. bx_center_content('#{$sSearchResultBlockId}', '{$this->sCenterContentUnitSelector}', true);
  57. });
  58. </script>";
  59. }
  60. }
  61. bx_alert('simple_search', 'show_data', 0, false, array('object' => &$this->aCurrent, 'mode' => $this->_sMode, 'search_results' => &$sCode));
  62. return $sCode;
  63. }
  64. function displaySearchBox ($sContent, $sPaginate = '')
  65. {
  66. $sContent .= $sPaginate;
  67. $sMenu = $this->getDesignBoxMenu();
  68. if ($this->id) {
  69. $sTitle = _t($this->aCurrent['title']);
  70. $sCode = $this->oFunctions->designBoxContent($sTitle, $sContent, $this->iDesignBoxTemplate, $sMenu);
  71. return '<div class="bx-page-block-container bx-search-results bx-def-padding-sec-topbottom bx-clearfix" id="bx-page-block-' . $this->id . '">' . $sCode . '</div>';
  72. }
  73. $this->addPageRssLink ();
  74. return array(
  75. 'content' => $sContent,
  76. 'menu' => $sMenu,
  77. );
  78. }
  79. function displaySearchUnit ($aData)
  80. {
  81. $oMain = $this->getMain();
  82. return $oMain->_oTemplate->unit($aData, $this->bProcessPrivateContent, $this->_bLiveSearch ? $this->sUnitTemplateLiveSearch : $this->sUnitTemplate, $this->aUnitParams);
  83. }
  84. function setUnitTemplate ($s)
  85. {
  86. $this->sUnitTemplate = $s;
  87. }
  88. function getDesignBoxMenu ()
  89. {
  90. return array();
  91. }
  92. protected function addPageRssLink ()
  93. {
  94. if (false === ($sLink = $this->getRssPageUrl ()))
  95. return;
  96. if (!($oTemplate = BxDolTemplate::getInstance()))
  97. return;
  98. $oTemplate->addPageRssLink($this->aCurrent['title'], $sLink);
  99. }
  100. function getRssPageUrl ()
  101. {
  102. if (!isset($this->aCurrent['rss']) || !$this->aCurrent['rss']['link'])
  103. return false;
  104. $oPermalinks = BxDolPermalinks::getInstance();
  105. return BX_DOL_URL_ROOT . bx_append_url_params($oPermalinks->permalink($this->aCurrent['rss']['link']), 'rss=1');
  106. }
  107. function showAdminActionsPanel($sWrapperId, $aButtons, $sCheckboxName = 'entry', $bSelectAll = true, $bSelectAllChecked = false, $sCustomHtml = '')
  108. {
  109. $aBtns = array();
  110. foreach ($aButtons as $k => $v) {
  111. if(is_array($v)) {
  112. $aBtns[] = $v;
  113. continue;
  114. }
  115. $aBtns[] = array(
  116. 'type' => 'submit',
  117. 'name' => $k,
  118. 'value' => '_' == $v[0] ? _t($v) : $v,
  119. 'onclick' => '',
  120. );
  121. }
  122. return BxDolTemplate::getInstance()->parseHtmlByName('adminActionsPanel.html', array(
  123. 'bx_repeat:buttons' => $aBtns,
  124. 'bx_if:custom_html' => array(
  125. 'condition' => strlen($sCustomHtml) > 0,
  126. 'content' => array(
  127. 'custom_html' => $sCustomHtml,
  128. )
  129. ),
  130. 'bx_if:selectAll' => array(
  131. 'condition' => $bSelectAll,
  132. 'content' => array(
  133. 'wrapper_id' => $sWrapperId,
  134. 'checkbox_name' => $sCheckboxName,
  135. 'checked' => ($bSelectAll && $bSelectAllChecked ? 'checked="checked"' : '')
  136. )
  137. ),
  138. ));
  139. }
  140. function showAdminFilterPanel($sFilterValue, $sInputId = 'filter_input_id', $sCheckboxId = 'filter_checkbox_id', $sFilterName = 'filter', $sOnApply = '')
  141. {
  142. $sFilterValue = bx_html_attribute($sFilterValue);
  143. $isChecked = $sFilterValue ? ' checked="checked" ' : '';
  144. if(empty($sOnApply))
  145. $sOnApply = "on_filter_apply(this, '" . $sInputId . "', '" . $sFilterName . "')";
  146. return BxDolTemplate::getInstance()->parseHtmlByName('adminFilterPanel.html', array(
  147. 'input_id' => $sInputId,
  148. 'filter_value' => $sFilterValue,
  149. 'checkbox_id' => $sCheckboxId,
  150. 'is_checked' => $isChecked,
  151. 'on_apply' => $sOnApply,
  152. ));
  153. }
  154. function showPagination($bAdmin = false, $bChangePage = true, $bPageReload = true)
  155. {
  156. $oMain = $this->getMain();
  157. $sPageUrl = $this->getCurrentUrl(array(), false);
  158. $sOnClick = $this->getCurrentOnclick(array(), false);
  159. $oPaginate = new BxTemplPaginate(array(
  160. 'page_url' => $sPageUrl,
  161. 'on_change_page' => $sOnClick,
  162. 'num' => $this->aCurrent['paginate']['num'],
  163. 'per_page' => $this->aCurrent['paginate']['perPage'],
  164. 'start' => $this->aCurrent['paginate']['start'],
  165. ));
  166. return $sOnClick ? $oPaginate->getSimplePaginate() : $oPaginate->getPaginate();
  167. }
  168. /**
  169. * Get current browse URL with current page and additional params
  170. * @param $aAdditionalParams set custom additional params as key value pair
  171. * @param $bReplacePagesParams replace paginate params with current values or leave markers for use in paginate class
  172. * @return ready to use URL string with BX_DOL_URL_ROOT added in the beginning
  173. */
  174. protected function getCurrentUrl($aAdditionalParams = array(), $bReplacePagesParams = true)
  175. {
  176. if (BX_DOL_SEARCH_KEYWORD_PAGE === $this->sBrowseUrl || $this->bForceAjaxPaginate)
  177. return 'javascript:void(0);';
  178. $oPermalinks = BxDolPermalinks::getInstance();
  179. $sUrlStart = BX_DOL_URL_ROOT . $oPermalinks->permalink($this->sBrowseUrl);
  180. return $this->addAdditionalUrlParams($sUrlStart, $aAdditionalParams, $bReplacePagesParams);
  181. }
  182. /**
  183. * Get current browse URL with current page and additional params
  184. * @param $aAdditionalParams set custom additional params as key value pair
  185. * @param $bReplacePagesParams replace paginate params with current values or leave markers for use in paginate class
  186. * @return ready to use URL string with BX_DOL_URL_ROOT added in the beginning
  187. */
  188. protected function getCurrentOnclick($aAdditionalParams = array(), $bReplacePagesParams = true)
  189. {
  190. if (BX_DOL_SEARCH_KEYWORD_PAGE !== $this->sBrowseUrl && !$this->bForceAjaxPaginate)
  191. return '';
  192. if (BX_DOL_SEARCH_KEYWORD_PAGE === $this->sBrowseUrl) {
  193. $sLoadDynamicUrl = BX_DOL_URL_ROOT . 'searchKeywordContent.php?searchMode=ajax&section[]=' . $this->aCurrent['name'] . ($this->_sCategoryObject ? '&cat=' . $this->_sCategoryObject : '');
  194. $sLoadDynamicUrl = $this->addAdditionalUrlParams($sLoadDynamicUrl, $aAdditionalParams, $bReplacePagesParams);
  195. $sKeyword = bx_get('keyword');
  196. if ($sKeyword !== false && mb_strlen($sKeyword) > 0)
  197. $sLoadDynamicUrl = bx_append_url_params($sLoadDynamicUrl, 'keyword=' . rawurlencode($sKeyword));
  198. return "return !loadDynamicBlockAuto(this, '{$sLoadDynamicUrl}');";
  199. } else {
  200. return "return !loadDynamicBlockAutoPaginate(this, '{start}', '{per_page}', " . (empty($aAdditionalParams) ? 'undefined' : "'" . trim($this->addAdditionalUrlParams('', $aAdditionalParams, false, false), '&?') . "'") . ");";
  201. }
  202. }
  203. protected function addAdditionalUrlParams($sUrl, $aAdditionalParams, $bReplacePagesParams, $bAddPaginateParams = true)
  204. {
  205. if ($bAddPaginateParams) { // add pages params
  206. $sUrl = bx_append_url_params($sUrl, array (
  207. 'type' => $this->_sMetaType,
  208. 'start' => $bReplacePagesParams ? (int)$this->aCurrent['paginate']['start'] : '{start}',
  209. 'per_page' => $bReplacePagesParams ? (int)$this->aCurrent['paginate']['perPage'] : '{per_page}',
  210. ));
  211. }
  212. foreach ($this->aGetParams as $sGetParam) {
  213. $sValue = false;
  214. if (isset($aAdditionalParams[$sGetParam]))
  215. $sValue = $aAdditionalParams[$sGetParam];
  216. elseif (false !== bx_get($sGetParam))
  217. $sValue = bx_get($sGetParam);
  218. if (false !== $sValue)
  219. $sUrl = bx_append_url_params($sUrl, $sGetParam . '=' . rawurlencode($sValue));
  220. }
  221. return $sUrl;
  222. }
  223. function clearFilters ($aPassParams = array(), $aPassJoins = array())
  224. {
  225. //clear sorting
  226. $this->aCurrent['sorting'] = 'last';
  227. //clear restrictions
  228. foreach ($this->aCurrent['restriction'] as $sKey => $aValue) {
  229. if (!in_array($sKey, $aPassParams))
  230. $this->aCurrent['restriction'][$sKey]['value'] = '';
  231. }
  232. //clear unnecessary joins (remains only profile join)
  233. $aPassJoins[] = 'profile';
  234. $aTemp = array();
  235. foreach ($aPassJoins as $sValue) {
  236. if (isset($this->aCurrent['join'][$sValue]) && is_array($this->aCurrent['join'][$sValue]))
  237. $aTemp[$sValue] = $this->aCurrent['join'][$sValue];
  238. }
  239. $this->aCurrent['join'] = $aTemp;
  240. }
  241. function fillFilters ($aParams)
  242. {
  243. // transform all given values to fields values
  244. if (is_array($aParams)) {
  245. foreach ($aParams as $sKey => $mixedValue) {
  246. if (isset($this->aCurrent['restriction'][$sKey]))
  247. $this->aCurrent['restriction'][$sKey]['value'] = $mixedValue;
  248. }
  249. }
  250. }
  251. function fillFiltersByObjects ($aParams)
  252. {
  253. // transform all given values to fields values
  254. if (is_array($aParams)) {
  255. foreach ($aParams as $sKey => $mixedValue) {
  256. $this->aCurrent['restriction'][$sKey] = $mixedValue;
  257. }
  258. }
  259. }
  260. /**
  261. * Set design box template id to use to wrap search results in
  262. */
  263. function setDesignBoxTemplateId ($i)
  264. {
  265. $this->iDesignBoxTemplate = $i;
  266. }
  267. /**
  268. * Set unit class selector for content centering
  269. */
  270. function setCenterContentUnitSelector ($s)
  271. {
  272. $this->sCenterContentUnitSelector = $s;
  273. }
  274. /**
  275. * Add class to search result container
  276. * @param $mixed CSS class name string or array of classes
  277. */
  278. function addContainerClass ($mixed)
  279. {
  280. if (!is_array($mixed))
  281. $mixed = array($mixed);
  282. foreach ($mixed as $s)
  283. if (false === array_search($s, $this->aContainerClasses))
  284. $this->aContainerClasses[] = $s;
  285. }
  286. /**
  287. * Remove class from search result container
  288. * @param $mixed CSS class name string or array of classes
  289. */
  290. function removeContainerClass ($mixed)
  291. {
  292. if (!is_array($mixed))
  293. $mixed = array($mixed);
  294. foreach ($mixed as $s)
  295. if (false !== ($i = array_search($s, $this->aContainerClasses)))
  296. unset($this->aContainerClasses[$i]);
  297. }
  298. /**
  299. * Add attribute(s) to search result container
  300. * @param $aAttributes an array of Key -> Value pair(s).
  301. */
  302. function addContainerAttribute ($aAttributes)
  303. {
  304. if(empty($aAttributes) || !is_array($aAttributes))
  305. return;
  306. $this->aContainerAttrs = array_merge($this->aContainerAttrs, $aAttributes);
  307. }
  308. /**
  309. * Remove attribute(s) from search result container
  310. * @param $mixedName attribute name or an array of names
  311. */
  312. function removeContainerAttribute ($mixedName)
  313. {
  314. if(!is_array($mixedName))
  315. $mixedName = array($mixedName);
  316. $this->aContainerAttrs = array_diff_assoc($this->aContainerAttrs, array_flip($mixedName));
  317. }
  318. public function setUnitParams ($aParamsAdd = array(), $aParamsRemove = array())
  319. {
  320. if ($aParamsAdd)
  321. $this->aUnitParams = array_merge($this->aUnitParams, $aParamsAdd);
  322. if ($aParamsRemove)
  323. $this->aUnitParams = array_diff_key($this->aUnitParams, $aParamsRemove);
  324. }
  325. }
  326. /** @} */