<?php
// Developer : Ekrem KAYA
// Website   : http://e-piksel.com

class ModelExtensionModuleEpikselAttributes extends Model {
	public function install() {
		$improved_query = $this->db->query("SHOW COLUMNS FROM " . DB_PREFIX . "attribute LIKE 'pages'");
		if (!$improved_query->num_rows) {
			$this->db->query("ALTER TABLE `" . DB_PREFIX . "attribute` ADD  `pages` tinyint(1) default '1' NOT NULL AFTER `attribute_group_id`;");
			$this->db->query("ALTER TABLE `" . DB_PREFIX . "attribute` ADD  `modules` tinyint(1) default '1' NOT NULL AFTER `pages`;");
			$this->db->query("ALTER TABLE `" . DB_PREFIX . "attribute` ADD  `compare` tinyint(1) default '1' NOT NULL AFTER `modules`;");
			$this->db->query("ALTER TABLE `" . DB_PREFIX . "attribute_description` ADD `tooltip` text NOT NULL AFTER `name`;");
		}

		$improved_query = $this->db->query("SHOW COLUMNS FROM " . DB_PREFIX . "attribute_group LIKE 'position'");
		if (!$improved_query->num_rows) {
			$this->db->query("ALTER TABLE `" . DB_PREFIX . "attribute_group` ADD  `position` varchar(64) default '0' NOT NULL AFTER `attribute_group_id`;");
			$this->db->query("ALTER TABLE `" . DB_PREFIX . "attribute_group` ADD  `pages` tinyint(1) default '0' NOT NULL AFTER `position`;");
			$this->db->query("ALTER TABLE `" . DB_PREFIX . "attribute_group` ADD  `modules` tinyint(1) default '0' NOT NULL AFTER `pages`;");
			$this->db->query("ALTER TABLE `" . DB_PREFIX . "attribute_group` ADD  `compare` tinyint(1) default '1' NOT NULL AFTER `modules`;");
			$this->db->query("ALTER TABLE `" . DB_PREFIX . "attribute_group_description` ADD `tooltip` text NOT NULL AFTER `name`;");
		}

	/* Default Setting */
		$this->db->query(
		"INSERT INTO `".DB_PREFIX."setting` (`setting_id`, `store_id`, `code`, `key`, `value`, `serialized`) VALUES
			('', '0', 'epiksel_attributes', 'epiksel_attributes_version', '".EPMOD12_VERSION."', '0'),
			('', '0', 'epiksel_attributes', 'epiksel_attributes_pages_status', '1', 0),
			('', '0', 'epiksel_attributes', 'epiksel_attributes_module_status', '1', 0),
			('', '0', 'epiksel_attributes', 'epiksel_attributes_search_in_attributes', '1', 0),
			('', '0', 'epiksel_attributes', 'epiksel_attributes_html_status', '1', 0);");
	}

	public function update() {
		/* Version number update */
		if ($this->config->get('epiksel_attributes_version')) {
			$this->db->query("UPDATE `".DB_PREFIX."setting` SET `value` = '".EPMOD12_VERSION."' WHERE `key` = 'epiksel_attributes_version'");
		} else {
			$this->db->query("INSERT INTO `".DB_PREFIX."setting` (`setting_id`, `store_id`, `code`, `key`, `value`, `serialized`) VALUES ('', '0', 'epiksel_attributes', 'epiksel_attributes_version', '".EPMOD12_VERSION."', '0');");
		}

		/* 1.2.0 update begin tooltip begin */
		$improved_query = $this->db->query("SHOW COLUMNS FROM " . DB_PREFIX . "attribute_description LIKE 'tooltip'");
		if (!$improved_query->num_rows) {
			$this->db->query("ALTER TABLE `" . DB_PREFIX . "attribute_description` ADD `tooltip` text NOT NULL AFTER `name`;");
			$this->db->query("ALTER TABLE `" . DB_PREFIX . "attribute_group_description` ADD `tooltip` text NOT NULL AFTER `name`;");
		}

		/* search in attributes begin */
		if (!$this->config->get('epiksel_attributes_search_in_attributes')) {
			$this->db->query("INSERT INTO `".DB_PREFIX."setting` (`setting_id`, `store_id`, `code`, `key`, `value`, `serialized`) VALUES ('', '0', 'epiksel_attributes', 'epiksel_attributes_search_in_attributes', '1', '0');");
		}
		/* 1.2.0 update end */
	}

	public function uninstall() {
		/* Remove column */
		$improved_query = $this->db->query("SHOW COLUMNS FROM " . DB_PREFIX . "attribute LIKE 'pages'");
		if ($improved_query->num_rows) {
			$this->db->query("ALTER TABLE  " . DB_PREFIX . "attribute DROP pages");
			$this->db->query("ALTER TABLE  " . DB_PREFIX . "attribute DROP modules");
			$this->db->query("ALTER TABLE  " . DB_PREFIX . "attribute DROP compare");
			$this->db->query("ALTER TABLE  " . DB_PREFIX . "attribute_description DROP tooltip");
		}

		$improved_query = $this->db->query("SHOW COLUMNS FROM " . DB_PREFIX . "attribute_group LIKE 'position'");
		if ($improved_query->num_rows) {
			$this->db->query("ALTER TABLE  " . DB_PREFIX . "attribute_group DROP position");
			$this->db->query("ALTER TABLE  " . DB_PREFIX . "attribute_group DROP pages");
			$this->db->query("ALTER TABLE  " . DB_PREFIX . "attribute_group DROP modules");
			$this->db->query("ALTER TABLE  " . DB_PREFIX . "attribute_group DROP compare");
			$this->db->query("ALTER TABLE  " . DB_PREFIX . "attribute_group_description DROP tooltip");
		}

		$this->load->model('setting/setting');
		$this->model_setting_setting->deleteSetting('epiksel_attributes');

		$this->load->model('extension/extension');
		$this->model_extension_extension->uninstall('module', 'epiksel_attributes');
	}

	public function getCheckDB() {
		$query = $this->db->query("SHOW COLUMNS FROM " . DB_PREFIX . "attribute LIKE 'pages'");

		return $query->rows;
	}
}