public function executeSearch() { $feed = new sfAtom1Feed(); $feed->setTitle('Hoydaa Snippets'); $feed->setLink('http://codesnippet.hoydaa.org'); $feed->setAuthorEmail('*****@*****.**'); $feed->setAuthorName('Hoydaa Snippets'); $querystring = $this->getRequestParameter('q'); $query = new sfLuceneCriteria($this->getLuceneInstance()); $query->addDescendingSortBy('createdat'); $query->addSane($querystring); $pager = new sfLucenePager($this->getLuceneInstance()->friendlyFind($query)); $num = $pager->getNbResults(); if ($num > 0) { $pager->setMaxPerPage(10); $pager->setPage(1); foreach ($pager->getResults() as $result) { $item = new sfFeedItem(); $item->setTitle($result->getTitle()); $item->setLink('snippet/show?id=' . $result->getId()); $item->setAuthorName($result->getContributor()); $item->setPubDate(strtotime($result->getCreatedAt())); //$item->setAuthorEmail(($code->getSfGuardUser() ? // $code->getSfGuardUser()->getProfile()->getEmail() : $code->getEmail())); $item->setUniqueId($result->getId()); $item->setDescription($result->getSummary()); $feed->addItem($item); } } $this->feed = $feed; $this->setTemplate('feed'); }
/** * Executes the search action. If there is a search query present in the request * parameters, then a search is executed and uses a paged result. If not, then * the search box is displayed to prompt the user to enter controls. */ public function executeSearch($request) { // determine if the user pressed the "Advanced" button if ($request->getParameter('commit') == $this->translate('Advanced')) { // user did, so redirect to advanced search $this->redirect($this->getModuleName() . '/advanced'); } $form = new sfLuceneSimpleForm(); $this->configureCategories($form); $form->bind($request->getParameter($form->getName(), array())); $this->form = $form; if (!$form->isValid()) { // display search controls $this->setTitleI18n('Search'); $this->setTemplate('searchControls'); } $values = $form->getValues(); if (count($values) == 0) { // display error $this->setTitleI18n('No Results Found'); $this->setTemplate('searchNoResults'); return sfView::SUCCESS; } // build the criteria $query = new sfLuceneCriteria(); $query->addSane($values['query']); if (sfConfig::get('app_lucene_categories', true) && isset($values['category']) && $values['category'] != $this->translate('All')) { $query->add('sfl_category: ' . $values['category']); } $pager = new sfLucenePager($this->getLuceneInstance()->friendlyFind($query)); // were any results returned? if ($pager->getNbResults() == 0) { // display error $this->setTitleI18n('No Results Found'); $this->setTemplate('searchNoResults'); return sfView::SUCCESS; } // display results $pager = $this->configurePager($pager, $form); $this->pager = $pager; $this->query = $values['query']; $this->setTitleNumResults($pager); $this->setTemplate('searchResults'); }
$t->diag('testing basic pagination functions'); try { $results->setPage(2); $t->pass('->setPage() accepts a integer page'); } catch (Exception $e) { $t->fail('->setPage() accepts a integer page'); } try { $results->setMaxPerPage(10); $t->pass('->setMaxPerPage() accepts an integer per page'); } catch (Exception $e) { $t->fail('->setMaxPerPage() accepts an integer per page'); } $t->is($results->getPage(), 2, '->getPage() returns current page'); $t->is($results->getMaxPerPage(), 10, '->getMaxPerPage() returns the max per page'); $t->is($results->getNbResults(), 1001, '->getNbResults() returns the total number of results'); $t->ok($results->haveToPaginate(), '->haveToPaginate() returns correct value'); $results->setPage(0); $t->is($results->getPage(), 1, '->setPage() to 0 sets the page to 1'); $results->setPage(100000); $t->is($results->getPage(), 101, '->setPage() above to upper bound resets to last page'); $results->setPage(2); $t->diag('testing ->getResults()'); $t->is_deeply($results->getResults()->toArray(), range(10, 20), '->getResults() returns the correct range'); $results->setPage(3); $t->is_deeply($results->getResults()->toArray(), range(20, 30), '->getResults() returns the correct range after page change'); $results->setMaxPerPage(0); $t->is_deeply($results->getResults()->toArray(), range(0, 1000), '->getResults() returns all results if the max per page is 0'); $results->setMaxPerPage(10); $t->diag('testing page numbers'); $t->is($results->getFirstPage(), 1, '->getFirstPage() returns 1 as first page');