<?php
    
class attr_LINK extends attr  {
  
  	var $linked_obj;
    var $lobj_name;
  
    function __construct( $attr_name, $obj ) {
           $this->attr_name = $attr_name;
           $this->obj = $obj;
           $full_type = $obj->attrs[$attr_name]['type'];
           $arr = explode("|", $full_type);
           $lobj_name = $arr[1];
		   if ($lobj_name == $obj->obj_name ) cdie('recursion');
           $this->lobj_name = $lobj_name;
           //$this->linked_obj = new cobject( $lobj_name, $this->obj->recursion_depth );
    }
    function getDBType() {
    	return "int(12)";
    }
    
    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();
    	$out = '';
    	$tpl = $this->obj->attrs[$this->attr_name]['templ'];
		if ( $id && $tpl) {
				$this->getLinkedObj()->byId( $id );
  			  	$out = $this->getLinkedObj()->assign( $tpl );
    	} elseif ( $id ) {
                return $id;
        }

    	return $out;
    }

    function render_input() {
		$db = $this->getLinkedObj()->getDB();
		$db2 = $db->q( $this->getLinkedObj()->getSelect( $this->sql_where  ) );
    	$id = $this->getCurVal();
   		$tpl = $this->obj->attrs[$this->attr_name]['templ'];
		$out = '<select name="'.$this->attr_name.'" class="select form-control">';
		if ( !$this->obj->attrs[$this->attr_name]['mand'] ) {
			$out .= '<option value="0">---</option>';
		}
		while ( $db2->r() ) {
   			$this->getLinkedObj()->lRow( $db2 );
			$tt = $this->getLinkedObj()->assign( $tpl );
			if ( $id == $db2->f( 'id' ) ) 	$checked = 'selected';
			  else  $checked = '';
			$out .= '<option value="'.$db2->f( 'id' ).'" ' . $checked . '>' . $tt . '</option>';
		}
		$out .= '</select>';
		return $out;
    }
    
    function getFilter( $sql_where = '' ) {
		$db = $this->getLinkedObj()->getDB();
		$fname = $this->obj->obj_name . '_' . $this->attr_name;
			$uniq_ids = array();	
		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->getLinkedObj()->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->getLinkedObj()->lRow($db2);
	 		$tpl = $this->getLinkedObj()->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').'\'" value="'.$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').'\'" value="'.$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\" onChange="location.href=\''._getCurl(array( $fname, 'curpage' )).'&' . $fname .'=\'+$(this).val()">';
	 	$ret['html'] .= implode('', $tmp);
		$ret['html'] .= "</select>";
		} else {
		 	$ret['html'] .= implode(' | ', $tmp_s);
		}
		return $ret;
    }

}

?>