BxBaseSearchExtended.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. public function __construct ($aObject, $oTemplate = null)
  22. {
  23. parent::__construct ($aObject);
  24. if ($oTemplate)
  25. $this->_oTemplate = $oTemplate;
  26. else if(!empty($this->_aObject['module']))
  27. $this->_oTemplate = BxDolModule::getInstance($this->_aObject['module'])->_oTemplate;
  28. else
  29. $this->_oTemplate = BxDolTemplate::getInstance();
  30. $this->_sFormClassName = '';
  31. $this->_sFormClassPath = '';
  32. $this->_oForm = null;
  33. $this->_bJsMode = false;
  34. }
  35. public function getForm($aParams = array())
  36. {
  37. if(!$this->isEnabled())
  38. return '';
  39. $oForm = $this->prepareForm($aParams);
  40. return $oForm->getCode();
  41. }
  42. /**
  43. * Get search results from search form or from custom condition
  44. *
  45. * @param $aParams['cond'] custom condition to pass instead of form submission,
  46. * conditions are key&value pair, where 'key' is form input name and
  47. * 'value' is the term to search for
  48. * @param $aParams['start'] position of first record to display
  49. * @param $aParams['per_page'] number of items per page
  50. * @param $aParams['template'] custom unit templates to use
  51. * @return HTML string with search results
  52. */
  53. public function getResults($aParams = array())
  54. {
  55. if(!$this->isEnabled())
  56. return '';
  57. $bJsMode = isset($aParams['js_mode']) ? (bool)$aParams['js_mode'] : $this->_bJsMode;
  58. $bCondition = !empty($aParams['cond']) && is_array($aParams['cond']);
  59. $iStart = !empty($aParams['start']) ? $aParams['start'] : 0;
  60. $iPerPage = !empty($aParams['per_page']) ? $aParams['per_page'] : 0;
  61. unset($aParams['start'], $aParams['per_page']);
  62. $sUnitTemplate = !empty($aParams['template']) ? $aParams['template'] : '';
  63. $oForm = $this->prepareForm($aParams);
  64. if($bCondition) {
  65. $mixedSubmitName = $oForm->aParams['db']['submit_name'];
  66. if(is_array($mixedSubmitName))
  67. $mixedSubmitName = array_pop($mixedSubmitName);
  68. $aParams['cond'][$mixedSubmitName] = 1;
  69. $oForm->aFormAttrs['method'] = BX_DOL_FORM_METHOD_SPECIFIC;
  70. $oForm->aParams['csrf']['disable'] = true;
  71. $oForm->initChecker(array(), $aParams['cond']);
  72. }
  73. if(!$oForm->isSubmittedAndValid() && !$this->_bFilterMode)
  74. return '';
  75. $oContentInfo = BxDolContentInfo::getObjectInstance($this->_aObject['object_content_info']);
  76. if(!$oContentInfo)
  77. return '';
  78. $aParamsSearch = array();
  79. foreach($this->_aObject['fields'] as $aField) {
  80. $mixedValue = $oForm->getCleanValue($aField['name']);
  81. if(empty($mixedValue) || (is_array($mixedValue) && bx_is_empty_array($mixedValue)))
  82. continue;
  83. $aParamsSearch[$aField['name']] = array(
  84. 'type' => $aField['search_type'],
  85. 'value' => $mixedValue,
  86. 'operator' => $aField['search_operator']
  87. );
  88. if(!$bCondition) {
  89. if(!isset($aParams['cond']))
  90. $aParams['cond'] = array();
  91. switch($oForm->aInputs[$aField['name']]['type']) {
  92. case 'location':
  93. case 'location_radius':
  94. $aParams['cond'][$aField['name']] = $mixedValue['string'];
  95. $aLocationComponents = BxDolMetatags::locationsParseComponents($mixedValue['array'], $aField['name']);
  96. if($oForm->aInputs[$aField['name']]['type'] == 'location_radius' && count($mixedValue['array']) > count($aLocationComponents))
  97. $aLocationComponents[$aField['name'] . '_rad'] = array_pop($mixedValue['array']);
  98. $aParams['cond'] = array_merge($aParams['cond'], $aLocationComponents);
  99. break;
  100. default:
  101. $aParams['cond'][$aField['name']] = $mixedValue;
  102. }
  103. }
  104. }
  105. if((empty($aParamsSearch) || !is_array($aParamsSearch)) && !$this->_bFilterMode)
  106. return '';
  107. if (!$iPerPage) {
  108. bx_import('BxDolSearch');
  109. $iPerPage = BX_DOL_SEARCH_RESULTS_PER_PAGE_DEFAULT;
  110. }
  111. $aResults = false;
  112. bx_alert('search', 'get_data', 0, false, array('object' => $this->_aObject, 'search_params' => &$aParamsSearch, 'search_results' => &$aResults));
  113. if($aResults === false)
  114. $aResults = $oContentInfo->getSearchResultExtended($aParamsSearch, $iStart, $iPerPage + 1, $this->_bFilterMode);
  115. if(empty($aResults) || !is_array($aResults))
  116. return '';
  117. if(!empty($aParams['cond']) && is_array($aParams['cond']))
  118. $aParams['cond'] = urlencode(serialize($aParams['cond']));
  119. $aPaginate = array('start' => $iStart, 'per_page' => $iPerPage);
  120. if(!$bJsMode) {
  121. $aParams['start'] = '{start}';
  122. $aParams['per_page'] = '{per_page}';
  123. list($sPageLink, $aPageParams) = bx_get_base_url_inline($aParams);
  124. $aPaginate['page_url'] = BxDolPermalinks::getInstance()->permalink(bx_append_url_params($sPageLink, $aPageParams));
  125. }
  126. else
  127. $aPaginate['on_change_page'] = "return !loadDynamicBlockAutoPaginate(this, '{start}', '{per_page}', " . bx_js_string(json_encode($aParams)) . ");";
  128. $oPaginate = new BxTemplPaginate($aPaginate);
  129. $oPaginate->setNumFromDataArray($aResults);
  130. $bTmplVarsPaginate = $iStart || $oPaginate->getNum() > $iPerPage;
  131. $aTmplVarsPaginate = $bTmplVarsPaginate ? array('paginate' => $oPaginate->getSimplePaginate()) : array();
  132. $sResults = '';
  133. foreach($aResults as $iId)
  134. $sResults .= $oContentInfo->getContentSearchResultUnit($iId, $sUnitTemplate);
  135. return $this->_oTemplate->parseHtmlByName('search_extended_results.html', array(
  136. 'code' => $sResults,
  137. 'bx_if:show_paginate' => array(
  138. 'condition' => $bTmplVarsPaginate,
  139. 'content' => $aTmplVarsPaginate
  140. )
  141. ));
  142. }
  143. protected function &prepareForm($aParams = array())
  144. {
  145. if(!empty($this->_oForm) && $this->_oForm instanceof BxDolForm)
  146. return $this->_oForm;
  147. $sForm = 'sys_search_extended_' . $this->_sObject;
  148. $sFormSubmit = 'search' . $this->_sObject;
  149. list($sPageLink, $aPageParams) = bx_get_base_url_inline();
  150. $aForm = array(
  151. 'form_attrs' => array(
  152. 'id' => $sForm,
  153. 'name' => $sForm,
  154. 'action' => BxDolPermalinks::getInstance()->permalink(bx_append_url_params($sPageLink, array('i' => $aPageParams['i']))),
  155. 'method' => 'post'
  156. ),
  157. 'params' => array(
  158. 'db' => array(
  159. 'table' => '',
  160. 'key' => '',
  161. 'uri' => '',
  162. 'uri_title' => '',
  163. 'submit_name' => $sFormSubmit
  164. ),
  165. ),
  166. 'inputs' => array()
  167. );
  168. foreach ($this->_aObject['fields'] as $aField) {
  169. if((int)$aField['active'] == 0)
  170. continue;
  171. if(in_array($aField['search_type'], array('checkbox_set', 'select_multiple'))) {
  172. if(isset($aField['values']['']))
  173. unset($aField['values']['']);
  174. if(isset($aField['values'][0]) && !is_array($aField['values'][0]))
  175. unset($aField['values'][0]);
  176. }
  177. $aAttrs = array();
  178. if(in_array($aField['search_type'], array('datepicker_range_age')) && isset($aField['search_value']) && $aField['search_value'] != ''){
  179. $aTmp = BxDolService::callSerialized($aField['search_value']);
  180. if (isset($aTmp['min']) && isset($aTmp['max'])){
  181. $aField['search_value'] = $aTmp['min'] . '-' . $aTmp['max'];
  182. $aAttrs = array('min' => $aTmp['min'], 'max' => $aTmp['max'], 'step' => 1);
  183. }
  184. }
  185. if(in_array($aField['search_type'], array('datepicker_range')) && isset($aField['search_value']) && $aField['search_value'] != ''){
  186. $aField['search_value'] = '';
  187. }
  188. $aForm['inputs'][$aField['name']] = array(
  189. 'type' => $aField['search_type'],
  190. 'name' => $aField['name'],
  191. 'caption' => _t($aField['caption']),
  192. 'info' => _t($aField['info']),
  193. 'values' => $aField['values'],
  194. 'value' => $aField['search_value'],
  195. 'attrs' => $aAttrs,
  196. 'db' => array(
  197. 'pass' => !empty($aField['pass']) ? $aField['pass'] : 'Xss'
  198. )
  199. );
  200. if(in_array($aField['search_type'], array('location', 'location_radius')))
  201. $aForm['inputs'][$aField['name']]['manual_input'] = true;
  202. }
  203. $aForm['inputs']['search'] = array(
  204. 'type' => 'submit',
  205. 'name' => $sFormSubmit,
  206. 'value' => _t($this->_bFilterMode ? '_Apply' : '_Search')
  207. );
  208. $sClass = 'BxTemplSearchExtendedForm';
  209. if(!empty($this->_sFormClassName)) {
  210. $sClass = $this->_sFormClassName;
  211. if(!empty($this->_sFormClassPath))
  212. require_once(BX_DIRECTORY_PATH_ROOT . $this->_sFormClassPath);
  213. }
  214. $bJsMode = isset($aParams['js_mode']) ? (bool)$aParams['js_mode'] : $this->_bJsMode;
  215. $bCondition = !empty($aParams['cond']) && is_array($aParams['cond']);
  216. $aValues = !$bJsMode && $bCondition ? $aParams['cond'] : array();
  217. $this->_oForm = new $sClass($aForm, $this->_oTemplate);
  218. $this->_oForm->initChecker($aValues, $aValues);
  219. return $this->_oForm;
  220. }
  221. }
  222. /** @} */