BxBaseSearchExtended.php 5.6 KB

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