BxBaseSearchResult.php 14 KB

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