BxBaseSearchResult.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 DolphinCore Dolphin 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 = BX_DB_PADDING_DEF; ///< design box ID to warp result in
  18. protected $aConstants;
  19. protected $sCenterContentUnitSelector = false;
  20. function __construct($oFunctions = false)
  21. {
  22. parent::__construct();
  23. if ($oFunctions) {
  24. $this->oFunctions = $oFunctions;
  25. } else {
  26. bx_import('BxTemplFunctions');
  27. $this->oFunctions = BxTemplFunctions::getInstance();
  28. }
  29. }
  30. function getMain()
  31. {
  32. // override this to return main module class
  33. }
  34. function displayResultBlock ()
  35. {
  36. $sCode = '';
  37. $aData = $this->getSearchData();
  38. if ($this->aCurrent['paginate']['num'] > 0) {
  39. $sCode .= $this->addCustomParts();
  40. foreach ($aData as $aValue)
  41. $sCode .= $this->displaySearchUnit($aValue);
  42. $sSearchResultBlockId = 'bx-search-result-block-' . rand(0, PHP_INT_MAX);
  43. $sCode = '<div id="' . $sSearchResultBlockId . '" class="bx-search-result-block bx-clearfix">' . $sCode . '</div>';
  44. if (!$this->_bLiveSearch && $this->sCenterContentUnitSelector) {
  45. $sCode .= "
  46. <script>
  47. $(document).ready(function() {
  48. bx_center_content('#{$sSearchResultBlockId}', '{$this->sCenterContentUnitSelector}');
  49. });
  50. </script>";
  51. }
  52. }
  53. return $sCode;
  54. }
  55. function displaySearchBox ($sCode, $sPaginate = '')
  56. {
  57. $sTitle = _t($this->aCurrent['title']);
  58. $sCode = $this->oFunctions->designBoxContent($sTitle, $sCode . $sPaginate, $this->iDesignBoxTemplate, $this->getDesignBoxMenu());
  59. if ($this->id)
  60. $sCode = '<div class="bx-page-block-container bx-clearfix" id="bx-page-block-' . $this->id . '">' . $sCode . '</div>';
  61. return $sCode;
  62. }
  63. function displaySearchUnit ($aData)
  64. {
  65. $oMain = $this->getMain();
  66. return $oMain->_oTemplate->unit($aData, $this->bProcessPrivateContent, $this->_bLiveSearch ? $this->sUnitTemplateLiveSearch : $this->sUnitTemplate);
  67. }
  68. function getDesignBoxMenu ()
  69. {
  70. if (false === ($sLink = $this->getRssPageUrl ()))
  71. return false;
  72. return array(
  73. array('name' => 'rss', 'title' => _t('_sys_menu_title_rss'), 'link' => $sLink, 'icon' => 'rss')
  74. );
  75. }
  76. function getRssPageUrl ()
  77. {
  78. if (!isset($this->aCurrent['rss']) || !$this->aCurrent['rss']['link'])
  79. return false;
  80. bx_import('BxDolPermalinks');
  81. $oPermalinks = BxDolPermalinks::getInstance();
  82. return BX_DOL_URL_ROOT . bx_append_url_params($oPermalinks->permalink($this->aCurrent['rss']['link']), 'rss=1');
  83. }
  84. function showAdminActionsPanel($sWrapperId, $aButtons, $sCheckboxName = 'entry', $bSelectAll = true, $bSelectAllChecked = false, $sCustomHtml = '')
  85. {
  86. $aBtns = array();
  87. foreach ($aButtons as $k => $v) {
  88. if(is_array($v)) {
  89. $aBtns[] = $v;
  90. continue;
  91. }
  92. $aBtns[] = array(
  93. 'type' => 'submit',
  94. 'name' => $k,
  95. 'value' => '_' == $v[0] ? _t($v) : $v,
  96. 'onclick' => '',
  97. );
  98. }
  99. $aUnit = array(
  100. 'bx_repeat:buttons' => $aBtns,
  101. 'bx_if:customHTML' => array(
  102. 'condition' => strlen($sCustomHtml) > 0,
  103. 'content' => array(
  104. 'custom_HTML' => $sCustomHtml,
  105. )
  106. ),
  107. 'bx_if:selectAll' => array(
  108. 'condition' => $bSelectAll,
  109. 'content' => array(
  110. 'wrapperId' => $sWrapperId,
  111. 'checkboxName' => $sCheckboxName,
  112. 'checked' => ($bSelectAll && $bSelectAllChecked ? 'checked="checked"' : '')
  113. )
  114. ),
  115. );
  116. return BxDolTemplate::getInstance()->parseHtmlByName('adminActionsPanel.html', $aUnit, array('{','}'));
  117. }
  118. function showAdminFilterPanel($sFilterValue, $sInputId = 'filter_input_id', $sCheckboxId = 'filter_checkbox_id', $sFilterName = 'filter', $sOnApply = '')
  119. {
  120. $sFilter = _t('_sys_admin_filter');
  121. $sApply = _t('_sys_admin_apply');
  122. $sFilterValue = bx_html_attribute($sFilterValue);
  123. $isChecked = $sFilterValue ? ' checked="checked" ' : '';
  124. $sJsContent = "";
  125. if(empty($sOnApply)) {
  126. $sOnApply = 'on_filter_apply(this)';
  127. ob_start();
  128. ?>
  129. <script type="text/javascript">
  130. function on_filter_apply (e) {
  131. var s = ('' + document.location).replace (/[&]{0,1}<?=$sFilterName;?>=.*/, ''); // remove filter
  132. s = s.replace(/page=\d+/, 'page=1'); // goto 1st page
  133. if (e.checked && $('#<?=$sInputId;?>').val().length > 2)
  134. s += (-1 == s.indexOf('?') ? '?' : '&') + '<?=$sFilterName;?>=' + $('#<?=$sInputId;?>').val(); // append filter
  135. document.location = s;
  136. }
  137. function on_filter_key_up (e) {
  138. if (13 == e.keyCode) {
  139. $('#<?php echo $sCheckboxId; ?>').attr('checked', 'checked');
  140. on_filter_apply($('#<?php echo $sCheckboxId; ?>').get(0));
  141. return false;
  142. } else {
  143. $('#<?php echo $sCheckboxId; ?>').attr('checked', '');
  144. return true;
  145. }
  146. }
  147. </script>
  148. <?
  149. $sJsContent = ob_get_clean();
  150. }
  151. return <<<EOF
  152. {$sJsContent}
  153. <div class="admin_filter_panel">
  154. <table>
  155. <tr>
  156. <td>{$sFilter}</td>
  157. <td>
  158. <div class="input_wrapper input_wrapper_text">
  159. <input type="text" id="{$sInputId}" value="{$sFilterValue}" class="form_input_text" onkeypress="return on_filter_key_up(event)" />
  160. <div class="input_close input_close_text">&nbsp;</div>
  161. </div>
  162. </td>
  163. <td><input type="checkbox" id="{$sCheckboxId}" $isChecked onclick="{$sOnApply}" /></td>
  164. <td><label for="{$sCheckboxId}">{$sApply}</label></td>
  165. </tr>
  166. </table>
  167. </div>
  168. EOF;
  169. }
  170. function showPagination($bAdmin = false, $bChangePage = true, $bPageReload = true)
  171. {
  172. $oMain = $this->getMain();
  173. $sPageUrl = $this->getCurrentUrl(array(), false);
  174. $sOnClick = $this->getCurrentOnclick(array(), false);
  175. bx_import('BxTemplPaginate');
  176. $oPaginate = new BxTemplPaginate(array(
  177. 'page_url' => $sPageUrl,
  178. 'on_change_page' => $sOnClick,
  179. 'num' => $this->aCurrent['paginate']['num'],
  180. 'per_page' => $this->aCurrent['paginate']['perPage'],
  181. 'start' => $this->aCurrent['paginate']['start'],
  182. ));
  183. return $sOnClick ? $oPaginate->getSimplePaginate() : $oPaginate->getPaginate();
  184. }
  185. /**
  186. * Get current browse URL with current page and additional params
  187. * @param $aAdditionalParams set custom additional params as key value pair
  188. * @param $bReplacePagesParams replace paginate params with current values or leave markers for use in paginate class
  189. * @return ready to use URL string with BX_DOL_URL_ROOT added in the beginning
  190. */
  191. protected function getCurrentUrl($aAdditionalParams = array(), $bReplacePagesParams = true)
  192. {
  193. if (BX_DOL_SEARCH_KEYWORD_PAGE === $this->sBrowseUrl || $this->bForceAjaxPaginate)
  194. return 'javascript:void(0);';
  195. bx_import('BxDolPermalinks');
  196. $oPermalinks = BxDolPermalinks::getInstance();
  197. $sUrlStart = BX_DOL_URL_ROOT . $oPermalinks->permalink($this->sBrowseUrl);
  198. return $this->addAdditionalUrlParams($sUrlStart, $bReplacePagesParams);
  199. }
  200. /**
  201. * Get current browse URL with current page and additional params
  202. * @param $aAdditionalParams set custom additional params as key value pair
  203. * @param $bReplacePagesParams replace paginate params with current values or leave markers for use in paginate class
  204. * @return ready to use URL string with BX_DOL_URL_ROOT added in the beginning
  205. */
  206. protected function getCurrentOnclick($aAdditionalParams = array(), $bReplacePagesParams = true)
  207. {
  208. if (BX_DOL_SEARCH_KEYWORD_PAGE !== $this->sBrowseUrl && !$this->bForceAjaxPaginate)
  209. return '';
  210. if (BX_DOL_SEARCH_KEYWORD_PAGE === $this->sBrowseUrl) {
  211. $sLoadDynamicUrl = BX_DOL_URL_ROOT . 'searchKeywordContent.php?searchMode=ajax&section[]=' . $this->aCurrent['name'];
  212. $sLoadDynamicUrl = $this->addAdditionalUrlParams($sLoadDynamicUrl, $bReplacePagesParams);
  213. $sKeyword = bx_get('keyword');
  214. if ($sKeyword !== false && mb_strlen($sKeyword) > 0)
  215. $sLoadDynamicUrl = bx_append_url_params($sLoadDynamicUrl, 'keyword=' . rawurlencode($sKeyword));
  216. return "return !loadDynamicBlockAuto(this, '{$sLoadDynamicUrl}');";
  217. } else {
  218. return "return !loadDynamicBlockAutoPaginate(this, '{start}', '{per_page}');";
  219. }
  220. }
  221. protected function addAdditionalUrlParams($sUrl, $bReplacePagesParams)
  222. {
  223. // add pages params
  224. $sUrl = bx_append_url_params($sUrl, array (
  225. 'start' => $bReplacePagesParams ? (int)$this->aCurrent['paginate']['start'] : '{start}',
  226. 'per_page' => $bReplacePagesParams ? (int)$this->aCurrent['paginate']['perPage'] : '{per_page}',
  227. ));
  228. foreach ($this->aGetParams as $sGetParam) {
  229. $sValue = false;
  230. if (isset($aAdditionalParams[$sGetParam]))
  231. $sValue = $aAdditionalParams[$sGetParam];
  232. elseif (false !== bx_get($sGetParam))
  233. $sValue = bx_get($sGetParam);
  234. if (false !== $sValue)
  235. $sUrl = bx_append_url_params($sUrl, $sGetParam . '=' . rawurlencode($sValue));
  236. }
  237. return $sUrl;
  238. }
  239. function clearFilters ($aPassParams = array(), $aPassJoins = array())
  240. {
  241. //clear sorting
  242. $this->aCurrent['sorting'] = 'last';
  243. //clear restrictions
  244. foreach ($this->aCurrent['restriction'] as $sKey => $aValue) {
  245. if (!in_array($sKey, $aPassParams))
  246. $this->aCurrent['restriction'][$sKey]['value'] = '';
  247. }
  248. //clear unnecessary joins (remains only profile join)
  249. $aPassJoins[] = 'profile';
  250. $aTemp = array();
  251. foreach ($aPassJoins as $sValue) {
  252. if (isset($this->aCurrent['join'][$sValue]) && is_array($this->aCurrent['join'][$sValue]))
  253. $aTemp[$sValue] = $this->aCurrent['join'][$sValue];
  254. }
  255. $this->aCurrent['join'] = $aTemp;
  256. }
  257. function fillFilters ($aParams)
  258. {
  259. // transform all given values to fields values
  260. if (is_array($aParams)) {
  261. foreach ($aParams as $sKey => $mixedValue) {
  262. if (isset($this->aCurrent['restriction'][$sKey]))
  263. $this->aCurrent['restriction'][$sKey]['value'] = $mixedValue;
  264. }
  265. }
  266. }
  267. /**
  268. * Set design box template id to use to wrap search results in
  269. */
  270. function setDesignBoxTemplateId ($i)
  271. {
  272. $this->iDesignBoxTemplate = $i;
  273. }
  274. }
  275. /** @} */