class.category.php 4.17 KB
<?php
/**
* @author:  Bunzia Alexander <nifus@mail.ru> <http://www.weblancer.net/users/nifus/>
* @copyright: Copyright (c) 2010, Bunzia Alexander
* @version: 1.0
* @license: http://www.gnu.org/copyleft/gpl.html GNU/GPL
* @package: HiLO CMS
*/



include_once(MAIN_SOURCE_PATH.'/inc/class.ns_tree.php');

class category_q extends ns_tree_q{


	

  /**
  * Фильтр по номеру алиасу категории
  *
  * @var: int $v - номер категории
  * @return: bool
  */
	public function where_alias($v){
		if ( empty($v) || ! sys_is_alias($v)  ){
				return FALSE;
			}
			return $this -> set_where( $this -> alias.'.c_alias',$v);
	}
	
  /**
  * Фильтр по номеру категории
  *
  * @var: int $v - номер категории
  * @return: bool
  */
	public function where_cid($v){
	  $v = intval($v);
		if ( empty($v)  ){
			return FALSE;
		}
		return $this -> set_where( $this -> alias.'.c_id',$v);
	}
	
	public function where_name($v){
	 
		return $this -> set_where( $this -> alias.'.c_name',$v);
	}

	protected function c_parent_id($v){
		return category::get_parent($v,$this -> table,$this -> alias);
	}
	
	static function upl_img($v){
	   //  загрузка аватарки
	  $a = new upload_img('image');
	      //  500 кб
	  $a -> set_max_size( 5000000 );
	      //  результирующий файл
	  $a -> set_file($v);
	      //  сюда будем грузить
	  $a -> set_path( 'market/img');
	      //  полный путь к папке
	  $a -> set_base( MAIN_URL.'/media/market/img');
	  return $a -> get('img',1,'c_image');
	}
	
  
	

	static function category_url($v){
		return sprintf(URL_CATEGORY,$v);
	}
	
	static function image_src($v){
		return MAIN_URL.'/media/market/img/'.$v;
	}
	

} //end class



class category extends ns_tree{
 	
	protected function __construct( $array=array(),$table,$alias ){
		$this -> table = $table;
		$this -> alias = $alias;
		$this -> info = $array;
		 $this -> main_key = $array['c_id'];
		
	}	
	
		//	загружаем указанный
	static function load_id($id,$table='system_category',$alias='c',$row = false){
		if ( false!==$res ){
			$n = new category_q( $table,$alias);
			if ( false==$n -> where_id($id) ){
				return false;
			}
			$n -> get('*',false);
			$row = $n -> row();
			if ( false===$row ){
				return false;
			}
		}
		return new category($row,$table,$alias);
	}
		//	создаём новый объект
	static function create($table,$alias){
		
		return  new category(array('c_id'=>0),$table,$alias);

		
	}
	

	public function error(){
  		return $this -> last_error;
	}
	
	
	public function get($k){
		if ( isset($this -> info[$k]) ){
			return $this -> info[$k];
		} 
		$this -> make_error(ERROR_500, sprintf(A_NOT_VAR,$k));
	}
	
	public function set($k,$v){
		if ( method_exists($this,'set_'.$k) ){
			$f = 'set_'.$k;
			return $this -> $f($v);
		}
		$this -> set_info[$k] = $v;
		return true;
	}
	


	public function set_c_alias($v){
	
		$v= category::make_alias($v,$this -> table,$this -> alias);
		if ( empty($v) ){
			$this -> make_error(E_NSTREE_EMPTY_ALIAS);
			return false;
		}
		$this -> set_info['c_alias']=$v;
		return true;
	}

	
	


	
	public function id(){
		return $this -> info['c_id'];
	}	
	
	
	
	
	
	
	
	static function make_alias($v,$table,$alias){
	
		$v =  sys_translit($v);
 		if ( !category::is_exists($v,$table,$alias) ){
 			return $v;
 		}
 		return category::make_alias($v.'-'.time(),$table,$alias );
	}
	 		
	
	function  make_error($msg){
		$this -> last_error = $msg;
	}


	 		//	проверяем существование новость по траснлит адресу
 	static function is_exists($v,$table,$alias){
 		$n = new category_q($table,$alias);
 		if ( !$n -> where_alias($v) ){
 			return false;
 		}
 		$n -> get('COUNT(*)');
 		list($count) = $n -> row();
 		if ( $count >0 ){
 			return true;
 		}
 		return false;
 	}
 	
 	static function get_path($id,$table,$alias='a'){
	  $p = new ns_tree_q($table,$alias);
	  $p -> where_up($id);
	  $p -> order_left('ASC');
	 // $p -> set_debug(1);
	  $p -> get('c_alias,c_name');
	  $rows= array();
	  while( list($c_alias,$c_name) = $p -> row() ){
	    $rows[$c_alias]=$c_name;
	  }
	  return $rows;
	}
	
	
}//end class



?>