BxBaseSearchExtended.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 $aCondition 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 $sUnitTemplate custom unit templates to use
  47. * @param $iStart position of first record to display
  48. * @param $iPerPage number of items per page
  49. * @return HTML string with search results
  50. */
  51. public function getResults($aCondition = array(), $sUnitTemplate = '', $iStart = 0, $iPerPage = 0)
  52. {
  53. if(!$this->isEnabled())
  54. return '';
  55. $oForm = $this->prepareForm();
  56. if ($aCondition) {
  57. $mixedSubmitName = $oForm->aParams['db']['submit_name'];
  58. if (is_array($mixedSubmitName))
  59. $mixedSubmitName = array_pop($mixedSubmitName);
  60. $aCondition[$mixedSubmitName] = 1;
  61. $oForm->aFormAttrs['method'] = BX_DOL_FORM_METHOD_SPECIFIC;
  62. $oForm->aParams['csrf']['disable'] = true;
  63. }
  64. $oForm->initChecker(array(), $aCondition);
  65. if(!$oForm->isSubmittedAndValid())
  66. return '';
  67. $oContentInfo = BxDolContentInfo::getObjectInstance($this->_aObject['object_content_info']);
  68. if(!$oContentInfo)
  69. return '';
  70. $aParams = array();
  71. foreach($this->_aObject['fields'] as $aField) {
  72. $sValue = $oForm->getCleanValue($aField['name']);
  73. if(empty($sValue) || (is_array($sValue) && bx_is_empty_array($sValue)))
  74. continue;
  75. $aParams[$aField['name']] = array(
  76. 'type' => $aField['search_type'],
  77. 'value' => $sValue,
  78. 'operator' => $aField['search_operator']
  79. );
  80. }
  81. if(empty($aParams) || !is_array($aParams))
  82. return '';
  83. if (!$iPerPage) {
  84. bx_import('BxDolSearch');
  85. $iPerPage = BX_DOL_SEARCH_RESULTS_PER_PAGE_DEFAULT;
  86. }
  87. $aResults = false;
  88. bx_alert('search', 'get_data', 0, false, array('object' => $this->_aObject, 'search_params' => $aParams, 'search_results' => &$aResults));
  89. if($aResults === false)
  90. $aResults = $oContentInfo->getSearchResultExtended($aParams, $iStart, $iPerPage);
  91. if(empty($aResults) || !is_array($aResults))
  92. return '';
  93. $sResults = '';
  94. foreach($aResults as $iId)
  95. $sResults .= $oContentInfo->getContentSearchResultUnit($iId, $sUnitTemplate);
  96. return $this->_oTemplate->parseHtmlByName('search_extended_results.html', array(
  97. 'code' => $sResults
  98. ));
  99. }
  100. protected function &prepareForm()
  101. {
  102. if(!empty($this->_oForm) && $this->_oForm instanceof BxDolForm)
  103. return $this->_oForm;
  104. $sForm = 'sys_search_extended_' . $this->_sObject;
  105. $sFormSubmit = 'search' . $this->_sObject;
  106. $aForm = array(
  107. 'form_attrs' => array(
  108. 'id' => $sForm,
  109. 'name' => $sForm,
  110. 'action' => '',
  111. 'method' => 'post'
  112. ),
  113. 'params' => array(
  114. 'db' => array(
  115. 'table' => '',
  116. 'key' => '',
  117. 'uri' => '',
  118. 'uri_title' => '',
  119. 'submit_name' => $sFormSubmit
  120. ),
  121. ),
  122. 'inputs' => array()
  123. );
  124. foreach ($this->_aObject['fields'] as $aField) {
  125. if((int)$aField['active'] == 0)
  126. continue;
  127. if(in_array($aField['search_type'], array('checkbox_set', 'select_multiple')) && isset($aField['values']['']))
  128. unset($aField['values']['']);
  129. $aForm['inputs'][$aField['name']] = array(
  130. 'type' => $aField['search_type'],
  131. 'name' => $aField['name'],
  132. 'caption' => _t($aField['caption']),
  133. 'info' => _t($aField['info']),
  134. 'values' => $aField['values'],
  135. 'value' => $aField['search_value'],
  136. 'db' => array(
  137. 'pass' => !empty($aField['pass']) ? $aField['pass'] : 'Xss'
  138. )
  139. );
  140. }
  141. $aForm['inputs']['search'] = array(
  142. 'type' => 'submit',
  143. 'name' => $sFormSubmit,
  144. 'value' => _t('_Search')
  145. );
  146. $sClass = 'BxTemplSearchExtendedForm';
  147. if(!empty($this->_sFormClassName)) {
  148. $sClass = $this->_sFormClassName;
  149. if(!empty($this->_sFormClassPath))
  150. require_once(BX_DIRECTORY_PATH_ROOT . $this->_sFormClassPath);
  151. }
  152. $this->_oForm = new $sClass($aForm, $this->_oTemplate);
  153. $this->_oForm->initChecker();
  154. return $this->_oForm;
  155. }
  156. }
  157. /** @} */