Location: PHPKode > scripts > PHP 4 Applications > php-4-applications/p4a_SimpleForm.php
<?php
	require_once("p4a_control.php");
	require_once("p4a_form.php");

class p4a_SimpleCombo
{
	var $name;
	var $data;

	function p4a_SimpleCombo( $_cn, $_array)
	{
		$this->name = $_cn;

		$i = 0;
		foreach( $_array as $k => $v)
		{
			$this->data[$i][0] = $k;
			$this->data[$i++][1] = $v;
		}
	}

	function flush( $_selec="")
	{
		
		$htmlresults = "<select name=\'c_".$this->name."\'>";
		for($i=0;$i<count($this->data);$i++)
		{
			if (empty($_selec))
				$htmlresults.="<option value=\'".htmlentities(stripslashes($this->data[$i][1]), ENT_QUOTES)."\'>".htmlentities(stripslashes($this->data[$i][0]), ENT_QUOTES)."</option>";
			else
			{
				if (stripslashes($this->data[$i][1])==$_selec)
					$htmlresults.="<option value=\'".htmlentities(stripslashes($this->data[$i][1]), ENT_QUOTES)."\' selected>".htmlentities(stripslashes($this->data[$i][0]), ENT_QUOTES)."</option>";
				else
					$htmlresults.="<option value=\'".htmlentities(stripslashes($this->data[$i][1]), ENT_QUOTES)."\'>".htmlentities( stripslashes($this->data[$i][0]), ENT_QUOTES)."</option>";
			}
		}
		$htmlresults.= "</select>";
		
		return $htmlresults;
	}

}

class p4a_SimpleForm extends p4a_Control
{
	var $data;
	var $fields;
	var $num_cols;
	var $onClickOk;
	var $onClickCancel;

	function p4a_SimpleForm()
	{
		$this->type ="simpleform";
		$this->num_cols = 1;
	}

	function setField( $_cn, $_la, $_ty='text', $_sz='60', $_edt=1, $_tableref='', $_vis_field='', $_inv_field='', $_fc='')
	{
		$this->fields[$_cn]['label']=$_la;
		$this->fields[$_cn]['editable']=$_edt;
		$this->fields[$_cn]['combo']='';
		$this->fields[$_cn]['type']=$_ty;
		$this->fields[$_cn]['size']=$_sz;

		if ((!empty($_tableref))&&(!empty($_vis_field))&&(!empty($_inv_field)))
		{
			$this->fields[$_cn]['combo'] = new p4a_ComboForm( $_tableref);
			$this->fields[$_cn]['combo']->setFields($_cn, $_vis_field, $_inv_field);
			$this->fields[$_cn]['combo']->setFilter( $_fc);
		}
	}

	function setTextArea( $_cn, $_la, $_r='2', $_c='60', $_edt=1)
	{
		$this->fields[$_cn]['label']=$_la;
		$this->fields[$_cn]['editable']=$_edt;
		$this->fields[$_cn]['combo']='';
		$this->fields[$_cn]['type']="textarea";
		$this->fields[$_cn]['rows']=$_r;
		$this->fields[$_cn]['cols']=$_c;
	}

	function setRadio( $_g, $_v, $_l, $_ck)
	{
		$this->fields[$_g]['editable']=1;
		$this->fields[$_g]['type']="radio";
		$this->fields[$_g]['combo']='';
		if (isset($this->fields[$_g]['num']))
			$this->fields[$_g]['num']++;
		else
			$this->fields[$_g]['num']=1;
		$this->fields[$_g]['v'][$this->fields[$_g]['num']-1]['v'] = $_v;
		$this->fields[$_g]['v'][$this->fields[$_g]['num']-1]['c'] = $_ck;
		$this->fields[$_g]['v'][$this->fields[$_g]['num']-1]['l'] = $_l;
	}

	function setCombo( $_cn, $_la, $_array_comb)
	{
		$this->fields[$_cn]['editable']=1;
		$this->fields[$_cn]['label']=$_la;
		$this->fields[$_cn]['type']='text';
		$this->fields[$_cn]['combo']= new p4a_SimpleCombo( $_cn, $_array_comb);
	}

	function setNumCols( $_nc)
	{
		$this->num_cols = $_nc;
	}

	function setValue( $_cn, $_v)
	{
		$this->data[$_cn] = $_v;
	}

	function setonClickOk( $_evt, $_l="Ok")
	{
		$this->onClickOk['evt'] = $_evt;
		$this->onClickOk['lbl'] = $_l;
	}

	function setonClickCancel( $_evt, $_l="Cancel")
	{
		$this->onClickCancel['evt'] = $_evt;
		$this->onClickCancel['lbl'] = $_l;
	}

	function convert( $_v)
	{
		// convierte entidades habituales html
		$_v = htmlentities( $_v, ENT_QUOTES);

		// convierte saltos de lĂ­nea en "\\n" aptos para javascript (usando innerHTML='...')

		$_v = preg_replace("(\r\n|\n|\r)", "\\n", $_v);
		
		return $_v;

	}

	function flush( $_ev=0)
	{
		if (!$_ev)
			$da="";
		else
			$da="parent.";

		$js = "<script language='JavaScript'>\n";
		$js .= $da.$this->id_html.".className='clsSimpleForm';\n";

		$js .= $da.$this->id_html.".innerHTML='";
		$js .= "<form name=\"form".$this->id_html."\" action=\"javascript:action".$this->id_html."(document.forms.form".$this->id_html.");\" method=\"post\" >";
		$js .= "<table>";
		$co=0;$ca=0;
		$js .= "<tr>";
		foreach( $this->fields as $campo_name => $campo_arr)
		{
			if ($campo_arr['type']!="radio")
				$js .= "<td class=\"clsForm_label\">".$campo_arr['label']."</td>";
			else
				$js .= "<td class=\"clsForm_label\">".$campo_name."</td>";

			if (($campo_arr['editable']==1)&&(count($this->data)>0))
			{
				if (empty($campo_arr['combo']))
				{
					if (isset($this->data[$campo_name]))
						switch( $campo_arr['type'])
						{
							case "text":
							case "password":
								$js .= "<td><input type=\"".$campo_arr['type']."\" size=\"".$campo_arr['size']."\" name=\"c_".$campo_name."\" value=\"".htmlentities( $this->data[$campo_name], ENT_QUOTES)."\"></td>";
								break;
							case "textarea":
								$js .= "<td><textarea rows=\"".$campo_arr['rows']."\" cols=\"".$campo_arr['cols']."\" name=\"c_".$campo_name."\" >".$this->convert( $this->data[$campo_name])."</textarea></td>";
								break;
							case "radio":
								$js .="<td><table>";
								for( $i=0;$i<$campo_arr['num'];$i++)
								{
									$js .= "<tr><td><input type=\"radio\" name=\"c_".$campo_name."\" value=\"".htmlentities( $campo_arr['v'][$i]['v'], ENT_QUOTES)."\"";
									if ($campo_arr['v'][$i]['c']==1)
										$js .= " checked>";
									else
										$js .=">";
									$js .= $campo_arr['v'][$i]['l']."</td></tr>";
								}
								$js .="</table></td>";
								break;
						}
					else
						$js .= "<td>----</td>";
				}
				else
					$js .= "<td>".$campo_arr['combo']->flush($this->data[$campo_name])."</td>";
			}
			else
			{
				if (isset($this->data[$campo_name]))
					$js .= "<td>".$this->convert($this->data[$campo_name])."</td>";
				else
					$js .= "<td>----</td>";
			}

			$co++;$ca++;
			if (($co==$this->num_cols)&&($ca<count($this->fields)))
			{
				$co = 0;
				$js .= "</tr><tr>";
			}
		}

		$js .= "</tr></table>";

		$js .= "<input type=\"hidden\" name=\"eventaction\" value=\"\">";

		$js .= "<table class=\"clsFormControls\"><tr><td></td>";

		if (isset($this->onClickOk))
			$js .= "<td onclick=\"javascript:document.forms.form".$this->id_html.".eventaction.value=&#039;".$this->onClickOk['evt']."&#039;;document.forms.form".$this->id_html.".submit();\">".$this->onClickOk['lbl']."</td>";

		if (isset($this->onClickCancel))
			$js .= "<td onclick=\"javascript:p4a_eventFrame.location.href=&#039;p4a_events.php?event=".$this->onClickCancel['evt']."&#039;\">".$this->onClickCancel['lbl']."</td>";


		$js .= "<td></td></tr></table>";
		$js .= "</form>';\n";

		if (empty($da))
		{
			$js .= "function action".$this->id_html."( objForm) {\n";
			$js .= "var qs ='';";
			$js .= "for (e=0;e<form".$this->id_html.".elements.length;e++) {\n";
			$js .= "if (form".$this->id_html.".elements[e].name!='') {";
			$js .= "qs+=\"&\" + form".$this->id_html.".elements[e].name+'=';\n";
			$js .= "form".$this->id_html.".elements[e].value=escape(objForm.elements[e].value);\n";
			//$js .= "for(i=0; i<objForm.elements[e].value.length; i++){\n";
			//$js .= "if(objForm.elements[e].value.indexOf(\"%0D%0A\") > -1){\n";
			//$js .= "objForm.elements[e].value=objForm.elements[e].value.replace(\"%0D%0A\",\"\\n\")}\n}\n";
			$js .= "qs += objForm.elements[e].value;}}\n";
			$js .= "p4a_eventFrame.location.href='p4a_events.php?event=' + objForm.eventaction.value + qs;\n";
			$js .= "}";
		}

		$js .= "</script>";

		return $js;
	}
}
?>
Return current item: PHP 4 Applications