Location: PHPKode > scripts > Encaps Gallery > encaps-2.3.22s/core/base.mdl.php
<?php 
class BaseModel
{
	//var $fields = aray();

	function BaseModel($db,$dbtable)
	{
		//echo "<br>".__FILE__.__LINE__;
		$this->fields["id"] = '';
		$this->fields["title"] = '';
		$this->fields["rank"] = '';
		$this->fields["is_enabled"] = '';
		$this->fields["description"] = '';		
		$this->fields["updated_at"] = '';			
		$this->db = $db;
		$this->dbtable = $dbtable;
	}

	function validate_fields($params)
	{
		foreach($this->fields as $key=>$value)
		{
			//if(isset($params[$key]))
				$this->fields[$key]=@$params[$key];
		}
	}
	
	function db_insert($fields=array())
	{
		//echo "<br>".__FILE__.__LINE__;
		if($fields)$this->fields=$fields;
		$this->validate_fields($this->fields);
		$sql = "INSERT INTO ".$this->dbtable." (";
		$i = 0;
		foreach($this->fields as $key=>$value)
		{
			if($i++ > 0)
				$sql .= ", ";
			$sql .= $key;
		} 
		$sql .= ') VALUES (';
		$i = 0;
		foreach($this->fields as $key=>$value)
		{
			if($i++ > 0)
				$sql .= ", ";
			$sql .="'$value'";
		} 
		$sql .= ');';
		//echo "\n".$sql;
		if($this->db->query($sql))
			return $this->fields["id"] = $this->get_max_id();
		return null;
	}

	function db_update($fields=array())
	{
		//echo "<br>".__FILE__.__LINE__;
		//var_dump($this->fields);
		if(!$fields)$fields = $this->fields;
		$this->validate_fields($fields);
		//echo "<br>".__FILE__.__LINE__;
		//var_dump($this->fields);
		$sql = "UPDATE ".$this->dbtable." SET ";
		$i = 0;
		foreach($fields as $key=>$value)
		{
			if($key=="id") continue;
			if($i++ > 0) $sql .= ", ";
			$sql .= "$key='$value'";
		} 
		$sql .= " WHERE id=".$fields["id"].";";
		//echo $sql;
		$this->db->query($sql);
		return $this->fields["id"];
	}
	
	function save($fields)
	{
		//echo "<br>".__FILE__.__LINE__;
		if(trim($fields["title"]) != "" )
			if( (!isset($fields["id"]) || (int)$fields["id"] == 0) )
				return $this->db_insert($fields);
			else
				return $this->db_update($fields);
	}
	function add($fields){return $this->save($fields);}
	function insert($fields){return $this->save($fields);}
	function update($fields){return $this->save($fields);}
		
	function gets($condition="")
	{
		$sql = "SELECT * FROM ".$this->dbtable." $condition";
		$res = $this->db->query($sql);
		//echo "\n<hr>".var_dump($res);
		return $res;
	}

	function get($condition="")
	{
		//echo " <br>".basename(__FILE__).":".__LINE__;
		//$sql = "SELECT * FROM ".$this->dbtable;
		$sql = "";
		if((int)$condition>0)
			$sql .= " WHERE id=".(int)$condition;
		else if(!$condition)
			$sql .= " WHERE id=".(int)$this->fields["id"];
		else
			$sql .= $condition;
		//echo $sql;
		$result = $this->gets($sql);
		//$result = $this->db->query($sql);
		if(isset($result[0]["id"]))
		{
			//$this->validate_fields($result[0]);	
			//return $this->fields;
			return $result[0];
		}
		return null;
	}

	function delete($condition="")
	{
		$sql = "DELETE FROM ".$this->dbtable." ";
		if((int)$condition>0)
			$sql .= " WHERE id=".(int)$condition;
		else if(!$condition)
			$sql .= " WHERE id=".(int)$this->fields["id"];
		else
			$sql .= $condition;
		//echo $sql;// = "DELETE FROM ".$this->dbtable." $sql";
		$this->db->query($sql);
	}	
	
	function count_($condition="")
	{
		//echo " \n<br>".basename(__FILE__).":".__LINE__.":";
		$sql = "SELECT COUNT(*) FROM ".$this->dbtable." $condition";
		//echo $sql;
		$result = $this->db->query($sql);
		return $result[0][0];
	}
	
	function get_max_id()
	{
		$sql = "SELECT MAX(id) FROM ".$this->dbtable.";";
		$result = $this->db->query($sql);
		return $result[0][0];
	}

	function get_min_id($condition="")
	{
		$sql = "SELECT MIN(id) FROM ".$this->dbtable." ".$condition;
		$result = $this->db->query($sql);
		return $result[0][0];
	}
}
?>
Return current item: Encaps Gallery