Location: PHPKode > scripts > Encaps Gallery > encaps-2.3.22s/core/catalog_categ.ctl.php
<?php 
class CatalogCategController extends BaseController
{
	function CatalogCategController($config,$db,$dbtable,$admin=false,$htpf=array())
	{
		parent::BaseController($config,$db,$dbtable,$admin);
		$this->htpf = $htpf;
		$this->model = new BaseModel($this->db,$this->config["db_prefix"].$this->dbtable);
		$this->model->fields['parent_id'] = null;
		$this->model->fields["description"] = null;
		$this->model->fields["img"] = null;
	}

	function gets($_parent_id=0)//override BaseController
	{
		//echo " ".basename(__FILE__).":".__LINE__;
		$result = array();
		$sql = " WHERE parent_id=$_parent_id ";
		if(!$this->admin )
		{
			$sql .= " AND is_enabled=1 ";
		}
		$sql .= "ORDER BY rank,title,id";
		$result = $this->model->gets($sql);
		//var_dump($result);
		for($i=0;$i<count($result);$i++)
		{
			$result[$i]['childs'] = $this->gets($result[$i]['id']);		
		}
		return $result;	
	}
	
	function get($condition=null)
	{
	//	echo " <br>".basename(__FILE__).":".__LINE__;
		if(!$condition)
		{
			$condition="";			
			if(!$this->admin)
			{
				$condition .= " WHERE is_enabled=1";
			}
			$condition .= " ORDER BY parent_id,rank,title,id LIMIT 1";
		}
//		echo $condition;
		$item = $this->model->get($condition);
		return $item;
	}
	
	function update($postget)//override
	{
		$fields["id"] = (int)@$postget["id"];		
		$fields["is_enabled"] = (isset($postget["is_enabled"]))?1:0;
		$fields["title"] = ($postget["title"] != "")?Misc::_nl2br($postget["title"]):"";
		$fields["description"] = Misc::_safe_db_text($postget["description"]);

		if((string)(int)$postget["rank"] == $postget["rank"])$fields["rank"] = $postget["rank"];
		$fields["parent_id"] = (int)$postget["parent_id"];
		//echo " ".basename(__FILE__).":".__LINE__;
		
		if(!@$postget["id"]>0)
			$this->model->fields = array();
		else
			$this->model->get("WHERE id=".(int)$postget["id"]);	
		$file_thumb = @$this->model->fields['img'];
		$dst_dir = "../rwx/";
		if(@$this->htpf['img']["name"])
		{	
			$file_thumb = Misc::file_upload($dst_dir,$this->htpf['img']);
Misc::resize($dst_dir.$file_thumb,$dst_dir.$file_thumb,$this->config["file_thumb_size"],$this->config["file_thumb_type"]);
			if($file_thumb && $this->model->fields['img'] != $file_thumb)
			{
				Misc::file_unlink($dst_dir,$this->model->fields['img']);
				$fields['img'] = $file_thumb;
			}
		}
		$this->model->update($fields);
 	} 
	
	function delete($id)//override
	{
		$this->category_thumb_delete($id);
		$this->model->delete($id);
	}

	function get_parents_($_parent_id=0)
	{
			$parent_id = (int)$_parent_id;
			if ($_parent_id==0)
					return array();
			$categ_parent = $this->get("WHERE id=".$parent_id);
			$categs_parent = array();
			$categs_parent[] = $categ_parent;
			$res = $this->get_parents_($categ_parent["parent_id"]);
			if(count($res))
					foreach($res as $_res)
							$categs_parent[] = $_res;
			return $categs_parent;
	}

	function get_subcategs($categ_id)
	{
		$result = array();
		$sql = " WHERE parent_id = ".$categ_id;
		if (!$this->admin)
		{
			$sql .= " AND is_enabled=1";
		}
		$subcategs = $this->model->gets($sql);
		foreach($subcategs as $subcateg)
		{
			$result[] = $subcateg["id"];
			$_subcategs = $this->get_subcategs($subcateg["id"]);	
			if (count($_subcategs))
				foreach($_subcategs as $_subcateg)
						$result[] = $_subcateg;

		}
		return $result;
	}
	
	function get_parents_ids($categs)
	{
		$parents_ids = array();
		foreach($categs as $categ)
			$parents_ids [] = $categ["id"];
		return $parents_ids;
	}
	
	function category_thumb_delete($id)
	{
		$this->model->get($id);
		Misc::file_unlink("../rwx/",$this->model->fields['img']);
		$this->model->fields["img"]="";
		$this->model->save($this->model->fields);		
	}
	
}
?>
Return current item: Encaps Gallery