Location: PHPKode > scripts > Encaps Gallery > encaps-2.3.22s/core/catalog_item.ctl.php
<?php 
class CatalogItemController extends BaseController
{
	function CatalogItemController($config,$db,$dbtable,$admin=false,$htpf=array())
	{
		//echo " ".basename(__FILE__).":".__LINE__;
		parent::BaseController($config,$db,$dbtable,$admin,$htpf);
		$this->htpf = $htpf;
		$this->model = new BaseModel($this->db,$this->config["db_prefix"].$this->dbtable);
		$this->model->fields['id'] = null;		
		$this->model->fields['title'] = null;		
		$this->model->fields['description'] = null;		
		$this->model->fields['rank'] = null;		
		$this->model->fields['is_enabled'] = null;		
		$this->model->fields['updated_at'] = null;				
		$this->model->fields['categ_id'] = null;		
		$this->model->fields['comment'] = null;		
		$this->model->fields['file'] = null;	
		$this->model->fields['file_align'] = null;			
		$this->model->fields['file_width'] = null;		
		$this->model->fields['file_height'] = null;
		$this->model->fields['file_thumb'] = null;
		$this->model->fields['file_thumb_align'] = null;				
		$this->model->fields['price'] = null;
	}

	function get_category_max_rank($categ_id)
	{
		$sql = "SELECT MAX(rank) FROM ".$this->model->dbtable." WHERE categ_id=$categ_id LIMIT 1";
		$result = $this->model->db->query($sql);
		return (int)$result[0];
	}

 	function get_by_id($id=0)
	{		
		//echo " ".basename(__FILE__).":".__LINE__;
 		$id = $id>0?(int)$id:$this->model->get_max_id();
		//$id = (int)$id;
		$conditions = " WHERE id=".(int)$id;
		if(!$this->admin)$conditions .= " AND is_enabled=1 ";
		return $this->model->get($conditions);
 	}

 	function _get_id_next($item)
	{
		//echo " \n<br>".basename(__FILE__).":".__LINE__;
 		$id["prev"] = "";
 		$id["next"] = "";
 		$id["curr"] = $item["id"];

		$sql = " WHERE categ_id=".(int)$item['categ_id'];
 		if(!$this->admin)
			$sql .= " AND is_enabled=1 ";
 		$sql .= " ORDER BY rank DESC, title, id DESC";
 		
 		$result = $this->model->gets($sql);
		//var_dump($result);
 		
 		for($i=0;$i<sizeof($result);$i++)
 			if($result[$i]["id"]==$item["id"]){
 				if($i>0)
 				    $id["prev"] = $result[$i-1]["id"];
 				if($i<sizeof($result)-1)
 				    $id["next"] = $result[$i+1]["id"];
 //				else
 //				    $id["next"] = $result[0]["id"];
 			}
 		return $id;
 	}	
	
	function update($postget)//override BaseController::update()
	{
		$fields =  array();
		$fields["title"] = (isset($postget["title"]))?Misc::_safe_db_text($postget["title"]):"";
		$fields["is_enabled"] = (isset($postget["is_enabled"]))?1:0;
		$fields["updated_at"] = (isset($postget["updated_at"]))?$postget["updated_at"]:date("Y-m-d H:i");
		$fields["price"] = (isset($postget["price"]))?(float)$postget["price"]:0.01;
		if(isset($postget["id"])) $fields["id"] = (int)($postget["id"]);
		if(isset($postget["comment"])) $fields["comment"] = Misc::_safe_db_text($postget["comment"]);
		if(isset($postget["description"])) $fields["description"] = Misc::_safe_db_text($postget["description"]);
		if(isset($postget["rank"])) $fields["rank"] = (int)$postget["rank"];
		if(isset($postget["categ_id"])) $fields['categ_id'] = (int)$postget['categ_id'];
		if(isset($postget["file_width"])) $fields['file_width'] = (int)$postget['file_width'];	
		if(isset($postget["file_height"])) $fields['file_height'] = (int)$postget['file_height'];
		if(isset($postget["file_align"])) $fields['file_align'] = $postget['file_align'];
		if(isset($postget["file_thumb_align"])) $fields['file_thumb_align'] = $postget['file_thumb_align'];
		if((int)@$fields["id"]==0) $fields['rank'] = $this->get_category_max_rank($fields['categ_id'])+1;

		$fieldsOld = array();
        if((int)@$postget["id"]>0)
            $fieldsOld = $this->model->get("WHERE id=".(int)$postget["id"]);
        //else
            //$this->model->fields = array();
 
        //$file = @$this->model->fields['file'];
        $dst_dir = "../rwx/";
        if($this->htpf['file']["name"])
        {    
            $file = Misc::file_upload($dst_dir,$this->htpf['file']);
            if($file && @$fieldsOld['file'] != $file)
            {
                Misc::file_unlink($dst_dir,@$fieldsOld['file']);
                $fields['file'] = $file;
            }
            if($this->config["file_size"]>0)
                Misc::resize($dst_dir.$file,$dst_dir.$file,$this->config["file_size"]);
        }
 
        $file_thumb = "";
        
        if($postget["thumb_static"]=="Y" && $this->htpf['file_thumb']["name"])
        {
            Misc::file_unlink($dst_dir,$fieldsOld['file_thumb']);
            $file_thumb = Misc::file_upload($dst_dir,$this->htpf['file_thumb']);
            Misc::resize($dst_dir.$file_thumb,$dst_dir.$file_thumb,$this->config["file_thumb_size"],$this->config["file_thumb_type"]);
        }
        else if($postget["thumb_static"]=="N" && $file)
        {
            Misc::file_unlink($dst_dir,@$fieldsOld['file_thumb']);
            $file_thumb = preg_replace("#(\.\S+)$#","_thumb$1",$file);            
            if(file_exists($dst_dir.$file_thumb))
                $file_thumb = preg_replace("#(\.\S+)$#","_".time()."$1",$file_thumb);        
            Misc::resize($dst_dir.$file,$dst_dir.$file_thumb,$this->config["file_thumb_size"],$this->config["file_thumb_type"]);
        }        
 
        if($file_thumb)
            $fields['file_thumb'] = $file_thumb;
 
        if(!$fields["title"])
            $fields["title"] = preg_replace("#(\.\S+)$#","",$fields["file"]);
 
        return $this->model->update($fields);
     }
	function delete($id)
	{
		$dst_dir = "../rwx/";
		$fields = $this->model->get("WHERE id=".(int)$id);
		Misc::file_unlink($dst_dir,$fields['file_thumb']);
		Misc::file_unlink($dst_dir,$fields['file']);
		$this->model->delete("WHERE id=$id");
	}
	
	function update_bunch()//override parent
	{
		$fields = array("is_enabled"=>0);
		$delete = false;
 		foreach ($this->postget as $key=>$value)
		{
 			if(preg_match("#^id(\d+)_title$#",$key))$fields["title"] = Misc::_safe_db_text($value);
 			if(preg_match("#^id(\d+)_comment$#",$key)) $fields["comment"] = Misc::_safe_db_text($value);
 			if(preg_match("#^id(\d+)_rank$#",$key)) $fields["rank"] = (int)$value;
 			if(preg_match("#^id(\d+)_is_enabled$#",$key)) $fields["is_enabled"] = (isset($value))?"1":"0";
			if(preg_match("#^id(\d+)_categ_id$#",$key)) $fields["categ_id"] = (int)$value;
			if(preg_match("#^id(\d+)_price$#",$key)) $fields["price"] = (float)$value;
 			if(preg_match("#^id(\d+)_delete$#",$key))$delete=true;
 			if(preg_match("#^id(\d+)$#",$key))
			{
 				$fields["id"] = $value;
		 		if($delete)
					$this->delete($fields["id"]);
				else
				{
					//var_dump($fields);
					$this->model->update($fields);
				}
					
				$fields = array("is_enabled"=>0);		
				$delete = false;
 			}
 		}
 	} 		 	
	
}
?>
Return current item: Encaps Gallery