1
0

BxBaseSearchResult.php 17 KB

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