<?
	require_once("objects/tables/_base.php");

	class TabLimited extends TabBase
	{
		var	$ulf;

		function TabLimited($table) 
		{
			$this->TabBase($table);
			$this->title = $GLOBALS[cat_m_tab_limited];

			$this->ulf = -1;
			for($i = 0; $i < count($this->domains); $i++) {
				if($this->domains[$i]->isLimit()) $this->ulf = $i;
			};
			if($this->ulf == -1) $this->valid = false;
		}

		function update($user)
		{
			global $cat_db, $cat_log;

			$key = $this->domains[$this->key]->getName();
			$val = $this->domains[$this->key]->getFromInput();
			$val = rawurldecode($val);

			$res = $cat_db->query("select * from $this->table where $key='$val'");		
			if(count($res) != 1) return $GLOBALS[cat_m_error_nofound];

			if($user->isLimited($this->table)) {
				$q = $this->domains[$this->ulf]->getFromInput();
				if($q != $user->getLogin()) return $GLOBALS[cat_m_error_deny]; 
			};

			$v = '';
			for($i = 0; $i < count($this->domains); $i++) {
				$this->domains[$i]->onEdit($res[0]);			
				$z = $this->domains[$i]->getFromInput();
				if(strlen($z) > 0) {
					if(strlen($v) > 0) $v .= ",".$this->domains[$i]->getName()."='".$z."'";
					else $v = $this->domains[$i]->getName()."='".$z."'";
				};
			};
	                $cat_db->query("update $this->table set $v where $key='$val'");
			$cat_log->event("$this->table","update [$key=$val]");
			return $GLOBALS[cat_m_ok];
		}
		
		
		
		function delete($user)
		{
			global $cat_db, $cat_log;

			$key = $this->domains[$this->key]->getName();
			$val = $this->domains[$this->key]->getFromInput();
			$val = rawurldecode($val);

			$res = $cat_db->query("select * from $this->table where $key='$val'");		
			if(count($res) != 1) return $GLOBALS[cat_m_error_nofound];

			if($user->isLimited($this->table)) {
				$q = $this->domains[$this->ulf]->getFromInput();
				if($q != $user->getLogin()) return $GLOBALS[cat_m_error_deny]; 
			};

			for($i = 0; $i < count($this->domains); $i++) {
				$this->domains[$i]->onDelete($res[0]);			
			};
	                $cat_db->query("delete from $this->table where $key='$val'");
			$cat_log->event("$this->table","delete [$key=$val]");
			return $GLOBALS[cat_m_ok];
		}		
		
	
	};




?>