1
0

BxBaseSearchResult.php 16 KB

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