BxBaseSearchResult.php 15 KB

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