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 $aPermalinks;
  18. protected $aConstants;
  19. function __construct($oFunctions = false) {
  20. parent::__construct();
  21. if ($oFunctions) {
  22. $this->oFunctions = $oFunctions;
  23. } else {
  24. bx_import('BxTemplFunctions');
  25. $this->oFunctions = BxTemplFunctions::getInstance();
  26. }
  27. }
  28. function getMain() {
  29. // override this to return main module class
  30. }
  31. function isPermalinkEnabled() {
  32. return isset($this->_isPermalinkEnabled) ? $this->_isPermalinkEnabled : ($this->_isPermalinkEnabled = (getParam($this->aPermalinks['param']) == 'on'));
  33. }
  34. function getCurrentUrl ($sType, $iId, $sUri, $aOwner = '') {
  35. $sLink = $this->aConstants['linksTempl'][$sType];
  36. $sLink = str_replace('{id}', $iId, $sLink);
  37. $sLink = str_replace('{uri}', $sUri, $sLink);
  38. if (is_array($aOwner) && !empty($aOwner)) {
  39. $sLink = str_replace('{ownerName}', $aOwner['ownerName'], $sLink); // TODO: urlencode
  40. $sLink = str_replace('{ownerId}', $aOwner['ownerId'], $sLink);
  41. }
  42. return BX_DOL_URL_ROOT . $sLink;
  43. }
  44. function displayResultBlock () {
  45. $sCode = '';
  46. $aData = $this->getSearchData();
  47. if ($this->aCurrent['paginate']['num'] > 0) {
  48. $sCode .= $this->addCustomParts();
  49. foreach ($aData as $aValue) {
  50. $sCode .= $this->displaySearchUnit($aValue);
  51. }
  52. $sCode = '<div class="bx-search-result-block">' . $sCode . '<div class="bx-clear"></div></div>';
  53. }
  54. return $sCode;
  55. }
  56. function displaySearchBox ($sCode, $sPaginate = '') {
  57. $aMenu = false;
  58. if (isset($this->aCurrent['rss']) && $this->aCurrent['rss']['link'])
  59. $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'));
  60. $sTitle = _t($this->aCurrent['title']);
  61. $sCode = $this->oFunctions->designBoxContent($sTitle, $sCode . $sPaginate, $this->iDesignBoxTemplate, $aMenu);
  62. if (!isset($_GET['searchMode']))
  63. $sCode = '<div id="page_block_'.$this->id.'">'.$sCode.'<div class="clear_both"></div></div>';
  64. return $sCode;
  65. }
  66. function displaySearchUnit ($aData) {
  67. $oMain = $this->getMain();
  68. return $oMain->_oTemplate->unit($aData, $this->sUnitTemplate);
  69. }
  70. function _transformData ($aUnit, $sTempl, $sCssHeader = '') {
  71. foreach ($aUnit as $sKey => $sValue)
  72. $sTempl = str_replace('{'.$sKey.'}', $sValue, $sTempl);
  73. $sCssHeader = strlen($sCssHeader) > 0 ? $sCssHeader : 'text_Unit';
  74. $sTempl = str_replace('{unitClass}', $sCssHeader, $sTempl);
  75. return $sTempl;
  76. }
  77. function showAdminActionsPanel($sWrapperId, $aButtons, $sCheckboxName = 'entry', $bSelectAll = true, $bSelectAllChecked = false, $sCustomHtml = '') {
  78. $aBtns = array();
  79. foreach ($aButtons as $k => $v) {
  80. if(is_array($v)) {
  81. $aBtns[] = $v;
  82. continue;
  83. }
  84. $aBtns[] = array(
  85. 'type' => 'submit',
  86. 'name' => $k,
  87. 'value' => '_' == $v[0] ? _t($v) : $v,
  88. 'onclick' => '',
  89. );
  90. }
  91. $aUnit = array(
  92. 'bx_repeat:buttons' => $aBtns,
  93. 'bx_if:customHTML' => array(
  94. 'condition' => strlen($sCustomHtml) > 0,
  95. 'content' => array(
  96. 'custom_HTML' => $sCustomHtml,
  97. )
  98. ),
  99. 'bx_if:selectAll' => array(
  100. 'condition' => $bSelectAll,
  101. 'content' => array(
  102. 'wrapperId' => $sWrapperId,
  103. 'checkboxName' => $sCheckboxName,
  104. 'checked' => ($bSelectAll && $bSelectAllChecked ? 'checked="checked"' : '')
  105. )
  106. ),
  107. );
  108. return BxDolTemplate::getInstance()->parseHtmlByName('adminActionsPanel.html', $aUnit, array('{','}'));
  109. }
  110. function showAdminFilterPanel($sFilterValue, $sInputId = 'filter_input_id', $sCheckboxId = 'filter_checkbox_id', $sFilterName = 'filter', $sOnApply = '') {
  111. $sFilter = _t('_sys_admin_filter');
  112. $sApply = _t('_sys_admin_apply');
  113. $sFilterValue = bx_html_attribute($sFilterValue);
  114. $isChecked = $sFilterValue ? ' checked="checked" ' : '';
  115. $sJsContent = "";
  116. if(empty($sOnApply)) {
  117. $sOnApply = 'on_filter_apply(this)';
  118. ob_start();
  119. ?>
  120. <script type="text/javascript">
  121. function on_filter_apply (e) {
  122. var s = ('' + document.location).replace (/[&]{0,1}<?=$sFilterName;?>=.*/, ''); // remove filter
  123. s = s.replace(/page=\d+/, 'page=1'); // goto 1st page
  124. if (e.checked && $('#<?=$sInputId;?>').val().length > 2)
  125. s += (-1 == s.indexOf('?') ? '?' : '&') + '<?=$sFilterName;?>=' + $('#<?=$sInputId;?>').val(); // append filter
  126. document.location = s;
  127. }
  128. function on_filter_key_up (e) {
  129. if (13 == e.keyCode) {
  130. $('#<?php echo $sCheckboxId; ?>').attr('checked', 'checked');
  131. on_filter_apply($('#<?php echo $sCheckboxId; ?>').get(0));
  132. return false;
  133. } else {
  134. $('#<?php echo $sCheckboxId; ?>').attr('checked', '');
  135. return true;
  136. }
  137. }
  138. </script>
  139. <?
  140. $sJsContent = ob_get_clean();
  141. }
  142. return <<<EOF
  143. {$sJsContent}
  144. <div class="admin_filter_panel">
  145. <table>
  146. <tr>
  147. <td>{$sFilter}</td>
  148. <td>
  149. <div class="input_wrapper input_wrapper_text">
  150. <input type="text" id="{$sInputId}" value="{$sFilterValue}" class="form_input_text" onkeypress="return on_filter_key_up(event)" />
  151. <div class="input_close input_close_text">&nbsp;</div>
  152. </div>
  153. </td>
  154. <td><input type="checkbox" id="{$sCheckboxId}" $isChecked onclick="{$sOnApply}" /></td>
  155. <td><label for="{$sCheckboxId}">{$sApply}</label></td>
  156. </tr>
  157. </table>
  158. </div>
  159. EOF;
  160. }
  161. function showPagination($bAdmin = false, $bChangePage = true, $bPageReload = true) {
  162. $oMain = $this->getMain();
  163. $oConfig = $oMain->_oConfig;
  164. bx_import('BxDolPaginate');
  165. $sUrlStart = BX_DOL_URL_ROOT . $oConfig->getBaseUri() . $this->sBrowseUrl;
  166. $sUrlStart .= (false === strpos($sUrlStart, '?') ? '?' : '&');
  167. bx_import('BxTemplPaginate');
  168. $oPaginate = new BxTemplPaginate(array(
  169. 'page_url' => $sUrlStart . 'start={start}&per_page={per_page}' . (false !== bx_get($this->sFilterName) ? '&' . $this->sFilterName . '=' . bx_get($this->sFilterName) : ''),
  170. 'num' => $this->aCurrent['paginate']['num'],
  171. 'per_page' => $this->aCurrent['paginate']['perPage'],
  172. 'start' => $this->aCurrent['paginate']['start'],
  173. ));
  174. return '<div class="clear_both"></div>'.$oPaginate->getPaginate();
  175. }
  176. function showPaginationAjax($sBlockId) {
  177. $oMain = $this->getMain();
  178. $oConfig = $oMain->_oConfig;
  179. bx_import('BxDolPaginate');
  180. $sUrlStart = BX_DOL_URL_ROOT . $oConfig->getBaseUri() . $this->sBrowseUrl;
  181. $sUrlStart .= (false === strpos($sUrlStart, '?') ? '?' : '&');
  182. $oPaginate = new BxDolPaginate(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. /** @} */