<?php
class ControllerServiceService extends Controller {

	public function info() {
		$this->load->language('service/service');

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

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

		if (isset($this->request->get['service_id'])) {
			$service_id = (int)$this->request->get['service_id'];
		} else {
			$service_id = 0;
		}

		$this->load->model('service/service');
		$this->load->model('service/category');

		$service_info = $this->model_service_service->getService($service_id);

		if ($service_info) {
			$service_category_path = $this->model_service_category->getPathByService($service_id);
			$path = '';

			foreach (explode('_', $service_category_path) as $service_category_id) {
				$service_category_id = (int)$service_category_id;
				$category_info = $this->model_service_category->getCategory($service_category_id);

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

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

			$data['breadcrumbs'][] = array(
				'text' => $service_info['name'],
				'href' => $this->url->link('service/service/info', 'service_category_id=' . $service_category_path . '&service_id=' . $this->request->get['service_id'])
			);

			if ($service_info['meta_title']) {
				$this->document->setTitle($service_info['meta_title']);
			} else {
				$this->document->setTitle($service_info['name']);
			}

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

			$this->document->setDescription($service_info['meta_description']);
			$this->document->setKeywords($service_info['meta_keyword']);
			$this->document->addLink($this->url->link('service/service/info', 'service_category_id=' . $service_category_path . '&service_id=' . $this->request->get['service_id']), 'canonical');

			if ($service_info['meta_h1']) {
				$data['heading_title'] = $service_info['meta_h1'];
			} else {
				$data['heading_title'] = $service_info['name'];
			}

			$data['text_related'] = $this->language->get('text_related');
			$data['text_related_product'] = $this->language->get('text_related_product');
			$data['button_more'] = $this->language->get('button_more');
			$data['button_request'] = $this->language->get('button_request');
			$data['button_all_services'] = $this->language->get('button_all_services');

			$data['service_id'] = $this->request->get['service_id'];
			$data['description'] = html_entity_decode($service_info['description'], ENT_QUOTES, 'UTF-8');
			$data['summary'] = $service_info['meta_description'];
			$data['category_href'] = $this->url->link('service/category', 'service_category_id=' . $service_category_path);

			$this->load->model('tool/image');

			if ($service_info['image']) {
				$data['thumb'] = $this->model_tool_image->resize($service_info['image'], 610, 818);
			} else {
				$data['thumb'] = '';
			}

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

			$results = $this->model_service_service->getServiceRelated($this->request->get['service_id']);

			foreach ($results as $result) {
				if ($result['image']) {
					$image = $this->model_tool_image->resize($result['image'], $this->config->get('configblog_image_related_width'), $this->config->get('configblog_image_related_height'));
				} else {
					$image = false;
				}

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

			$this->load->model('tool/image');
			$data['products'] = array();

			$results = $this->model_service_service->getServiceRelatedProduct($this->request->get['service_id']);

			foreach ($results as $result) {
				if ($result['image']) {
					$image = $this->model_tool_image->resize($result['image'], $this->config->get('configblog_image_related_width'), $this->config->get('configblog_image_related_height'));
				} else {
					$image = false;
				}
				if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
					$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
				} else {
					$price = false;
				}

				if ((float)$result['special']) {
					$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
				} else {
					$special = false;
				}

				if ($this->config->get('config_tax')) {
					$tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price'], $this->session->data['currency']);
				} else {
					$tax = false;
				}

				$data['text_tax'] = $this->language->get('text_tax');

				$data['products'][] = array(
					'product_id' => $result['product_id'],
					'thumb'      => $image,
					'name'       => $result['name'],
					'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('configblog_article_description_length')) . '..',
					'price'      => $price,
					'special'    => $special,
					'tax'        => $tax,
					'minimum'    => $result['minimum'] > 0 ? $result['minimum'] : 1,
					'href'       => $this->url->link('product/product', 'product_id=' . $result['product_id']),
				);
			}

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

			$results = $this->model_service_service->getDownloads($this->request->get['service_id']);

			foreach ($results as $result) {
				if (file_exists(DIR_DOWNLOAD . $result['filename'])) {
					$size = filesize(DIR_DOWNLOAD . $result['filename']);

					$i = 0;

					$suffix = array(
						'B',
						'KB',
						'MB',
						'GB',
						'TB',
						'PB',
						'EB',
						'ZB',
						'YB'
					);

					while (($size / 10024) > 1) {
						$size = $size / 10024;
						$i++;
					}

					$data['downloads'][] = array(
						'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
						'name'       => $result['name'],
						'size'       => round(substr($size, 0, strpos($size, '.') + 4), 2) . $suffix[$i],
						'href'       => $this->url->link('service/service/download', '&service_id='. $this->request->get['service_id']. '&download_id=' . $result['download_id'])
					);
				}
			}

			$this->model_service_service->updateViewed($this->request->get['service_id']);

			$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/service', $data));
		} else {
			$data['breadcrumbs'][] = array(
				'text' => $this->language->get('text_error'),
				'href' => $this->url->link('service/service/info', 'service_id=' . $service_id)
			);

			$this->document->setTitle($this->language->get('text_error'));

			$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');

			$this->response->addHeader($this->request->server['SERVER_PROTOCOL'] . ' 404 Not Found');

			$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));
		}
	}

	public function download() {

		$this->load->model('service/service');

		if (isset($this->request->get['download_id'])) {
			$download_id = $this->request->get['download_id'];
		} else {
			$download_id = 0;
		}

		if (isset($this->request->get['service_id'])) {
			$service_id = $this->request->get['service_id'];
		} else {
			$service_id = 0;
		}

		$download_info = $this->model_service_service->getDownload($service_id, $download_id);

		if ($download_info) {
			$file = DIR_DOWNLOAD . $download_info['filename'];
			$mask = basename($download_info['mask']);

			if (!headers_sent()) {
				if (file_exists($file)) {
					header('Content-Description: File Transfer');
					header('Content-Type: application/octet-stream');
					header('Content-Disposition: attachment; filename="' . ($mask ? $mask : basename($file)) . '"');
					header('Content-Transfer-Encoding: binary');
					header('Expires: 0');
					header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
					header('Pragma: public');
					header('Content-Length: ' . filesize($file));

					readfile($file, 'rb');

					exit;
				} else {
					exit('Error: Could not find file ' . $file . '!');
				}
			} else {
				exit('Error: Headers already sent out!');
			}
		} else {
			$this->redirect(HTTP_SERVER . 'index.php?route=account/download');
		}
	}

}
