1
0

BxBaseSearch.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
  10. class BxBaseSearch extends BxDolSearch
  11. {
  12. protected $_oTemplate;
  13. protected $_sIdForm = 'sys_search_form';
  14. protected $_sIdResults = 'sys_search_results';
  15. protected $_sIdLoading;
  16. protected $_sSuffixLiveSearch = '_quick';
  17. protected $_iSearchLenth;
  18. protected $_sSearchFunctionParams;
  19. public function __construct($aChoice, $oTemplate)
  20. {
  21. if (!is_array($aChoice) && $aChoice != '')
  22. $aChoice = array($aChoice);
  23. parent::__construct($aChoice);
  24. $this->_oTemplate = $oTemplate ? $oTemplate : BxDolTemplate::getInstance();
  25. $this->_sIdLoading = $this->_sIdForm;
  26. $this->_iSearchLenth = (int)getParam('sys_live_search_limit');
  27. $this->_sSearchFunctionParams = "%d, '%s', '%s', '%s', %d, %d";
  28. }
  29. public function setLiveSearch($bLiveSearch)
  30. {
  31. parent::setLiveSearch($bLiveSearch);
  32. if($this->_bLiveSearch) {
  33. $this->_sIdForm .= $this->_sSuffixLiveSearch;
  34. $this->_sIdResults .= $this->_sSuffixLiveSearch;
  35. $this->_sIdLoading = $this->_sIdResults;
  36. }
  37. }
  38. public function getForm($iDesignBoxTemplate = BX_DB_PADDING_DEF, $sTitle = false, $bOnlyForm = false)
  39. {
  40. if ($this->_sMetaType || $this->_sCategoryObject)
  41. return '';
  42. if (false === $sTitle)
  43. $sTitle = _t( "_Search");
  44. $aSection = '';
  45. if (bx_get('type'))
  46. $aSection = bx_process_input(bx_get('type'));
  47. else if (bx_get('section'))
  48. $aSection = bx_process_input(bx_get('section'));
  49. $aForm = $this->_getForm([
  50. 'section' => $aSection,
  51. 'keyword' => bx_get('keyword') ? bx_process_input(bx_get('keyword')) : ''
  52. ]);
  53. $oForm = new BxTemplFormView($aForm);
  54. $sForm = $oForm->getCode();
  55. if (!$this->_bLiveSearch) {
  56. $o = new BxTemplPaginate(array());
  57. $o->addCssJs();
  58. }
  59. if ($bOnlyForm)
  60. return $sForm;
  61. return $this->_oTemplate->parseHtmlByName('designbox_container.html', array(
  62. 'class_add' => '',
  63. 'bx_if:show_html_id' => array(
  64. 'condition' => false,
  65. 'content' => array(),
  66. ),
  67. 'content' => DesignBoxContent($sTitle, $sForm, $iDesignBoxTemplate)
  68. ));
  69. }
  70. public function getResultsContainer($sCode = '')
  71. {
  72. return $this->_oTemplate->parseHtmlByName('search_result.html', [
  73. 'html_id' => $this->_sIdResults,
  74. 'class' => str_replace('_', '-', $this->_sIdResults),
  75. 'content' => $sCode
  76. ]);
  77. }
  78. protected function _getForm($aValues = array())
  79. {
  80. $aForm = array(
  81. 'form_attrs' => array(
  82. 'id' => $this->_sIdForm,
  83. 'action' => BX_DOL_URL_ROOT . 'searchKeyword.php',
  84. 'method' => 'post',
  85. ),
  86. 'params' => array(
  87. 'csrf' => array(
  88. 'disable' => true,
  89. ),
  90. ),
  91. 'inputs' => array(
  92. 'live_search' => array(
  93. 'type' => 'hidden',
  94. 'name' => 'live_search',
  95. 'value' => $this->_bLiveSearch ? 1 : 0,
  96. ),
  97. 'section' => array(
  98. 'type' => 'select',
  99. 'name' => 'section',
  100. 'caption' => _t('_Section'),
  101. 'values' => array_merge(array('' => _t('_search_in_all_section')), $this->getKeyTitlesPairs()),
  102. 'value' => $aValues['section'],
  103. ),
  104. 'keyword' => array(
  105. 'type' => 'text',
  106. 'name' => 'keyword',
  107. 'caption' => _t('_Keyword'),
  108. 'value' => $aValues['keyword'],
  109. ),
  110. 'search' => array(
  111. 'type' => 'submit',
  112. 'name' => 'search',
  113. 'value' => _t('_Search'),
  114. )
  115. )
  116. );
  117. if ($this->_bLiveSearch) {
  118. $sJsParams = sprintf($this->_sSearchFunctionParams, $this->_iSearchLenth, '#' . $this->_sIdForm, '#' . $this->_sIdResults, '#' . $this->_sIdLoading, $this->_bLiveSearch, (int)getParam('sys_search_keyword_min_len'));
  119. unset($aForm['inputs']['section']);
  120. unset($aForm['inputs']['search']);
  121. unset($aForm['inputs']['keyword']['caption']);
  122. $aForm['inputs']['keyword']['attrs']['placeholder'] = _t('_sys_search_placeholder');
  123. $aForm['inputs']['keyword']['attrs']['onkeydown'] = "return bx_search_on_type(event, $sJsParams);";
  124. $aForm['inputs']['keyword']['attrs']['onpaste'] = $aForm['inputs']['keyword']['attrs']['onkeydown'];
  125. }
  126. /**
  127. * @hooks
  128. * @hookdef hook-search-get_form 'search', 'get_form' - hook in create search form
  129. * - $unit_name - equals `search`
  130. * - $action - equals `get_form`
  131. * - $object_id - not used
  132. * - $sender_id - not used
  133. * - $extra_params - array of additional params with the following array keys:
  134. * - `object` - [object] current search object
  135. * - `override_result` - [array] by ref, form arrray defenition, can be overridden in hook processing
  136. * @hook @ref hook-search-get_form
  137. */
  138. bx_alert('search', 'get_form', 0, 0, array('object' => $this, 'override_result' => &$aForm));
  139. return $aForm;
  140. }
  141. protected function getKeyTitlesPairs ()
  142. {
  143. $a = array();
  144. foreach ($this->aClasses as $sKey => $r)
  145. if ($this->_sMetaType || $r['GlobalSearch'])
  146. $a[$sKey] = _t($r['title']);
  147. return $a;
  148. }
  149. }
  150. /** @} */