BxBaseSearchResult.php 16 KB

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