<?
	require_once "xslt/codegen/g_xslt.php";
	require_once "xslt/codegen/g_agents.php";
	require_once "xslt/codegen/g_menu.php";
	require_once "xslt/codegen/g_page.php";
	require_once "xslt/codegen/g_functions.php";

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

	class TabPublished extends TabBase
	{
		function TabPublished($table) 
		{
			$this->TabBase($table);
			$this->title = $GLOBALS[cat_m_tab_published];
		}		
		
		function insert($user)
		{
			global $cat_db, $cat_log;
			
			for($i = 0; $i < count($this->domains); $i++) {
				$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."'";

					if($i != $this->key) {
						if(strlen($q) > 0) $q .= " and ".$this->domains[$i]->getName()."='".$z."'";
						else $q = $this->domains[$i]->getName()."='".$z."'";
					};					
				};
			};
	                $cat_db->query("insert into $this->table set $v");
			$cat_log->event("$this->table","insert");

			if($user->isPublish($this->table)) {
				$key = $this->domains[$this->key]->getName();
		                $res = $cat_db->query("select $key from $this->table where $q");

				$pub = new XSLTGenerator; 
				foreach($res as $r) {
					$pub->sync($this->table,$key,$r[$key]);			
				};
			};
			return $GLOBALS[cat_m_ok];
		}
		
		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];

			$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]");
			if($user->isPublish($this->table)) {
				$pub = new XSLTGenerator; 
				$pub->sync($this->table,$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];

			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]");
			$pub = new XSLTGenerator; 
			$pub->clean($this->table,$key,$val);			
			return $GLOBALS[cat_m_ok];
		}		
		
	};




?>