BxBaseSearchExtended.php 8.2 KB

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