PHP Classes
elePHPant
Icontem

File: jsrs-adr-php/adr-form-process.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of yo keller  >  metaForm  >  jsrs-adr-php/adr-form-process.php  >  Download  
File: jsrs-adr-php/adr-form-process.php
Role: Auxiliary script
Content type: text/plain
Description: php server scripts for the jsrs database interface
Class: metaForm
Generate and validate forms from XML definitions
Author: By
Last change:
Date: 10 years ago
Size: 6,629 bytes
 

Contents

Class file image Download
<?php
// adr-form-process.php
/*
    base_undo
    db_record
    incrDbRecord
    buildSearchQry
    buildFullSearchQry
    buildUpdQry
*/
session_start();
if (empty(
$_SESSION['valid_user'])){
   
$_SESSION['valid_user'] = 'guest';
   
$s_echo = "adr-form-process.php -- _SESSION: ";
   
$s_echo .= print_r(_SESSION, true);
   
//$s_echo = substr($s_echo,0,70);
   
$s_echo .= "<hr />\n";
   
jsrsLog($s_echo);
}
require_once(
'../class.undo.php');
require_once(
"../jsrsServer.inc.php");
$verb = 1;
if (
$verb){
       
//echo "<h2>adr-form-process.php</h2>";
   
$s_echo = "adr-form-process.php -- _REQUEST: ";
   
$s_echo .= print_r($_REQUEST, true);
   
//$s_echo = substr($s_echo,0,70);
   
$s_echo .= "<hr />\n";
   
$s_echo .= "-- _SESSION: ";
   
$s_echo .= print_r($_SESSION, true);
   
jsrsLog($s_echo);
}
jsrsDispatch( "buildSearchQry buildUpdQry db_record incrDbRecord", $verb);

function
base_undo($verb=0)
{
   
$my_undo = new dbUndo();
   
$my_undo->init('config.inc.php');
   
$my_undo->verb = $verb;
    if (!
$my_undo->setup()){
        echo
"cannot setup the undo machinery!!!<br />\n";
    }
    return
$my_undo;
}

// actually validate a data entry step -- essential for new records
//function db_record($formName,$table)
function db_record($fid,$str_args,$str_val)
{
    if(!empty(
$_REQUEST['id'])) $id = $_REQUEST['id'];
    else
       
$id = 0;
   
//if (empty($id))
    // return '';
    // recover all the form data
   
$args = explode('~',$str_args);
   
$val = explode('~',$str_val);
   
$my_undo = base_undo();
   
$tbl = $val[1];
    if (!empty(
$_SESSION['mark_record']))
       
$mark_id = $_SESSION['mark_record'];
    if (
$mark_id < 0)
    {
       
// create a new record based on the $mark_id data in the undo_tbl
        //$my_undo = base_undo();
       
       
$id = $my_undo->apply_all_recent_updates($tbl,$mark_id,$tbl_keyname='id');
       
$_SESSION['mark_record'] = 0;
       
//return "$id";
   
}
    if (empty(
$id))
       
$id = $fid;
   
// create an effective array of fld->value assoc
   
$fld_val = array();
    for(
$i=2;$i<count($val);$i++)
    {
       
$fld_val[$args[$i]] = $val[$i];
    }
   
// record all these values in the database
   
$apply_qry = $my_undo->apply_updates($tbl,$id, $fld_val,$tbl_keyname='id');
   
   
$s_echo = "adr-form-process.php:db_record -- _SESSION: ";
   
$s_echo .= print_r($_SESSION, true);
   
$s_echo .= "<br />\nfld_val:";
   
$s_echo .= print_r($fld_val, true);
   
$s_echo .= "<br />\napplied qry: ".$apply_qry."<br />\n";
   
jsrsLog($s_echo);
    return
"$id";
}
// end function db_record

// smoothly record the data being entered and provide data entry assistance
function incrDbRecord($baseTerm,$table,$field,$tbl_id=0)
{
   
// 1-smoothly record data
   
if(!empty($_REQUEST['id'])) $id = $_REQUEST['id'];
    else
       
$id = 0;
    if (!empty(
$id))
       
$tbl_id = $id; //int($id);
   
elseif (!empty($tbl_id) and empty($id))
       
$id = $tbl_id; //int($tbl_id);
   
$_SESSION['tbl_id'] = $tbl_id;
    if (!empty(
$_SESSION['mark_record'])){
       
$mark_id = $_SESSION['mark_record'];
        if (empty(
$id) && $mark_id < 0)
           
$id = $mark_id;
    }
   
$my_undo = base_undo();
    if(!empty(
$_REQUEST['tble'])) $tble = $_REQUEST['tble'];
    else
$tble = $table;
    if (empty(
$id)){
       
// prepare a dummy undo-record
        //$my_undo = base_undo();
       
$tbl = $table;
       
$new_id = $my_undo->mark_dummy_record($tble); // create a dummy undo-record
       
$_SESSION['mark_record'] = $new_id;
       
$id = $new_id;
    }
    elseif ((
$id>0) && empty($_SESSION['mark_record'])){
       
//$my_undo = base_undo();
       
$new_id = $my_undo->mark_record($tbl,$id, 'id');
       
$_SESSION['mark_record'] = $new_id;
       
//$id = $new_id;
   
}
    if (empty(
$_SESSION['field']))
       
$_SESSION['field'] = $field;
    elseif (
$_SESSION['field'] != $field){
       
// set the field definition now
       
$_SESSION['field'] = $field;
    }
   
// record the field info
    // while saving the possibility for an undo
   
$tbl = 'adresse';
   
// prepare for an undo of the field being changed
   
$my_undo->cur_tbl = $tbl;
   
$my_undo->cur_tbl_keyname = 'id';
   
$my_undo->cur_col = $field;
    if (
$id>0) // a valid id!! -- not a dummy record id
   
{
       
$my_undo->cur_id = $id;
       
$old_col = $my_undo->update_col($field,$baseTerm); //mark_record_upd($field]);
   
}
    else
    {
       
$my_undo->update_col_dummy($tbl,$id,$field,$baseTerm);
    }
   
$s_echo = "adr-form-process.php:incrDbRecord: ".$field.'|'.$baseTerm."\n";
   
$s_echo .= "id: $id this->cur_id: ".$my_undo->cur_id."\n";
   
$s_echo .= " -- _SESSION: ";
   
$s_echo .= print_r($_SESSION, true);
   
jsrsLog($s_echo);
    return
$field.'|'.$baseTerm;

}
// end function incrDbRecord

// smoothly record the data being entered and provide data entry assistance
function buildSearchQry($baseTerm,$table,$field,$tbl_id=0)
{
   
// 1-smoothly record data
   
incrDbRecord($baseTerm,$table,$field,$tbl_id);
   
// in any case keep track of current updates
   
    //2- ease the data entry by a search on already entered data
   
include ("config.inc.php");
   
$db_h = mysql_connect($dbhost,$dbuser,$dbpwd);
   
mysql_select_db($dbname);
   
   
//$qry = "select * from $table where $field like \"".$baseTerm."%\"";
   
$qry = "select $field from $table where $field like \"".$baseTerm."%\"";
   
$res = mysql_query($qry);
    if (empty(
$res)){
       
$s_echo = "buildSearchQry -- ".$qry."--".mysql_errno().": ".mysql_error()."<br>";
       
jsrsLog($s_echo);
    }
   
$s = '';
    while (
$row = mysql_fetch_row($res)) {
   
$s .= join( $row, '~') . "|";
    }
   
mysql_close($db_h);
   
$s_echo = "buildSearchQry $baseTerm,$table,$field :".$s;
   
jsrsLog($s_echo);
    return
$s;
   
}
// end buildSearchQry

function buildFullSearchQry($baseTerm,$table,$field){
    include (
"config.inc.php");
   
$db_h = mysql_connect($dbhost,$dbuser,$dbpwd);
   
mysql_select_db($dbname);

   
$fields = mysql_list_fields("database1", "table1", $db_h);
   
$columns = mysql_num_fields($fields);

   
$cols = array();
    for (
$i = 0; $i < $columns; $i++) {
       
//echo mysql_field_name($fields, $i) . "\n";
       
$cols[] = mysql_field_name($fields, $i);
    }
   
// let the 1st row be the col names
   
$s = join( $cols, '~') . "|";
   
$qry = "select * from $table where $field like \"".$baseTerm."%\"";
   
$res = mysql_query($qry);
   
    while (
$row = mysql_fetch_row($res)) {
   
$s .= join( $row, '~') . "|";
    }

   
mysql_close($db_h);
    return
$s;
   
}
// end buildFullSearchQry


function buildUpdQry($baseTerm,$table,$field,$id){
    include (
"config.inc.php");
   
$db_h = mysql_connect($dbhost,$dbuser,$dbpwd);
   
mysql_select_db($dbname);
    if (
$id == -1){
       
$query="insert into $table ($field) values ('".$baseTerm."')";
    }
    else
       
$query="update $table set $field='".$baseTerm."' where id='".$id."'";
   
$res = mysql_query($query);

   
mysql_close($db_h);
    return ;
}
// end buildUpdQry

?>