BxBaseSearchResult.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  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 $aContainerClasses = array('bx-search-result-block', 'bx-def-margin-bottom', 'bx-clearfix');
  21. function __construct($oFunctions = false)
  22. {
  23. parent::__construct();
  24. if ($oFunctions) {
  25. $this->oFunctions = $oFunctions;
  26. } else {
  27. bx_import('BxTemplFunctions');
  28. $this->oFunctions = BxTemplFunctions::getInstance();
  29. }
  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. if ($this->aCurrent['paginate']['num'] > 0) {
  40. $sCode .= $this->addCustomParts();
  41. foreach ($aData as $aValue)
  42. $sCode .= $this->displaySearchUnit($aValue);
  43. $sSearchResultBlockId = 'bx-search-result-block-' . rand(0, PHP_INT_MAX);
  44. $sClasses = implode(' ', $this->aContainerClasses);
  45. $sCode = '<div id="' . $sSearchResultBlockId . '" class="' . $sClasses . '">' . $sCode . '</div>';
  46. if (!$this->_bLiveSearch && $this->sCenterContentUnitSelector) {
  47. $sCode .= "
  48. <script>
  49. $(document).ready(function() {
  50. bx_center_content('#{$sSearchResultBlockId}', '{$this->sCenterContentUnitSelector}', true);
  51. });
  52. </script>";
  53. }
  54. }
  55. return $sCode;
  56. }
  57. function displaySearchBox ($sContent, $sPaginate = '')
  58. {
  59. $sContent .= $sPaginate;
  60. $sMenu = $this->getDesignBoxMenu();
  61. if ($this->id) {
  62. $sTitle = _t($this->aCurrent['title']);
  63. $sCode = $this->oFunctions->designBoxContent($sTitle, $sContent, $this->iDesignBoxTemplate, $sMenu);
  64. return '<div class="bx-page-block-container bx-search-results bx-def-padding-topbottom bx-clearfix" id="bx-page-block-' . $this->id . '">' . $sCode . '</div>';
  65. }
  66. $this->addPageRssLink ();
  67. return array(
  68. 'content' => $sContent,
  69. 'menu' => $sMenu,
  70. );
  71. }
  72. function displaySearchUnit ($aData)
  73. {
  74. $oMain = $this->getMain();
  75. return $oMain->_oTemplate->unit($aData, $this->bProcessPrivateContent, $this->_bLiveSearch ? $this->sUnitTemplateLiveSearch : $this->sUnitTemplate);
  76. }
  77. function getDesignBoxMenu ()
  78. {
  79. return array();
  80. }
  81. protected function addPageRssLink ()
  82. {
  83. if (false === ($sLink = $this->getRssPageUrl ()))
  84. return;
  85. bx_import('BxDolTemplate');
  86. if (!($oTemplate = BxDolTemplate::getInstance()))
  87. return;
  88. $oTemplate->addPageRssLink($this->aCurrent['title'], $sLink);
  89. }
  90. function getRssPageUrl ()
  91. {
  92. if (!isset($this->aCurrent['rss']) || !$this->aCurrent['rss']['link'])
  93. return false;
  94. bx_import('BxDolPermalinks');
  95. $oPermalinks = BxDolPermalinks::getInstance();
  96. return BX_DOL_URL_ROOT . bx_append_url_params($oPermalinks->permalink($this->aCurrent['rss']['link']), 'rss=1');
  97. }
  98. function showAdminActionsPanel($sWrapperId, $aButtons, $sCheckboxName = 'entry', $bSelectAll = true, $bSelectAllChecked = false, $sCustomHtml = '')
  99. {
  100. $aBtns = array();
  101. foreach ($aButtons as $k => $v) {
  102. if(is_array($v)) {
  103. $aBtns[] = $v;
  104. continue;
  105. }
  106. $aBtns[] = array(
  107. 'type' => 'submit',
  108. 'name' => $k,
  109. 'value' => '_' == $v[0] ? _t($v) : $v,
  110. 'onclick' => '',
  111. );
  112. }
  113. return BxDolTemplate::getInstance()->parseHtmlByName('adminActionsPanel.html', array(
  114. 'bx_repeat:buttons' => $aBtns,
  115. 'bx_if:custom_html' => array(
  116. 'condition' => strlen($sCustomHtml) > 0,
  117. 'content' => array(
  118. 'custom_html' => $sCustomHtml,
  119. )
  120. ),
  121. 'bx_if:selectAll' => array(
  122. 'condition' => $bSelectAll,
  123. 'content' => array(
  124. 'wrapper_id' => $sWrapperId,
  125. 'checkbox_name' => $sCheckboxName,
  126. 'checked' => ($bSelectAll && $bSelectAllChecked ? 'checked="checked"' : '')
  127. )
  128. ),
  129. ));
  130. }
  131. function showAdminFilterPanel($sFilterValue, $sInputId = 'filter_input_id', $sCheckboxId = 'filter_checkbox_id', $sFilterName = 'filter', $sOnApply = '')
  132. {
  133. $sFilterValue = bx_html_attribute($sFilterValue);
  134. $isChecked = $sFilterValue ? ' checked="checked" ' : '';
  135. if(empty($sOnApply))
  136. $sOnApply = "on_filter_apply(this, '" . $sInputId . "', '" . $sFilterName . "')";
  137. return BxDolTemplate::getInstance()->parseHtmlByName('adminFilterPanel.html', array(
  138. 'input_id' => $sInputId,
  139. 'filter_value' => $sFilterValue,
  140. 'checkbox_id' => $sCheckboxId,
  141. 'is_checked' => $isChecked,
  142. 'on_apply' => $sOnApply,
  143. ));
  144. }
  145. function showPagination($bAdmin = false, $bChangePage = true, $bPageReload = true)
  146. {
  147. $oMain = $this->getMain();
  148. $sPageUrl = $this->getCurrentUrl(array(), false);
  149. $sOnClick = $this->getCurrentOnclick(array(), false);
  150. bx_import('BxTemplPaginate');
  151. $oPaginate = new BxTemplPaginate(array(
  152. 'page_url' => $sPageUrl,
  153. 'on_change_page' => $sOnClick,
  154. 'num' => $this->aCurrent['paginate']['num'],
  155. 'per_page' => $this->aCurrent['paginate']['perPage'],
  156. 'start' => $this->aCurrent['paginate']['start'],
  157. ));
  158. return $sOnClick ? $oPaginate->getSimplePaginate() : $oPaginate->getPaginate();
  159. }
  160. /**
  161. * Get current browse URL with current page and additional params
  162. * @param $aAdditionalParams set custom additional params as key value pair
  163. * @param $bReplacePagesParams replace paginate params with current values or leave markers for use in paginate class
  164. * @return ready to use URL string with BX_DOL_URL_ROOT added in the beginning
  165. */
  166. protected function getCurrentUrl($aAdditionalParams = array(), $bReplacePagesParams = true)
  167. {
  168. if (BX_DOL_SEARCH_KEYWORD_PAGE === $this->sBrowseUrl || $this->bForceAjaxPaginate)
  169. return 'javascript:void(0);';
  170. bx_import('BxDolPermalinks');
  171. $oPermalinks = BxDolPermalinks::getInstance();
  172. $sUrlStart = BX_DOL_URL_ROOT . $oPermalinks->permalink($this->sBrowseUrl);
  173. return $this->addAdditionalUrlParams($sUrlStart, $aAdditionalParams, $bReplacePagesParams);
  174. }
  175. /**
  176. * Get current browse URL with current page and additional params
  177. * @param $aAdditionalParams set custom additional params as key value pair
  178. * @param $bReplacePagesParams replace paginate params with current values or leave markers for use in paginate class
  179. * @return ready to use URL string with BX_DOL_URL_ROOT added in the beginning
  180. */
  181. protected function getCurrentOnclick($aAdditionalParams = array(), $bReplacePagesParams = true)
  182. {
  183. if (BX_DOL_SEARCH_KEYWORD_PAGE !== $this->sBrowseUrl && !$this->bForceAjaxPaginate)
  184. return '';
  185. if (BX_DOL_SEARCH_KEYWORD_PAGE === $this->sBrowseUrl) {
  186. $sLoadDynamicUrl = BX_DOL_URL_ROOT . 'searchKeywordContent.php?searchMode=ajax&section[]=' . $this->aCurrent['name'];
  187. $sLoadDynamicUrl = $this->addAdditionalUrlParams($sLoadDynamicUrl, $aAdditionalParams, $bReplacePagesParams);
  188. $sKeyword = bx_get('keyword');
  189. if ($sKeyword !== false && mb_strlen($sKeyword) > 0)
  190. $sLoadDynamicUrl = bx_append_url_params($sLoadDynamicUrl, 'keyword=' . rawurlencode($sKeyword));
  191. return "return !loadDynamicBlockAuto(this, '{$sLoadDynamicUrl}');";
  192. } else {
  193. return "return !loadDynamicBlockAutoPaginate(this, '{start}', '{per_page}', " . (empty($aAdditionalParams) ? 'undefined' : "'" . trim($this->addAdditionalUrlParams('', $aAdditionalParams, false, false), '&?') . "'") . ");";
  194. }
  195. }
  196. protected function addAdditionalUrlParams($sUrl, $aAdditionalParams, $bReplacePagesParams, $bAddPaginateParams = true)
  197. {
  198. if ($bAddPaginateParams) { // add pages params
  199. $sUrl = bx_append_url_params($sUrl, array (
  200. 'start' => $bReplacePagesParams ? (int)$this->aCurrent['paginate']['start'] : '{start}',
  201. 'per_page' => $bReplacePagesParams ? (int)$this->aCurrent['paginate']['perPage'] : '{per_page}',
  202. ));
  203. }
  204. foreach ($this->aGetParams as $sGetParam) {
  205. $sValue = false;
  206. if (isset($aAdditionalParams[$sGetParam]))
  207. $sValue = $aAdditionalParams[$sGetParam];
  208. elseif (false !== bx_get($sGetParam))
  209. $sValue = bx_get($sGetParam);
  210. if (false !== $sValue)
  211. $sUrl = bx_append_url_params($sUrl, $sGetParam . '=' . rawurlencode($sValue));
  212. }
  213. return $sUrl;
  214. }
  215. function clearFilters ($aPassParams = array(), $aPassJoins = array())
  216. {
  217. //clear sorting
  218. $this->aCurrent['sorting'] = 'last';
  219. //clear restrictions
  220. foreach ($this->aCurrent['restriction'] as $sKey => $aValue) {
  221. if (!in_array($sKey, $aPassParams))
  222. $this->aCurrent['restriction'][$sKey]['value'] = '';
  223. }
  224. //clear unnecessary joins (remains only profile join)
  225. $aPassJoins[] = 'profile';
  226. $aTemp = array();
  227. foreach ($aPassJoins as $sValue) {
  228. if (isset($this->aCurrent['join'][$sValue]) && is_array($this->aCurrent['join'][$sValue]))
  229. $aTemp[$sValue] = $this->aCurrent['join'][$sValue];
  230. }
  231. $this->aCurrent['join'] = $aTemp;
  232. }
  233. function fillFilters ($aParams)
  234. {
  235. // transform all given values to fields values
  236. if (is_array($aParams)) {
  237. foreach ($aParams as $sKey => $mixedValue) {
  238. if (isset($this->aCurrent['restriction'][$sKey]))
  239. $this->aCurrent['restriction'][$sKey]['value'] = $mixedValue;
  240. }
  241. }
  242. }
  243. /**
  244. * Set design box template id to use to wrap search results in
  245. */
  246. function setDesignBoxTemplateId ($i)
  247. {
  248. $this->iDesignBoxTemplate = $i;
  249. }
  250. /**
  251. * Add class to search result container
  252. * @param $mixed CSS class name string or array of classes
  253. */
  254. function addContainerClass ($mixed)
  255. {
  256. if (!is_array($mixed))
  257. $mixed = array($mixed);
  258. foreach ($mixed as $s)
  259. if (false === array_search($s, $this->aContainerClasses))
  260. $this->aContainerClasses[] = $s;
  261. }
  262. /**
  263. * Remove class from search result container
  264. * @param $mixed CSS class name string or array of classes
  265. */
  266. function removeContainerClass ($mixed)
  267. {
  268. if (!is_array($mixed))
  269. $mixed = array($mixed);
  270. foreach ($mixed as $s)
  271. if (false !== ($i = array_search($s, $this->aContainerClasses)))
  272. unset($this->aContainerClasses[$i]);
  273. }
  274. }
  275. /** @} */