<?php


function GETFORMITEM( $VALUE, $TYPE, $NAME, $params = array() ) {
//VERSION 2006-03-20 , ale na l2 moze byc swiezsze 
//VERSION 2008-11-25 , added shorted substr for enum type by sqix
//VERSION 2010-06-10 , rm ' from enum, split(depreciated) -> explode by plabudda
//VERSION 2012-04-24 , added decimal(58,8) for search purps
	global $thiss;

	if (strstr($TYPE, "enum")) {
		list($SMIEC, $TYPE) = explode("(", $TYPE, 2);
		list($TYPE, $SMIEC) = explode(")", $TYPE, 2);
		$TYPE = explode(",", $TYPE);

		$html_field_name = $NAME;
		$attr_multiple = '';
		if (isset($params['multiple']) && $params['multiple'] == true) {
			$attr_multiple = ' multiple ';
			$html_field_name .= '[]';
		}
		echo'<select name="'.$html_field_name.'" id="'.$NAME.'" '.$attr_multiple.'>';//TODO 2006-03 sprawdzic czy dawanie do wyszukiwania getformitem w menu % zawsze moze dawac??
		// add default option '%' in search field
		//if ((in_array($_SESSION['CURRENT_MENU'], array('USERS','TELBOXES','DEVICES','BUILDINGS')))
		if (!in_array($_SESSION['CURRENT_MENU'], array($thiss->DETECT_EDIT_FUNCTION, 'NEW_RECORD'))) {
			$sel = false;
			if (isset($params['multiple']) && $params['multiple'] == true) {
				if (is_array($VALUE)) {
					if (empty($VALUE) || in_array('%', $VALUE)) {
						$sel = true;
					}
				}
			}
			$sel = ($sel)? ' selected="selected"' : '';
			echo'<option value="%"'.$sel.'>%</option>';
		}
		foreach ($TYPE as $v) {
			$v = trim($v,"' ");//usuwa spacje i ' z poczatku i konca
			//echo "***  $v =============== $VALUE *** * *** *";
			$SUBSTR_VALUE = ($_SESSION['CURRENT_MENU'])? 26 : 100;
			$sel = false;
			if (isset($params['multiple']) && $params['multiple'] == true) {
				if (is_array($VALUE) && in_array($v, $VALUE)) {
					$sel = true;
				}
			} else if (!empty($VALUE) && "$VALUE" == $v) {
				$sel = true;
			}
			$sel = ($sel)? ' selected="selected"' : '';
			echo'<option value="'.$v.'"'.$sel.'>'.substr($v,0,$SUBSTR_VALUE).' </option>';
		} //EOF foreach
		echo'</select>';
	}
	else if ((strstr($TYPE, "char")) || (strstr($TYPE, "int")) || (strstr($TYPE, "decimal"))   ) {
		//echo "THIS IS CHAR";
		list($SMIEC, $TYPE) = explode("(", $TYPE, 2);
		list($TYPE, $SMIEC) = explode(")", $TYPE, 2);
		$SIZE = strlen($VALUE) + 5;

		if (!$_SESSION['CURRENT_MENU']) {
			if (strlen($VALUE) == 0) $SIZE = strlen($VALUE) + 10;
			else $SIZE = strlen($VALUE) + 5;
		}
		if ($_SESSION['CURRENT_MENU'] == 'NEW_RECORD') $SIZE = $TYPE;
		if ($SIZE > 50) $SIZE = 50;

		$SIZE = "size=$SIZE";
		if (in_array($_SESSION['CURRENT_MENU'], array('USERS','TELBOXES','DEVICES','BUILDINGS'))) if(!($VALUE)) $VALUE = "%";
		echo'<input type="text" name="'.$NAME.'" id="'.$NAME.'" value="'.$VALUE.'" maxlength="'.$TYPE.'" '.$SIZE.' />';
	}
	else if ((strstr($TYPE, "datetime")) || (strstr($TYPE, "date") || strstr($TYPE, "timestamp"))) {

      if(strstr($TYPE,'(')) { //@2012-10-12 by sqix : warning byly , chyba typ powinien byc w nawiasie, a jak nie jest to else...
		list($SMIEC, $TYPE) = explode("(", $TYPE, 2);
		list($TYPE, $SMIEC) = explode(")", $TYPE, 2);
      } else $TYPE=10;
		$VALUE = substr($VALUE, 0, 10);
		$SIZE = 10;
		$SIZE="size=$SIZE";
		echo'<input type="text" name="'.$NAME.'" id="'.$NAME.'" value="'.$VALUE.'" maxlength="'.$TYPE.'" '.$SIZE.' />';
	}
	else if(strstr($TYPE,"double") || strstr($TYPE,"float")) {
		list($SMIEC,$TYPE)=explode("(",$TYPE,2);
		list($TYPE,$SMIEC)=explode(")",$TYPE,2);
		$VALUE=substr($VALUE,0,10);
		$SIZE = 4;
		$SIZE="size=$SIZE";
		echo'<input type="text" name="'.$NAME.'" id="'.$NAME.'" value="'.$VALUE.'" maxlength="'.$TYPE.'" '.$SIZE.' />';
	}
	else if(strstr($TYPE,"text")) {
		//echo "THIS IS CHAR";
		//$SIZE = strlen($VALUE) + 50;
		//$SIZE="size=$SIZE";
		if(in_array($_SESSION['CURRENT_MENU'],array('USERS','TELBOXES','DEVICES','BUILDINGS'))) if(!($VALUE)) $VALUE = "%";
		echo'<textarea name="'.$NAME.'" id="'.$NAME.'" rows="5" cols="100">'.$VALUE.'</textarea>';
	}
	else {
		echo "THIS IS TEXT_OR_OTHER $TYPE";
	}
}



function GETFORMITEM2( $name, $sql_type, $value = '' ) {
//VERSION 2006-03-20 , ale na l2 moze byc swiezsze 
//VERSION 2008-11-25 , added shorted substr for enum type by sqix
//VERSION 2010-06-10 , rm ' from enum, split(depreciated) -> explode by plabudda
//VERSION 2010-10-13 , add % in enum, add @param $name by plabudda
// TODO: add DEAFULT and not null to TYPE

	$ret = '';
	//$type = $_SESSION[$table_column]['TYPE'][$field];
	$type = $sql_type;
	//$name = $field;//$_SESSION[$table_column]['DESC'][ $_SESSION[$table_column]['DESC_TO_KEY'][$field] ];
	//if ($_SESSION['CURRENT_MENU']=='NEW_RECORD') $name = 'NEW_RECORD['.$name.']';

	if(strstr($type,"enum")) {
		$type = end( explode("(",$type) );
		$type = reset( explode(")",$type) );
		$type = explode(",", $type);

		$ret .= '<select name="'.$name.'" id="'.$name.'">';
			$ret .= '<option value="%">'." [ Wybierz ] ".'</option>';
			sort($type);
			foreach ($type as $v) {
				$v = trim($v,"' ");//usuwa spacje i ' z poczatku i konca
				$substr_value=26;
				$sel = ($v=="$value")?'selected="selected"':'';
				$ret .= '<option value="'.$v.'" '.$sel.'>'.substr($v,0,$substr_value).' </option>';
			} //EOF foreach
		$ret .= '</select>';
	}
	else if((strstr($type,"char")) || (strstr($type,"int"))) {
		$type = end( explode("(",$type) );
		$type = reset( explode(")",$type) );
		$SIZE = ($type > 50)? 50 : $type;
		$SIZE="size=$SIZE";
		$ret .= '<input type="text" name="'.$name.'" id="'.$name.'" value="'.$value.'" maxlength="'.$type.'" '.$SIZE.' />';
	}
	else if((strstr($type,"datetime"))||(strstr($type,"date")||strstr($type,"timestamp"))) {
		$type = end( explode("(",$type) );
		$type = reset( explode(")",$type) );
		$value = substr($value, 0, 10);
		$SIZE="size=10";
		$ret .= '<input type="text" name="'.$name.'" id="'.$name.'" value="'.$value.'" maxlength="'.$type.'" '.$SIZE.' class="date-pick" />';
	}
	else if(strstr($type,"double") || strstr($type,"float")) {
		$type = end( explode("(",$type) );
		$type = reset( explode(")",$type) );
		$value=substr($value,0,10);
		$SIZE="size=6";
		$ret .= '<input type="text" name="'.$name.'" id="'.$name.'" value="'.$value.'" maxlength="'.$type.'" '.$SIZE.' />';
	}
	else if(strstr($type,"text")) {
		$ret .= '<textarea name="'.$name.'" id="'.$name.'" rows="5" cols="100">'.$value.'</textarea>';
	}
	else {
		$ret .= "UNKNOWN $type";
	}
	return $ret;
}



/*

function GETFORMITEM($VALUE,$TYPE,$NAME) {
//VERSION 2006-03-20 , ale na l2 moze byc swiezsze 
//VERSION 2008-11-25 , added shorted substr for enum type by sqix

global $thiss;
//echo "***************$VALUE,,,,$TYPE";
if(strstr($TYPE,"enum")) { 
list($SMIEC,$TYPE)=split("\(",$TYPE);
list($TYPE,$SMIEC)=split("\)",$TYPE);
list($TYPE1,$SMIEC1)=split(",",$TYPE);

$TYPE=explode(",",$TYPE);
echo "<select name=$NAME id='$NAME'>"; //TODO 2006-03 sprawdzic czy dawanie do wyszukiwania getformitem w menu % zawsze moze dawac??
if(($_SESSION[CURRENT_MENU]==USERS) || ($_SESSION[CURRENT_MENU]==TELBOXES) || ($_SESSION[CURRENT_MENU]==DEVICES) || ($_SESSION[CURRENT_MENU]==BUILDINGS) ||($_SESSION[CURRENT_MENU]!=$thiss->DETECT_EDIT_FUNCTION)) echo "<option value='%'>%</option>";
foreach ($TYPE as $value) {
//echo "***  $value =============== $VALUE *** * *** *";
	if($_SESSION[CURRENT_MENU]) $SUBSTR_VALUE=20; else $SUBSTR_VALUE="100";


if ("'$VALUE'"==$value) { echo "<option value=$value selected=true>".substr($value,0,$SUBSTR_VALUE)." </option>"; } else echo "<option value=$value>".substr($value,0,20)."</option>";
 } //EOF foreach
echo "</select>";
} else if((strstr($TYPE,"char")) || (strstr($TYPE,"int"))) { 
//echo "THIS IS CHAR";
list($SMIEC,$TYPE)=split("\(",$TYPE);
list($TYPE,$SMIEC)=split("\)",$TYPE);
$SIZE = strlen($VALUE) + 5;

if(!$_SESSION[CURRENT_MENU]) { 
	if(strlen($VALUE)=='0') $SIZE=strlen($VALUE)+10 ;
	else $SIZE=strlen($VALUE)+5;
}

if($_SESSION[CURRENT_MENU]=='NEW_RECORD') $SIZE=$TYPE;

$SIZE="size=$SIZE";
if(($_SESSION[CURRENT_MENU]==USERS) || ($_SESSION[CURRENT_MENU]==TELBOXES) || ($_SESSION[CURRENT_MENU]==DEVICES) || ($_SESSION[CURRENT_MENU]==BUILDINGS) ) if(!($VALUE)) $VALUE = "%";
echo "<INPUT TYPE=TEXT NAME='$NAME' id='$NAME' VALUE='$VALUE' maxlength='$TYPE' $SIZE >";
} else  if((strstr($TYPE,"datetime"))||(strstr($TYPE,"date")||strstr($TYPE,"timestamp"))) { 
list($SMIEC,$TYPE)=split("\(",$TYPE);
list($TYPE,$SMIEC)=split("\)",$TYPE);
$VALUE=substr($VALUE,0,10);
$SIZE = 10 ;
$SIZE="size=$SIZE";
echo "<INPUT TYPE=TEXT NAME='$NAME' id='$NAME' VALUE='$VALUE' maxlength='$TYPE' $SIZE >";
} else  if(strstr($TYPE,"double")) { 
list($SMIEC,$TYPE)=split("\(",$TYPE);
list($TYPE,$SMIEC)=split("\)",$TYPE);
$VALUE=substr($VALUE,0,10);
$SIZE = 4 ;
$SIZE="size=$SIZE";
echo "<INPUT TYPE=TEXT NAME='$NAME' id='$NAME' VALUE='$VALUE' maxlength='$TYPE' $SIZE >";
} else if(strstr($TYPE,"text")) { 
//echo "THIS IS CHAR";
//$SIZE = strlen($VALUE) + 50;
//$SIZE="size=$SIZE";
if(($_SESSION[CURRENT_MENU]==USERS) || ($_SESSION[CURRENT_MENU]==TELBOXES) || ($_SESSION[CURRENT_MENU]==DEVICES) || ($_SESSION[CURRENT_MENU]==BUILDINGS) ) if(!($VALUE)) $VALUE = "%";
echo "<TEXTAREA NAME='$NAME' id='$NAME' rows='5' cols='100'>$VALUE</TEXTAREA>";
}else { 
echo "THIS IS TEXT_OR_OTHER $TYPE";
}
}
*/
