<?php
class ControllerProductSearch extends Controller {
	public function index() {
		$this->load->language('product/search');
		$this->load->model('catalog/product');
		$this->load->model('tool/image');

		$search = isset($this->request->get['search']) ? trim((string)$this->request->get['search']) : '';
		$search = utf8_substr($search, 0, 120);
		$page = isset($this->request->get['page']) ? max(1, (int)$this->request->get['page']) : 1;
		$limit = (int)$this->config->get('theme_' . $this->config->get('config_theme') . '_product_limit');

		if ($limit < 1) {
			$limit = 12;
		}

		$this->document->setTitle($search ? sprintf($this->language->get('heading_results'), $search) : $this->language->get('heading_title'));
		$this->document->setRobots('noindex,follow');

		$data['breadcrumbs'] = array(
			array(
				'text' => $this->language->get('text_home'),
				'href' => $this->url->link('common/home')
			),
			array(
				'text' => $this->language->get('heading_title'),
				'href' => $this->url->link('product/search')
			)
		);

		$data['heading_title'] = $this->language->get('heading_title');
		$data['text_search_eyebrow'] = $this->language->get('text_search_eyebrow');
		$data['text_search_hint'] = $this->language->get('text_search_hint');
		$data['text_search_results'] = $this->language->get('text_search_results');
		$data['text_search_for'] = $this->language->get('text_search_for');
		$data['text_found'] = $this->language->get('text_found');
		$data['text_start_search'] = $this->language->get('text_start_search');
		$data['text_search_note'] = $this->language->get('text_search_note');
		$data['text_empty'] = $this->language->get('text_empty');
		$data['text_empty_hint'] = $this->language->get('text_empty_hint');
		$data['text_keyword'] = $this->language->get('text_keyword');
		$data['entry_search'] = $this->language->get('entry_search');
		$data['button_search'] = $this->language->get('button_search');
		$data['price_prefix'] = $this->language->get('price_prefix');
		$data['search'] = $search;
		$data['search_url'] = $this->url->link('product/search');
		$data['products'] = array();
		$data['product_total'] = 0;
		$data['pagination'] = '';
		$data['results'] = '';

		if ($search !== '') {
			$filter_data = array(
				'filter_name' => $search,
				'sort'        => 'relevance',
				'order'       => 'DESC',
				'start'       => ($page - 1) * $limit,
				'limit'       => $limit
			);

			$data['product_total'] = $this->model_catalog_product->getTotalProducts($filter_data);
			$results = $this->model_catalog_product->getProducts($filter_data);

			foreach ($results as $result) {
				$image = $this->model_tool_image->resize($result['image'] ? $result['image'] : 'placeholder.png', $this->config->get('theme_' . $this->config->get('config_theme') . '_image_product_width'), $this->config->get('theme_' . $this->config->get('config_theme') . '_image_product_height'));
				$product_images = $this->model_catalog_product->getProductImages($result['product_id']);
				$additional_image = $product_images ? $this->model_tool_image->resize($product_images[0]['image'], $this->config->get('theme_' . $this->config->get('config_theme') . '_image_product_width'), $this->config->get('theme_' . $this->config->get('config_theme') . '_image_product_height')) : false;

				$data['products'][] = array(
					'product_id'       => $result['product_id'],
					'thumb'            => $image,
					'additional_thumb' => $additional_image,
					'price_n'          => $result['price'],
					'price_2'          => $this->currency->format($this->tax->calculate($result['price_2'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']),
					'price_2_n'        => $result['price_2'],
					'price_3'          => $this->currency->format($this->tax->calculate($result['price_3'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']),
					'name'             => $result['name'],
					'min_price'        => $this->currency->format($this->tax->calculate(min(array($result['price'], $result['price_2'])), $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']),
					'href'             => $this->url->link('product/product', 'product_id=' . $result['product_id'])
				);
			}

			$pagination = new Pagination();
			$pagination->total = $data['product_total'];
			$pagination->page = $page;
			$pagination->limit = $limit;
			$pagination->url = $this->url->link('product/search', 'search=' . rawurlencode($search) . '&page={page}');

			$data['pagination'] = $pagination->render();
			$data['results'] = sprintf($this->language->get('text_pagination'), ($data['product_total']) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($data['product_total'] - $limit)) ? $data['product_total'] : ((($page - 1) * $limit) + $limit), $data['product_total'], ceil($data['product_total'] / $limit));
		}

		$data['content_top'] = $this->load->controller('common/content_top');
		$data['content_bottom'] = $this->load->controller('common/content_bottom');
		$data['footer'] = $this->load->controller('common/footer');
		$data['header'] = $this->load->controller('common/header');

		$this->response->setOutput($this->load->view('product/search', $data));
	}
}
