BxBaseSearchExtended.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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'] = self::encodeConditions($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, true, ['{start}', '{per_page}']));
  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. $aTxtDirection = [
  144. 'asc' => _t('_order_asc'),
  145. 'desc' => _t('_order_desc')
  146. ];
  147. $sSort = '';
  148. if ($sResults != ''){
  149. $aData = $this->_aObject['sortable_fields'];
  150. $aValues = [];
  151. foreach($aData as $aField) {
  152. if ($aField['active'] == 0)
  153. continue;
  154. $sLangKey = $aField['caption'] . '_' . $aField['direction'];
  155. $sLangVal = _t($sLangKey);
  156. $aValues[$aField['name'] . ':' . $aField['direction']] = strcmp($sLangKey, $sLangVal) != 0 ? $sLangVal : _t($aField['caption']) . ' ' . $aTxtDirection[$aField['direction']];
  157. }
  158. if (!empty($aValues)){
  159. $aValues = array_merge(['' => _t('_sys_txt_search_sort_by_default')], $aValues);
  160. }
  161. $oForm = new BxTemplFormView(array());
  162. $sOnChange = '';
  163. if(!$bJsMode) {
  164. unset($aParams['start']);
  165. unset($aParams['per_page']);
  166. list($sPageLink, $aPageParams) = bx_get_base_url_inline($aParams);
  167. $sOnChange = "bx_search_extnded_sort(this,'" . BxDolPermalinks::getInstance()->permalink(bx_append_url_params($sPageLink, $aPageParams)) . "')";
  168. }
  169. else{
  170. $sOnChange = "return !loadDynamicBlockAutoSort(this, $(this).val()," . bx_js_string(json_encode($aParams)) . ");";
  171. }
  172. $sSort = '';
  173. if (!empty($aValues)){
  174. $aInputSort = array(
  175. 'type' => 'select',
  176. 'name' => 'sort',
  177. 'value' => bx_get('sort') ? bx_get('sort') : '',
  178. 'values' => $aValues,
  179. 'caption' => _t('_sys_txt_search_sort_by'),
  180. 'attrs' => array(
  181. 'onChange' => $sOnChange,
  182. ),
  183. 'tr_attrs' => array(
  184. 'class' => 'sort'
  185. )
  186. );
  187. $sSort = $oForm->genRow($aInputSort);
  188. }
  189. }
  190. return $this->_oTemplate->parseHtmlByName('search_extended_results.html', array(
  191. 'sort' => $sSort,
  192. 'code' => $sResults,
  193. 'bx_if:show_paginate' => array(
  194. 'condition' => $bTmplVarsPaginate,
  195. 'content' => $aTmplVarsPaginate
  196. )
  197. ));
  198. }
  199. protected function &prepareForm($aParams = array())
  200. {
  201. if(!empty($this->_oForm) && $this->_oForm instanceof BxDolForm)
  202. return $this->_oForm;
  203. $sForm = 'sys_search_extended_' . $this->_sObject;
  204. $sFormSubmit = 'search' . $this->_sObject;
  205. list($sPageLink, $aPageParams) = bx_get_base_url_inline();
  206. $aForm = array(
  207. 'form_attrs' => array(
  208. 'id' => $sForm,
  209. 'name' => $sForm,
  210. 'action' => BxDolPermalinks::getInstance()->permalink(bx_append_url_params($sPageLink, array('i' => $aPageParams['i']))),
  211. 'method' => 'post'
  212. ),
  213. 'params' => array(
  214. 'db' => array(
  215. 'table' => '',
  216. 'key' => '',
  217. 'uri' => '',
  218. 'uri_title' => '',
  219. 'submit_name' => $sFormSubmit
  220. ),
  221. 'checker_helper' => 'BxBaseSearchExtendedFormCheckerHelper',
  222. ),
  223. 'inputs' => array()
  224. );
  225. foreach ($this->_aObject['fields'] as $aField) {
  226. if((int)$aField['active'] == 0)
  227. continue;
  228. if(in_array($aField['search_type'], array('checkbox_set', 'select_multiple'))) {
  229. if(isset($aField['values']['']))
  230. unset($aField['values']['']);
  231. if(isset($aField['values'][0]) && !is_array($aField['values'][0]))
  232. unset($aField['values'][0]);
  233. }
  234. $aAttrs = array();
  235. if(in_array($aField['search_type'], array('datepicker_range_age')) && !empty($aField['search_value'])) {
  236. $aFieldParams = BxDolService::callSerialized($aField['search_value']);
  237. $iMin = isset($aFieldParams['min']) && is_numeric($aFieldParams['min']) ? $aFieldParams['min'] : $this->_iAgeMin;
  238. $iMax = isset($aFieldParams['max']) && is_numeric($aFieldParams['max']) ? $aFieldParams['max'] : $this->_iAgeMax;
  239. $aField['search_value'] = $iMin . '-' . $iMax;
  240. $aAttrs = array('min' => $iMin, 'max' => $iMax, 'step' => 1);
  241. }
  242. if(in_array($aField['search_type'], array('datepicker_range')) && !empty($aField['search_value'])) {
  243. $aField['search_value'] = '';
  244. }
  245. $aForm['inputs'][$aField['name']] = array(
  246. 'type' => $aField['search_type'],
  247. 'name' => $aField['name'],
  248. 'caption' => _t($aField['caption']),
  249. 'info' => _t($aField['info']),
  250. 'values' => $aField['values'],
  251. 'value' => $aField['search_value'],
  252. 'attrs' => $aAttrs,
  253. 'db' => array(
  254. 'pass' => 'datepicker_range_age' === $aField['search_type'] ? 'AgeRange' : (!empty($aField['pass']) ? $aField['pass'] : 'Xss')
  255. )
  256. );
  257. if(in_array($aField['search_type'], array('location', 'location_radius')))
  258. $aForm['inputs'][$aField['name']]['manual_input'] = true;
  259. }
  260. $aForm['inputs']['search'] = array(
  261. 'type' => 'submit',
  262. 'name' => $sFormSubmit,
  263. 'value' => _t($this->_bFilterMode ? '_Apply' : '_Search')
  264. );
  265. $sClass = 'BxTemplSearchExtendedForm';
  266. if(!empty($this->_sFormClassName)) {
  267. $sClass = $this->_sFormClassName;
  268. if(!empty($this->_sFormClassPath))
  269. require_once(BX_DIRECTORY_PATH_ROOT . $this->_sFormClassPath);
  270. }
  271. $bJsMode = isset($aParams['js_mode']) ? (bool)$aParams['js_mode'] : $this->_bJsMode;
  272. $bCondition = !empty($aParams['cond']) && is_array($aParams['cond']);
  273. $aValues = !$bJsMode && $bCondition ? $aParams['cond'] : array();
  274. $this->_oForm = new $sClass($aForm, $this->_oTemplate);
  275. $this->_oForm->initChecker($aValues, $aValues);
  276. return $this->_oForm;
  277. }
  278. }
  279. class BxBaseSearchExtendedFormCheckerHelper extends BxDolFormCheckerHelper
  280. {
  281. static public function passAgeRange ($s)
  282. {
  283. if (is_array($s)) {
  284. $a = array ();
  285. foreach ($s as $k => $v)
  286. $a[$k] = self::_passAgeRange ($v);
  287. return $a;
  288. }
  289. return self::_passAgeRange ($s);
  290. }
  291. static public function _passAgeRange ($s)
  292. {
  293. $a = explode ('-', $s);
  294. if (!$a || count($a) != 2)
  295. return $s;
  296. $i0 = new \DateInterval("P".(int)$a[0]."Y");
  297. $i1 = new \DateInterval("P".(int)$a[1]."Y");
  298. return [
  299. (new DateTime("now", new DateTimeZone('UTC')))->sub($i1)->format("Y-m-d"),
  300. (new DateTime("now", new DateTimeZone('UTC')))->sub($i0)->format("Y-m-d"),
  301. ];
  302. }
  303. }
  304. /** @} */