Login   Register  
PHP Classes
elePHPant
Icontem

File: buildProcessForm.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Richard Munroe  >  SQL Data  >  buildProcessForm.php  >  Download  
File: buildProcessForm.php
Role: Application script
Content type: text/plain
Description: Build the "save" action associated with a generated form
Class: SQL Data
Generate classes to store objects in SQL databases
Author: By
Last change: Minor tweak for better php5 compatibility.
Date: 6 years ago
Size: 5,327 bytes
 

Contents

Class file image Download
<?php
/**
 * @author Dick Munroe <munroe@csworks.com>
 * @copyright copyright @ Dick Munroe, 2004-2006, All rights reserved
 * @license http://www.csworks.com/publications/ModifiedNetBSD.html
 * @version 2.0.0
 */

//
// Construct a processor of an "edit" form using the class interface to a
// mySQL table generated by buildClass
//
// Build a processor of an HTML form from a mySQL table description that uses
// a SQLData derived class to access the data for the form.
// The processor program generated will require tweaking in order
// to be complete.  Experience shows that the generated processor form is
// about 80% of the work.
//
//    $Author: dickmunroe $
//    $Date: 2007/12/26 15:41:56 $
//
// Edit History:
//
//  Dick Munroe munroe@csworks.com 06-Nov-2004
//      Initial Version Created
//
//  Dick Munroe munroe@csworks.com 04-May-2005
//    Clean up for publication.
//
//  Dick Munroe munroe@csworks.com 14-Mar-2006
//    Change licensing, reorganize includes.
//
//  Dick Munroe (munroe@csworks.com) 15-Oct-2006
//      Switch to database independent interfaces.
//
//  Dick Munroe (munroe@csworks.com) 02-Nov-2006
//      Minor tweak for better php5 compatibility.
//

include_once('SQLData/options.php') ;

$theOptions getopt("h:p:u:d:") ;

if (
count($_SERVER['argv']) < 3)
{
    print(
"
buildProcessForm [-h hostname] [-u username] [-p password] [-d DBType] tableName databaseName

    Write a file named \"process.tableName.php\" and renames any existing file of
    the same name to process.tableName.php.old.
"
) ;
    return 
;
}

//
// Unfortunately PHP doesn't do the argv reduction common to all
// other implementations of getopt, so I'm requiring that the
// table and database names be the first two arguments.
//

$theTableName $_SERVER['argv'][count($_SERVER['argv']) - 2] ;
$theDatabaseName $_SERVER['argv'][count($_SERVER['argv']) - 1] ;

if (empty(
$theTableName))
{
    die(
'A table name is needed') ;
}

if (empty(
$theDatabaseName))
{
    die(
'A database name is needed') ;
}

options($theOptions) ;

$theValidResults = array() ;

$theDB =
    
FactoryDB::factory(
        
$theOptions['u'],                               // Username
        
$theOptions['p'],                               // Password
        
$theDatabaseName,                               // Database
        
$theOptions['h'],                               // Host
        
$theOptions['d']) ;                             // Database Type


//
// Save the old output file.
//

$theFileName sprintf("process.%s.php"ucfirst($theTableName)) ;
$theOldFileName $theFileName ".old" ;

if (
file_exists($theFileName))
{
  if (
is_file($theFileName))
    {
      if (!
rename($theFileName$theOldFileName))
    {
      exit(
1) ;
    }
    }
  else
    {
      exit(
2) ;
    }
}

if (!(
$theStream = @fopen($theFileName'w')))
{
  exit(
3) ;
}

//
// Emit the rough draft of the form processor.
//

fwrite($theStreamsprintf('<?php

//
// Edit form processor for:
//
// Class: %s
// Table: %s
// Database: %s
//
// Generated by buildProcessForm.php, written by Dick Munroe (munroe@csworks.com)
//

include_once("class.%s.php") ;
include_once("config.%s.php") ;
include_once("requestUtils/requestUtils.class.php") ;

session_start() ;

$the%s = new %s($the%sDatabase, $the%sHost, $the%sUser, $the%sPassword) ;

'
,     ucfirst($theTableName),
       
$theTableName,
       
$theDatabaseName,
       
ucfirst($theTableName),
       
$theDatabaseName,
       
ucfirst($theTableName),
       
ucfirst($theTableName),
       
$theDatabaseName,
       
$theDatabaseName,
       
$theDatabaseName,
       
$theDatabaseName)) ;

fwrite(
    
$theStream
    
'$theQuery = (empty($_SESSION["query"]) ? NULL : $_SESSION["query"]) ;

'
) ;

$thePrimaryKey "" ;
$thePrimaryKeyIsAutoIncrement false ;

$theResult $theDB->describeTable($theTableName) ;

foreach (
$theResult as $theRow)
{
  if (!
preg_match("/auto_increment/"$theRow['Extra']))
    {
      
fwrite($theStreamsprintf('$the%s->set%s(stripslashes(requestUtils::getRequestObject("%s"))) ;
'
,           ucfirst($theTableName),
         
ucfirst($theRow['Field']),
         
$theRow['Field'])) ;
    } ;

  if (
$theRow['Key'] == 'PRI')
    {
      
$thePrimaryKey $theRow['Field'] ;
      
$thePrimaryKeyIsAutoIncrement preg_match("/auto_increment/"$theRow['Extra']) ;
    }
}

fwrite($theStream'
if (empty($theQuery))
{
'
) ;

fwrite($theStreamsprintf('    if (!$the%s->insert())
    {
        $the%s->showErrors() ;
        die() ;
    }
'
,     ucfirst($theTableName),
       
ucfirst($theTableName))) ;

if (empty(
$thePrimaryKey))
{
  
fwrite($theStream'    $theQuery = "where theField = theValue" ;
    die("You must specify a unique selector and store it in $theQuery") ;
'
) ;
}
else
{
  
fwrite($theStreamsprintf('    $theQuery = "where %s = \'" . $the%s->escape_string($the%s->get("%s")) . "\'" ;
'
,     addcslashes($theDB->quoteIdentifier($thePrimaryKey), '"'),
     
ucfirst($theTableName),
     
ucfirst($theTableName),
     
$thePrimaryKey)) ;
}

fwrite($theStreamsprintf('}
else
{
    if (!$the%s->update($theQuery))
    {
        $the%s->showErrors() ;
        die() ;
    }
}
'
,     ucfirst($theTableName),
       
ucfirst($theTableName))) ;

fwrite($theStream'
$_SESSION["query"] = $theQuery ;
$theUrl = explode("?", $_SERVER["HTTP_REFERER"]) ;

header("Location: " . $theUrl[0]) ;
?>
'
) ;
?>