<?php
    
class attr_tree_object extends attr  {

  	var $linked_obj;
	var $sql_where;
    var $lobj_name;
    
    function __construct( $attr_name, $obj ) {
           $this->attr_name = $attr_name;
           $this->obj = $obj;
           $full_type = $obj->attrs[$attr_name]['type'];
           $lobj_name = $obj->attrs[$attr_name]['templ'];
           if ($lobj_name == "") {
           		$lobj_name = $obj->obj_name;
           }

           $this->lobj_name = $lobj_name;
    }

    function getDBType() {
    	return "varchar(255)";
    }

    function lPost() {
    	if (is_array($_POST[$this->attr_name])) { 
        	$this->obj->set($this->attr_name,implode(",",$_POST[$this->attr_name]));
        } else {
            $this->obj->set($this->attr_name,$_POST[$this->attr_name]);
        }
    }  

    function getLinkedObj() {
        if ( !$this->linked_obj )  {
            $this->linked_obj = new cobject( $this->lobj_name, $this->obj->recursion_depth );
        }
        return $this->linked_obj;
    }
    
    function render_text( $crop = false ) {
    	$id = $this->getCurVal();
    	if (!$id) return '';

    	if ( $id ) $ids = explode( ",", $id );

       	$this->getLinkedObj()->byId( $id );
        	
      if ($this->getLinkedObj()->get('path_titles')) {
        return $this->getLinkedObj()->assign('{path_titles}');
    } elseif ($this->getLinkedObj()->get('path_ids')) {
   			$template_path = explode(',', $this->getLinkedObj()->get('path_ids'));
   			$path_data = array();
  			foreach( $template_path as $id ) {
  				$this->getLinkedObj()->byId( $id );
  				$path_data[] =  $this->getLinkedObj()->assign('{title}');
  			}
			
		  	return implode(' > ', $path_data);
   		} else {
			return $this->getLinkedObj()->assign('{title}');
   		}

    }


    function render_input() {
  		$db = $this->getLinkedObj()->getDB();
    	$id = $this->getCurVal();
   		$tpl = $this->obj->attrs[$this->attr_name]['templ'];

      	$out  = '<select name="'.$this->attr_name.'" id="'.$this->attr_name.'" class="form-control">';
      	$out .= '<option value="0">---</option>';
      	if ($this->sql_where) {
          $out .= $this->r_subtree(intval($this->sql_where));
        } else {
          $out .= $this->r_subtree(0);
        }
        $out .= "</select>";
	   	return $out;
    }
    
    function r_subtree( $cat, $level = 0 ) {
		global $R, $core_db;

		$cid = $this->getCurVal();
		if ($cid) {
			$cids  = explode(",", $cid );
		} else {
			$cids = array();
		}
		
		$db2 = $this->getLinkedObj()->getDB()->q( $this->getLinkedObj()->getSelect( ' AND `parent_id` = "'.$cat.'" AND !`_sys_unvisible` ORDER BY `title` ') );
		while ( $db2->r() ) {
			
			if ( in_array( $db2->f('id'), $cids  ) ) {
				$check = 'selected';
			} else {
				$check = '';	
			}
			$out.= '<option value="'.$db2->f('id').'" '.$check.'>';
			for( $i=0; $i<$level; $i++ ) {
			 $out.= '&nbsp;&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 = '' ) {
		return '';
    }

}

?>