BxBaseSearchExtended.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * Extended Search objects representation.
  11. *
  12. * @see BxDolSearchExtended
  13. */
  14. class BxBaseSearchExtended extends BxDolSearchExtended
  15. {
  16. protected $_sFormClassName;
  17. protected $_sFormClassPath;
  18. protected $_oForm;
  19. protected $_oTemplate;
  20. protected $_bJsMode;
  21. protected $_iAgeMin;
  22. protected $_iAgeMax;
  23. public function __construct ($aObject, $oTemplate = null)
  24. {
  25. parent::__construct ($aObject);
  26. if ($oTemplate)
  27. $this->_oTemplate = $oTemplate;
  28. else if(!empty($this->_aObject['module']))
  29. $this->_oTemplate = BxDolModule::getInstance($this->_aObject['module'])->_oTemplate;
  30. else
  31. $this->_oTemplate = BxDolTemplate::getInstance();
  32. $this->_sFormClassName = '';
  33. $this->_sFormClassPath = '';
  34. $this->_oForm = null;
  35. $this->_bJsMode = false;
  36. $this->_iAgeMin = 1;
  37. $this->_iAgeMax = 75;
  38. }
  39. public function getForm($aParams = array())
  40. {
  41. if(!$this->isEnabled())
  42. return '';
  43. $oForm = $this->prepareForm($aParams);
  44. return $oForm->getCode();
  45. }
  46. /**
  47. * Get search results from search form or from custom condition
  48. *
  49. * @param $aParams['cond'] custom condition to pass instead of form submission,
  50. * conditions are key&value pair, where 'key' is form input name and
  51. * 'value' is the term to search for
  52. * @param $aParams['start'] position of first record to display
  53. * @param $aParams['per_page'] number of items per page
  54. * @param $aParams['template'] custom unit templates to use
  55. * @return HTML string with search results
  56. */
  57. public function getResults($aParams = array())
  58. {
  59. if(!$this->isEnabled())
  60. return '';
  61. $bJsMode = isset($aParams['js_mode']) ? (bool)$aParams['js_mode'] : $this->_bJsMode;
  62. $bCondition = !empty($aParams['cond']) && is_array($aParams['cond']);
  63. $iStart = !empty($aParams['start']) ? $aParams['start'] : 0;
  64. $iPerPage = !empty($aParams['per_page']) ? $aParams['per_page'] : 0;
  65. unset($aParams['start'], $aParams['per_page']);
  66. $sUnitTemplate = !empty($aParams['template']) ? $aParams['template'] : '';
  67. $oForm = $this->prepareForm($aParams);
  68. if($bCondition) {
  69. $mixedSubmitName = $oForm->aParams['db']['submit_name'];
  70. if(is_array($mixedSubmitName))
  71. $mixedSubmitName = array_pop($mixedSubmitName);
  72. $aParams['cond'][$mixedSubmitName] = 1;
  73. $oForm->aFormAttrs['method'] = BX_DOL_FORM_METHOD_SPECIFIC;
  74. $oForm->aParams['csrf']['disable'] = true;
  75. $oForm->initChecker(array(), $aParams['cond']);
  76. }
  77. if(!$oForm->isSubmittedAndValid() && !$this->_bFilterMode)
  78. return '';
  79. $oContentInfo = BxDolContentInfo::getObjectInstance($this->_aObject['object_content_info']);
  80. if(!$oContentInfo)
  81. return '';
  82. $aParamsSearch = array();
  83. foreach($this->_aObject['fields'] as $aField) {
  84. $mixedValue = $oForm->getCleanValue($aField['name']);
  85. if(empty($mixedValue) || (is_array($mixedValue) && bx_is_empty_array($mixedValue)))
  86. continue;
  87. $aParamsSearch[$aField['name']] = array(
  88. 'type' => $aField['search_type'],
  89. 'value' => $mixedValue,
  90. 'operator' => $aField['search_operator']
  91. );
  92. if(!$bCondition) {
  93. if(!isset($aParams['cond']))
  94. $aParams['cond'] = array();
  95. switch($oForm->aInputs[$aField['name']]['type']) {
  96. case 'location':
  97. case 'location_radius':
  98. $aParams['cond'][$aField['name']] = $mixedValue['string'];
  99. $aLocationComponents = BxDolMetatags::locationsParseComponents($mixedValue['array'], $aField['name']);
  100. if($oForm->aInputs[$aField['name']]['type'] == 'location_radius' && count($mixedValue['array']) > count($aLocationComponents))
  101. $aLocationComponents[$aField['name'] . '_rad'] = array_pop($mixedValue['array']);
  102. $aParams['cond'] = array_merge($aParams['cond'], $aLocationComponents);
  103. break;
  104. default:
  105. $aParams['cond'][$aField['name']] = $mixedValue;
  106. }
  107. }
  108. }
  109. if((empty($aParamsSearch) || !is_array($aParamsSearch)) && !$this->_bFilterMode)
  110. return '';
  111. if (!$iPerPage) {
  112. bx_import('BxDolSearch');
  113. $iPerPage = BX_DOL_SEARCH_RESULTS_PER_PAGE_DEFAULT;
  114. }
  115. $aResults = false;
  116. if (bx_get('sort')){
  117. $aTmp = explode(':', bx_get('sort'));
  118. $aParamsSearch['order'] = [['field' => $aTmp[0], 'direction' => $aTmp[1]]];
  119. }
  120. bx_alert('search', 'get_data', 0, false, array('object' => $this->_aObject, 'search_params' => &$aParamsSearch, 'search_results' => &$aResults));
  121. if($aResults === false)
  122. $aResults = $oContentInfo->getSearchResultExtended($aParamsSearch, $iStart, $iPerPage + 1, $this->_bFilterMode);
  123. if(empty($aResults) || !is_array($aResults))
  124. return '';
  125. if(!empty($aParams['cond']) && is_array($aParams['cond']))
  126. $aParams['cond'] = urlencode(serialize($aParams['cond']));
  127. $aPaginate = array('start' => $iStart, 'per_page' => $iPerPage);
  128. if(!$bJsMode) {
  129. $aParams['start'] = '{start}';
  130. $aParams['per_page'] = '{per_page}';
  131. list($sPageLink, $aPageParams) = bx_get_base_url_inline($aParams);
  132. $aPaginate['page_url'] = BxDolPermalinks::getInstance()->permalink(bx_append_url_params($sPageLink, $aPageParams));
  133. }
  134. else
  135. $aPaginate['on_change_page'] = "return !loadDynamicBlockAutoPaginate(this, '{start}', '{per_page}', " . bx_js_string(json_encode($aParams)) . ");";
  136. $oPaginate = new BxTemplPaginate($aPaginate);
  137. $oPaginate->setNumFromDataArray($aResults);
  138. $bTmplVarsPaginate = $iStart || $oPaginate->getNum() > $iPerPage;
  139. $aTmplVarsPaginate = $bTmplVarsPaginate ? array('paginate' => $oPaginate->getSimplePaginate()) : array();
  140. $sResults = '';
  141. foreach($aResults as $iId)
  142. $sResults .= $oContentInfo->getContentSearchResultUnit($iId, $sUnitTemplate);
  143. $sSort = '';
  144. if ($sResults != ''){
  145. $aData = $this->_aObject['sortable_fields'];
  146. $aValues = [];
  147. foreach($aData as $aField) {
  148. if ($aField['active'] == 1)
  149. $aValues[$aField['name'] . ':' . $aField['direction']] = _t($aField['caption']) . ' ' . $aField['direction'];
  150. }
  151. if (!empty($aValues)){
  152. $aValues = array_merge(['' => _t('_sys_txt_search_sort_by_default')], $aValues);
  153. }
  154. $oForm = new BxTemplFormView(array());
  155. $sOnChange = '';
  156. if(!$bJsMode) {
  157. unset($aParams['start']);
  158. unset($aParams['per_page']);
  159. list($sPageLink, $aPageParams) = bx_get_base_url_inline($aParams);
  160. $sOnChange = "bx_search_extnded_sort(this,'" . BxDolPermalinks::getInstance()->permalink(bx_append_url_params($sPageLink, $aPageParams)) . "')";
  161. }
  162. else{
  163. $sOnChange = "return !loadDynamicBlockAutoSort(this, $(this).val()," . bx_js_string(json_encode($aParams)) . ");";
  164. }
  165. $sSort = '';
  166. if (!empty($aValues)){
  167. $aInputSort = array(
  168. 'type' => 'select',
  169. 'name' => 'sort',
  170. 'value' => bx_get('sort') ? bx_get('sort') : '',
  171. 'values' => $aValues,
  172. 'caption' => _t('_sys_txt_search_sort_by'),
  173. 'attrs' => array(
  174. 'onChange' => $sOnChange,
  175. ),
  176. 'tr_attrs' => array(
  177. 'class' => 'sort'
  178. )
  179. );
  180. $sSort = $oForm->genRow($aInputSort);
  181. }
  182. }
  183. return $this->_oTemplate->parseHtmlByName('search_extended_results.html', array(
  184. 'sort' => $sSort,
  185. 'code' => $sResults,
  186. 'bx_if:show_paginate' => array(
  187. 'condition' => $bTmplVarsPaginate,
  188. 'content' => $aTmplVarsPaginate
  189. )
  190. ));
  191. }
  192. protected function &prepareForm($aParams = array())
  193. {
  194. if(!empty($this->_oForm) && $this->_oForm instanceof BxDolForm)
  195. return $this->_oForm;
  196. $sForm = 'sys_search_extended_' . $this->_sObject;
  197. $sFormSubmit = 'search' . $this->_sObject;
  198. list($sPageLink, $aPageParams) = bx_get_base_url_inline();
  199. $aForm = array(
  200. 'form_attrs' => array(
  201. 'id' => $sForm,
  202. 'name' => $sForm,
  203. 'action' => BxDolPermalinks::getInstance()->permalink(bx_append_url_params($sPageLink, array('i' => $aPageParams['i']))),
  204. 'method' => 'post'
  205. ),
  206. 'params' => array(
  207. 'db' => array(
  208. 'table' => '',
  209. 'key' => '',
  210. 'uri' => '',
  211. 'uri_title' => '',
  212. 'submit_name' => $sFormSubmit
  213. ),
  214. ),
  215. 'inputs' => array()
  216. );
  217. foreach ($this->_aObject['fields'] as $aField) {
  218. if((int)$aField['active'] == 0)
  219. continue;
  220. if(in_array($aField['search_type'], array('checkbox_set', 'select_multiple'))) {
  221. if(isset($aField['values']['']))
  222. unset($aField['values']['']);
  223. if(isset($aField['values'][0]) && !is_array($aField['values'][0]))
  224. unset($aField['values'][0]);
  225. }
  226. $aAttrs = array();
  227. if(in_array($aField['search_type'], array('datepicker_range_age')) && !empty($aField['search_value'])) {
  228. $aFieldParams = BxDolService::callSerialized($aField['search_value']);
  229. $iMin = isset($aFieldParams['min']) && is_numeric($aFieldParams['min']) ? $aFieldParams['min'] : $this->_iAgeMin;
  230. $iMax = isset($aFieldParams['max']) && is_numeric($aFieldParams['max']) ? $aFieldParams['max'] : $this->_iAgeMax;
  231. $aField['search_value'] = $iMin . '-' . $iMax;
  232. $aAttrs = array('min' => $iMin, 'max' => $iMax, 'step' => 1);
  233. }
  234. if(in_array($aField['search_type'], array('datepicker_range')) && !empty($aField['search_value'])) {
  235. $aField['search_value'] = '';
  236. }
  237. $aForm['inputs'][$aField['name']] = array(
  238. 'type' => $aField['search_type'],
  239. 'name' => $aField['name'],
  240. 'caption' => _t($aField['caption']),
  241. 'info' => _t($aField['info']),
  242. 'values' => $aField['values'],
  243. 'value' => $aField['search_value'],
  244. 'attrs' => $aAttrs,
  245. 'db' => array(
  246. 'pass' => !empty($aField['pass']) ? $aField['pass'] : 'Xss'
  247. )
  248. );
  249. if(in_array($aField['search_type'], array('location', 'location_radius')))
  250. $aForm['inputs'][$aField['name']]['manual_input'] = true;
  251. }
  252. $aForm['inputs']['search'] = array(
  253. 'type' => 'submit',
  254. 'name' => $sFormSubmit,
  255. 'value' => _t($this->_bFilterMode ? '_Apply' : '_Search')
  256. );
  257. $sClass = 'BxTemplSearchExtendedForm';
  258. if(!empty($this->_sFormClassName)) {
  259. $sClass = $this->_sFormClassName;
  260. if(!empty($this->_sFormClassPath))
  261. require_once(BX_DIRECTORY_PATH_ROOT . $this->_sFormClassPath);
  262. }
  263. $bJsMode = isset($aParams['js_mode']) ? (bool)$aParams['js_mode'] : $this->_bJsMode;
  264. $bCondition = !empty($aParams['cond']) && is_array($aParams['cond']);
  265. $aValues = !$bJsMode && $bCondition ? $aParams['cond'] : array();
  266. $this->_oForm = new $sClass($aForm, $this->_oTemplate);
  267. $this->_oForm->initChecker($aValues, $aValues);
  268. return $this->_oForm;
  269. }
  270. }
  271. /** @} */