コード例 #1
0
ファイル: search.php プロジェクト: ranrolls/ras-full-portal
 protected function search()
 {
     $this->_request = SPRequest::search('field_');
     $this->_request['search_for'] = str_replace('*', '%', SPRequest::string('sp_search_for', null));
     $this->_request['phrase'] = SPRequest::string('spsearchphrase', Sobi::Cfg('search.form_searchphrase_def', 'all'));
     $this->_request['phrase'] = strlen($this->_request['phrase']) ? $this->_request['phrase'] : Sobi::Cfg('search.form_searchphrase_def', 'all');
     $ssid = SPRequest::cmd('ssid', SPRequest::cmd('ssid', null, 'cookie'));
     $this->_fields = $this->loadFields();
     $searchForString = false;
     Sobi::Trigger('OnRequest', 'Search', array(&$this->_request));
     $searchLimit = Sobi::Cfg('search.result_limit', 1000);
     for ($i = 1; $i < 11; $i++) {
         $this->_resultsByPriority[$i] = array();
     }
     // if the visitor wasn't on the search page first
     if (!$ssid || SPRequest::int('reset', 0)) {
         $this->session($ssid);
     }
     /* clean request */
     if (count($this->_request)) {
         foreach ($this->_request as $i => $v) {
             if (is_array($v)) {
                 foreach ($v as $index => $value) {
                     $v[$index] = htmlspecialchars_decode($value, ENT_QUOTES);
                 }
                 $this->_request[$i] = SPRequest::cleanArray($v, true);
             } else {
                 $this->_request[$i] = $this->_db->escape($v);
             }
         }
     }
     /* sort fields by priority */
     usort($this->_fields, array('self', 'sortByPrio'));
     /* First the basic search ..... */
     /* if we have a string to search */
     if (strlen($this->_request['search_for']) && $this->_request['search_for'] != Sobi::Txt('SH.SEARCH_FOR_BOX')) {
         $searchForString = true;
         $this->_narrowing = true;
         switch ($this->_request['phrase']) {
             case 'exact':
                 $this->searchPhrase();
                 break;
             default:
             case 'all':
             case 'any':
                 $this->searchWords($this->_request['phrase'] == 'all');
                 break;
         }
         $this->_results = array_unique($this->_results);
     }
     Sobi::Trigger('AfterBasic', 'Search', array(&$this->_results, &$this->_resultsByPriority));
     /* ... now the extended search. Check which data we've received */
     if (count($this->_fields)) {
         $results = null;
         foreach ($this->_fields as $field) {
             if (isset($this->_request[$field->get('nid')]) && $this->_request[$field->get('nid')] != null) {
                 $this->_narrowing = true;
                 $fr = $field->searchData($this->_request[$field->get('nid')], Sobi::Section());
                 $priority = $field->get('priority');
                 if (is_array($fr)) {
                     $this->_resultsByPriority[$priority] = array_merge($this->_resultsByPriority[$priority], $fr);
                 }
                 /* if we didn't got any results before this array contains the results */
                 if (!is_array($results)) {
                     $results = $fr;
                 } else {
                     if (is_array($fr)) {
                         $results = array_intersect($results, $fr);
                     }
                 }
             }
         }
         /** Tue, Oct 21, 2014 10:18:37
          * No result is also a result so no "count"
          * */
         //			if ( is_array( $results ) && count( $results ) ) {
         if (is_array($results)) {
             /* if we had also a string to search we have to get the intersection */
             if ($searchForString) {
                 $this->_results = array_intersect($this->_results, $results);
             } else {
                 $this->_results = $results;
             }
         }
     }
     $this->verify();
     /** @since 1.1 - a method to narrow the search results down */
     if (count($this->_fields)) {
         // If we have any results already - the we are limiting results down
         // if we don't have results but we were already searching then skip - because there is nothing to narrow down
         // if we don't have results but we weren't searching for anything else - then we are narrowing down everything
         if (count($this->_results) || !$this->_narrowing) {
             foreach ($this->_fields as &$field) {
                 $request = isset($this->_request[$field->get('nid')]) ? $this->_request[$field->get('nid')] : null;
                 if ($request) {
                     $field->searchNarrowResults($request, $this->_results, $this->_resultsByPriority);
                 }
             }
         }
     }
     $this->_request['search_for'] = str_replace('%', '*', $this->_request['search_for']);
     if (count($this->_results) > $searchLimit) {
         SPFactory::message()->error(Sobi::Txt('SH.SEARCH_TOO_MANY_RESULTS', count($this->_results), $searchLimit), false);
         $this->_resultsByPriority = array();
         $this->_results = array_slice($this->_results, 0, $searchLimit);
     } else {
         $this->sortPriority();
     }
     Sobi::Trigger('AfterExtended', 'Search', array(&$this->_results, &$this->_resultsByPriority));
     $req = is_array($this->_request) && count($this->_request) ? SPConfig::serialize($this->_request) : null;
     $res = is_array($this->_results) && count($this->_results) ? implode(', ', $this->_results) : null;
     $cre = is_array($this->_categoriesResults) && count($this->_categoriesResults) ? implode(', ', $this->_categoriesResults) : null;
     /* determine the search parameters */
     $attr = array('entriesResults' => array('results' => $res, 'resultsByPriority' => $this->_resultsByPriority), 'catsResults' => $cre, 'uid' => Sobi::My('id'), 'browserData' => SPConfig::serialize(SPBrowser::getInstance()));
     if (strlen($req)) {
         $attr['requestData'] = $req;
     }
     /* finally save */
     try {
         Sobi::Trigger('OnSave', 'Search', array(&$attr, &$ssid));
         $this->_db->update('spdb_search', $attr, array('ssid' => $ssid));
     } catch (SPException $x) {
         Sobi::Error($this->name(), SPLang::e('CANNOT_CREATE_SESSION_DB_ERR', $x->getMessage()), SPC::ERROR, 500, __LINE__, __FILE__);
     }
     $url = array('task' => 'search.results', 'sid' => Sobi::Section());
     // For Peter's Components Anywhere extension and other
     $params = Sobi::Cfg('search.params_to_pass');
     if (count($params)) {
         foreach ($params as $param) {
             $val = SPRequest::raw($param);
             if ($val) {
                 $url[$param] = SPRequest::raw($param);
             }
         }
     }
     /* if we cannot transfer the search id in cookie */
     if (!SPRequest::cmd('ssid', null, 'cookie')) {
         $url['ssid'] = $ssid;
     }
     if (Sobi::Cfg('cache.unique_search_url')) {
         $url['t'] = microtime(true);
     }
     Sobi::Redirect(Sobi::Url($url));
 }