PageRenderTime 21ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Ecart/Search/controllers/IndexController.php

https://code.google.com/p/ecartcommerce/
PHP | 139 lines | 81 code | 14 blank | 44 comment | 8 complexity | ff99c107b1ef464c9cb9bf33cbb3ac19 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Ecart
  4. *
  5. * This file is part of Ecart.
  6. *
  7. * Ecart is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Ecart is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Ecart. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @category Ecart
  21. * @package Ecart_Search
  22. * @copyright Copyright 2008-2009 E-Cart LLC
  23. * @license GNU Public License V3.0
  24. */
  25. /**
  26. *
  27. * @category Ecart
  28. * @package Ecart_Search
  29. * @subpackage Controller
  30. * @author Ecart Core Team <core@ecartcommerce.com>
  31. */
  32. class Ecart_Search_IndexController extends Ecart_Core_Controller_Front
  33. {
  34. public function indexAction()
  35. {
  36. // render only layout blocks
  37. }
  38. public function resultAction()
  39. {
  40. $paging = array();
  41. $queryStr = (string) $this->_getParam('q');
  42. $this->view->pageTitle = Ecart::translate('search')->__(
  43. "Search results for '%s'", trim($queryStr)
  44. );
  45. $this->view->query = $queryStr = trim($queryStr);
  46. $paging['page'] = $page = (int) $this->_getParam('page', 1);
  47. if (empty($queryStr)) {
  48. $this->render();
  49. return;
  50. }
  51. /*$cacheDir = Ecart::config()->system->path . '/var/cache/search/';
  52. if (!is_dir($cacheDir) && !@mkdir($cacheDir, 0777, true)) {
  53. throw new Ecart_Exception(
  54. Ecart::translate('search')->__(
  55. "Can't create folder %s. Permission denied", $cacheDir
  56. ));
  57. }
  58. if (!is_writable($cacheDir) && !@chmod($cacheDir, 0777)) {
  59. throw new Ecart_Exception(
  60. Ecart::translate('search')->__(
  61. "Can't write to folder %s. Permission denied", $cacheDir
  62. ));
  63. }*/
  64. try {
  65. $lucene = Ecart::single('search/lucene');
  66. } catch (Exception $e) {
  67. Ecart::message()->addError($e->getMessage());
  68. $this->view->results = array();
  69. $this->render();
  70. return;
  71. }
  72. $result = array();
  73. $query = $lucene->createFuzzyQuery($queryStr);
  74. $result = $lucene->findFuzzy($query);
  75. Ecart::single('search/log')->logging(array(
  76. 'num_results' => count($result),
  77. 'query' => $queryStr,
  78. ));
  79. if (!count($result)) { // if nothing found
  80. $this->view->results = array();
  81. $this->render();
  82. return;
  83. }
  84. if ($this->_hasParam('limit')) {
  85. $limit = $this->_getParam('limit');
  86. } elseif (Ecart::session('catalog')->limit) {
  87. $limit = Ecart::session('catalog')->limit;
  88. } else {
  89. $limit = Ecart::config('catalog/listing/perPageDefault');
  90. }
  91. $paging['limit'] = $limit;
  92. $paging['page'] = $page = (int) $this->_getParam('page', 1);
  93. $paging['count'] = count($result);
  94. $paging['perPage'] = array();
  95. foreach (explode(',', Ecart::config('catalog/listing/perPage')) as $perPage) {
  96. $url = $this->view->url(array(
  97. 'limit' => $perPage, 'page' => null, 'q' => $queryStr
  98. ));
  99. $paging['perPage'][$url] = $perPage;
  100. }
  101. Ecart::session('catalog')->limit = $limit;
  102. if ('all' === $limit) {
  103. $paging['limit'] = $paging['count'];
  104. $limit = $paging['count'];
  105. }
  106. $this->view->paging = $paging;
  107. $founded = array();
  108. for ($i = ($page - 1) * $limit, $n = $i + $limit;
  109. isset($result[$i]) && $i < $n;
  110. $i++)
  111. {
  112. $founded[] = $lucene->getDocumentData($result[$i], $query);
  113. }
  114. foreach ($founded as &$found) {
  115. $found['url'] = urlencode($found['url']);
  116. }
  117. Ecart::dispatch('search_use', array(
  118. 'query' => $queryStr,
  119. 'result' => $founded,
  120. 'customer_id' => Ecart::getCustomerId()
  121. ));
  122. $this->view->results = $founded;
  123. $this->render();
  124. }
  125. }