/app/code/Ecart/Search/controllers/IndexController.php
PHP | 139 lines | 81 code | 14 blank | 44 comment | 8 complexity | ff99c107b1ef464c9cb9bf33cbb3ac19 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
- <?php
- /**
- * Ecart
- *
- * This file is part of Ecart.
- *
- * Ecart is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Ecart is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Ecart. If not, see <http://www.gnu.org/licenses/>.
- *
- * @category Ecart
- * @package Ecart_Search
- * @copyright Copyright 2008-2009 E-Cart LLC
- * @license GNU Public License V3.0
- */
- /**
- *
- * @category Ecart
- * @package Ecart_Search
- * @subpackage Controller
- * @author Ecart Core Team <core@ecartcommerce.com>
- */
- class Ecart_Search_IndexController extends Ecart_Core_Controller_Front
- {
- public function indexAction()
- {
- // render only layout blocks
- }
- public function resultAction()
- {
- $paging = array();
- $queryStr = (string) $this->_getParam('q');
- $this->view->pageTitle = Ecart::translate('search')->__(
- "Search results for '%s'", trim($queryStr)
- );
- $this->view->query = $queryStr = trim($queryStr);
- $paging['page'] = $page = (int) $this->_getParam('page', 1);
- if (empty($queryStr)) {
- $this->render();
- return;
- }
- /*$cacheDir = Ecart::config()->system->path . '/var/cache/search/';
- if (!is_dir($cacheDir) && !@mkdir($cacheDir, 0777, true)) {
- throw new Ecart_Exception(
- Ecart::translate('search')->__(
- "Can't create folder %s. Permission denied", $cacheDir
- ));
- }
- if (!is_writable($cacheDir) && !@chmod($cacheDir, 0777)) {
- throw new Ecart_Exception(
- Ecart::translate('search')->__(
- "Can't write to folder %s. Permission denied", $cacheDir
- ));
- }*/
- try {
- $lucene = Ecart::single('search/lucene');
- } catch (Exception $e) {
- Ecart::message()->addError($e->getMessage());
- $this->view->results = array();
- $this->render();
- return;
- }
- $result = array();
- $query = $lucene->createFuzzyQuery($queryStr);
- $result = $lucene->findFuzzy($query);
- Ecart::single('search/log')->logging(array(
- 'num_results' => count($result),
- 'query' => $queryStr,
- ));
- if (!count($result)) { // if nothing found
- $this->view->results = array();
- $this->render();
- return;
- }
- if ($this->_hasParam('limit')) {
- $limit = $this->_getParam('limit');
- } elseif (Ecart::session('catalog')->limit) {
- $limit = Ecart::session('catalog')->limit;
- } else {
- $limit = Ecart::config('catalog/listing/perPageDefault');
- }
- $paging['limit'] = $limit;
- $paging['page'] = $page = (int) $this->_getParam('page', 1);
- $paging['count'] = count($result);
- $paging['perPage'] = array();
- foreach (explode(',', Ecart::config('catalog/listing/perPage')) as $perPage) {
- $url = $this->view->url(array(
- 'limit' => $perPage, 'page' => null, 'q' => $queryStr
- ));
- $paging['perPage'][$url] = $perPage;
- }
- Ecart::session('catalog')->limit = $limit;
- if ('all' === $limit) {
- $paging['limit'] = $paging['count'];
- $limit = $paging['count'];
- }
- $this->view->paging = $paging;
- $founded = array();
- for ($i = ($page - 1) * $limit, $n = $i + $limit;
- isset($result[$i]) && $i < $n;
- $i++)
- {
- $founded[] = $lucene->getDocumentData($result[$i], $query);
- }
- foreach ($founded as &$found) {
- $found['url'] = urlencode($found['url']);
- }
- Ecart::dispatch('search_use', array(
- 'query' => $queryStr,
- 'result' => $founded,
- 'customer_id' => Ecart::getCustomerId()
- ));
- $this->view->results = $founded;
- $this->render();
- }
- }