<?
class form
{//DigiHex Systems\\


	function select_get_options($query, $selected=false) {
		global $X;

		$cols=explode(',', $X->STR->text_between('SELECT','FROM', $query));
		$val=trim($cols[0]);
		unset($cols[0]);
		$txt=trim(@implode($cols));

		if(!is_array($selected)) { $t=$selected; unset($selected); $selected[]=$t; }

		$sql=$X->DB->q($query);
		while($dane=$X->DB->far($sql)) $html.='<option value="'.$dane[0].'"'.(@in_array($dane[0],$selected) ? ' selected="selected"':'').'>'.$dane[1].'</option>';
		return $html;
	}


	function get_options_tree($query, $replace_what, $replace_with, $selected=false, $parent_depth=0, $level=0) {
		global $X;


		if($parent_depth>0 && $level>=$parent_depth-1) return false;
		if($level>0) $lvl=str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;',$level).str_repeat('&nbsp;',$level).'|&mdash;&nbsp;';
		if(!empty($selected) && !is_array($selected)) {
			$t=$selected;
			unset($selected);
			$selected[]=$t;
		}

		$sql=$X->DB->query(str_replace($replace_what, $replace_with, $query));
		while($dane=$X->DB->far($sql)) {
			$html.='<option value="'.$X->STR->outt($dane[0]).'"'.(@in_array($X->STR->outt($dane[0]),$selected) ? ' selected="selected"':'').'>'.$lvl.$X->STR->outt($dane[1]).'</option>';
			if($X->DB->num_rows($X->DB->query(str_replace($replace_what, $dane[0], $query)))>0) {
				$html.=$this->get_options_tree($query, $replace_what, $X->STR->outt($dane[0]), $selected, $parent_depth, $level+1);
			}
		}

		return $html;
	}


	function get_options_group_tree($query, $replace_what, $replace_with, $selected=false, $parent_depth=0, $level=0) {
		global $X;


		if($parent_depth>0 && $level>=$parent_depth-1) return false;
//		if($level>0) $lvl=str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;',$level).str_repeat('&nbsp;',$level).'|&mdash;&nbsp;';
		if(!empty($selected) && !is_array($selected)) {
			$t=$selected;
			unset($selected);
			$selected[]=$t;
		}

		$sql=$X->DB->query(str_replace($replace_what, $replace_with, $query));
		while($dane=$X->DB->far($sql)) {
			if($X->DB->num_rows($X->DB->query(str_replace($replace_what, $dane[0], $query)))>0) {
				$html.='<optgroup label="'.$X->STR->outt($dane[1]).'">'.$this->get_options_group_tree($query, $replace_what, $X->STR->outt($dane[0]), $selected, $parent_depth, $level+1).'</optgroup>';
			} else {
				$html.='<option value="'.$X->STR->outt($dane[0]).'"'.(@in_array($X->STR->outt($dane[0]),$selected) ? ' selected="selected"':'').'>'.$lvl.$X->STR->outt($dane[1]).'</option>';
			}
		}

		return $html;
	}


	function get_array_tree($query, $replace_what, $replace_with, $selected=false, $parent_depth=0, $level=0, &$array=false) {
		global $X;


		if($parent_depth>0 && $level>=$parent_depth-1) return false;
		if($level>0) $lvl=str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;',$level).str_repeat('&nbsp;',$level).'|&mdash;&nbsp;';
		if(!empty($selected) && !is_array($selected)) {
			$t=$selected;
			unset($selected);
			$selected[]=$t;
		}

		$sql=$X->DB->query(str_replace($replace_what, $replace_with, $query));
		while($dane=$X->DB->far($sql)) {
			$array[$X->STR->outt($dane[0])] = $lvl.$X->STR->outt($dane[1]);

			if($X->DB->num_rows($X->DB->query(str_replace($replace_what, $dane[0], $query)))>0) {
				$this->get_array_tree($query, $replace_what, $X->STR->outt($dane[0]), $selected, $parent_depth, $level+1, $array);
			}
		}

		if($level==0) return $array;
	}


	function get_options_from_array($array, $selected=false) {
		global $X;

		if(!is_array($array) || count($array)==0) return false;
		foreach($array as $value => $label) $html.='<option value="'.$value.'"'.($value==$selected ? ' selected="selected"':'').'>'.$label.'</option>';
		return $html;
	}


}//DigiHex Systems\\
?>