BxBaseSearchResult.php 12 KB

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