[phpwebapp-commits] CVS: web_app/webobjects/dbTable editRecord.php,1.5,1.6 dbTable.php,1.5,1.6
Brought to you by:
dashohoxha
From: Dashamir H. <das...@us...> - 2004-07-22 16:26:21
|
Update of /cvsroot/phpwebapp/web_app/webobjects/dbTable In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9224/webobjects/dbTable Modified Files: editRecord.php dbTable.php Log Message: reformated and modified the copyleft notice Index: editRecord.php =================================================================== RCS file: /cvsroot/phpwebapp/web_app/webobjects/dbTable/editRecord.php,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** editRecord.php 11 May 2004 06:45:29 -0000 1.5 --- editRecord.php 22 Jul 2004 16:25:36 -0000 1.6 *************** *** 1,7 **** <?php /* ! Copyright 2001,2002,2003 Dashamir Hoxha, das...@us... ! This file is part of phpWebApp. phpWebApp is free software; you can redistribute it and/or modify it --- 1,8 ---- <?php /* ! This file is part of phpWebApp, which is a framework for building web ! application based on relational databases. ! Copyright 2001,2002,2003,2004 Dashamir Hoxha, das...@us... phpWebApp is free software; you can redistribute it and/or modify it *************** *** 28,139 **** { function init() ! { ! /** state can be 'add', 'edit' or 'hidden' */ ! $this->addSVar("state", "hidden"); ! /** the rec_id of the record that is being edited */ ! $this->addSVar("rec_id", UNDEFINED); ! } function on_cancel($event_args) ! { ! $this->setSVar("state", "hidden"); ! $this->setSVar("rec_id", UNDEFINED); ! } function on_save($event_args) ! { ! //get the related dbTable ! $edTbl = $this->get_dbTable(); ! //get params 'table' and 'fields' of the dbTable ! $table = $edTbl->params["table"]; ! $table = WebApp::replaceVars($table); ! $fields = $edTbl->params["fields"]; ! //get $values ! while (list($name, $value) = each($event_args)) ! { ! $arr_values[] = "$name='$value'"; ! } ! $values = implode(", ", $arr_values); ! $state = $this->getSVar("state"); ! if ($state=="add") ! { ! //insert ! $vars["table"] = $table; ! $vars["values"] = $values; ! WebApp::execDBCmd("editRecord::insert", $vars); ! } ! else if ($state=="edit") ! { ! //update ! $rec_id = $this->getSVar("rec_id"); ! $rec_id = str_replace("\\'", "'", $rec_id); ! $vars = array( ! "table" => $table, ! "values" => $values, ! "rec_id" => $rec_id ! ); ! WebApp::execDBCmd("editRecord::update", $vars); ! } ! else ! { ! $msg = "editRecord::save(): invalid state '$state'."; ! print WebApp::warning_msg($msg); ! } ! $this->setSVar("state", "hidden"); ! } /** returns the related 'dbTable' object */ function get_dbTable() ! { ! global $webPage; ! $obj_vars = $this->getObjVars(); ! $edTable_id = "dbTable::".$obj_vars["obj_name"]; ! $dbTable = $webPage->getObject($edTable_id); ! return $dbTable; ! } function onRender() ! { ! //get the related dbTable ! $edTbl = $this->get_dbTable(); ! //get params 'table' and 'fields' of the dbTable ! $table = $edTbl->params["table"]; ! $table = WebApp::replaceVars($table); ! $fields = $edTbl->params["fields"]; ! //get the recordset {{table}}->fields ! global $webPage; ! $table_fields = $webPage->getRecordset($table."->fields"); ! $state = $this->getSVar("state"); ! if ($state=="add") ! { ! //add empty variables for each field ! $table_fields->MoveFirst(); ! while (!$table_fields->EOF()) ! { ! $fld_name = $table_fields->Field("fld_name"); ! WebApp::addVar($fld_name, ""); ! $table_fields->MoveNext(); ! } ! } ! else if ($state=="edit") ! { ! //add vars for each field of the selected record ! $rec_id = $this->getSVar("rec_id"); ! $rec_id = str_replace("\\'", "'", $rec_id); ! $query = "SELECT $fields FROM $table WHERE $rec_id"; ! $rs = new Recordset($edTbl->params['id'].'_editRecord', $query); ! $rs->Open(); ! WebApp::addVars($rs->Fields()); ! } ! } } ?> \ No newline at end of file --- 29,140 ---- { function init() ! { ! /** state can be 'add', 'edit' or 'hidden' */ ! $this->addSVar("state", "hidden"); ! /** the rec_id of the record that is being edited */ ! $this->addSVar("rec_id", UNDEFINED); ! } function on_cancel($event_args) ! { ! $this->setSVar("state", "hidden"); ! $this->setSVar("rec_id", UNDEFINED); ! } function on_save($event_args) ! { ! //get the related dbTable ! $edTbl = $this->get_dbTable(); ! //get params 'table' and 'fields' of the dbTable ! $table = $edTbl->params["table"]; ! $table = WebApp::replaceVars($table); ! $fields = $edTbl->params["fields"]; ! //get $values ! while (list($name, $value) = each($event_args)) ! { ! $arr_values[] = "$name='$value'"; ! } ! $values = implode(", ", $arr_values); ! $state = $this->getSVar("state"); ! if ($state=="add") ! { ! //insert ! $vars["table"] = $table; ! $vars["values"] = $values; ! WebApp::execDBCmd("editRecord::insert", $vars); ! } ! else if ($state=="edit") ! { ! //update ! $rec_id = $this->getSVar("rec_id"); ! $rec_id = str_replace("\\'", "'", $rec_id); ! $vars = array( ! "table" => $table, ! "values" => $values, ! "rec_id" => $rec_id ! ); ! WebApp::execDBCmd("editRecord::update", $vars); ! } ! else ! { ! $msg = "editRecord::save(): invalid state '$state'."; ! print WebApp::warning_msg($msg); ! } ! $this->setSVar("state", "hidden"); ! } /** returns the related 'dbTable' object */ function get_dbTable() ! { ! global $webPage; ! $obj_vars = $this->getObjVars(); ! $edTable_id = "dbTable::".$obj_vars["obj_name"]; ! $dbTable = $webPage->getObject($edTable_id); ! return $dbTable; ! } function onRender() ! { ! //get the related dbTable ! $edTbl = $this->get_dbTable(); ! //get params 'table' and 'fields' of the dbTable ! $table = $edTbl->params["table"]; ! $table = WebApp::replaceVars($table); ! $fields = $edTbl->params["fields"]; ! //get the recordset {{table}}->fields ! global $webPage; ! $table_fields = $webPage->getRecordset($table."->fields"); ! $state = $this->getSVar("state"); ! if ($state=="add") ! { ! //add empty variables for each field ! $table_fields->MoveFirst(); ! while (!$table_fields->EOF()) ! { ! $fld_name = $table_fields->Field("fld_name"); ! WebApp::addVar($fld_name, ""); ! $table_fields->MoveNext(); ! } ! } ! else if ($state=="edit") ! { ! //add vars for each field of the selected record ! $rec_id = $this->getSVar("rec_id"); ! $rec_id = str_replace("\\'", "'", $rec_id); ! $query = "SELECT $fields FROM $table WHERE $rec_id"; ! $rs = new Recordset($edTbl->params['id'].'_editRecord', $query); ! $rs->Open(); ! WebApp::addVars($rs->Fields()); ! } ! } } ?> \ No newline at end of file Index: dbTable.php =================================================================== RCS file: /cvsroot/phpwebapp/web_app/webobjects/dbTable/dbTable.php,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** dbTable.php 11 May 2004 06:45:29 -0000 1.5 --- dbTable.php 22 Jul 2004 16:25:36 -0000 1.6 *************** *** 1,7 **** <?php /* ! Copyright 2001,2002,2003 Dashamir Hoxha, das...@us... ! This file is part of phpWebApp. phpWebApp is free software; you can redistribute it and/or modify it --- 1,8 ---- <?php /* ! This file is part of phpWebApp, which is a framework for building web ! application based on relational databases. ! Copyright 2001,2002,2003,2004 Dashamir Hoxha, das...@us... phpWebApp is free software; you can redistribute it and/or modify it *************** *** 28,128 **** { function init() ! { ! $this->addSVar("selected_rec", UNDEFINED); ! } function on_rmAll($event_args) ! { ! $table = $this->params["table"]; ! $table = WebApp::replaceVars($table); ! $where = $this->params["where"]; ! $condition = ($where=="" ? "1=1" : $where); ! //remove all ! $vars["table"] = $table; ! $vars["condition"] = $condition; ! WebApp::execDBCmd("dbTable::removeAll", $vars); ! } function on_rm($event_args) ! { ! $table = $this->params["table"]; ! $table = WebApp::replaceVars($table); ! $rec_id = $event_args["rec_id"]; ! $rec_id = str_replace("\\'", "'", $rec_id); ! //remove ! $vars["table"] = $table; ! $vars["rec_id"] = $rec_id; ! WebApp::execDBCmd("dbTable::remove", $vars); ! } function on_add($event_args) ! { ! $objVars = $this->getObjVars(); ! $obj_name = $objVars["obj_name"]; ! $editRecId = "editRecord::".$obj_name; ! WebApp::setSVar($editRecId."->rec_id", UNDEFINED); ! WebApp::setSVar($editRecId."->state", "add"); ! } function on_edit($event_args) ! { ! $rec_id = $event_args["rec_id"]; ! $objVars = $this->getObjVars(); ! $obj_name = $objVars["obj_name"]; ! $editRecId = "editRecord::".$obj_name; ! WebApp::setSVar($editRecId."->rec_id", $rec_id); ! WebApp::setSVar($editRecId."->state", "edit"); ! } function on_select($event_args) ! { ! $rec_id = $event_args["rec_id"]; ! $this->setSVar("selected_rec", $rec_id); ! } function onParse() ! { ! $this->add_selectedRec_vars(); ! } /** add the fields of the selected record as variables */ function add_selectedRec_vars() ! { ! $rec_id = $this->getSVar("selected_rec"); ! if ($rec_id==UNDEFINED) return; ! $rec_id = str_replace("\\'", "'", $rec_id); ! $vars["rec_id"] = $rec_id; ! $table = $this->params["table"]; ! $table = WebApp::replaceVars($table); ! $vars["table"] = $table; ! $rs = WebApp::openRS("dbTable::select", $vars); ! WebApp::addGlobalVars($rs->Fields()); ! } function onRender() ! { ! //get all the fields of the table ! $table = $this->params["table"]; ! $table = WebApp::replaceVars($table); ! $rs = WebApp::execQuery("SHOW FIELDS FROM $table"); ! //get the key fields and add the variable {{rec_id}} ! $arr_key = $this->getKey($rs); ! $this->add_rec_id($arr_key); ! //get the recordsets "{{table}}->fields" and "{{table}}->records" ! $table_fields = $this->getTableFields($rs); ! $table_records = $this->getTableRecords($arr_key); ! //add the recordsets to $webPage ! global $webPage; ! $webPage->addRecordset($table_fields); ! $webPage->addRecordset($table_records); ! } /** --- 29,129 ---- { function init() ! { ! $this->addSVar("selected_rec", UNDEFINED); ! } function on_rmAll($event_args) ! { ! $table = $this->params["table"]; ! $table = WebApp::replaceVars($table); ! $where = $this->params["where"]; ! $condition = ($where=="" ? "1=1" : $where); ! //remove all ! $vars["table"] = $table; ! $vars["condition"] = $condition; ! WebApp::execDBCmd("dbTable::removeAll", $vars); ! } function on_rm($event_args) ! { ! $table = $this->params["table"]; ! $table = WebApp::replaceVars($table); ! $rec_id = $event_args["rec_id"]; ! $rec_id = str_replace("\\'", "'", $rec_id); ! //remove ! $vars["table"] = $table; ! $vars["rec_id"] = $rec_id; ! WebApp::execDBCmd("dbTable::remove", $vars); ! } function on_add($event_args) ! { ! $objVars = $this->getObjVars(); ! $obj_name = $objVars["obj_name"]; ! $editRecId = "editRecord::".$obj_name; ! WebApp::setSVar($editRecId."->rec_id", UNDEFINED); ! WebApp::setSVar($editRecId."->state", "add"); ! } function on_edit($event_args) ! { ! $rec_id = $event_args["rec_id"]; ! $objVars = $this->getObjVars(); ! $obj_name = $objVars["obj_name"]; ! $editRecId = "editRecord::".$obj_name; ! WebApp::setSVar($editRecId."->rec_id", $rec_id); ! WebApp::setSVar($editRecId."->state", "edit"); ! } function on_select($event_args) ! { ! $rec_id = $event_args["rec_id"]; ! $this->setSVar("selected_rec", $rec_id); ! } function onParse() ! { ! $this->add_selectedRec_vars(); ! } /** add the fields of the selected record as variables */ function add_selectedRec_vars() ! { ! $rec_id = $this->getSVar("selected_rec"); ! if ($rec_id==UNDEFINED) return; ! $rec_id = str_replace("\\'", "'", $rec_id); ! $vars["rec_id"] = $rec_id; ! $table = $this->params["table"]; ! $table = WebApp::replaceVars($table); ! $vars["table"] = $table; ! $rs = WebApp::openRS("dbTable::select", $vars); ! WebApp::addGlobalVars($rs->Fields()); ! } function onRender() ! { ! //get all the fields of the table ! $table = $this->params["table"]; ! $table = WebApp::replaceVars($table); ! $rs = WebApp::execQuery("SHOW FIELDS FROM $table"); ! //get the key fields and add the variable {{rec_id}} ! $arr_key = $this->getKey($rs); ! $this->add_rec_id($arr_key); ! //get the recordsets "{{table}}->fields" and "{{table}}->records" ! $table_fields = $this->getTableFields($rs); ! $table_records = $this->getTableRecords($arr_key); ! //add the recordsets to $webPage ! global $webPage; ! $webPage->addRecordset($table_fields); ! $webPage->addRecordset($table_records); ! } /** *************** *** 131,159 **** */ function getKey(&$rs) ! { ! $arr_keys = array(); ! $rs->MoveFirst(); ! while (!$rs->EOF()) ! { ! $key = $rs->Field("Key"); ! $fld = $rs->Field("Field"); ! if ($key=="PRI") $arr_keys[] = $fld; ! $rs->MoveNext(); ! } ! $rs->MoveFirst(); ! //if no key found, take all the fields as key ! if (sizeof($arr_keys)==0) ! { ! while (!$rs->EOF()) ! { ! $arr_keys[]=$key; ! $rs->MoveNext(); ! } ! $rs->MoveFirst(); ! } ! return $arr_keys; ! } /** --- 132,160 ---- */ function getKey(&$rs) ! { ! $arr_keys = array(); ! $rs->MoveFirst(); ! while (!$rs->EOF()) ! { ! $key = $rs->Field("Key"); ! $fld = $rs->Field("Field"); ! if ($key=="PRI") $arr_keys[] = $fld; ! $rs->MoveNext(); ! } ! $rs->MoveFirst(); ! //if no key found, take all the fields as key ! if (sizeof($arr_keys)==0) ! { ! while (!$rs->EOF()) ! { ! $arr_keys[]=$key; ! $rs->MoveNext(); ! } ! $rs->MoveFirst(); ! } ! return $arr_keys; ! } /** *************** *** 162,174 **** */ function add_rec_id($arr_key) ! { ! for ($i=0; $i < sizeof($arr_key); $i++) ! { ! $fld = $arr_key[$i]; ! $arr_flds[] = $fld."=\\'{{".$fld."}}\\'"; ! } ! $rec_id = implode(" AND ", $arr_flds); ! WebApp::addVar("rec_id", $rec_id); ! } /** --- 163,175 ---- */ function add_rec_id($arr_key) ! { ! for ($i=0; $i < sizeof($arr_key); $i++) ! { ! $fld = $arr_key[$i]; ! $arr_flds[] = $fld."=\\'{{".$fld."}}\\'"; ! } ! $rec_id = implode(" AND ", $arr_flds); ! WebApp::addVar("rec_id", $rec_id); ! } /** *************** *** 178,213 **** */ function getTableFields(&$rs) ! { ! //get the params of the <WebObject> ! $table = $this->params["table"]; ! $table = WebApp::replaceVars($table); ! $fields = $this->params["fields"]; ! $table_fields = new EditableRS($table."->fields"); ! if ($fields=="*") ! { ! //all the fields of the table ! $rs->MoveFirst(); ! while (!$rs->EOF()) ! { ! $fld_name = $rs->Field("Field"); ! $rec = array("fld_name"=>$fld_name); ! $table_fields->addRec($rec); ! $rs->MoveNext(); ! } ! } ! else ! { ! //add the fields selected in the parameter ! $arr_fields = explode(",", $fields); ! for ($i=0; $i < sizeof($arr_fields); $i++) ! { ! $fld_name = $arr_fields[$i]; ! $rec = array("fld_name"=>$fld_name); ! $table_fields->addRec($rec); ! } ! } ! return $table_fields; ! } /** --- 179,214 ---- */ function getTableFields(&$rs) ! { ! //get the params of the <WebObject> ! $table = $this->params["table"]; ! $table = WebApp::replaceVars($table); ! $fields = $this->params["fields"]; ! $table_fields = new EditableRS($table."->fields"); ! if ($fields=="*") ! { ! //all the fields of the table ! $rs->MoveFirst(); ! while (!$rs->EOF()) ! { ! $fld_name = $rs->Field("Field"); ! $rec = array("fld_name"=>$fld_name); ! $table_fields->addRec($rec); ! $rs->MoveNext(); ! } ! } ! else ! { ! //add the fields selected in the parameter ! $arr_fields = explode(",", $fields); ! for ($i=0; $i < sizeof($arr_fields); $i++) ! { ! $fld_name = $arr_fields[$i]; ! $rec = array("fld_name"=>$fld_name); ! $table_fields->addRec($rec); ! } ! } ! return $table_fields; ! } /** *************** *** 216,250 **** */ function getTableRecords($arr_key) ! { ! //get parameter values ! $table = $this->params["table"]; ! $fields = $this->params["fields"]; ! $where = $this->params["where"]; ! $table = WebApp::replaceVars($table); ! $where = WebApp::replaceVars($where); ! //make sure that the key fields are among the $fields ! if ($fields<>"*") ! { ! $arr_fields = explode(",", $fields); ! for ($i=0; $i < sizeof($arr_key); $i++) ! { ! $fld = $arr_key[$i]; ! if (!in_array($fld, $arr_fields)) array_unshift($arr_fields, $fld); ! } ! $fields = implode(",", $arr_fields); ! } ! //construct the query ! if ($where<>"") $where = "WHERE $where"; ! $query = "SELECT $fields FROM $table $where"; ! //create and open the recordset ! $id = $table."->records"; ! $table_records = new EditableRS($id, $query); ! $table_records->Open(); ! return $table_records; ! } } ?> \ No newline at end of file --- 217,251 ---- */ function getTableRecords($arr_key) ! { ! //get parameter values ! $table = $this->params["table"]; ! $fields = $this->params["fields"]; ! $where = $this->params["where"]; ! $table = WebApp::replaceVars($table); ! $where = WebApp::replaceVars($where); ! //make sure that the key fields are among the $fields ! if ($fields<>"*") ! { ! $arr_fields = explode(",", $fields); ! for ($i=0; $i < sizeof($arr_key); $i++) ! { ! $fld = $arr_key[$i]; ! if (!in_array($fld, $arr_fields)) array_unshift($arr_fields, $fld); ! } ! $fields = implode(",", $arr_fields); ! } ! //construct the query ! if ($where<>"") $where = "WHERE $where"; ! $query = "SELECT $fields FROM $table $where"; ! //create and open the recordset ! $id = $table."->records"; ! $table_records = new EditableRS($id, $query); ! $table_records->Open(); ! return $table_records; ! } } ?> \ No newline at end of file |