Frontend\Modules\Search\Engine\Model::search PHP Method

    public static function search($term, $limit = 20, $offset = 0)
    {
        // revalidate searches
        if (FrontendModel::get('fork.settings')->get('Search', 'validate_search', true) == true) {
            self::validateSearch();
        }
        // @note: on heavy sites with a lot of inactive search indices better
        // use a cronjob (which will automatically set this module setting to N)
        // execute the actual search
        $searchResults = self::execSearch($term, $limit, $offset);
        // get the total amount of results (we'll get back to this later ;) )
        $total = count($searchResults);
        // none found? return empty :(
        if (!$searchResults) {
            return array();
        }
        // prepare to send to modules
        $moduleResults = array();
        // loop the result set
        foreach ($searchResults as $searchResult) {
            $moduleResults[$searchResult['module']][] = $searchResult['other_id'];
        }
        // pass the results to the modules
        foreach ($moduleResults as $module => $otherIds) {
            // check if this module actually is prepared to handle searches
            $class = 'Frontend\\Modules\\' . $module . '\\Engine\\Model';
            if (is_callable(array($class, 'search'))) {
                // get the required info from our module
                $moduleResults[$module] = call_user_func(array($class, 'search'), $otherIds);
            } else {
                // does not exist, let's get this module out of here
                unset($moduleResults[$module]);
            }
        }
        // now place the prepared data back in our original result set, which has our results in correct order
        foreach ($searchResults as $i => $result) {
            // loop parsed results for this specific module to find the one we want here
            foreach ($moduleResults[$result['module']] as $otherId => $moduleResult) {
                // that's the one..
                if ($otherId == $result['other_id']) {
                    $searchResults[$i] = array_merge(array('module' => $result['module']), $moduleResult);
                    continue 2;
                }
            }
            // if we made it here, we obviously did not get this result parsed by the module, so remove it!
            unset($searchResults[$i]);
            self::statusIndex($result['module'], (array) $result['other_id'], false);
        }
        // results got removed by the module? oh noes :o have another run,
        // because now we've deactivated those responsible for the holes :)
        if (count($searchResults) < $total && $total == $limit) {
            $searchResults = self::search($term, $limit, $offset);
        }
        // return results
        return $searchResults;
    }

Usage Example

Example #1
0
 /**
  * Load the data
  */
 private function getRealData()
 {
     // no search term = no search
     if (!$this->term) {
         return;
     }
     // set url
     $this->pagination['url'] = FrontendNavigation::getURLForBlock('Search') . '?form=search&q=' . $this->term;
     // populate calculated fields in pagination
     $this->pagination['limit'] = $this->limit;
     $this->pagination['offset'] = $this->offset;
     $this->pagination['requested_page'] = $this->requestedPage;
     // get items
     $this->items = FrontendSearchModel::search($this->term, $this->pagination['limit'], $this->pagination['offset']);
     // populate count fields in pagination
     // this is done after actual search because some items might be
     // activated/deactivated (getTotal only does rough checking)
     $this->pagination['num_items'] = FrontendSearchModel::getTotal($this->term);
     $this->pagination['num_pages'] = (int) ceil($this->pagination['num_items'] / $this->pagination['limit']);
     // num pages is always equal to at least 1
     if ($this->pagination['num_pages'] == 0) {
         $this->pagination['num_pages'] = 1;
     }
     // redirect if the request page doesn't exist
     if ($this->requestedPage > $this->pagination['num_pages'] || $this->requestedPage < 1) {
         $this->redirect(FrontendNavigation::getURL(404));
     }
     // debug mode = no cache
     if (!$this->getContainer()->getParameter('kernel.debug')) {
         // set cache content
         $filesystem = new Filesystem();
         $filesystem->dumpFile($this->cacheFile, "<?php\n" . '$pagination = ' . var_export($this->pagination, true) . ";\n" . '$items = ' . var_export($this->items, true) . ";\n?>");
     }
 }