BxBaseSearchResult.php 13 KB

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