Note: please be aware that this is an approximate amount. It IS possible
that this is not the exact amount of search results,
since search results may vary in time (entries may not yet/no longer be
shown) and we will not rebuild the entire search index
on every search (would be a great performance killer and huge scalability
loss)
This function can be called with either a string as parameter (simple
search) or an array (advanced search)
Simple search: all search index fields will be searched for the given
term
Advanced search: only the given fields (keys in the array) will be
matched to the corresponding values (corresponding values in the array)
/** * Execute the action */ public function execute() { parent::execute(); // get parameters $charset = $this->getContainer()->getParameter('kernel.charset'); $searchTerm = \SpoonFilter::getPostValue('term', null, ''); $term = $charset == 'utf-8' ? \SpoonFilter::htmlspecialchars($searchTerm) : \SpoonFilter::htmlentities($searchTerm); // validate search term if ($term == '') { $this->output(self::BAD_REQUEST, null, 'term-parameter is missing.'); } else { // previous search result $previousTerm = \SpoonSession::exists('searchTerm') ? \SpoonSession::get('searchTerm') : ''; \SpoonSession::set('searchTerm', ''); // save this term? if ($previousTerm != $term) { // format data $this->statistics = array(); $this->statistics['term'] = $term; $this->statistics['language'] = LANGUAGE; $this->statistics['time'] = FrontendModel::getUTCDate(); $this->statistics['data'] = serialize(array('server' => $_SERVER)); $this->statistics['num_results'] = FrontendSearchModel::getTotal($term); // save data FrontendSearchModel::save($this->statistics); } // save current search term in cookie \SpoonSession::set('searchTerm', $term); // output $this->output(self::OK); } }
/** * 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?>"); } }