<?php
    
class attr_tree extends attr  {
  
  	var $linked_obj;
  
    function __construct( $attr_name, $obj ) {
           $this->attr_name = $attr_name;
           $this->obj = $obj;
           $full_type = $obj->attrs[$attr_name]['type'];
           $lobj_name = $obj->obj_name;
		   $this->linked_obj = new cobject( $lobj_name, $this->obj->recursion_depth );
    }
    function getDBType() {
    	return "int(12)";
    }

    function render_text( $crop = false ) {
    	$id = $this->getCurVal();
    	$out = '';
    	$tpl = $this->obj->attrs[$this->attr_name]['templ'];
		if ( $id ) {
				$this->linked_obj->byId( $id );
  			  	$out = $this->linked_obj->assign( $tpl );
    	}
    	return $out;
    }

    function render_input() {
		$db = $this->linked_obj->getDB();
		$db2 = $db->q( $this->linked_obj->getSelect( $this->sql_where  ) );
    	$id = $this->getCurVal();
   		$tpl = $this->obj->attrs[$this->attr_name]['templ'];
		$out = '<select id="'.$this->attr_name.'" name="'.$this->attr_name.'" class="select form-control">';
		$out .= '<option value="0">---</option>';
		$out .= $this->r_subtree(0);
		$out .= '</select>';
		return $out;
    }
    
   	function r_subtree( $cat, $level = 0 ) {
		global $R, $core_db;
		
		$db2 = $this->obj->getDB()->q( $this->obj->getSelect( ' AND `'.$this->attr_name.'` = "'.$cat.'" ORDER BY `title` ') );
		while ( $db2->r() ) {
            
            if ( $db2->f('id') ==  $this->getCurVal()  ) {
				$check = 'selected';
			} else {
				$check = '';	
			}
            
			$out.= '<option '.$check.' value="'.$db2->f('id').'">';
			for( $i=0; $i<$level; $i++ ) {
			 $out.= '&nbsp;&nbsp;&nbsp;&nbsp;';
			}
			$out .= '|-> ' . $db2->f('title');
			$out.= '</option>';
			$out .= $this->r_subtree($db2->f('id'), ($level+1));
		}

		return $out;
	}

    
    function getFilter( $sql_where = '' ) {
		$db = $this->linked_obj->getDB();
		$fname = $this->obj->obj_name . '_' . $this->attr_name;
		if ( $this->obj->obj_name ) {
			$db3 = $this->obj->getDB()->q( $this->obj->getSelectEx( ' DISTINCT(`'.$this->attr_name.'`) as `'.$this->attr_name.'` ', '' ) );
			while ( $db3->nr() ) {
				$uniq_ids[] = $db3->f($this->attr_name);
			}
		} else {
			$uniq_ids = array();	
		}

	 	if (!$_GET[$fname]) {
	 		$tmp = array('<option onClick="location.href=\''._getCurl(array( $fname, 'curpage' )).'\'" selected>Все</option>');
	 		$tmp_s = array('<a href="'._getCurl(array( $fname, 'curpage' )).'"><b>Все</b></a>');
	 	} else {
	 		$tmp = array('<option onClick="location.href=\''._getCurl(array( $fname, 'curpage' )).'\'">Все</option>');
	 		$tmp_s = array('<a href="'._getCurl(array( $fname, 'curpage' )).'">Все</a>');
	 	}
	 	
	 	$db2 = $db->q( $this->linked_obj->getSelect( $sql_where)  );
	 	$c = 0;
	 	while ( $db2->r() ) {
	 		if ( $this->obj->obj_name && !in_array( $db2->f('id'), $uniq_ids ) ) continue;
	 		$tpl = strip_tags( $this->getAttrTempl() );
	 		$this->linked_obj->lRow($db2);
	 		$tpl = $this->linked_obj->assign($tpl, true);
	 		if ( strlen($tpl) > 100 )	$tpl = substr($tpl,0,100) . '...';
	 		
		 	if ($_GET[$fname] != $db2->f('id')) {
	 			$tplh = '<option onClick="location.href=\''._getCurl(array( $fname, 'curpage' )).'&' . $fname .'='.$db2->f('id').'\'">' .$tpl . '</option>';
	 			$tplh_s = '<a href="'._getCurl(array( $fname, 'curpage' )).'&' . $fname .'='.$db2->f('id').'">' .$tpl . '</a>';
	 		} else {
	 			$tplh = '<option onClick="location.href=\''._getCurl(array( $fname, 'curpage' )).'&' . $fname .'='.$db2->f('id').'\'" selected>' . $tpl . '</option>';
	 			$tplh_s = '<a href="'._getCurl(array( $fname, 'curpage' )).'&' . $fname .'='.$db2->f('id').'"><b>' .$tpl . '</b></a>';
			 	$ret['sql'] = " AND " . ($this->obj->obj_name?('`'.$this->obj->obj_name."`.`".$this->attr_name.'`'):($this->attr_name)) . " = '" . $db2->f('id') . "' " ;
	 		}
	 		$tmp[] = $tplh; 
	 		$tmp_s[] = $tplh_s; 
	 		$c++;
	 	}
	 	
	 	if ($c > 10 ) {
		$ret['html'] = "<select  class=\"form-control form-control-sm\">";
	 	$ret['html'] .= implode('', $tmp);
		$ret['html'] .= "</select>";
		} else {
		 	$ret['html'] .= implode(' | ', $tmp_s);
		}
		return $ret;
    }

}

?>