/** * Search page * * @access public */ public function action__search($keyword = null) { $items = null; $products = array(); //if(!$get = \Input::get()) \Response::redirect(\Input::referrer(\Uri::create('/'))); //if(empty($get['searchTerm'])) \Response::redirect(\Input::referrer(\Uri::create('/'))); if (strlen(trim($keyword)) < 3) { \Messages::error('Search word must be at least 3 characters long.'); } else { $search_keywords = array_map('trim', explode(',', $keyword)); $products = Model_Product::find(function ($query) use($search_keywords) { $query->where('status', '=', 1); if (count($search_keywords) > 1) { foreach ($search_keywords as $key => $keyword) { if ($key == 0) { $query->where('title', 'like', "%{$keyword}%"); } else { $query->or_where('title', 'like', "%{$keyword}%"); } } } else { $query->where('title', 'like', "%{$search_keywords[0]}%"); } $query->order_by('title'); return $query; }, 'id'); $items = $products; if ($items) { // Price range if (\Input::get('range')) { $range = explode('-', \Input::get('range')); $price_from = isset($range[0]) && is_numeric($range[0]) ? $range[0] : null; $price_to = isset($range[1]) && is_numeric($range[1]) ? $range[1] : null; if (!is_null($price_from) && !is_null($price_to)) { foreach ($items as $key => $item) { if ($item->default_price[0] >= $price_from && $item->default_price[0] <= $price_to) { continue; } unset($items[$key]); } } } } if ($items) { if (\Input::get('sort') == 'price_asc') { $items = \Product\Model_Product::get_sorted_products($items); } elseif (\Input::get('sort') == 'price_desc') { $items = array_reverse(\Product\Model_Product::get_sorted_products($items)); } } } \Helper::sorting($items); // Reset to empty array if there are no result found by query if (is_null($items)) { $items = array(); } // Initiate pagination $pagination = \Hybrid\Pagination::forge(array('total_items' => count($items), 'per_page' => \Input::get('per_page', 30), 'uri_segment' => null, 'base_uri' => 'search')); // Remove unwanted items, and show only required ones $items = array_slice($items, $pagination->offset, $pagination->per_page); \View::set_global('title', 'Search'); \Theme::instance()->set_partial('content', $this->view_dir . 'search')->set('items', $items, false)->set('pagination', $pagination, false)->set('products', $products, false); }