<?php

class filter {
   var $html = array();
   var $sql;
   var $obj;
   var $add_sql = array();
   var $filter_columns = array();

   function filter( $obj_name ) {
   	   $this->obj = new cobject ( $obj_name );
   }

   function add_filter( $attr_name ) {
   		$this->filter_columns[] = $attr_name;
   }

   function parse() {
   	   foreach( $this->obj->attrs as $name => $attr ) {
   	   	   if (!empty( $this->filter_columns ) && !in_array($name, $this->filter_columns ) ) continue;
   	   	   $arr = $attr['attr_obj']->getFilter($this->add_sql[$name]);
   	   	   if ($arr['html']) {
   			      $this->sql .= $arr['sql'];
   	   	   	$this->html[$name] = '<div class="form-group mb-1 form-sm"><label class="mb-0 small text-muted">'.($this->descs[$name]?$this->descs[$name]:$attr['desc']) .'</label><div>' . $arr['html'] .'</div></div>';
	   	   }
   	   }
   }
   
   function add_sql( $attr_name, $sql_where ) {
   		$this->add_sql[$attr_name] = $sql_where;   
   }

   function set_desc( $attr_name, $sql_where ) {
   		$this->descs[$attr_name] = $sql_where;   
   }


   function getHTML($attrs = array()) {
  		if (empty($attrs) ) return implode("",$this->html);
  		
  		$ret = array();
  		foreach( $attrs as $k ) {
  			if ( $this->html[$k] )
  				$ret[] = $this->html[$k];
  		}
  		
  		return  implode("<br>",$ret);
   }

   function getSQL() {
 		return $this->sql;
   }

}

?>
