<?php
class ModelServiceService extends Model {
	public function updateViewed($service_id) {
		$this->db->query("UPDATE " . DB_PREFIX . "service SET viewed = (viewed + 1) WHERE service_id = '" . (int)$service_id . "'");
	}

	public function getService($service_id) {
		if ($this->customer->isLogged()) {
			$customer_group_id = $this->customer->getGroupId();
		} else {
			$customer_group_id = $this->config->get('config_customer_group_id');
		}

		$query = $this->db->query("SELECT DISTINCT *, pd.name AS name, p.image, p.sort_order FROM " . DB_PREFIX . "service p LEFT JOIN " . DB_PREFIX . "service_description pd ON (p.service_id = pd.service_id) LEFT JOIN " . DB_PREFIX . "service_to_store p2s ON (p.service_id = p2s.service_id) WHERE p.service_id = '" . (int)$service_id . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'");

		if ($query->num_rows) {
			return array(
				'meta_title'       => $query->row['meta_title'],
				'noindex'          => $query->row['noindex'],
				'meta_h1'          => $query->row['meta_h1'],
				'service_id'       => $query->row['service_id'],
				'name'             => $query->row['name'],
				'description'      => $query->row['description'],
				'meta_description' => $query->row['meta_description'],
				'meta_keyword'     => $query->row['meta_keyword'],
				'image'            => $query->row['image'],
				'sort_order'       => $query->row['sort_order'],
				'status'           => $query->row['status'],
				'date_added'       => $query->row['date_added'],
				'date_modified'    => $query->row['date_modified'],
				'viewed'           => $query->row['viewed']
			);
		} else {
			return false;
		}
	}

	public function getServices($data = array()) {
		if ($this->customer->isLogged()) {
			$customer_group_id = $this->customer->getGroupId();
		} else {
			$customer_group_id = $this->config->get('config_customer_group_id');
		}

		$cache = 'service.' . (int)$this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . (int)$customer_group_id . '.' . md5(http_build_query($data));

		$service_data = $this->cache->get($cache);

		if (!$service_data) {
			$sql = "SELECT p.service_id FROM " . DB_PREFIX . "service p LEFT JOIN " . DB_PREFIX . "service_description pd ON (p.service_id = pd.service_id) LEFT JOIN " . DB_PREFIX . "service_to_store p2s ON (p.service_id = p2s.service_id)";

			if (!empty($data['filter_service_category_id'])) {
				$sql .= " LEFT JOIN " . DB_PREFIX . "service_to_category s2c ON (p.service_id = s2c.service_id)";
			}

			$sql .= " WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'";

			if (!empty($data['filter_service_category_id'])) {
				$sql .= " AND s2c.service_category_id = '" . (int)$data['filter_service_category_id'] . "'";
			}

			$sql .= " GROUP BY p.service_id";

			$sort_data = array(
				'pd.name',
				'p.viewed',
				'p.sort_order',
				'p.date_added'
			);

			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.date_added') {
					$sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
				} else {
					$sql .= " ORDER BY " . $data['sort'];
				}
			} else {
				$sql .= " ORDER BY p.sort_order";
			}

			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC, LCASE(pd.name) DESC";
			} else {
				$sql .= " ASC, LCASE(pd.name) ASC";
			}

			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}

				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}

			$service_data = array();

			$query = $this->db->query($sql);

			foreach ($query->rows as $result) {
				$service_data[$result['service_id']] = $this->getService($result['service_id']);
			}

			$this->cache->set($cache, $service_data);
		}

		return $service_data;
	}

	public function getLatestServices($limit) {
		if ($this->customer->isLogged()) {
			$customer_group_id = $this->customer->getGroupId();
		} else {
			$customer_group_id = $this->config->get('config_customer_group_id');
		}

		$cache = 'service.latest.' . (int)$this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . $customer_group_id . '.' . (int)$limit;
		$service_data = $this->cache->get($cache);

		if (!$service_data) {
			$query = $this->db->query("SELECT p.service_id FROM " . DB_PREFIX . "service p LEFT JOIN " . DB_PREFIX . "service_to_store p2s ON (p.service_id = p2s.service_id) WHERE p.status = '1' AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' ORDER BY p.date_added DESC LIMIT " . (int)$limit);

			foreach ($query->rows as $result) {
				$service_data[$result['service_id']] = $this->getService($result['service_id']);
			}

			$this->cache->set($cache, $service_data);
		}

		return $service_data;
	}

	public function getPopularServices($limit) {
		$service_data = array();

		$query = $this->db->query("SELECT p.service_id FROM " . DB_PREFIX . "service p LEFT JOIN " . DB_PREFIX . "service_to_store p2s ON (p.service_id = p2s.service_id) WHERE p.status = '1' AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' ORDER BY p.viewed DESC, p.date_added DESC LIMIT " . (int)$limit);

		foreach ($query->rows as $result) {
			$service_data[$result['service_id']] = $this->getService($result['service_id']);
		}

		return $service_data;
	}

	public function getServiceImages($service_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "service_image WHERE service_id = '" . (int)$service_id . "' ORDER BY sort_order ASC");

		return $query->rows;
	}

	public function getServiceRelated($service_id) {
		$service_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "service_related pr LEFT JOIN " . DB_PREFIX . "service p ON (pr.related_id = p.service_id) LEFT JOIN " . DB_PREFIX . "service_to_store p2s ON (p.service_id = p2s.service_id) WHERE pr.service_id = '" . (int)$service_id . "' AND p.status = '1' AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'");

		foreach ($query->rows as $result) {
			$service_data[$result['related_id']] = $this->getService($result['related_id']);
		}

		return $service_data;
	}

	public function getServiceRelatedProduct($service_id) {
		$product_data = array();
		$this->load->model('catalog/product');
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "service_related_product np LEFT JOIN " . DB_PREFIX . "product p ON (np.product_id = p.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE np.service_id = '" . (int)$service_id . "' AND p.status = '1' AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'");

		foreach ($query->rows as $result) {
			$product_data[$result['product_id']] = $this->model_catalog_product->getProduct($result['product_id']);
		}

		return $product_data;
	}

	public function getServiceLayoutId($service_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "service_to_layout WHERE service_id = '" . (int)$service_id . "' AND store_id = '" . (int)$this->config->get('config_store_id') . "'");

		if ($query->num_rows) {
			return $query->row['layout_id'];
		} else {
			return $this->config->get('config_layout_service');
		}
	}

	public function getDownloads($service_id) {

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "service_to_download pd LEFT JOIN " . DB_PREFIX . "download d ON(pd.download_id=d.download_id) LEFT JOIN " . DB_PREFIX . "download_description dd ON(pd.download_id=dd.download_id) WHERE service_id = '" . (int)$service_id . "' AND dd.language_id = '" . (int)$this->config->get('config_language_id')."'");

		return $query->rows;
	}

	public function getDownload($service_id, $download_id) {
		$download="";
		if($download_id!=0)$download=" AND d.download_id=".(int)$download_id;
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "service_to_download pd LEFT JOIN " . DB_PREFIX . "download d ON(pd.download_id=d.download_id) LEFT JOIN " . DB_PREFIX . "download_description dd ON(pd.download_id=dd.download_id) WHERE service_id = '" . (int)$service_id . "' ".$download." AND dd.language_id = '" . (int)$this->config->get('config_language_id')."'");

		return $query->row;
	}

	public function getTotalServices($data = array()) {
		if ($this->customer->isLogged()) {
			$customer_group_id = $this->customer->getGroupId();
		} else {
			$customer_group_id = $this->config->get('config_customer_group_id');
		}

		$cache = md5(http_build_query($data));

		$service_data = $this->cache->get('service.total.' . (int)$this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . (int)$customer_group_id . '.' . $cache);

		$service_data = [];

		if (!$service_data) {
			$sql = "SELECT COUNT(DISTINCT p.service_id) AS total FROM " . DB_PREFIX . "service p LEFT JOIN " . DB_PREFIX . "service_description pd ON (p.service_id = pd.service_id) LEFT JOIN " . DB_PREFIX . "service_to_store p2s ON (p.service_id = p2s.service_id)";

			if (!empty($data['filter_service_category_id'])) {
				$sql .= " LEFT JOIN " . DB_PREFIX . "service_to_category s2c ON (p.service_id = s2c.service_id)";
			}

			$sql .= " WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'";

			if (!empty($data['filter_service_category_id'])) {
				$sql .= " AND s2c.service_category_id = '" . (int)$data['filter_service_category_id'] . "'";
			}

			$query = $this->db->query($sql);

			$service_data = $query->row['total'];

			$this->cache->set('service.total.' . (int)$this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . (int)$customer_group_id . '.' . $cache, $service_data);
		}

		return $service_data;
	}

}
