<?php

#.begin
#.mod[filiant]
abstract class xItemObj {
	protected $m_data;
	protected $m_changed;
	public static $stru;
	abstract public function select($id);
	abstract public function update();
	/**
	 * Tell if this object loaded info.
	 * @return bool
	 */
	public function isValid() {
		if ($this->m_data)
			return true;
		return false;
	}
	public function __destruct() {
		$this->update();
	}
	/**
	 * Test mr0 with $on and $off if $on is okay but $off.
	 * @param uint32 $on
	 * @param uint32 $off
	 */
	public function is($on, $off = 0) {
		return ($this->mr0 & ($on | $off)) == $on;
	}
	/**
	 * Test mr0 with $on if any is true thereof.
	 * @param uint32 $on
	 * @param uint32 $off
	 */
	public function isAny($on, $off = 0) {
		if (!($this->mr0 & $off))
			return false;
		return ($this->mr0 & $on);
	}
	/**
	 * Add a property of file.
	 * @param string $varName
	 * @param int $varVal
	 */
	public function add($varName, $dulta, $conds = false) {
		if (isset($this->m_data[$varName]) && self::$stru->isInteger($dulta) && self::$stru->isInteger($this->$varName)) {
			$this->m_data[$varName] += $dulta;
			$this->m_changed[] = array($varName, $dulta, $conds);
			return $this->m_data[$varName];
		}
		return null;
	}
	/**
	 * Add a property of file.
	 * @param string $varName
	 * @param int $varVal
	 */
	public function addTo($varName, $val, $conds = false) {
		if (isset($this->m_data[$varName]) && self::$stru->isInteger($this->$varName)) {
			$this->m_changed[] = array($varName, '('.$val.' - '.$this->$varName.')', $conds);
			$this->m_data[$varName] = $val;
			return $this->m_data[$varName];
		}
		return null;
	}
	protected function doUpdate($tb, $conds = false, $refresh = false) {
		if ($this->m_changed && $this->isValid()) {
			$sqls = array();
			$condsSet = array();
			foreach ($this->m_changed as $key => $v)
				if (is_array($v)) {
					$sqls[] = '`'.$v[0].'` = `'.$v[0].'` + '.$v[1].'';
					if ($v[2])
						$condsSet[] = $v[2];
				} else
					$sqls[] = '`'.$key.'` = "'.xGbl::$mysql->escape($this->m_data[$key]).'"';
			if (!$conds)
				$conds = '`id`='.$this->id;
			if ($condsSet)
				$condsSet = ' AND '.join(' AND ', $condsSet);
			else
				$condsSet = '';
			xGbl::$mysql->exec('UPDATE `'.$tb.'` SET '.join(',', $sqls).' WHERE '.$conds.$condsSet.' LIMIT 1');
			$this->m_changed = array();
			if (xGbl::$mysql->getAffectRows()) {
				if ($refresh) {
					$res = xGbl::$mysql->exec('SELECT * FROM `'.$tb.'` WHERE '.$conds.' LIMIT 1');
					if ($r = $res->fetch_assoc())
						$this->m_data = $r;
					$res->free();
					return $this->isValid();
				}
				return true;
			}
		}
		return false;
	}
	public function __get($varName) {
		return $this->m_data[$varName];
	}
	public function __isset($varName) {
		return isset($this->m_data[$varName]);
	}
	public function __unset($varName) {
		unset($this->m_data[$varName]);
	}
	public function __set($varName, $varVal) {
		if (isset($this->m_data[$varName])) {
			$this->m_data[$varName] = $varVal;
			$this->m_changed[$varName] = 0;
			return $varVal;
		}
		return null;
	}
	
}
#.mod[/filiant]
#.end