<?php
/*-------------------------------------
	版权所有：南京牛昆网络科技有限公司（TEL-15896098989）
	程序版本：1.0
	更新日期：2015-03-11   
-------------------------------------*/
class area
{
	private $db;
	private $table;

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

	function arealist($parentid=0)
	{
		$parentid=intval($parentid);
		return $this->db->fetch_all("SELECT * FROM `$this->table` WHERE `$this->table`.`parentid`=$parentid ORDER BY `$this->table`.`orderby` ASC");
	}

	function areadata($parentid=0,$m=1)
	{
		$parentid=intval($parentid);
		$m=intval($m);
		return $this->db->fetch_all("SELECT * FROM `$this->table` WHERE `$this->table`.`parentid`=$parentid AND `$this->table`.`m`=$m ORDER BY `$this->table`.`orderby` ASC");
	}

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

	function getchildid($id,$withself=1)
	{
		$id=intval($id);
		$childid=$withself?array($id):array();

		$r=$this->db->fetch_all("SELECT `id` FROM `$this->table` WHERE `$this->table`.`parentid`=".intval($id));

		if($r)
		{
			foreach($r as $_r) 
			{
				$childid[]=$_r['id'];
				$r2=$this->db->fetch_all("SELECT `id` FROM `$this->table` WHERE `$this->table`.`parentid`=".intval($_r['id']));
				if($r2)
				{
					foreach($r2 as $_r2) 
					{
						$childid[]=$_r2['id'];
						$r3=$this->db->fetch_all("SELECT `id` FROM `$this->table` WHERE `$this->table`.`parentid`=".intval($_r2['id']));
						if($r3)
						{
							foreach($r3 as $_r3) 
							{
								$childid[]=$_r3['id'];
								$r4=$this->db->fetch_all("SELECT `id` FROM `$this->table` WHERE `$this->table`.`parentid`=".intval($_r3['id']));
								if($r4)
								{
									foreach($r4 as $_r4) 
									{
										$childid[]=$_r4['id'];
									}
								}

							}
						}
					}
				}
			}
		}

		return array_unique($childid);
	}

	function getname($id)
	{
		$r=$this->info($id);
		return $r?$r['name']:'不限';
	}

	function parentid($id)
	{
		$r=$this->info($id);
		return $r['parentid'];
	}

	function m($id)
	{
		$r=$this->info($id);
		return $r['m'];
	}

	function oparea($m=1,$parentid=0,$ids,$orderby,$name,$spell,$simplespell,$ishot,$neworderby,$newname,$newspell,$newsimplespell,$newishot)
	{
		$this->db->query("DELETE FROM `$this->table` WHERE `$this->table`.`parentid`=$parentid AND `$this->table`.`m`=$m");

		$newarea=array();
		if($ids)foreach($ids as $id)
		{
			if($name[$id])
			{
				$newarea['id']=$id;
				$newarea['parentid']=$parentid;
				$newarea['m']=$m;
				$newarea['orderby']=$orderby[$id];
				$newarea['name']=$name[$id];
				$newarea['spell']=$spell[$id];
				$newarea['simplespell']=$simplespell[$id];
				$newarea['ishot']=$ishot[$id];
				$newarea['c']=strtoupper(substr($simplespell[$id],0,1));
				$this->db->insert($this->table,$newarea,true);
			}
		}
		$newarea=array();
		if($neworderby)foreach($neworderby as $key => $val)
		{
			if($newname[$key])
			{
				$newarea['orderby']=$val;
				$newarea['parentid']=$parentid;
				$newarea['m']=$m;
				$newarea['name']=$newname[$key];
				$newarea['spell']=$newspell[$key];
				$newarea['simplespell']=$newsimplespell[$key];
				$newarea['ishot']=$newishot[$key];
				$newarea['c']=strtoupper(substr($newsimplespell[$key],0,1));
				$this->db->insert($this->table,$newarea);
			}
		}

		return true;
	}
}
?>
