<?php
class ModelSettingExtension extends Model {

	public function getInstalled($type, $subtype=null) {

		$extension_data = array();
        $sql = "SELECT * FROM " . DB_PREFIX . "extension WHERE type = '".$type."'";
        if ($subtype != null){
            $sql .= " and `subtype`=".$subtype;
        }

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

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

		return $extension_data;
	}

	public function getInstalledAll() {
		$extension_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "extension");

		foreach ($query->rows as $result) {
			$extension_data[$result['type']][] = $result['code'];
		}

		return $extension_data;
	}

	public function install($type, $code , $subtype = 0) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "extension SET `type` = '" . $this->db->escape($type) . "', `code` = '" . $this->db->escape($code) . "', `subtype` = " . $subtype );
	}

	public function uninstall($type, $code) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "extension WHERE `type` = '" . $this->db->escape($type) . "' AND `code` = '" . $this->db->escape($code) . "'");
	}

	public function uninstallSetting($type, $code){
		$this->db->query("DELETE FROM " . DB_PREFIX . "extension WHERE `type` = '" . $this->db->escape($type) . "' AND `code` = '" . $this->db->escape($code) . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "setting WHERE `group` = '" . $this->db->escape($code) . "'");
	}

	/* Estou comentando essa função, pois depois de buscar em muitos lugares, em nenhum lugar é utilizado essa função... ByMatheus 28-09-2016
	  public function sql($sql) {
			$query = '';

			foreach($lines as $line) {
				if ($line && (substr($line, 0, 2) != '--') && (substr($line, 0, 1) != '#')) {
					$query .= $line;

					if (preg_match('/;\s*$/', $line)) {
						$query = str_replace("DROP TABLE IF EXISTS `oc_", "DROP TABLE IF EXISTS `" . $data['db_prefix'], $query);
						$query = str_replace("CREATE TABLE `oc_", "CREATE TABLE `" . $data['db_prefix'], $query);
						$query = str_replace("INSERT INTO `oc_", "INSERT INTO `" . $data['db_prefix'], $query);

						$result = mysql_query($query, $connection);

						if (!$result) {
							die(mysql_error());
						}

						$query = '';
					}
				}
			}
	}*/

	public function getExtensions($type,$subtype=null) {
        $sql = "SELECT * FROM " . DB_PREFIX . "extension WHERE `type` = '" . $this->db->escape($type) . "'";
        if ($subtype != null){
            $sql .= " and `subtype`=".$subtype;
        }
		$query = $this->db->query($sql);

		return $query->rows;
	}

  public function getInstalledByCode($type,$code) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "extension WHERE `type` = '" . $this->db->escape($type) . "' and `code` = '".$this->db->escape($code)."'");

		return $query->rows;
	}
}
