BxBaseSearchResult.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. // TODO: clean it more carefully !
  10. bx_import('BxDolSearch');
  11. class BxBaseSearchResult extends BxDolSearchResult {
  12. protected $iDesignBoxTemplate = 11;
  13. protected $aPermalinks;
  14. protected $aConstants;
  15. function __construct($oFunctions = false) {
  16. parent::__construct();
  17. if ($oFunctions) {
  18. $this->oFunctions = $oFunctions;
  19. } else {
  20. bx_import('BxTemplFunctions');
  21. $this->oFunctions = BxTemplFunctions::getInstance();
  22. }
  23. }
  24. function isPermalinkEnabled() {
  25. return isset($this->_isPermalinkEnabled) ? $this->_isPermalinkEnabled : ($this->_isPermalinkEnabled = (getParam($this->aPermalinks['param']) == 'on'));
  26. }
  27. function getCurrentUrl ($sType, $iId, $sUri, $aOwner = '') {
  28. $sLink = $this->aConstants['linksTempl'][$sType];
  29. $sLink = str_replace('{id}', $iId, $sLink);
  30. $sLink = str_replace('{uri}', $sUri, $sLink);
  31. if (is_array($aOwner) && !empty($aOwner)) {
  32. $sLink = str_replace('{ownerName}', $aOwner['ownerName'], $sLink); // TODO: urlencode
  33. $sLink = str_replace('{ownerId}', $aOwner['ownerId'], $sLink);
  34. }
  35. return BX_DOL_URL_ROOT . $sLink;
  36. }
  37. function displayResultBlock () {
  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. }
  45. $sCode = '<div class="bx-search-result-block">' . $sCode . '<div class="bx-clear"></div></div>';
  46. }
  47. return $sCode;
  48. }
  49. function displaySearchBox ($sCode, $sPaginate = '') {
  50. $aMenu = false;
  51. if (isset($this->aCurrent['rss']) && $this->aCurrent['rss']['link'])
  52. $aMenu = array(array('name' => 'rss', 'title' => _t('_sys_menu_title_rss'), 'link' => $this->aCurrent['rss']['link'] . (false === strpos($this->aCurrent['rss']['link'], '?') ? '?' : '&') . 'rss=1', 'icon' => 'rss'));
  53. $sTitle = _t($this->aCurrent['title']);
  54. $sCode = $this->oFunctions->designBoxContent($sTitle, $sCode . $sPaginate, $this->iDesignBoxTemplate, $aMenu);
  55. if (!isset($_GET['searchMode']))
  56. $sCode = '<div id="page_block_'.$this->id.'">'.$sCode.'<div class="clear_both"></div></div>';
  57. return $sCode;
  58. }
  59. function _transformData ($aUnit, $sTempl, $sCssHeader = '') {
  60. foreach ($aUnit as $sKey => $sValue)
  61. $sTempl = str_replace('{'.$sKey.'}', $sValue, $sTempl);
  62. $sCssHeader = strlen($sCssHeader) > 0 ? $sCssHeader : 'text_Unit';
  63. $sTempl = str_replace('{unitClass}', $sCssHeader, $sTempl);
  64. return $sTempl;
  65. }
  66. function showAdminActionsPanel($sWrapperId, $aButtons, $sCheckboxName = 'entry', $bSelectAll = true, $bSelectAllChecked = false, $sCustomHtml = '') {
  67. $aBtns = array();
  68. foreach ($aButtons as $k => $v) {
  69. if(is_array($v)) {
  70. $aBtns[] = $v;
  71. continue;
  72. }
  73. $aBtns[] = array(
  74. 'type' => 'submit',
  75. 'name' => $k,
  76. 'value' => '_' == $v[0] ? _t($v) : $v,
  77. 'onclick' => '',
  78. );
  79. }
  80. $aUnit = array(
  81. 'bx_repeat:buttons' => $aBtns,
  82. 'bx_if:customHTML' => array(
  83. 'condition' => strlen($sCustomHtml) > 0,
  84. 'content' => array(
  85. 'custom_HTML' => $sCustomHtml,
  86. )
  87. ),
  88. 'bx_if:selectAll' => array(
  89. 'condition' => $bSelectAll,
  90. 'content' => array(
  91. 'wrapperId' => $sWrapperId,
  92. 'checkboxName' => $sCheckboxName,
  93. 'checked' => ($bSelectAll && $bSelectAllChecked ? 'checked="checked"' : '')
  94. )
  95. ),
  96. );
  97. return BxDolTemplate::getInstance()->parseHtmlByName('adminActionsPanel.html', $aUnit, array('{','}'));
  98. }
  99. function showAdminFilterPanel($sFilterValue, $sInputId = 'filter_input_id', $sCheckboxId = 'filter_checkbox_id', $sFilterName = 'filter', $sOnApply = '') {
  100. $sFilter = _t('_sys_admin_filter');
  101. $sApply = _t('_sys_admin_apply');
  102. $sFilterValue = bx_html_attribute($sFilterValue);
  103. $isChecked = $sFilterValue ? ' checked="checked" ' : '';
  104. $sJsContent = "";
  105. if(empty($sOnApply)) {
  106. $sOnApply = 'on_filter_apply(this)';
  107. ob_start();
  108. ?>
  109. <script type="text/javascript">
  110. function on_filter_apply (e) {
  111. var s = ('' + document.location).replace (/[&]{0,1}<?=$sFilterName;?>=.*/, ''); // remove filter
  112. s = s.replace(/page=\d+/, 'page=1'); // goto 1st page
  113. if (e.checked && $('#<?=$sInputId;?>').val().length > 2)
  114. s += (-1 == s.indexOf('?') ? '?' : '&') + '<?=$sFilterName;?>=' + $('#<?=$sInputId;?>').val(); // append filter
  115. document.location = s;
  116. }
  117. function on_filter_key_up (e) {
  118. if (13 == e.keyCode) {
  119. $('#<?php echo $sCheckboxId; ?>').attr('checked', 'checked');
  120. on_filter_apply($('#<?php echo $sCheckboxId; ?>').get(0));
  121. return false;
  122. } else {
  123. $('#<?php echo $sCheckboxId; ?>').attr('checked', '');
  124. return true;
  125. }
  126. }
  127. </script>
  128. <?
  129. $sJsContent = ob_get_clean();
  130. }
  131. return <<<EOF
  132. {$sJsContent}
  133. <div class="admin_filter_panel">
  134. <table>
  135. <tr>
  136. <td>{$sFilter}</td>
  137. <td>
  138. <div class="input_wrapper input_wrapper_text">
  139. <input type="text" id="{$sInputId}" value="{$sFilterValue}" class="form_input_text" onkeypress="return on_filter_key_up(event)" />
  140. <div class="input_close input_close_text">&nbsp;</div>
  141. </div>
  142. </td>
  143. <td><input type="checkbox" id="{$sCheckboxId}" $isChecked onclick="{$sOnApply}" /></td>
  144. <td><label for="{$sCheckboxId}">{$sApply}</label></td>
  145. </tr>
  146. </table>
  147. </div>
  148. EOF;
  149. }
  150. function showPagination($bAdmin = false, $bChangePage = true, $bPageReload = true) {
  151. $sPageLink = $this->getCurrentUrl('browseAll', 0, '');
  152. $aLinkAddon = $this->getLinkAddByPrams();
  153. if ($aLinkAddon) {
  154. foreach($aLinkAddon as $sValue)
  155. $sPageLink .= $sValue;
  156. }
  157. if(!$this->id)
  158. $this->id = 0;
  159. $sLoadDynamicUrl = $this->id .', \'searchKeywordContent.php?searchMode=ajax&section[]=' . $this->aCurrent['name'] . $aLinkAddon['params'];
  160. $sKeyword = bx_get('keyword');
  161. if ($sKeyword !== false && mb_strlen($sKeyword) > 0)
  162. $sLoadDynamicUrl .= '&keyword=' . rawurlencode($sKeyword);
  163. bx_import('BxDolPaginate');
  164. $oPaginate = new BxDolPaginate(
  165. array(
  166. 'page_url' => $sPageLink,
  167. 'num' => $this->aCurrent['paginate']['num'],
  168. 'per_page' => $this->aCurrent['paginate']['perPage'],
  169. 'start' => $this->aCurrent['paginate']['start'],
  170. ),
  171. $bAdmin ? BxDolTemplateAdmin::getInstance() : BxDolTemplate::getInstance()
  172. );
  173. return '<div class="clear_both"></div>' . $oPaginate->getPaginate();
  174. }
  175. function getLinkAddByPrams ($aExclude = array()) {
  176. $aExclude[] = '_r';
  177. $aExclude[] = 'pageBlock';
  178. $aExclude[] = 'searchMode';
  179. $aExclude[] = 'section';
  180. $aExclude[] = 'keyword';
  181. $aLinks = array();
  182. $aCurrParams = array();
  183. $aParams = array();
  184. foreach ($this->aCurrent['restriction'] as $sKey => $aValue) {
  185. if (isset($aValue['paramName'])) {
  186. if (is_array($aValue['value']))
  187. $aCurrParams[$aValue['paramName']] = $aValue['value'];
  188. elseif (mb_strlen($aValue['value']) > 0)
  189. $aCurrParams[$aValue['paramName']] = $aValue['value'];
  190. }
  191. }
  192. // add get params
  193. $aExclude = array_merge($aExclude, array('page', 'per_page'));
  194. $aParams = array_merge($_GET, $aCurrParams);
  195. $aLinks['params'] = bx_encode_url_params ($_GET, $aExclude);
  196. //paginate
  197. $aLinks['paginate'] = '&start={start}';
  198. $aLinks['paginate'] .= '&per_page={per_page}';
  199. return $aLinks;
  200. }
  201. function clearFilters ($aPassParams = array(), $aPassJoins = array()) {
  202. //clear sorting
  203. $this->aCurrent['sorting'] = 'last';
  204. //clear restrictions
  205. foreach ($this->aCurrent['restriction'] as $sKey => $aValue) {
  206. if (!in_array($sKey, $aPassParams))
  207. $this->aCurrent['restriction'][$sKey]['value'] = '';
  208. }
  209. //clear unnecessary joins (remains only profile join)
  210. $aPassJoins[] = 'profile';
  211. $aTemp = array();
  212. foreach ($aPassJoins as $sValue) {
  213. if (isset($this->aCurrent['join'][$sValue]) && is_array($this->aCurrent['join'][$sValue]))
  214. $aTemp[$sValue] = $this->aCurrent['join'][$sValue];
  215. }
  216. $this->aCurrent['join'] = $aTemp;
  217. }
  218. function fillFilters ($aParams) {
  219. // transform all given values to fields values
  220. if (is_array($aParams)) {
  221. foreach ($aParams as $sKey => $mixedValue) {
  222. if (isset($this->aCurrent['restriction'][$sKey]))
  223. $this->aCurrent['restriction'][$sKey]['value'] = $mixedValue;
  224. }
  225. }
  226. }
  227. function getTopMenu ($aExclude = array()) {
  228. }
  229. function getBottomMenu ($sAllLinkType = 'browseAll', $iId = 0, $sUri = '', $aExclude = array()) {
  230. if (strpos($sAllLinkType, 'http') === false) {
  231. if (isset($this->aConstants['linksTempl'][$sAllLinkType]))
  232. $sAllUrl = $this->getCurrentUrl($sAllLinkType, $iId, $sUri);
  233. else
  234. $sAllUrl = $this->getCurrentUrl('browseAll', 0, '');
  235. }
  236. else
  237. $sAllUrl = $sAllLinkType;
  238. $sModeName = $this->aCurrent['name'] . '_mode';
  239. $sMode = isset($_GET[$sModeName]) ? '&' . $sModeName . '=' . rawurlencode($_GET[$sModeName]) : $sModeName . '=' . $this->aCurrent['sorting'];
  240. $aLinkAddon = $this->getLinkAddByPrams($aExclude);
  241. $sLink = bx_html_attribute($_SERVER['PHP_SELF']);
  242. bx_import('BxDolPaginate');
  243. $oPaginate = new BxDolPaginate(array(
  244. 'page_url' => $this->getCurrentUrl($sAllUrl, 0, ''),
  245. 'num' => $this->aCurrent['paginate']['num'],
  246. 'per_page' => $this->aCurrent['paginate']['perPage'],
  247. 'start' => $this->aCurrent['paginate']['start'],
  248. ));
  249. return $oPaginate->getSimplePaginate($sAllUrl);
  250. }
  251. function getBrowseBlock ($aParams, $aCustom = array(), $sMainUrl = '', $bClearJoins = true) {
  252. $aJoins = $bClearJoins ? array('albumsObjects', 'albums') : array_keys($this->aCurrent['join']);
  253. $this->clearFilters(array('activeStatus', 'albumType', 'album_status', 'ownerStatus'), $aJoins);
  254. $this->addCustomParts();
  255. $aCustomTmpl = array(
  256. 'enable_center' => true,
  257. 'unit_css_class' => ' > div:not(.clear_both)',
  258. 'start' => 0,
  259. 'per_page' => 10,
  260. 'sorting' => 'last',
  261. 'simple_paginate' => true,
  262. 'dynamic_paginate' => true,
  263. 'menu_top' => false,
  264. 'menu_bottom' => true,
  265. 'menu_bottom_type' => 'browseAll',
  266. 'menu_bottom_param'=> ''
  267. );
  268. $aCustom = array_merge($aCustomTmpl, $aCustom);
  269. $this->aCurrent['paginate']['perPage'] = (int)$aCustom['per_page'];
  270. $this->aCurrent['paginate']['start'] = (int)$aCustom['start'] > 0 ? $aCustom['start'] : 0;
  271. $this->aCurrent['sorting'] = $aCustom['sorting'];
  272. foreach ($aParams as $sKey => $mixedValues) {
  273. if (isset($this->aCurrent['restriction'][$sKey]))
  274. $this->aCurrent['restriction'][$sKey]['value'] = $mixedValues;
  275. }
  276. $aList = $this->getSearchData();
  277. $bWrap = true;
  278. if ($this->aCurrent['paginate']['num'] > 0) {
  279. $bWrap = false;
  280. foreach ($aList as $aData)
  281. $sCode .= $this->displaySearchUnit($aData);
  282. if ($aCustom['enable_center'])
  283. $sCode = $GLOBALS['oFunctions']->centerContent($sCode, $aCustom['unit_css_class']);
  284. if (mb_strlen($aCustom['wrapper_class']) > 0)
  285. $sCode = '<div class="' . $aCustom['wrapper_class'] . '">' . $sCode . '</div>';
  286. if ($aCustom['dynamic_paginate']) {
  287. $aExclude = array($this->aCurrent['name'] . '_mode', 'r');
  288. $aLinkAddon = $this->getLinkAddByPrams($aExclude);
  289. $sOnChange = 'return !loadDynamicBlock({id}, \'' . $sMainUrl . $aLinkAddon['params'] . $aLinkAddon['paginate'] . '\');';
  290. }
  291. }
  292. $aMenuTop = $aCustom['menu_top'] ? $this->getTopMenu($aExclude): array();
  293. $sMenuBottom = $aCustom['menu_bottom'] ? $this->getBottomMenu($aCustom['menu_bottom_type'], 0, $this->aCurrent['restriction'][$aCustom['menu_bottom_param']]['value']): array();
  294. return array('code' => $sCode, 'menu_top'=> $aMenuTop, 'menu_bottom' => $sMenuBottom, 'wrapper' => $bWrap);
  295. }
  296. function serviceGetBrowseBlock ($aParams, $sMainUrl = '', $aCustom = array()) {
  297. $aCode = $this->getBrowseBlock($aParams, $aCustom, $sMainUrl);
  298. return $aCode['code'] . $aCode['menu_bottom'];
  299. }
  300. /*
  301. * Get number of all elements under specified search criterias
  302. * @param array $aFilter - search criteria like 'restriction key'=>'rest. value'
  303. * @param array $aJoin - list of joins elements from $this->aCurrent['join'] field which shouldn't be cleared,
  304. if empty then all current joins will be left
  305. * @return integer number of found elements
  306. */
  307. function serviceGetAllCount ($aFilter, $aJoin = array()) {
  308. if (is_array($aFilter)) {
  309. // collect all current joins, but clear almost all search values
  310. if (!is_array($aJoin) || empty($aJoin))
  311. $aCurrJoins = array_keys($this->aCurrent['join']);
  312. else
  313. $aCurrJoins = $aJoin;
  314. $this->clearFilters(array('activeStatus'), $aCurrJoins);
  315. foreach ($aFilter as $sKey => $mixedValue) {
  316. if (isset($this->aCurrent['restriction'][$sKey]))
  317. $this->aCurrent['restriction'][$sKey]['value'] = $mixedValue;
  318. }
  319. return $this->getCount();
  320. }
  321. }
  322. /**
  323. * Set design box template id to use to wrap search results to
  324. */
  325. function setDesignBoxTemplateId ($i) {
  326. $this->iDesignBoxTemplate = $i;
  327. }
  328. }
  329. /** @} */