<?php
/**
 * Columns data in $_SESSION['columns']
 * 
 * Data structure:
 * $_SESSION['columns'][$TableID] = array(
 *   [ID] => array(
 *     [type] => int(10)
 *     [null] => bool
 *     [default] => 
 *   )
 * );
 * 
 * 
 */

class ColumnCrm {

	private $_table;
	private $_data;
	private $_acl = null;
	private $_initialized;

	public function __construct($table, $colData) {
		$this->_table = $table;
		$this->_data = $colData;
		$this->_initialized = (V::get('__checked', '', $colData))? true : false;
	}

	/**
	 * Initialize column and store data i session.
	 * 
	 */
	public static function init($tableID, $tblAcl, $force = false) {
		$col = self::getInstance($tableID);
		if ($col && $col->initialized() && $force == false) {
			return;
		}

		$colData = self::fetchTableStruct($tblAcl->getName(), $tblAcl->getDB());
		$colData['__checked'] = true;
		self::saveColumn($tableID, $colData);
	}

	public function setAcl($acl) {
		$this->_acl = $acl;

		foreach ($this->_acl->getFields() as $fieldID => $vField) {
			if (array_key_exists($vField['name'], $this->_data)) {
				$vField['type'] = $this->_data[$vField['name']];
			} else {
				$this->_acl->removeField($fieldID);
			}
		}
	}

	public function getAcl() {
		return $this->_acl;
	}

	public function getTableID() {
		return $this->_table;
	}

	public function getTableName() {
		if ($this->_acl) {
			return $this->_acl->getName();
		}
		return null;
	}

	public function getDB() {
		if ($this->_acl) {
			return $this->_acl->getDB();
		}
		return null;
	}

	public function initialized() {
		return $this->_initialized;
	}

	public function type($fieldName) {
		$type = V::get($fieldName, '', $this->_data['TYPE']);
		return $type;
	}

	public static function fetchTableStruct($tblName, $dbID) {
		$struct = array();

		$db = DB::getDB($dbID);
		$res = $db->query("show fields from `$tblName` ");
		while ($h = $db->fetch_row($res)) {
			$fieldName = $h[0];
			$fieldType = $h[1];
			$struct[$fieldName] = array('type'=>$h[1], 'null'=>('YES' == $h[2]), 'default'=>$h[4]);
		}
		return $struct;
	}

	public static function fetchHistTableStruct($tblName, $dbID) {
		$col = array();
		$tblHistName = "{$tblName}_HIST";
		$db = DB::getDB($dbID);
		$res = $db->query("show fields from `$tblHistName` ");
		while ($h = $db->fetch_row($res)) {
			$fieldName = $h[0];
			$fieldType = $h[1];
			$col[$fieldName] = $fieldType;
		}
		return $col;
	}

	public function getVisibleFieldList() {
		$cols = array();
		foreach ($this->_data as $fieldName => $fieldType) {
			if ($fieldName == '__checked') {
				continue;
			}
			$cols[] = $fieldName;
		}
		return $cols;
	}

	public function getFieldList() {
		$cols = array();
		foreach ($this->_data['ID'] as $field_id) {
			$field_name = $this->_data['DESC'][$field_id];
			$cols[$field_id] = $field_name;
		}
		return $cols;
	}

	/**
	 * Get column object. Not initialize
	 * @returns object - column instance if exists else null
	 * 
	 * static
	 */
	public static function getInstance($tableID) {
		static $_cache;
		if (!$_cache) $_cache = array();
		if (array_key_exists($tableID, $_cache)) {
			return $_cache[$tableID];
		}
		if (!empty($_SESSION['columns'][$tableID])) {
			$col = new ColumnCrm($tableID, $_SESSION['columns'][$tableID]);
			if ($col->initialized()) {
				$_cache[$tableID] = $col;
				return $_cache[$tableID];
			}
		}
		return null;
	}

	/**
	 * static
	 */
	public static function saveColumn($tableID, $data) {
		$_SESSION['columns'][$tableID] = $data;
	}

}
