BxBaseSearchExtended.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. bx_alert('search', 'get_data', 0, false, array('object' => $this->_aObject, 'search_params' => &$aParamsSearch, 'search_results' => &$aResults));
  117. if($aResults === false)
  118. $aResults = $oContentInfo->getSearchResultExtended($aParamsSearch, $iStart, $iPerPage + 1, $this->_bFilterMode);
  119. if(empty($aResults) || !is_array($aResults))
  120. return '';
  121. if(!empty($aParams['cond']) && is_array($aParams['cond']))
  122. $aParams['cond'] = urlencode(serialize($aParams['cond']));
  123. $aPaginate = array('start' => $iStart, 'per_page' => $iPerPage);
  124. if(!$bJsMode) {
  125. $aParams['start'] = '{start}';
  126. $aParams['per_page'] = '{per_page}';
  127. list($sPageLink, $aPageParams) = bx_get_base_url_inline($aParams);
  128. $aPaginate['page_url'] = BxDolPermalinks::getInstance()->permalink(bx_append_url_params($sPageLink, $aPageParams));
  129. }
  130. else
  131. $aPaginate['on_change_page'] = "return !loadDynamicBlockAutoPaginate(this, '{start}', '{per_page}', " . bx_js_string(json_encode($aParams)) . ");";
  132. $oPaginate = new BxTemplPaginate($aPaginate);
  133. $oPaginate->setNumFromDataArray($aResults);
  134. $bTmplVarsPaginate = $iStart || $oPaginate->getNum() > $iPerPage;
  135. $aTmplVarsPaginate = $bTmplVarsPaginate ? array('paginate' => $oPaginate->getSimplePaginate()) : array();
  136. $sResults = '';
  137. foreach($aResults as $iId)
  138. $sResults .= $oContentInfo->getContentSearchResultUnit($iId, $sUnitTemplate);
  139. return $this->_oTemplate->parseHtmlByName('search_extended_results.html', array(
  140. 'code' => $sResults,
  141. 'bx_if:show_paginate' => array(
  142. 'condition' => $bTmplVarsPaginate,
  143. 'content' => $aTmplVarsPaginate
  144. )
  145. ));
  146. }
  147. protected function &prepareForm($aParams = array())
  148. {
  149. if(!empty($this->_oForm) && $this->_oForm instanceof BxDolForm)
  150. return $this->_oForm;
  151. $sForm = 'sys_search_extended_' . $this->_sObject;
  152. $sFormSubmit = 'search' . $this->_sObject;
  153. list($sPageLink, $aPageParams) = bx_get_base_url_inline();
  154. $aForm = array(
  155. 'form_attrs' => array(
  156. 'id' => $sForm,
  157. 'name' => $sForm,
  158. 'action' => BxDolPermalinks::getInstance()->permalink(bx_append_url_params($sPageLink, array('i' => $aPageParams['i']))),
  159. 'method' => 'post'
  160. ),
  161. 'params' => array(
  162. 'db' => array(
  163. 'table' => '',
  164. 'key' => '',
  165. 'uri' => '',
  166. 'uri_title' => '',
  167. 'submit_name' => $sFormSubmit
  168. ),
  169. ),
  170. 'inputs' => array()
  171. );
  172. foreach ($this->_aObject['fields'] as $aField) {
  173. if((int)$aField['active'] == 0)
  174. continue;
  175. if(in_array($aField['search_type'], array('checkbox_set', 'select_multiple'))) {
  176. if(isset($aField['values']['']))
  177. unset($aField['values']['']);
  178. if(isset($aField['values'][0]) && !is_array($aField['values'][0]))
  179. unset($aField['values'][0]);
  180. }
  181. $aAttrs = array();
  182. if(in_array($aField['search_type'], array('datepicker_range_age')) && !empty($aField['search_value'])) {
  183. $aFieldParams = BxDolService::callSerialized($aField['search_value']);
  184. $iMin = isset($aFieldParams['min']) && is_numeric($aFieldParams['min']) ? $aFieldParams['min'] : $this->_iAgeMin;
  185. $iMax = isset($aFieldParams['max']) && is_numeric($aFieldParams['max']) ? $aFieldParams['max'] : $this->_iAgeMax;
  186. $aField['search_value'] = $iMin . '-' . $iMax;
  187. $aAttrs = array('min' => $iMin, 'max' => $iMax, 'step' => 1);
  188. }
  189. if(in_array($aField['search_type'], array('datepicker_range')) && !empty($aField['search_value'])) {
  190. $aField['search_value'] = '';
  191. }
  192. $aForm['inputs'][$aField['name']] = array(
  193. 'type' => $aField['search_type'],
  194. 'name' => $aField['name'],
  195. 'caption' => _t($aField['caption']),
  196. 'info' => _t($aField['info']),
  197. 'values' => $aField['values'],
  198. 'value' => $aField['search_value'],
  199. 'attrs' => $aAttrs,
  200. 'db' => array(
  201. 'pass' => !empty($aField['pass']) ? $aField['pass'] : 'Xss'
  202. )
  203. );
  204. if(in_array($aField['search_type'], array('location', 'location_radius')))
  205. $aForm['inputs'][$aField['name']]['manual_input'] = true;
  206. }
  207. $aForm['inputs']['search'] = array(
  208. 'type' => 'submit',
  209. 'name' => $sFormSubmit,
  210. 'value' => _t($this->_bFilterMode ? '_Apply' : '_Search')
  211. );
  212. $sClass = 'BxTemplSearchExtendedForm';
  213. if(!empty($this->_sFormClassName)) {
  214. $sClass = $this->_sFormClassName;
  215. if(!empty($this->_sFormClassPath))
  216. require_once(BX_DIRECTORY_PATH_ROOT . $this->_sFormClassPath);
  217. }
  218. $bJsMode = isset($aParams['js_mode']) ? (bool)$aParams['js_mode'] : $this->_bJsMode;
  219. $bCondition = !empty($aParams['cond']) && is_array($aParams['cond']);
  220. $aValues = !$bJsMode && $bCondition ? $aParams['cond'] : array();
  221. $this->_oForm = new $sClass($aForm, $this->_oTemplate);
  222. $this->_oForm->initChecker($aValues, $aValues);
  223. return $this->_oForm;
  224. }
  225. }
  226. /** @} */