Location: PHPKode > projects > RabbitBB - Online Web Storage Project > mywebdrive/PHP_CON/file/DAOGetFileDirAttr.php
<?PHP

class DAOGetFileDirAttr
{
	var $db;
	var $db_table;
	var $db_field;

	function DAOGetFileDirAttr()
	{
		global $db, $db_table, $db_field;
		$this->db = $db;
		$this->db_table = $db_table;
		$this->db_field = $db_field;
	}

	function getFileDirAttrById($_pid, $_ownerid, $_type=DIRECTORY)
	{
		$sqlq = new SQLSelect();
		$sqlq->setSelect($this->db_field['filedirdb'],
			$this->db_table['filedirdb']);
		$sqlq->setSelect($this->db_field['dirattr'],
			$this->db_table['dirattr']);
		$sqlq->setWhere($this->db_table['filedirdb'].".id=".$_pid);
		$tobj = $this->populate($sqlq->getRow($this->db, $sqlq->SQLReturn()));
		if (count($tobj)>0)
			return $tobj[0];
		else
			return null;
	}

	function getFileById($_id)
	{
		$sqlq=new SQLSelect();
		$sqlq->setSelect($this->db_field['filedirdb'],
			$this->db_table['filedirdb']);
		$sqlq->setSelect($this->db_field['fileattr'],
			$this->db_table['fileattr']);
		$sqlq->setWhere($this->db_table['filedirdb'].".id=".$_id);
		$sqlq->setWhere($this->db_table['filedirdb'].".linkid=".
			$this->db_table['fileattr'].".id");
		$tobj = $this->populate($sqlq->getRow($this->db, $sqlq->SQLReturn()));
		if (count($tobj)>0)
			return $tobj[0];
		else
			return null;
	}

	function getDirById($_id)
	{
		$sqlq=new SQLSelect();
		$sqlq->setSelect($this->db_field['filedirdb'],
			$this->db_table['filedirdb']);
		$sqlq->setSelect($this->db_field['dirattr'],
			$this->db_table['dirattr']);
		$sqlq->setWhere($this->db_table['filedirdb'].".id=".$_id);
		$sqlq->setWhere($this->db_table['filedirdb'].".linkid=".
			$this->db_table['dirattr'].".id");
		$tobj = $this->populate($sqlq->getRow($this->db, $sqlq->SQLReturn()));
		if (count($tobj)>0)
			return $tobj[0];
		else
			return null;
	}

	function getFileDirAttr($_ownerid, $_filename, $_type)
	{
		$patharray = $this->name2array($_filename);

		$sqlq = new SQLSelect();

		$sqlq->setSelect($this->db_field['filedirdb'],
			$this->db_table['filedirdb']);
		if ($_type==DIRECTORY)
			$sqlq->setSelect($this->db_field['dirattr'],
				$this->db_table['dirattr']);
		elseif ($_type==SFILE)
			$sqlq->setSelect($this->db_field['fileattr'],
				$this->db_table['fileattr']);
		$sqlq->setSelect($this->db_field['dirindex'],
			$this->db_table['dirindex']);
		// Get the dir index to id
		$tsql = $this->db_table['dirindex'].".dirname IN (";
		for ($i=0; $i<count($patharray); $i++)
		{
			$tsql.= "\"".$patharray[$i]."\"";
			if ((count($patharray)-1)>$i)
			{
				$tsql.=",";
			}
		}
		$tsql.=")";
		$sqlq->setWhere($tsql);
		unset($tsql);

		//$sqlq->setSelectOther("COUNT(*) AS other__matchword");
		$sqlq->setWhere($this->db_table['filedirdb'].".ownerid=".$_ownerid);
		$sqlq->setWhere($this->db_table['dirindex'].".fileid=".$this->db_table['filedirdb'].".id");
		$sqlq->setWhere($this->db_table['dirindex'].".totalword=".count($patharray));
		$sqlq->setGroup($this->db_table['filedirdb'].".id");
		
		if ($_type==DIRECTORY)
		{
			$sqlq->setWhere($this->db_table['filedirdb'].".type=".DIRECTORY);
			$sqlq->setWhere($this->db_table['filedirdb'].".linkid=".
				$this->db_table['dirattr'].".id");
			
		}
		elseif ($_type==SFILE)
		{
			$sqlq->setWhere($this->db_table['filedirdb'].".type=".SFILE);
			$sqlq->setWhere($this->db_table['filedirdb'].".linkid=".
				$this->db_table['fileattr'].".id");
		}
		//$sqlq->setHave("other__matchword=".count($patharray));
		$tobj = $this->populate($sqlq->getRow($this->db, $sqlq->SQLReturn()));
		//echo($sqlq->SQLReturn());
		if (count($tobj)>1)
		{
			for ($i=0; $i<count($tobj); $i++)
			{
				$ttemp = $tobj[$i]->getobj();
				$ts = $this->array2name($patharray);
				if ($tobj[$i]->gettype()==DIRECTORY)
				{
					if ($ts==$ttemp->getfullpath())
					{
						return $tobj[$i];
					}
					
				}
				elseif ($tobj[$i]->gettype()==SFILE)
				{
					if ($ts==($ttemp->getfullpath()."/".$ttemp->getfilename()))
					{
						return $tobj[$i];
					}
				}
			}
			return null;
		}
		elseif (count($tobj)>0)
			return $tobj[0];
		else
			return null;

		
	}

	function populate($_res)
	{
		$tobj = array();
		
		if (!isset($_res[$this->db_table['filedirdb']]))
		{
			return $tobj;
		}

		$ufdres = $_res[$this->db_table['filedirdb']];
		
		if (isset($_res[$this->db_table['dirattr']]))
		{
			$dirattrres = $_res[$this->db_table['dirattr']];

			for ($i=0; $i<count($dirattrres); $i++)
			{
				$tufd = $this->createUfd($ufdres[$i]);
				$tufd->setobj($this->createWDir($dirattrres[$i]));
				$tobj[] = $tufd;
			}
			unset($dirattrres);
		}
		elseif (isset($_res[$this->db_table['fileattr']]))
		{
			$fileattrres = $_res[$this->db_table['fileattr']];
			
			for ($i=0; $i<count($fileattrres); $i++)
			{
				$tufd = $this->createUfd($ufdres[$i]);
				$tufd->setobj($this->createWFile($fileattrres[$i]));
				$tobj[] = $tufd;
			}
			unset($fileattrres);
		}
		return $tobj;
	}

	function createUfd($_row)
	{
		return new Ufd($_row['id'], $_row['linkid'], $_row['type'], $_row['pid'], 
			$_row['status'], $_row['ownerid'], $_row['ownertype']);
	}

	function createWFile($_row)
	{
		return new WFile($_row['id'], $_row['fullpath'], $_row['filename'],
			$_row['filesize'], $_row['contenttype'],
			$_row['location'], $_row['status'], $_row['temppass']);
	}

	function createWDir($_row)
	{
		return new WDir($_row['id'], $_row['dirname'], $_row['fullpath'],
			$_row['temppass']);
	}

	function name2array($_name, $_sep="/")
	{
		$arr = explode($_sep, $_name);
		return $arr;
	}

	function array2name($_arr, $_sep="/")
	{
		$ts = "";
		for ($j=0; $j<count($_arr); $j++)
		{
			$ts .= $_arr[$j];
			if ($j<(count($_arr)-1))
			{
				$ts.=$_sep;
			}
		}
		return $ts;
	}

}

?>
Return current item: RabbitBB - Online Web Storage Project