<?php
require_once("p4a_constants.php");
require_once("p4a_control.php");
require_once("p4a_datasource.php");
class p4a_ComboForm
{
var $db;
var $table;
var $visible;
var $intern;
var $name;
var $data;
var $filter;
function p4a_ComboForm( $_ta, $_db="", $_us="", $_pa="")
{
if ((!empty($_us))&&(!empty($_pa)))
{
$this->db = mysql_connect('localhost',$_us,$_pa);
if (!$this->db)
{
echo '¡¡Error en la conexion a la base de datos!!<p/>';
echo mysql_error();
exit;
}
}
$this->table=$_ta;
unset($this->data);
}
function setFields( $_n, $_v, $_i)
{
$this->visible = $_v;
$this->intern = $_i;
$this->name = $_n;
}
function setFilter( $_fc)
{
if (!empty($_fc))
$this->filter = $_fc;
$this->load();
}
function load()
{
unset($this->data);
$sql = "select ".$this->visible.",".$this->intern." from ".$this->table;
if (isset($this->filter))
if (!empty($this->filter))
$sql .= " where ".$this->filter;
$result = mysql_query( $sql);
$num_results = mysql_num_rows($result);
for($i=0;$i<$num_results;$i++)
$this->data[$i] = mysql_fetch_array( $result, MYSQL_NUM);
}
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_Form extends p4a_Control
{
var $source;
var $data;
var $status;
var $fields;
var $num_cols;
var $onClickNext;
var $onClickPrevious;
var $onClickDelete;
var $onClickUpdate;
var $onClickInsert;
var $onClickUnlock;
var $onClickCancel;
function p4a_Form()
{
$this->type ="form";
$this->status = P4A_FORMSTATUS_EDITION;
$this->num_cols = 1;
}
function setStatus( $_s=P4A_FORMSTATUS_EDITION)
{
if (( $_s & P4A_FORMSTATUS_INSERTING)&&( $_s & P4A_FORMSTATUS_FILTERED))
$r = 0;
else
{
$r = 1;
$this->status = $_s;
}
return $r;
}
function getStatus()
{
return $this->status;
}
function setSource( $_obj)
{
$this->source = $_obj;
}
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 setNumCols( $_nc)
{
$this->num_cols = $_nc;
}
function setonClickNext( $_evt, $_l="Next")
{
$this->onClickNext['evt'] = $_evt;
$this->onClickNext['lbl'] = $_l;
}
function setonClickPrevious( $_evt, $_l="Previous")
{
$this->onClickPrevious['evt'] = $_evt;
$this->onClickPrevious['lbl'] = $_l;
}
function setonClickDelete( $_evt, $_l="Delete")
{
$this->onClickDelete['evt'] = $_evt;
$this->onClickDelete['lbl'] = $_l;
}
function setonClickUpdate( $_evt, $_l="Update")
{
$this->onClickUpdate['evt'] = $_evt;
$this->onClickUpdate['lbl'] = $_l;
}
function setonClickInsert( $_evt, $_l="Insert")
{
$this->onClickInsert['evt'] = $_evt;
$this->onClickInsert['lbl'] = $_l;
}
function setonClickUnlock( $_evt, $_l="______")
{
$this->onClickUnlock['evt'] = $_evt;
$this->onClickUnlock['lbl'] = $_l;
}
function setonClickCancel( $_evt, $_l="Cancel")
{
$this->onClickCancel['evt'] = $_evt;
$this->onClickCancel['lbl'] = $_l;
}
function setFilter( $_f)
{
if (!empty($_f))
{
$this->setStatus( P4A_FORMSTATUS_EDITION | P4A_FORMSTATUS_FILTERED);
}
else
{
if ($this->getStatus() & P4A_FORMSTATUS_FILTERED)
$this->setStatus( $this->getStatus() ^ P4A_FORMSTATUS_FILTERED);
}
$this->data = $this->source->setFilter( $_f);
}
function next()
{
$_paso = $this->source->getRecords( $this->source->getPointer(), 1);
if (!empty($_paso))
$this->data = $_paso;
}
function previous()
{
$this->data = $this->source->getRecords( $this->source->getPointer() - 2, 1);
}
function update( $_matrix_asoc)
{
if ($this->getStatus() & P4A_FORMSTATUS_EDITION)
{
$r = $this->source->update( $_matrix_asoc);
$this->data = $this->source->getRecords( $this->source->getPointer() - 1, 1);
}
if ($this->getStatus() & P4A_FORMSTATUS_INSERTING)
{
$r = $this->insert( $_matrix_asoc);
$this->setStatus( P4A_FORMSTATUS_EDITION);
$this->setFilter( "");
}
return $r;
}
function insert( $_matrix_asoc)
{
$r = $this->source->insert( $_matrix_asoc);
if ($r==1)
$this->data = $this->source->getRecords( $this->source->getPointer()-1, 1);
return $r;
}
function refresh()
{
$this->data = $this->source->getRecords( $this->source->getPointer()-1, 1);
}
function delete()
{
$r = $this->source->delete();
$this->data = $this->source->getRecords( $this->source->getPointer(), 1);
$this->setStatus( P4A_FORMSTATUS_EDITION);
$this->setFilter( "");
return $r;
}
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)
{
$this->refresh();
if (!$_ev)
$da="";
else
$da="parent.";
$js = "<script language='JavaScript'>\n";
$js .= $da.$this->id_html.".className='clsForm';\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)
{
$js .= "<td class=\"clsForm_label\">".$campo_arr['label']."</td>";
switch ($this->status)
{
case P4A_FORMSTATUS_EDITION:
case (P4A_FORMSTATUS_FILTERED | P4A_FORMSTATUS_EDITION):
if (($campo_arr['editable']==1)&&(count($this->data)>0))
{
if (empty($campo_arr['combo']))
{
if (isset($this->data[0][$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[0][$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[0][$campo_name])."</textarea></td>";
break;
}
else
$js .= "<td>----</td>";
}
else
{
$campo_arr['combo']->load();
$js .= "<td>".$campo_arr['combo']->flush($this->data[0][$campo_name])."</td>";
}
}
else
{
if (isset($this->data[0][$campo_name]))
$js .= "<td>".$this->convert($this->data[0][$campo_name])."</td>";
else
$js .= "<td>----</td>";
}
break;
case P4A_FORMSTATUS_INSERTING:
if (!empty($campo_arr['combo']))
{
$campo_arr['combo']->load();
$js .= "<td>".$campo_arr['combo']->flush()."</td>";
}
else
switch( $campo_arr['type'])
{
case "text":
case "password":
$js .= "<td><input type=\"".$campo_arr['type']."\" size=\"".$campo_arr['size']."\" name=\"c_".$campo_name."\" value=\"\"></td>";
break;
case "textarea":
$js .= "<td><textarea rows=\"".$campo_arr['rows']."\" cols=\"".$campo_arr['cols']."\" name=\"c_".$campo_name."\" value=\"\"></textarea></td>";
break;
}
break;
}
$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>";
if (isset($this->onClickInsert))
{
if ($this->status & P4A_FORMSTATUS_INSERTING)
$js .= "<td class=\"clsDisabled\">".$this->onClickInsert['lbl']."</td>";
else
$js .= "<td onclick=\"javascript:p4a_eventFrame.location.href='p4a_events.php?event=".$this->onClickInsert['evt']."'\">".$this->onClickInsert['lbl']."</td>";
//$js .= "<td onclick=\"javascript:document.forms.form".$this->id_html.".eventaction.value='".$this->onClickInsert['evt']."';document.forms.form".$this->id_html.".submit();\">".$this->onClickInsert['lbl']."</td>";
}
if (isset($this->onClickUpdate))
{
$js .= "<td onclick=\"javascript:document.forms.form".$this->id_html.".eventaction.value='".$this->onClickUpdate['evt']."';document.forms.form".$this->id_html.".submit();\">".$this->onClickUpdate['lbl']."</td>";
}
if (isset($this->onClickDelete))
{
if ($this->status & P4A_FORMSTATUS_EDITION)
$js .= "<td onclick=\"javascript:confirm('Are you sure?')?p4a_eventFrame.location.href='p4a_events.php?event=".$this->onClickDelete['evt']."':1\">".$this->onClickDelete['lbl']."</td>";
//$js .= "<td onclick=\"javascript:confirm('¿seguro que desea borrar?')?document.forms.form".$this->id_html.".eventaction.value='".$this->onClickDelete['evt']."':1;document.forms.form".$this->id_html.".submit();\">".$this->onClickDelete['lbl']."</td>";
else
$js .= "<td class=\"clsDisabled\">".$this->onClickDelete['lbl']."</td>";
}
if (isset($this->onClickPrevious))
{
if (($this->status & P4A_FORMSTATUS_EDITION)&&(!($this->status & P4A_FORMSTATUS_FILTERED)))
$js .= "<td onclick=\"javascript:p4a_eventFrame.location.href='p4a_events.php?event=".$this->onClickPrevious['evt']."'\">".$this->onClickPrevious['lbl']."</td>";
else
$js .= "<td class=\"clsDisabled\">".$this->onClickPrevious['lbl']."</td>";
}
if (isset($this->onClickNext))
{
if (($this->status & P4A_FORMSTATUS_EDITION)&&(!($this->status & P4A_FORMSTATUS_FILTERED)))
$js .= "<td onclick=\"javascript:p4a_eventFrame.location.href='p4a_events.php?event=".$this->onClickNext['evt']."'\">".$this->onClickNext['lbl']."</td>";
else
$js .= "<td class=\"clsDisabled\">".$this->onClickNext['lbl']."</td>";
}
if (isset($this->onClickUnlock))
{
if ($this->status & P4A_FORMSTATUS_FILTERED)
$js .= "<td class=\"clsUnlock\" onclick=\"javascript:p4a_eventFrame.location.href='p4a_events.php?event=".$this->onClickUnlock['evt']."'\">".$this->onClickUnlock['lbl']."</td>";
else
$js .= "<td class=\"clsDisabledUnlock\">".$this->onClickUnlock['lbl']."</td>";
}
if (isset($this->onClickCancel))
{
$js .= "<td onclick=\"javascript:p4a_eventFrame.location.href='p4a_events.php?event=".$this->onClickCancel['evt']."'\">".$this->onClickCancel['lbl']."</td>";
}
$js .= "</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;
}
}
?>