<?php
class ControllerServiceCategory extends Controller {
	public function index() {
		$this->load->language('service/category');
		$this->load->model('service/category');
		$this->load->model('service/service');
		$this->load->model('tool/image');

		$sort = isset($this->request->get['sort']) ? $this->request->get['sort'] : 'p.sort_order';
		$order = isset($this->request->get['order']) ? $this->request->get['order'] : 'ASC';
		$page = isset($this->request->get['page']) ? (int)$this->request->get['page'] : 1;
		$limit = isset($this->request->get['limit']) ? (int)$this->request->get['limit'] : (int)$this->config->get('configblog_article_limit');
		$service_category_path = isset($this->request->get['service_category_id']) ? (string)$this->request->get['service_category_id'] : '';

		if ($page < 1) {
			$page = 1;
		}

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

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

		$service_category_id = 0;
		$path = '';

		foreach (explode('_', $service_category_path) as $path_id) {
			$path_id = (int)$path_id;

			if (!$path_id) {
				continue;
			}

			$category_info = $this->model_service_category->getCategory($path_id);

			if ($category_info) {
				$path = $path ? $path . '_' . $path_id : $path_id;
				$service_category_id = $path_id;

				$data['breadcrumbs'][] = array(
					'text' => $category_info['name'],
					'href' => $this->url->link('service/category', 'service_category_id=' . $path)
				);
			}
		}

		$category_info = $this->model_service_category->getCategory($service_category_id);

		if (!$category_info) {
			return $this->renderNotFound($data);
		}

		$this->document->setTitle($category_info['meta_title'] ? $category_info['meta_title'] : $category_info['name']);
		$this->document->setDescription($category_info['meta_description']);
		$this->document->setKeywords($category_info['meta_keyword']);
		$this->document->addLink($this->url->link('service/category', 'service_category_id=' . $path), 'canonical');

		if ($category_info['noindex'] <= 0 && $this->config->get('config_noindex_status')) {
			$this->document->setRobots('noindex,follow');
		}

		$data['heading_title'] = $category_info['meta_h1'] ? $category_info['meta_h1'] : $category_info['name'];
		$data['description'] = html_entity_decode($category_info['description'], ENT_QUOTES, 'UTF-8');
		$data['button_more'] = $this->language->get('button_more');
		$data['button_request'] = $this->language->get('button_request');
		$data['text_empty'] = $this->language->get('text_empty');
		$data['text_refine'] = $this->language->get('text_refine');

		if ($category_info['image']) {
			$data['thumb'] = $this->model_tool_image->resize($category_info['image'], $this->config->get('configblog_image_category_width'), $this->config->get('configblog_image_category_height'));
		} else {
			$data['thumb'] = '';
		}

		$data['categories'] = array();

		foreach ($this->model_service_category->getCategories($service_category_id) as $category) {
			$data['categories'][] = array(
				'name' => $category['name'],
				'href' => $this->url->link('service/category', 'service_category_id=' . $path . '_' . $category['service_category_id'])
			);
		}

		$filter_data = array(
			'filter_service_category_id' => $service_category_id,
			'sort'                       => $sort,
			'order'                      => $order,
			'start'                      => ($page - 1) * $limit,
			'limit'                      => $limit
		);

		$service_total = $this->model_service_service->getTotalServices($filter_data);
		$data['service_total'] = $service_total;
		$data['services'] = array();

		foreach ($this->model_service_service->getServices($filter_data) as $service) {
			if ($service['image']) {
				$image = $this->model_tool_image->resize($service['image'], 610, 818);
			} else {
				$image = false;
			}

			$data['services'][] = array(
				'service_id'  => $service['service_id'],
				'thumb'       => $image,
				'name'        => $service['name'],
				'description' => utf8_substr(strip_tags(html_entity_decode($service['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('configblog_article_description_length')) . '..',
				'href'        => $this->url->link('service/service/info', 'service_category_id=' . $path . '&service_id=' . $service['service_id'])
			);
		}

		$url = '&service_category_id=' . $path;

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		if (isset($this->request->get['limit'])) {
			$url .= '&limit=' . $this->request->get['limit'];
		}

		$pagination = new Pagination();
		$pagination->total = $service_total;
		$pagination->page = $page;
		$pagination->limit = $limit;
		$pagination->url = $this->url->link('service/category', $url . '&page={page}');

		$data['pagination'] = $pagination->render();
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['column_right'] = $this->load->controller('common/column_right');
		$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('service/category', $data));
	}

	private function renderNotFound($data) {
		$this->document->setTitle($this->language->get('text_error'));
		$this->response->addHeader($this->request->server['SERVER_PROTOCOL'] . ' 404 Not Found');

		$data['heading_title'] = $this->language->get('text_error');
		$data['text_error'] = $this->language->get('text_error');
		$data['button_continue'] = $this->language->get('button_continue');
		$data['continue'] = $this->url->link('common/home');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['column_right'] = $this->load->controller('common/column_right');
		$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('error/not_found', $data));
	}
}
