BxBaseSearchExtended.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. public function getResults()
  39. {
  40. if(!$this->isEnabled())
  41. return '';
  42. $oForm = $this->prepareForm();
  43. if(!$oForm->isSubmittedAndValid())
  44. return '';
  45. $oContentInfo = BxDolContentInfo::getObjectInstance($this->_aObject['object_content_info']);
  46. if(!$oContentInfo)
  47. return '';
  48. $aParams = array();
  49. foreach($this->_aObject['fields'] as $aField) {
  50. $sValue = $oForm->getCleanValue($aField['name']);
  51. if(empty($sValue))
  52. continue;
  53. $aParams[$aField['name']] = array(
  54. 'type' => $aField['search_type'],
  55. 'value' => $sValue,
  56. 'operator' => $aField['search_operator']
  57. );
  58. }
  59. if(empty($aParams) || !is_array($aParams))
  60. return '';
  61. $aResults = false;
  62. bx_alert('search', 'get_data', 0, false, array('object' => $this->_aObject, 'search_params' => $aParams, 'search_results' => &$aResults));
  63. if($aResults === false)
  64. $aResults = $oContentInfo->getSearchResultExtended($aParams);
  65. if(empty($aResults) || !is_array($aResults))
  66. return '';
  67. $sResults = '';
  68. foreach($aResults as $iId)
  69. $sResults .= $oContentInfo->getContentSearchResultUnit($iId);
  70. return $sResults;
  71. }
  72. protected function &prepareForm()
  73. {
  74. if(!empty($this->_oForm) && $this->_oForm instanceof BxDolForm)
  75. return $this->_oForm;
  76. $sForm = 'sys_search_extended_' . $this->_sObject;
  77. $sFormSubmit = 'search' . $this->_sObject;
  78. $aForm = array(
  79. 'form_attrs' => array(
  80. 'id' => $sForm,
  81. 'name' => $sForm,
  82. 'action' => '',
  83. 'method' => 'post'
  84. ),
  85. 'params' => array(
  86. 'db' => array(
  87. 'table' => '',
  88. 'key' => '',
  89. 'uri' => '',
  90. 'uri_title' => '',
  91. 'submit_name' => $sFormSubmit
  92. ),
  93. ),
  94. 'inputs' => array()
  95. );
  96. foreach ($this->_aObject['fields'] as $aField) {
  97. if((int)$aField['active'] == 0)
  98. continue;
  99. if(in_array($aField['search_type'], array('checkbox_set', 'select_multiple')) && isset($aField['values']['']))
  100. unset($aField['values']['']);
  101. $aForm['inputs'][$aField['name']] = array(
  102. 'type' => $aField['search_type'],
  103. 'name' => $aField['name'],
  104. 'caption' => _t($aField['caption']),
  105. 'values' => $aField['values'],
  106. 'value' => $aField['search_value'],
  107. 'db' => array(
  108. 'pass' => 'Xss'
  109. )
  110. );
  111. }
  112. $aForm['inputs']['search'] = array(
  113. 'type' => 'submit',
  114. 'name' => $sFormSubmit,
  115. 'value' => _t('_Search')
  116. );
  117. $sClass = 'BxTemplSearchExtendedForm';
  118. if(!empty($this->_sFormClassName)) {
  119. $sClass = $this->_sFormClassName;
  120. if(!empty($this->_sFormClassPath))
  121. require_once(BX_DIRECTORY_PATH_ROOT . $this->_sFormClassPath);
  122. }
  123. $this->_oForm = new $sClass($aForm, $this->_oTemplate);
  124. $this->_oForm->initChecker();
  125. return $this->_oForm;
  126. }
  127. }
  128. /** @} */