| 
<?php/**
 * @author Dick Munroe <[email protected]>
 * @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 [email protected] 06-Nov-2004
 //      Initial Version Created
 //
 //  Dick Munroe [email protected] 04-May-2005
 //    Clean up for publication.
 //
 //  Dick Munroe [email protected] 14-Mar-2006
 //    Change licensing, reorganize includes.
 //
 //  Dick Munroe ([email protected]) 15-Oct-2006
 //      Switch to database independent interfaces.
 //
 //  Dick Munroe ([email protected]) 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 0 ;
 }
 
 //
 // 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($theStream, sprintf('<?php
 
 //
 // Edit form processor for:
 //
 // Class: %s
 // Table: %s
 // Database: %s
 //
 // Generated by buildProcessForm.php, written by Dick Munroe ([email protected])
 //
 
 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($theStream, sprintf('$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($theStream, sprintf('    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($theStream, sprintf('    $theQuery = "where %s = \'" . $the%s->escape_string($the%s->get("%s")) . "\'" ;
 ',     addcslashes($theDB->quoteIdentifier($thePrimaryKey), '"'),
 ucfirst($theTableName),
 ucfirst($theTableName),
 $thePrimaryKey)) ;
 }
 
 fwrite($theStream, sprintf('}
 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]) ;
 ?>
 ') ;
 ?>
 
 |