BxBaseSearchResult.php 14 KB

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