<?php
class ModelExtensionModuleItagsModule extends Model {
	public function install() {
		$this->db->query(
		"CREATE TABLE IF NOT EXISTS " . DB_PREFIX . "itag (
			itag_id int(11) NOT NULL AUTO_INCREMENT,
			location varchar(64) NOT NULL DEFAULT 'left',
			position varchar(64) NOT NULL DEFAULT 'bottom_right',
			settings text NOT NULL,
			status tinyint(1) NOT NULL DEFAULT '0',
			sort_order int(3) NOT NULL,
			date_start date NOT NULL DEFAULT '0000-00-00',
			date_end date NOT NULL DEFAULT '0000-00-00',
			PRIMARY KEY (itag_id)
		) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;");

		$this->db->query(
		"CREATE TABLE IF NOT EXISTS " . DB_PREFIX . "itag_description (
			itag_id int(11) NOT NULL,
			language_id int(11) NOT NULL,
			name varchar(64) NOT NULL DEFAULT '',
			caption text NOT NULL DEFAULT '',
			image varchar(255) DEFAULT NULL,
			PRIMARY KEY (itag_id,language_id)
		) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;");

		$this->db->query(
		"CREATE TABLE IF NOT EXISTS " . DB_PREFIX . "itag_to_category (
			itag_id int(11) NOT NULL,
			category_id int(11) NOT NULL,
			PRIMARY KEY (itag_id,category_id),
			KEY category_id (category_id)
		) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;");

		$this->db->query(
		"CREATE TABLE IF NOT EXISTS " . DB_PREFIX . "itag_to_customer_group (
		 	itag_id int(11) NOT NULL,
			customer_group_id int(11) NOT NULL,
			PRIMARY KEY (itag_id,customer_group_id)
		) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;");

		$this->db->query(
		"CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "itag_to_store` (
			itag_id int(11) NOT NULL,
			store_id int(11) NOT NULL,
			PRIMARY KEY (itag_id,store_id)
		 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;");

		$this->db->query(
		"CREATE TABLE IF NOT EXISTS " . DB_PREFIX . "product_to_itag (
			product_id int(11) NOT NULL,
			itag_id int(11) NOT NULL,
			PRIMARY KEY (product_id,itag_id),
			KEY itag_id (itag_id)
		) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;");

		/* Default Settings */
		$this->getDefaultSettings();
	}

	public function update() {
		/* date_added column removed */
		$column_exists = $this->db->query("SHOW COLUMNS FROM " . DB_PREFIX . "itag LIKE 'date_added'");

		if ($column_exists->num_rows) {
			$this->db->query("ALTER TABLE " . DB_PREFIX . "itag DROP COLUMN date_added");
		}

		/* iTags v2.0.0 Update */
		if ($this->config->get('epiksel_itags_version') < '2.0.0') {
			$this->load->model('setting/setting');
			$this->model_setting_setting->deleteSetting('epiksel_itags');

			$this->getDefaultSettings();
		}

		/* Update Version */
		$this->setVersion();

		$column_exists = $this->db->query("SHOW COLUMNS FROM " . DB_PREFIX . "itag LIKE 'image'");
		if ($column_exists->num_rows) {
			$this->db->query("ALTER TABLE " . DB_PREFIX . "itag ADD `location` varchar(64) NOT NULL DEFAULT 'left' AFTER `itag_id`;");
			$this->db->query("ALTER TABLE " . DB_PREFIX . "itag ADD `settings` text NOT NULL AFTER `position`;");
			$this->db->query("ALTER TABLE " . DB_PREFIX . "itag ADD `status` tinyint(1) NOT NULL DEFAULT '0' AFTER `settings`;");
			$this->db->query("ALTER TABLE " . DB_PREFIX . "itag CHANGE `position` `position` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'bottom_right';");
			$this->db->query("ALTER TABLE " . DB_PREFIX . "itag_description ADD `image` varchar(255) NULL DEFAULT NULL AFTER `caption`;");

			$this->db->query("ALTER TABLE " . DB_PREFIX . "itag DROP `tag_link`, DROP `target`, DROP `modules`, DROP `pages`, DROP `image`, DROP `image_width`, DROP `image_height`, DROP `tooltip`");
		}

		/* iTags v2.1.0 Update */
		if (file_exists(DIR_APPLICATION . 'controller/extension/module/epiksel_itag.php')) {
			unlink(DIR_APPLICATION . 'controller/extension/module/epiksel_itag.php');
		}

		$column_exists = $this->db->query("SHOW COLUMNS FROM " . DB_PREFIX . "itag LIKE 'date_start'");

		if (!$column_exists->num_rows) {
			$this->db->query("ALTER TABLE " . DB_PREFIX . "itag ADD `date_start` date NOT NULL DEFAULT '0000-00-00' AFTER `sort_order`;");
			$this->db->query("ALTER TABLE " . DB_PREFIX . "itag ADD `date_end` date NOT NULL DEFAULT '0000-00-00' AFTER `date_start`;");
		}

		$table_query = $this->db->query("SHOW TABLES LIKE '" . DB_PREFIX . "itag_to_category'");
		if (!$table_query->num_rows) {
			$this->db->query(
			"CREATE TABLE IF NOT EXISTS " . DB_PREFIX . "itag_to_category (
				itag_id int(11) NOT NULL,
				category_id int(11) NOT NULL,
				PRIMARY KEY (itag_id,category_id),
				KEY category_id (category_id)
			) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;");
		}
	}

	public function uninstall() {
		$this->db->query("DROP TABLE " . DB_PREFIX . "itag, " . DB_PREFIX . "itag_description, " . DB_PREFIX . "itag_to_category, " . DB_PREFIX . "itag_to_customer_group, " . DB_PREFIX . "itag_to_store, " . DB_PREFIX . "product_to_itag;");

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

		$this->load->model('user/user_group');
		$this->model_user_user_group->removePermission($this->user->getGroupId(), 'access', 'extension/epiksel/itags');
		$this->model_user_user_group->removePermission($this->user->getGroupId(), 'modify', 'extension/epiksel/itags');
	}

	public function removeDatabase() {
		$this->db->query("TRUNCATE TABLE `" . DB_PREFIX . "itag_to_category`;");
		$this->db->query("TRUNCATE TABLE `" . DB_PREFIX . "product_to_itag`;");
	}

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

		return $query->rows;
	}

	protected function setVersion() {
		$this->load->model('setting/setting');

		$this->model_setting_setting->editSetting('epiksel_itags', array(
			'epiksel_itags_version' => EPMOD14_VERSION
		));
	}

	/* Default Setting */
	protected function getDefaultSettings() {
		$this->load->model('user/user_group');
		$this->model_user_user_group->addPermission($this->user->getGroupId(), 'access', 'extension/epiksel/itags');
		$this->model_user_user_group->addPermission($this->user->getGroupId(), 'modify', 'extension/epiksel/itags');

		$defaults['epiksel_itags_version'] = EPMOD14_VERSION;
		$defaults['epiksel_itags_setting']['width'] = 50;
		$defaults['epiksel_itags_setting']['height'] = 50;
		$defaults['epiksel_itags_setting']['tooltip_status'] = 1;
		$defaults['epiksel_itags_setting']['tooltip_position'] = 'top';
		$defaults['epiksel_itags_setting']['tooltip_title'] = 1;
		$defaults['epiksel_itags_setting']['pages_status'] = 1;
		$defaults['epiksel_itags_setting']['modules_status'] = 1;
		$defaults['epiksel_itags_setting']['shipping_tag_id'] = 0;
		$defaults['epiksel_itags_setting']['special_tag_id'] = 0;
		$defaults['epiksel_itags_status'] = 1;

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

		$this->model_setting_setting->editSetting('epiksel_itags', $defaults);
	}
}