<?php
class ModelExtensionModuleItagsCatalog extends Model {
	public function addItag($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "itag SET location = '" . $this->db->escape($data['location']) . "', position = '" . $this->db->escape($data['position']) . "', settings = '" . (isset($data['settings']) ? $this->db->escape(json_encode($data['settings'])) : '') . "', date_start = '" . $this->db->escape($data['date_start']) . "', date_end = '" . $this->db->escape($data['date_end']) . "', status = '" . (int)$data['status'] . "', sort_order = '" . (int)$data['sort_order'] . "'");

		$itag_id = $this->db->getLastId();

		foreach ($data['itag_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "itag_description SET itag_id = '" . (int)$itag_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', caption = '" . $this->db->escape($value['caption']) . "', image = '" . $this->db->escape($value['image']) . "'");
		}

		if (isset($data['itag_category'])) {
			foreach ($data['itag_category'] as $category_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "itag_to_category SET itag_id = '" . (int)$itag_id . "', category_id = '" . (int)$category_id . "'");
			}
		}

		if (isset($data['all_products']) && (int)$data['all_products']) {
			$query = $this->db->query("SELECT product_id AS id from " . DB_PREFIX . "product");
			foreach ($query->rows as $product) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_itag SET itag_id = '" . (int)$itag_id . "', product_id = '" . (int)$product['id'] . "'");
			}
		} else {
			if (isset($data['linked_products'])) {
				foreach ($data['linked_products'] as $product_id) {
					$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_itag SET itag_id = '" . (int)$itag_id . "', product_id = '" . (int)$product_id . "'");
				}
			}
		}

		if (isset($data['itag_customer_group'])) {
			foreach ($data['itag_customer_group'] as $customer_group_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "itag_to_customer_group SET itag_id = '" . (int)$itag_id . "', customer_group_id = '" . (int)$customer_group_id . "'");
			}
		} else {
			$this->db->query("INSERT INTO " . DB_PREFIX . "itag_to_customer_group SET itag_id = '" . (int)$itag_id . "', customer_group_id = '0'");
		}

		if (isset($data['itag_store'])) {
			foreach ($data['itag_store'] as $store_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "itag_to_store SET itag_id = '" . (int)$itag_id . "', store_id = '" . (int)$store_id . "'");
			}
		}
	}

	public function editItag($itag_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "itag SET location = '" . $this->db->escape($data['location']) . "', position = '" . $this->db->escape($data['position']) . "', settings = '" . (isset($data['settings']) ? $this->db->escape(json_encode($data['settings'])) : '') . "', date_start = '" . $this->db->escape($data['date_start']) . "', date_end = '" . $this->db->escape($data['date_end']) . "', status = '" . (int)$data['status'] . "', sort_order = '" . (int)$data['sort_order'] . "' WHERE itag_id = '" . (int)$itag_id . "'");

		$this->db->query("DELETE FROM " . DB_PREFIX . "itag_description WHERE itag_id = '" . (int)$itag_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "itag_to_customer_group WHERE itag_id = '" . (int)$itag_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "itag_to_store WHERE itag_id = '" . (int)$itag_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_to_itag WHERE itag_id = '" . (int)$itag_id . "'");

		foreach ($data['itag_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "itag_description SET itag_id = '" . (int)$itag_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', caption = '" . $this->db->escape($value['caption']) . "', image = '" . $this->db->escape($value['image']) . "'");
		}

		$this->db->query("DELETE FROM " . DB_PREFIX . "itag_to_category WHERE itag_id = '" . (int)$itag_id . "'");

		if (isset($data['itag_category'])) {
			foreach ($data['itag_category'] as $category_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "itag_to_category SET itag_id = '" . (int)$itag_id . "', category_id = '" . (int)$category_id . "'");
			}
		}

		if (isset($data['all_products']) && (int)$data['all_products']) {
			$query = $this->db->query("SELECT product_id AS id from " . DB_PREFIX . "product");
			foreach ($query->rows as $product) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_itag SET itag_id = '" . (int)$itag_id . "', product_id = '" . (int)$product['id'] . "'");
			}
		} else {
			if (isset($data['linked_products'])) {
				foreach ($data['linked_products'] as $product_id) {
					$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_itag SET itag_id = '" . (int)$itag_id . "', product_id = '" . (int)$product_id . "'");
				}
			}
		}

		if (isset($data['itag_customer_group'])) {
			foreach ($data['itag_customer_group'] as $customer_group_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "itag_to_customer_group SET itag_id = '" . (int)$itag_id . "', customer_group_id = '" . (int)$customer_group_id . "'");
			}
		} else {
			$this->db->query("INSERT INTO " . DB_PREFIX . "itag_to_customer_group SET itag_id = '" . (int)$itag_id . "', customer_group_id = '0'");
		}

		if (isset($data['itag_store'])) {
			foreach ($data['itag_store'] as $store_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "itag_to_store SET itag_id = '" . (int)$itag_id . "', store_id = '" . (int)$store_id . "'");
			}
		}

		$this->cache->delete('itag');
	}

	public function deleteItag($itag_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "itag WHERE itag_id = '" . (int)$itag_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "itag_description WHERE itag_id = '" . (int)$itag_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "itag_to_category WHERE itag_id = '" . (int)$itag_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "itag_to_customer_group WHERE itag_id = '" . (int)$itag_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "itag_to_store WHERE itag_id = '" . (int)$itag_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_to_itag WHERE itag_id = '" . (int)$itag_id . "'");

		$this->cache->delete('itag');
	}

	public function copyItag($itag_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "itag it LEFT JOIN " . DB_PREFIX . "itag_description itd ON (it.itag_id = itd.itag_id) WHERE it.itag_id = '" . (int)$itag_id . "' AND itd.language_id = '" . (int)$this->config->get('config_language_id') . "'");

		if ($query->num_rows) {
			$data = $query->row;

			$data['status'] = '0';
			$data['settings'] = json_decode($query->row['settings'], true);

			$data['itag_description'] = $this->getItagDescriptions($itag_id);
			$data['itag_category'] = $this->getItagCategories($itag_id);
			$data['linked_products'] = $this->getLinkedProducts($itag_id);
			$data['itag_customer_group'] = $this->getItagCustomerGroups($itag_id);
			$data['itag_store'] = $this->getItagStores($itag_id);

			$this->addItag($data);
		}
	}

	public function getItag($itag_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "itag it LEFT JOIN " . DB_PREFIX . "itag_description itd ON (it.itag_id = itd.itag_id) WHERE it.itag_id = '" . (int)$itag_id . "' AND itd.language_id = '" . (int)$this->config->get('config_language_id') . "'");

		return $query->row;
	}

	public function getItags($data = array()) {
		$sql = "SELECT * FROM " . DB_PREFIX . "itag it LEFT JOIN " . DB_PREFIX . "itag_description itd ON (it.itag_id = itd.itag_id) WHERE itd.language_id = '" . (int)$this->config->get('config_language_id') . "'";

		$sort_data = array(
			'itd.name',
			'it.location',
			'it.position',
			'it.status',
			'it.sort_order'
		);

		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];
		} else {
			$sql .= " ORDER BY itd.name";
		}

		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " 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'];
		}

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

		return $query->rows;
	}

	public function getItagDescriptions($itag_id) {
		$itag_description_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "itag_description WHERE itag_id = '" . (int)$itag_id . "'");

		foreach ($query->rows as $result) {
			$itag_description_data[$result['language_id']] = array(
				'language_id' => $result['language_id'],
				'name'        => $result['name'],
				'caption'     => $result['caption'],
				'image'       => $result['image']
			);
		}

		return $itag_description_data;
	}

	public function getTotalProductsByItagId($itag_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "itag WHERE itag_id = '" . (int)$itag_id . "'");

		return $query->row['total'];
	}

	public function getTotalItags() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "itag");

		return $query->row['total'];
	}

	public function getItagCategories($itag_id) {
		$itag_category_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "itag_to_category WHERE itag_id = '" . (int)$itag_id . "'");

		foreach ($query->rows as $result) {
			$itag_category_data[] = $result['category_id'];
		}

		return $itag_category_data;
	}

	public function getLinkedProducts($itag_id) {
		$linked_product_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_itag WHERE itag_id = '" . (int)$itag_id . "'");

		foreach ($query->rows as $result) {
			$linked_product_data[] = $result['product_id'];
		}

		return $linked_product_data;
	}

	public function getItagCustomerGroups($itag_id) {
		$itag_customer_group_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "itag_to_customer_group WHERE itag_id = '" . (int)$itag_id . "'");

		foreach ($query->rows as $result) {
			$itag_customer_group_data[] = $result['customer_group_id'];
		}

		return $itag_customer_group_data;
	}

	public function getItagStores($itag_id) {
		$itag_store_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "itag_to_store WHERE itag_id = '" . (int)$itag_id . "'");

		foreach ($query->rows as $result) {
			$itag_store_data[] = $result['store_id'];
		}

		return $itag_store_data;
	}

	public function getCheckITagDB() {
		$query = $this->db->query("SHOW TABLES LIKE '". DB_PREFIX ."itag'");

		return $query->rows;
	}
}