<?
class form
{//DigiHex Systems\\


	//TOOLS ===========================================================
	function link_js($href, $anchor, $img_before=false, $img_after=false) {
		if(!empty($img_before)) $img_before='<img src="'.$img_before.'" border="0" style="vertical-align:middle" /> ';
		if(!empty($img_after)) 	$img_after=' <img src="'.$img_after.'" border="0" style="vertical-align:middle" />';
		return $img_before.'<a onclick="return go(\''.$href.'\')" href="javascript:void(0);" class="link_js">'.$anchor.'</a>'.$img_after;
	}


	function potwierdzenie($link, $anchor, $tekst=false) {
		if(empty($tekst)) $tekst='Czy na pewno?';
		return '<a href="javascript:void(0)" onclick="return $.modal.confirm(\''.$tekst.'\', \''.$link.'\');">'.$anchor.'</a>';
	}


	function focus($id) {
		return '<script type="text/javascript">$("#'.$id.'").focus();</script>';
	}


	function tip($value, $on_target=false, $off_x=false, $off_y=false, $class=false) {
		if(empty($value)) return false;
		$value=str_replace('
','<br />',$value);
		$value=str_replace('"','&quot;',$value);
		return 'onmouseover="tip(this,\''.$value.'\','.($on_target==true ? 1:0).','.($off_x!=false ? $off_x:'null').','.($off_y!=false ? $off_y:'null').','.($class!=false ? '\''.$class.'\'':'null').')"';
	}


	function info($tip, $img=false) {
		global $X;
		if(empty($img)) $img=ICONS.'/info.png';
		return '<img src="'.$img.'" alt="" '.$X->FORM->tip($tip).' />';
	}


	function tr() {
		for($i=0;$i<func_num_args();$i++) $td.='<td>'.func_get_arg($i).'</td>';
		return '<tr>'.$td.'</tr>';
	}


	function tr_sep($p=3) {
		return '<tr><td colspan="25" style="padding:'.$p.'px 0px '.$p.'px 0px"><hr style="margin:0px; border:0px; border-top:1px solid #eee;" /></td></tr>';
	}


	function get($form_name, $inline_req, $html, $js_conf=false, $form_action='save') {
		global $X;
		if(!empty($inline_req) && is_array($inline_req)) {
			$req=$X->FORM->hidden($form_name.'_req_data', $X->STR->hide(serialize($inline_req)));
			$js_req=$this->get_req(false, $inline_req);
			$no_cfg_file=true;
		} else {
			$js_req=$this->get_req($form_name);
		}

		if(ADMIN===true) {
			$action=($form_action!=false ? $X->URL->get('action='.$form_action) : "");
			$_SESSION['form_pre_validation_backend'][$form_name]=true;
		} else {
			$action='/X/';
			unset($_SESSION['form_pre_validation_backend'][$form_name]);
		}

		$_SESSION['form_pre_validation'][$form_name]=md5('dh@'.$form_name.' >> '.$X->CONF->BROWSER['name'].' '.$X->CONF->BROWSER['major_version'].' << ajax_form');

		return '
			<div>
			<form name="'.$form_name.'_form" id="'.$form_name.'_form" method="post" action="'.$action.'" enctype="multipart/form-data">
				'.$html.$req.'
				'.(!empty($_GET['id']) ? $X->FORM->hidden('row_id',(int)$_GET['id']):'').'
			</form>
			</div>
			<script type="text/javascript" id="'.$form_name.'_form_js">/*<![CDATA[*/  var '.$form_name.'_xForm; $("#'.$form_name.'_form").xForm({'.($no_cfg_file==true ? '"inline_req":1,':'').'"req":'.$js_req.(!empty($js_conf) ? ','.substr(json_encode($js_conf),1,-1):'').'}, function(){ '.$form_name.'_xForm=this; });  /*]]>*/</script>
		';
	}


	function get_normal($form_name, $html, $form_action=false) {
		global $X;


		if(ADMIN===true) {
			$action=($form_action!=false ? $X->URL->get('action='.$form_action) : "");
			$_SESSION['form_pre_validation_backend'][$form_name]=true;
		} else {
			$action=($form_action!=false ? $form_action : URL);
			unset($_SESSION['form_pre_validation_backend'][$form_name]);
		}

		$_SESSION['form_pre_validation'][$form_name]=md5('dh@'.$form_name.' >> '.$X->CONF->BROWSER['name'].' '.$X->CONF->BROWSER['major_version'].' << ajax_form');
		$_SESSION['form_validation'][$form_name]=true;

		return '
			<div>
			<form name="'.$form_name.'_form" id="'.$form_name.'_form" method="post" action="'.$action.'" enctype="multipart/form-data">
				'.$html.$req.'
				'.(!empty($_GET['id']) ? $X->FORM->hidden('row_id',(int)$_GET['id']):'').'
			</form>
			</div>
		';
	}


	function internal_validate($form_id) {
		global $X;

		require_once(INC.'/forms/'.$form_id.'.php');
		$this->FORM=$_GET['form']=$form_id.'_form';
		$GLOBALS['VALIDATION_PASSED']=true;

		if(is_array($REQ) && count($REQ)>0) foreach($REQ as $req) {
			if(@in_array($req[0],$this->FILE_FIELDS)) $req[1].='_';
			$PASS=$this->check_field($req);
			if(@in_array($req[0],$this->FILE_FIELDS)) $req[1]=substr($req[1],0,-1);
			if($PASS==false) {
				$GLOBALS['VALIDATION_PASSED']=false;
				$GLOBALS['VALIDATION_ERRORS'][]=array(
					'type' 			=> $req[0],
					'name' 			=>	$req[2],
					'label' 			=> $req[3],
					'message_type' => $this->MESSAGE_TYPE,
					'message_data' => $message,
				);
			}

		}

		if($GLOBALS['VALIDATION_PASSED']==true) {
			return true;
		} else {
			return @serialize($GLOBALS['VALIDATION_ERRORS']);
		}
	}


	function check_field($req) {
		$type 		= $req[0];
		$min_len		= $req[1];
		$post_name 	= $req[2];
		$value 		= @trim($_POST[$post_name]);
		$message 	= $req[3];
		$config 		= $req[4];

		return require(LIBS_PATH.'/_form/validation_methods.php');
	}


	function get_req($file, $array=false) {
		if(!empty($array)) {
			$REQ=$array;
		} else {
			$cfg=INC_PATH.'/forms/'.$file.'.php';
			if(file_exists($cfg)) require_once($cfg);
		}

		if(is_array($REQ) && count($REQ)>0) foreach($REQ as $req) {
			if($req[1]==0) continue;
			$data['TYP']=$req[0];
			$data['NAM']=$req[2];
			$data['LEN']=$req[1];
			if(!empty($req[3])) $data['LAB']=$req[3];
			if(!empty($req[4]['ext'])) $data['EXT']=$req[4]['ext'];
			$JS_REQ[]=$data;
			unset($data);
		}
		return json_encode($JS_REQ);
	}


	function label($for, $label, $req=false) {
		return '<label'.(!empty($for) ? ' for="'.$for.'"':'').'><span>'.$label.'</span>'.($req==true ? ' <b class="req">*</b>':'').'</label>';
	}


	function vars2hidden($data, $skip=false) {
		global $X;

		if(count($data)==0) return false;
		if(!empty($skip)) $exclude=@explode('&',$skip);
		foreach($data as $key => $value) {
			if(!empty($value) && !@in_array($key,$exclude)) $html.=$X->FORM->hidden($key,$value);
		}
		return $html;
	}



	function sortable_parse($table) {
		global $X;
		$X->JS[]=JS.'/jquery.sort/jquery.tablesort.js';

		if(empty($table['body'])) $table['body']=$table['empty_body'];

		$row_index=1;
		if(count($table['body'])>0) foreach($table['body'] as $cols) {
			$tbody.='
				<tr'.HOVER.'>
					<td class="drag2sort"><i></i>'.$X->FORM->hidden_id($table['name'].'_row['.$row_index.']','',$row_index).'</td>
					'.str_replace('[]','['.$row_index.']',$cols).'
					'.($table['simple']!=true ? '<td class="sortable_actions"><a href="#" onclick="sortable_del_row(this); return false;"><span class="ico b_minus"></span></a></td>':'').'
				</tr>
			';
			$row_index++;
		}

		$table['head']=str_replace('</th>','&nbsp;&nbsp;</th>',$table['head']);

		return '
			<table id="'.$table['name'].'_sortable" class="sortable inline'.(!empty($table['class']) ? ' '.$table['class']:'').'"'.(!empty($table['width']) ? ' width="'.$table['width'].'"':'').'>
				'.(!empty($table['head']) ? '<thead><tr><th class="drag2sort"><i></i></th>'.$table['head'].($table['simple']!=true ? '<th><a href="#" onclick="sortable_add_row(this); return false;"><span class="ico b_plus"></span></a></th>':'').'</tr></thead>':'').'
				<tbody>'.$tbody.'</tbody>
			</table>

			<script type="text/javascript">
				$("#'.$table['name'].'_sortable tbody").tableDnD();
				$("#'.$table['name'].'_sortable").attr("last_row_id",$("#'.$table['name'].'_sortable tbody > tr").length);
			</script>
		';
	}


	function sortable_save($data) {
		global $X;

		$pozycja=1;
		$f=$data['name'];
		$cond='parent_id="'.$data['parent_id'].'" AND param="'.$data['param'].'" ';

		if(count($_POST[$f.'_row'])>0) foreach($_POST[$f.'_row'] as $post_key) {
			$query='';
			$empty_row=true;

			if(count($data['fields'])>0) foreach($data['fields'] as $field) {
				$value = $_POST[$f.'_'.$field][$post_key];
				if($data['dot_conv']==true) $value=str_replace(',','.',$value);

				$query.=$field.'="'.$value.'", ';
				if(!empty($value)) $empty_row=false;
			}

			if($empty_row==false) {
				if($X->DB->fast(' SELECT id FROM '.$data['table'].' WHERE '.$cond.' AND pozycja="'.$pozycja.'" ')==0) {
					$X->DB->q(' INSERT INTO '.$data['table'].' (parent_id,param,pozycja) VALUES ("'.$data['parent_id'].'","'.$data['param'].'","'.$pozycja.'") ');
				}
				$X->DB->q('UPDATE '.$data['table'].' SET '.substr($query,0,-2).' WHERE '.$cond.' AND pozycja="'.$pozycja.'" ');
				$pozycja++;
			}
		}

		$X->DB->q(' DELETE FROM '.$data['table'].' WHERE '.$cond.' AND pozycja>="'.$pozycja.'" ');
	}


	function sortable_save2($data) {
		global $X;

		$pozycja=1;
		$f=$data['name'];
		$cond='parent_id="'.$data['parent_id'].'" AND param="'.$data['param'].'" ';

		if(count($_POST[$f.'_row'])>0) foreach($_POST[$f.'_row'] as $post_key) {
			$X->DB->q(' UPDATE '.$data['table'].' SET pozycja="'.$pozycja.'" WHERE '.$cond.' AND id="'.(int)$_POST['oferta_id'][$post_key].'" ');
			$pozycja++;
		}
	}


}//DigiHex Systems\\
?>