BxBaseSearchResult.php 16 KB

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