<?php

class siteid
{
	private $db;
	public $table;

	function __construct()
	{
		global $db;
		$this->db=$db;
		$this->table=LA_PRE.'siteid';
	}

	function get($siteid)
	{
		$r=$this->db->fetch_one("SELECT * FROM	`$this->table` WHERE `$this->table`.`siteid`=".intval($siteid));
		
		if(!$r)
		{
			$r=$this->db->fetch_one("SELECT * FROM	`$this->table` WHERE `$this->table`.`siteid`=1");
		}
		return $r;
	}

	function getbase($siteid)
	{
		$r=$this->db->fetch_one("SELECT * FROM	`$this->table` WHERE `$this->table`.`siteid`=".intval($siteid));
		return $r;
	}

	function sitelist($isall=0)
	{
		global $_siteid,$_roleid;
		if($_siteid && !$isall)
		{
			return $this->db->fetch_all("SELECT * FROM	`$this->table` WHERE `$this->table`.`grandparentid`=".intval($_siteid)." OR `$this->table`.`parentid`=".intval($_siteid));
		}
		else
		{
			return $this->db->fetch_all("SELECT * FROM	`$this->table` WHERE 1");
		}
	}

	function adminsitelist($parentid=0)
	{
		global $page,$_siteid,$siteidobj;
		$roleid=intval($roleid);
		
		$where=$_siteid?'siteid IN('.implode(',',$siteidobj->getchildid($_siteid)).')':1;
		$where.=' AND parentid='.intval($parentid);
		include SYSTEM_ROOT.'include/datalist.class.php';
		$datalist = new datalist();
		$orderby='siteid ASC';
		$page=max(isset($page)?intval($page):1,1);
		$pagesize=10;
		$result=$datalist->getlist($this->table,$where,$orderby,$page,$pagesize);
		$this->pagestring=$datalist->pagestring;
		return $result;
	}

	function parentid($id)
	{
		$id=intval($id);
		$r=$this->get($id);
		return $r?$r['parentid']:0;
	}

	function areaid($id)
	{
		$id=intval($id);
		$r=$this->get($id);
		return $r?$r['areaid']:0;
	}

	function grandparentid($id)
	{
		$id=intval($id);
		$r=$this->get($id);
		return $r?$r['grandparentid']:0;
	}
	
	function getchildsite($id)
	{
		$id=intval($id);
		return $this->db->fetch_all("SELECT `siteid` FROM `$this->table` WHERE `$this->table`.`parentid`=".intval($id));
	}
	
	function getchildid($id,$withself=1)
	{
		$id=intval($id);
		$childid=$withself?array($id):array();

		$r=$this->db->fetch_all("SELECT `siteid` FROM `$this->table` WHERE `$this->table`.`parentid`=".intval($id));
		if(!$r)
		{
			$r=$this->db->fetch_all("SELECT `siteid` FROM `$this->table` WHERE `$this->table`.`grandparentid`=".intval($id));
		}

		if($r)
		{
			foreach($r as $key => $_r) 
			{
				$childid[$key+1]=$_r['siteid'];
			}
		}

		return array_unique($childid);
	}

	function in_parentid($id,$parentid=0)
	{
		return ($this->parentid($id)==$parentid || $this->grandparentid($id)==$parentid);
	}

	function sitename($siteid)
	{
		$r=$this->get($siteid);
		return $r['sitename'];
	}

	function siteareaname($siteid)
	{
		global $areaobj;
		$r=$this->get($siteid);
		return $areaobj->getname($r['areaid']);
	}

	function edit($info,$id)
	{
		$info['grandparentid']=$this->grandparentid($info['parentid']);
		return $this->db->update($this->table,$info,'siteid='.intval($id));
	}

	function add($info)
	{
		if($this->db->fetch_one("SELECT * FROM `$this->table` WHERE `$this->table`.`www`='".$info['www']."' OR `$this->table`.`siteurl`='".$info['siteurl']."'"))
		{
			return false;
		}
		$info['grandparentid']=$this->grandparentid($info['parentid']);
		return $this->db->insert($this->table,$info);
	}

	function delete($id)
	{
		$id=intval($id);
		return $this->db->mysql_delete($this->table,$id,'siteid');
	}
}
?>
