<?php
class ModelExtensionCustommenu extends Model {
	public function addCustommenu($data) {
		

		$this->db->query("INSERT INTO " . DB_PREFIX . "custommenu SET parent_id = '" . (int)$data['parent_id'] . "', `top` = '" . (isset($data['top']) ? (int)$data['top'] : 0) . "', `column` = '" . (int)$data['column'] . "', sort_order = '" . (int)$data['sort_order'] . "', target = '" . $this->db->escape($data['target']) . "', custlink = '" . $this->db->escape($data['custlink']) . "', status = '" . (int)$data['status'] . "', date_modified = NOW(), date_added = NOW()");

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

		// MySQL Hierarchical Data Closure Table Pattern
		$level = 0;

		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "custommenu_path` WHERE custommenu_id = '" . (int)$data['parent_id'] . "' ORDER BY `level` ASC");

		foreach ($query->rows as $result) {
			$this->db->query("INSERT INTO `" . DB_PREFIX . "custommenu_path` SET `custommenu_id` = '" . (int)$custommenu_id . "', `path_id` = '" . (int)$result['path_id'] . "', `level` = '" . (int)$level . "'");

			$level++;
		}

		$this->db->query("INSERT INTO `" . DB_PREFIX . "custommenu_path` SET `custommenu_id` = '" . (int)$custommenu_id . "', `path_id` = '" . (int)$custommenu_id . "', `level` = '" . (int)$level . "'");
 
		if (isset($data['custommenu_store'])) {
			foreach ($data['custommenu_store'] as $store_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "custommenu_to_store SET custommenu_id = '" . (int)$custommenu_id . "', store_id = '" . (int)$store_id . "'");
			}
		}
 

		$this->cache->delete('custommenu');

		

		return $custommenu_id;
	}

	public function editCustommenu($custommenu_id, $data) {
		

		$this->db->query("UPDATE " . DB_PREFIX . "custommenu SET parent_id = '" . (int)$data['parent_id'] . "', `top` = '" . (isset($data['top']) ? (int)$data['top'] : 0) . "', `column` = '" . (int)$data['column'] . "', sort_order = '" . (int)$data['sort_order'] . "', target = '" . $this->db->escape($data['target']) . "', custlink = '" . $this->db->escape($data['custlink']) . "', status = '" . (int)$data['status'] . "', date_modified = NOW() WHERE custommenu_id = '" . (int)$custommenu_id . "'");

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

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

		// MySQL Hierarchical Data Closure Table Pattern
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "custommenu_path` WHERE path_id = '" . (int)$custommenu_id . "' ORDER BY level ASC");

		if ($query->rows) {
			foreach ($query->rows as $custommenu_path) {
				// Delete the path below the current one
				$this->db->query("DELETE FROM `" . DB_PREFIX . "custommenu_path` WHERE custommenu_id = '" . (int)$custommenu_path['custommenu_id'] . "' AND level < '" . (int)$custommenu_path['level'] . "'");

				$path = array();

				// Get the nodes new parents
				$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "custommenu_path` WHERE custommenu_id = '" . (int)$data['parent_id'] . "' ORDER BY level ASC");

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

				// Get whats left of the nodes current path
				$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "custommenu_path` WHERE custommenu_id = '" . (int)$custommenu_path['custommenu_id'] . "' ORDER BY level ASC");

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

				// Combine the paths with a new level
				$level = 0;

				foreach ($path as $path_id) {
					$this->db->query("REPLACE INTO `" . DB_PREFIX . "custommenu_path` SET custommenu_id = '" . (int)$custommenu_path['custommenu_id'] . "', `path_id` = '" . (int)$path_id . "', level = '" . (int)$level . "'");

					$level++;
				}
			}
		} else {
			// Delete the path below the current one
			$this->db->query("DELETE FROM `" . DB_PREFIX . "custommenu_path` WHERE custommenu_id = '" . (int)$custommenu_id . "'");

			// Fix for records with no paths
			$level = 0;

			$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "custommenu_path` WHERE custommenu_id = '" . (int)$data['parent_id'] . "' ORDER BY level ASC");

			foreach ($query->rows as $result) {
				$this->db->query("INSERT INTO `" . DB_PREFIX . "custommenu_path` SET custommenu_id = '" . (int)$custommenu_id . "', `path_id` = '" . (int)$result['path_id'] . "', level = '" . (int)$level . "'");

				$level++;
			}

			$this->db->query("REPLACE INTO `" . DB_PREFIX . "custommenu_path` SET custommenu_id = '" . (int)$custommenu_id . "', `path_id` = '" . (int)$custommenu_id . "', level = '" . (int)$level . "'");
		}

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

		if (isset($data['custommenu_store'])) {
			foreach ($data['custommenu_store'] as $store_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "custommenu_to_store SET custommenu_id = '" . (int)$custommenu_id . "', store_id = '" . (int)$store_id . "'");
			}
		}
 
 
		$this->cache->delete('custommenu');

		
	}

	public function deleteCustommenu($custommenu_id) {
		

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

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "custommenu_path WHERE path_id = '" . (int)$custommenu_id . "'");

		foreach ($query->rows as $result) {
			$this->deleteCustommenu($result['custommenu_id']);
		}

		$this->db->query("DELETE FROM " . DB_PREFIX . "custommenu WHERE custommenu_id = '" . (int)$custommenu_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "custommenu_description WHERE custommenu_id = '" . (int)$custommenu_id . "'");
 		$this->db->query("DELETE FROM " . DB_PREFIX . "custommenu_to_store WHERE custommenu_id = '" . (int)$custommenu_id . "'");
  
		$this->cache->delete('custommenu');

		
	}
  
	public function getCustommenu($custommenu_id) {
		$query = $this->db->query("SELECT DISTINCT *, (SELECT GROUP_CONCAT(cd1.name ORDER BY level SEPARATOR '&nbsp;&nbsp;&gt;&nbsp;&nbsp;') FROM " . DB_PREFIX . "custommenu_path cp LEFT JOIN " . DB_PREFIX . "custommenu_description cd1 ON (cp.path_id = cd1.custommenu_id AND cp.custommenu_id != cp.path_id) WHERE cp.custommenu_id = c.custommenu_id AND cd1.language_id = '" . (int)$this->config->get('config_language_id') . "' GROUP BY cp.custommenu_id) AS path FROM " . DB_PREFIX . "custommenu c LEFT JOIN " . DB_PREFIX . "custommenu_description cd2 ON (c.custommenu_id = cd2.custommenu_id) WHERE c.custommenu_id = '" . (int)$custommenu_id . "' AND cd2.language_id = '" . (int)$this->config->get('config_language_id') . "'");

		return $query->row;
	}

	public function getCustommenus($data = array()) {
		$sql = "SELECT cp.custommenu_id AS custommenu_id, GROUP_CONCAT(cd1.name ORDER BY cp.level SEPARATOR '&nbsp;&nbsp;&gt;&nbsp;&nbsp;') AS name, c1.parent_id, c1.sort_order, c1.status FROM " . DB_PREFIX . "custommenu_path cp LEFT JOIN " . DB_PREFIX . "custommenu c1 ON (cp.custommenu_id = c1.custommenu_id) LEFT JOIN " . DB_PREFIX . "custommenu c2 ON (cp.path_id = c2.custommenu_id) LEFT JOIN " . DB_PREFIX . "custommenu_description cd1 ON (cp.path_id = cd1.custommenu_id) LEFT JOIN " . DB_PREFIX . "custommenu_description cd2 ON (cp.custommenu_id = cd2.custommenu_id) WHERE cd1.language_id = '" . (int)$this->config->get('config_language_id') . "' AND cd2.language_id = '" . (int)$this->config->get('config_language_id') . "'";

		if (!empty($data['filter_name'])) {
			$sql .= " AND cd2.name LIKE '" . $this->db->escape($data['filter_name']) . "%'";
		}

		$sql .= " GROUP BY cp.custommenu_id";

		$sort_data = array(
			'name',
			'sort_order'
		);

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

		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 getCustommenuDescriptions($custommenu_id) {
		$custommenu_description_data = array();

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

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

		return $custommenu_description_data;
	}
 
	public function getCustommenuStores($custommenu_id) {
		$custommenu_store_data = array();

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

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

		return $custommenu_store_data;
	}
 

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

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