| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup UnaBaseView UNA Base Representation Classes
- * @{
- */
- /**
- * Extended Search objects representation.
- *
- * @see BxDolSearchExtended
- */
- class BxBaseSearchExtended extends BxDolSearchExtended
- {
- protected $_sFormClassName;
- protected $_sFormClassPath;
- protected $_oForm;
- protected $_oTemplate;
- protected $_bJsMode;
- protected $_iAgeMin;
- protected $_iAgeMax;
- public function __construct ($aObject, $oTemplate = null)
- {
- parent::__construct ($aObject);
- if ($oTemplate)
- $this->_oTemplate = $oTemplate;
- else if(!empty($this->_aObject['module']))
- $this->_oTemplate = BxDolModule::getInstance($this->_aObject['module'])->_oTemplate;
- else
- $this->_oTemplate = BxDolTemplate::getInstance();
- $this->_sFormClassName = '';
- $this->_sFormClassPath = '';
- $this->_oForm = null;
- $this->_bJsMode = false;
- $this->_iAgeMin = 1;
- $this->_iAgeMax = 75;
- }
- public function getForm($aParams = array())
- {
- if(!$this->isEnabled())
- return '';
- $oForm = $this->prepareForm($aParams);
- return bx_is_api() ? bx_api_get_block('form', $oForm->getCodeAPI(), ['id' => 2, 'ext' => ['name' => $this->_aObject['module'] . '_serach', 'request' => ['url' => '/api.php?r=system/get_form/TemplSearchExtendedServices¶ms[]=' . $this->_aObject['module'], 'immutable' => true]]]) : $oForm->getCode();
- }
-
- /**
- * Get search results from search form or from custom condition
- *
- * @param $aParams['cond'] custom condition to pass instead of form submission,
- * conditions are key&value pair, where 'key' is form input name and
- * 'value' is the term to search for
- * @param $aParams['start'] position of first record to display
- * @param $aParams['per_page'] number of items per page
- * @param $aParams['template'] custom unit templates to use
- * @return HTML string with search results
- */
- public function getResults($aParams = array())
- {
- if(!$this->isEnabled())
- return '';
- $bJsMode = isset($aParams['js_mode']) ? (bool)$aParams['js_mode'] : $this->_bJsMode;
- $bCondition = !empty($aParams['cond']) && is_array($aParams['cond']);
- $iStart = !empty($aParams['start']) ? $aParams['start'] : 0;
- $iPerPage = !empty($aParams['per_page']) ? $aParams['per_page'] : 0;
- unset($aParams['start'], $aParams['per_page']);
- $sUnitTemplate = !empty($aParams['template']) ? $aParams['template'] : '';
- $oForm = $this->prepareForm($aParams);
- if($bCondition) {
- $mixedSubmitName = $oForm->aParams['db']['submit_name'];
- if(is_array($mixedSubmitName))
- $mixedSubmitName = array_pop($mixedSubmitName);
- $aParams['cond'][$mixedSubmitName] = 1;
- $oForm->aFormAttrs['method'] = BX_DOL_FORM_METHOD_SPECIFIC;
- $oForm->aParams['csrf']['disable'] = true;
- $oForm->initChecker(array(), $aParams['cond']);
- }
- if(!$oForm->isSubmittedAndValid() && !$this->_bFilterMode && (!$this->_bIsApi || !isset($aParams['search_params'])))
- return '';
- $oContentInfo = BxDolContentInfo::getObjectInstance($this->_aObject['object_content_info']);
-
- if(!$oContentInfo)
- return '';
- $aParamsSearch = [];
- if(empty($aParams['search_params'])) {
- foreach($this->_aObject['fields'] as $aField) {
- $mixedValue = $oForm->getCleanValue($aField['name']);
- if(empty($mixedValue) || (is_array($mixedValue) && bx_is_empty_array($mixedValue)))
- continue;
- $aParamsSearch[$aField['name']] = array(
- 'type' => $aField['search_type'],
- 'value' => $mixedValue,
- 'operator' => $aField['search_operator']
- );
- if(!$bCondition) {
- if(!isset($aParams['cond']))
- $aParams['cond'] = array();
- switch($oForm->aInputs[$aField['name']]['type']) {
- case 'location':
- case 'location_radius':
- $aParams['cond'][$aField['name']] = $mixedValue['string'];
- $aLocationComponents = BxDolMetatags::locationsParseComponents($mixedValue['array'], $aField['name']);
- if($oForm->aInputs[$aField['name']]['type'] == 'location_radius' && count($mixedValue['array']) > count($aLocationComponents))
- $aLocationComponents[$aField['name'] . '_rad'] = array_pop($mixedValue['array']);
- $aParams['cond'] = array_merge($aParams['cond'], $aLocationComponents);
- break;
- default:
- $aParams['cond'][$aField['name']] = $mixedValue;
- }
- }
- }
- }
- else
- $aParamsSearch = $aParams['search_params'];
- if((empty($aParamsSearch) || !is_array($aParamsSearch)) && !$this->_bFilterMode)
- return $this->_bIsApi ? bx_api_get_msg(_t('Not Found'), ['ext' => ['msg_type' => 'result']]) : '';
- if (!$iPerPage) {
- bx_import('BxDolSearch');
- $iPerPage = BX_DOL_SEARCH_RESULTS_PER_PAGE_DEFAULT;
- }
- $aResults = false;
-
- if (bx_get('sort')){
- $aTmp = explode(':', bx_get('sort'));
- $aParamsSearch['order'] = [['field' => $aTmp[0], 'direction' => $aTmp[1]]];
- }
-
- /**
- * @hooks
- * @hookdef hook-search-get_data 'search', 'get_data' - hook in get data for serach
- * - $unit_name - equals `search`
- * - $action - equals `get_data`
- * - $object_id - not used
- * - $sender_id - not used
- * - $extra_params - array of additional params with the following array keys:
- * - `object` - [array] by ref, object info for current search, can be overridden in hook processing
- * - `search_params` - [string] by ref, search params, can be overridden in hook processing
- * - `search_results` - [string] by ref, html for block, can be overridden in hook processing
- * @hook @ref hook-search-get_data
- */
- bx_alert('search', 'get_data', 0, false, array('object' => $this->_aObject, 'search_params' => &$aParamsSearch, 'search_results' => &$aResults));
- if($aResults === false)
- $aResults = $oContentInfo->getSearchResultExtended($aParamsSearch, $iStart, $iPerPage + 1, $this->_bFilterMode);
- if((empty($aResults) || !is_array($aResults)) && !$this->_bIsApi)
- return _t('Nothing found');
- if(!empty($aParams['cond']) && is_array($aParams['cond']))
- $aParams['cond'] = self::encodeConditions($aParams['cond']);
- $aPaginate = ['start' => $iStart, 'per_page' => $iPerPage];
- if(!empty($aParams['total'])) {
- if(is_numeric($aParams['total']))
- $aPaginate['total'] = (int)$aParams['total'];
- else
- $aPaginate['total'] = count($oContentInfo->getSearchResultExtended($aParamsSearch, 0, 0, $this->_bFilterMode));
- }
- if(!$bJsMode) {
- $aParams['start'] = '{start}';
- $aParams['per_page'] = '{per_page}';
- list($sPageLink, $aPageParams) = bx_get_base_url_inline($aParams);
- if(!$this->_bIsApi)
- $aPaginate['page_url'] = BxDolPermalinks::getInstance()->permalink(bx_append_url_params($sPageLink, $aPageParams, true, ['{start}', '{per_page}']));
- }
- else
- $aPaginate['on_change_page'] = "return !loadDynamicBlockAutoPaginate(this, '{start}', '{per_page}', " . bx_js_string(json_encode($aParams)) . ");";
- $oPaginate = new BxTemplPaginate($aPaginate);
- $oPaginate->setNumFromDataArray($aResults);
- $bTmplVarsPaginate = $iStart || $oPaginate->getNum() > $iPerPage;
- $aTmplVarsPaginate = $bTmplVarsPaginate ? array('paginate' => $oPaginate->getSimplePaginate()) : array();
- $mixedResults = $this->_bIsApi ? [] : '';
- foreach($aResults as $iId) {
- $mixedResult = $oContentInfo->getContentSearchResultUnit($iId, $sUnitTemplate);
- if($this->_bIsApi)
- $mixedResults[] = $mixedResult;
- else
- $mixedResults .= $mixedResult;
- }
- if($this->_bIsApi) {
- if(isset($aParams['search_params']))
- return [$this->getResultsAPI($mixedResults, $iPerPage, $iStart, $aParamsSearch)];
- else
- return [$this->getResultsAPI($mixedResults, $iPerPage, $iStart, $aParamsSearch)];
- }
- return $this->_oTemplate->parseHtmlByName('search_extended_results.html', [
- 'class' => str_replace('_', '-', $this->_sObject),
- 'sort' => $mixedResults != '' ? $this->getSorting($aParams) : '',
- 'code' => $mixedResults,
- 'bx_if:show_paginate' => [
- 'condition' => $bTmplVarsPaginate,
- 'content' => $aTmplVarsPaginate
- ]
- ]);
- }
- public function getResultsAPI($aData, $iPerPage, $iStart, $aParamsSearch = '')
- {
- return bx_api_get_block('browse', [
- 'nocache' => true,
- 'module' => $this->_aObject['module'],
- 'unit' => 'general-content-list',
- 'request_url' => '/api.php?r=system/get_results/TemplSearchExtendedServices¶ms[]=',
- 'data' => defined('BX_API_PAGE') ? [] : $aData,
- 'params' => ['per_page' => $iPerPage, 'start' => $iStart, 'object' => $this->_aObject['module'], 'search_params' => $aParamsSearch],
- ]);
- }
- public function getSorting($aParams = [])
- {
- if(!$this->isEnabled())
- return '';
- $aTxtDirection = [
- 'asc' => _t('_order_asc'),
- 'desc' => _t('_order_desc')
- ];
- $bJsMode = isset($aParams['js_mode']) ? (bool)$aParams['js_mode'] : $this->_bJsMode;
- $aValues = [];
- foreach($this->_aObject['sortable_fields'] as $aField) {
- if($aField['active'] == 0)
- continue;
- $sLangKey = $aField['caption'] . '_' . $aField['direction'];
- $sLangVal = _t($sLangKey);
- $aValues[] = [
- 'key' => $aField['name'] . ':' . $aField['direction'],
- 'value' => strcmp($sLangKey, $sLangVal) != 0 ? $sLangVal : _t($aField['caption']) . ' ' . $aTxtDirection[$aField['direction']]
- ];
- }
- if(!empty($aValues))
- $aValues = array_merge([['key' => '', 'value' => _t('_sys_txt_search_sort_by_default')]], $aValues);
- if($this->_bIsApi)
- return $this->getSortingAPI($aValues);
- $oForm = new BxTemplFormView([]);
- $sOnChange = '';
- if(!$bJsMode) {
- unset($aParams['start'], $aParams['per_page']);
- list($sPageLink, $aPageParams) = bx_get_base_url_inline($aParams);
- $sOnChange = "bx_search_extnded_sort(this,'" . BxDolPermalinks::getInstance()->permalink(bx_append_url_params($sPageLink, $aPageParams)) . "')";
- }
- else
- $sOnChange = "return !loadDynamicBlockAutoSort(this, $(this).val()," . bx_js_string(json_encode($aParams)) . ");";
- $sSort = '';
- if(!empty($aValues)) {
- $aInputSort = [
- 'type' => 'select',
- 'name' => 'sort',
- 'value' => bx_get('sort') ? bx_get('sort') : '',
- 'values' => $aValues,
- 'caption' => _t('_sys_txt_search_sort_by'),
- 'attrs' => [
- 'onChange' => $sOnChange,
- ],
- 'tr_attrs' => [
- 'class' => 'sort'
- ]
- ];
- $sSort = $oForm->genRow($aInputSort);
- }
- return $sSort;
- }
- public function getSortingAPI($aData)
- {
- return bx_api_get_block('sorting', [
- 'nocache' => true,
- 'module' => $this->_aObject['module'],
- 'data' => $aData
- ]);
- }
- protected function &prepareForm($aParams = array())
- {
- if(!empty($this->_oForm) && $this->_oForm instanceof BxDolForm)
- return $this->_oForm;
- $sForm = 'sys_search_extended_' . $this->_sObject;
- $sFormSubmit = 'search' . $this->_sObject;
- list($sPageLink, $aPageParams) = bx_get_base_url_inline();
-
- $mDefValues = bx_get('filters');
-
- if ($mDefValues){
- $mDefValues = json_decode($mDefValues, true);
- }
- $aForm = array(
- 'form_attrs' => array(
- 'id' => $sForm,
- 'name' => $sForm,
- 'action' => !empty($aPageParams['i']) ? BxDolPermalinks::getInstance()->permalink(bx_append_url_params($sPageLink, array('i' => $aPageParams['i']))) : $sPageLink,
- 'method' => 'post'
- ),
- 'params' => array(
- 'db' => array(
- 'table' => '',
- 'key' => '',
- 'uri' => '',
- 'uri_title' => '',
- 'submit_name' => $sFormSubmit
- ),
- 'checker_helper' => 'BxBaseSearchExtendedFormCheckerHelper',
- ),
- 'inputs' => array()
- );
- foreach ($this->_aObject['fields'] as $aField) {
- if((int)$aField['active'] == 0)
- continue;
- if(in_array($aField['search_type'], array('checkbox_set', 'select_multiple'))) {
- if(isset($aField['values']['']))
- unset($aField['values']['']);
- if(isset($aField['values'][0]) && !is_array($aField['values'][0]))
- unset($aField['values'][0]);
- }
- $aAttrs = array();
- if(in_array($aField['search_type'], array('datepicker_range_age')) && !empty($aField['search_value'])) {
- $aFieldParams = BxDolService::callSerialized($aField['search_value']);
- $iMin = isset($aFieldParams['min']) && is_numeric($aFieldParams['min']) ? $aFieldParams['min'] : $this->_iAgeMin;
- $iMax = isset($aFieldParams['max']) && is_numeric($aFieldParams['max']) ? $aFieldParams['max'] : $this->_iAgeMax;
- $aField['search_value'] = $iMin . '-' . $iMax;
- $aAttrs = array('min' => $iMin, 'max' => $iMax, 'step' => 1);
- }
- if(in_array($aField['search_type'], array('datepicker_range')) && !empty($aField['search_value'])) {
- $aField['search_value'] = '';
- }
- $aForm['inputs'][$aField['name']] = array(
- 'type' => $aField['search_type'],
- 'name' => $aField['name'],
- 'caption' => _t($aField['caption']),
- 'info' => _t($aField['info']),
- 'values' => $aField['values'],
- 'value' => $aField['search_value'] == '' && isset($mDefValues) && isset($mDefValues[$aField['name']]) ? $mDefValues[$aField['name']] : $aField['search_value'],
- 'attrs' => $aAttrs,
- 'db' => array(
- 'pass' => 'datepicker_range_age' === $aField['search_type'] ? 'AgeRange' : (!empty($aField['pass']) ? $aField['pass'] : 'Xss')
- )
- );
- if(in_array($aField['search_type'], array('location', 'location_radius')))
- $aForm['inputs'][$aField['name']]['manual_input'] = true;
- }
- $aForm['inputs']['search'] = array(
- 'type' => 'submit',
- 'name' => $sFormSubmit,
- 'value' => _t($this->_bFilterMode ? '_Apply' : '_Search')
- );
- $sClass = 'BxTemplSearchExtendedForm';
- if(!empty($this->_sFormClassName)) {
- $sClass = $this->_sFormClassName;
- if(!empty($this->_sFormClassPath))
- require_once(BX_DIRECTORY_PATH_ROOT . $this->_sFormClassPath);
- }
- $bJsMode = isset($aParams['js_mode']) ? (bool)$aParams['js_mode'] : $this->_bJsMode;
- $bCondition = !empty($aParams['cond']) && is_array($aParams['cond']);
- $aValues = !$bJsMode && $bCondition ? $aParams['cond'] : array();
- $this->_oForm = new $sClass($aForm, $this->_oTemplate);
- $this->_oForm->initChecker($aValues, $aValues);
- return $this->_oForm;
- }
- }
- class BxBaseSearchExtendedFormCheckerHelper extends BxDolFormCheckerHelper
- {
- static public function passAgeRange ($s)
- {
- if (is_array($s)) {
- $a = array ();
- foreach ($s as $k => $v)
- $a[$k] = self::_passAgeRange ($v);
- return $a;
- }
- return self::_passAgeRange ($s);
- }
- static public function _passAgeRange ($s)
- {
- $a = explode ('-', $s);
- if (!$a || count($a) != 2)
- return $s;
- $i0 = new \DateInterval("P".(int)$a[0]."Y");
- $i1 = new \DateInterval("P".(int)$a[1]."Y");
- return [
- (new DateTime("now", new DateTimeZone('UTC')))->sub($i1)->format("Y-m-d"),
- (new DateTime("now", new DateTimeZone('UTC')))->sub($i0)->format("Y-m-d"),
- ];
- }
- }
- /** @} */
|