PHP Classes

File: buildForm.php

Recommend this page to a friend!
  Classes of Richard Munroe   SQL Data   buildForm.php   Download  
File: buildForm.php
Role: Application script
Content type: text/plain
Description: Input/Edit Form Builder
Class: SQL Data
Generate classes to store objects in SQL databases
Author: By
Last change: Minor tweak for better php5 compatibility.
Fix integration with syntactic validation style sheet.
Date: 16 years ago
Size: 12,851 bytes
 

Contents

Class file image Download
<?php /** * @author Dick Munroe <munroe@csworks.com> * @copyright copyright (c) Dick Munroe, 2004-2006, All rights reserved. * @license http://www.csworks.com/publications/ModifiedNetBSD.html * @version 2.0.0 */ // // Construct an "edit" form using the class interface to a mySQL table // generated by buildClass // // Build an HTML form from a mySQL table description that uses // a SQLData derived class to access the data for the form. // The form/PHP program generated will require tweaking in order // to be complete. Experience shows that the generated form is // about 80% of the work. // // My thanks to Chris Sands, the Director of IT at the Florida Democratic // Party, for permission to submit this program for general use. // // $Author: dickmunroe $ // $Date: 2007/12/26 15:41:56 $ // // Edit History: // // Dick Munroe munroe@csworks.com 21-Oct-2004 // Initial Version Created // // Dick Munroe munroe@csworks.com 06-Nov-2004 // Class include names are of the form: class.tablename.php // Can't rely on position for database and table name, so they // have to be the last two argument in argv. // process pages are of the form process.editpagename // // Dick Munroe munroe@csworks.com 07-Nov-2004 // Add check for bad query state. // // Dick Munroe munroe@csworks.com 14-Nov-2004 // Stop using deprecated language tag. // // Dick Munroe munroe@csworks.com 21-Nov-2004 // Deal with "constant" enumerations. // // Dick Munroe munroe@csworks.com 21-Nov-2004 // It turns out that dealing with the enum issue requires // a redesign of the way output is done. // // Dick Munroe munroe@csworks.com 04-Dec-2004 // Add spans around name blocks to allow alternative error processing. // // Dick Munroe munroe@csworks.com 03-May-2005 // Pull the JavaScript generation stuff as part of the writing of the // article for the OpenVMS Technical Journal. // // 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. // Fix integration with syntactic validation style sheet. // include_once('SQLData/options.php') ; include_once('SDD/class.SDD.php') ; $theOptions = getopt("h:p:u:d:") ; if (count($_SERVER['argv']) < 3) { print(" buildForm [-h hostname] [-u username] [-p password] [-d DBType] tableName databaseName Writes a file named \"form.tableName.php\" and renames any existing file of the same name to form.tableName.php.old. It expects to include drivers for the syntactic validation framework so if validation code is to be generated, a file of the form \"form.tableName.js\" must exist. ") ; 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) ; $theFileName = sprintf("form.%s.php", ucfirst($theTableName)) ; $theOldFileName = $theFileName . ".old" ; $theProcessFileName = sprintf("process.%s.php", ucfirst($theTableName)) ; $theValidationFileName = sprintf("form.%s.js", ucfirst($theTableName)) ; $theValidationFlag = file_exists($theValidationFileName) ; $theDB = FactoryDB::factory( $theOptions['u'], // Username $theOptions['p'], // Password $theDatabaseName, // Database $theOptions['h'], // Host $theOptions['d']) ; // Database Type $theValidResults = array() ; $theResult = $theDB->describeTable($theTableName) ; $theEncodeType = " " ; foreach ($theResult as $theResultArray) { if (!preg_match('/auto_increment/', $theResultArray['Extra'])) { if (preg_match('/blob/', $theResultArray['Type'])) { $theEncodeType = ' enctype="multipart/form-data" ' ; } array_push($theValidResults, $theResultArray) ; } } // // Emit the rough draft of the form. // $theForm = array() ; $theForm[] = sprintf('<?php // // Edit form for: // // Class: %s // Table: %s // Database: %s // // Generated by buildForm.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() ; $theAction = stripslashes(requestUtils::getRequestObject("action")) ; if ($theAction == "new") { unset($_SESSION["query"]) ; } // // The update versus insert code selects by passing a query around // that selects the object of interest. // $theQuery = (empty($_SESSION["query"]) ? NULL : $_SESSION["query"]) ; $the%s = new %s($the%sDatabase, $the%sHost, $the%sUser, $the%sPassword) ; if (!empty($theQuery)) { if(!$the%s->select($theQuery)) { if ($the%s->hasErrors()) { $the%s->showErrors() ; die() ; } else { unset($_SESSION["query"]) ; } } } ?> ', ucfirst($theTableName), $theTableName, $theDatabaseName, ucfirst($theTableName), $theDatabaseName, ucfirst($theTableName), ucfirst($theTableName), $theDatabaseName, $theDatabaseName, $theDatabaseName, $theDatabaseName, ucfirst($theTableName), ucfirst($theTableName), ucfirst($theTableName)) ; $theForm[] = '<link rel="stylesheet" type="text/css" href="syntacticValidationFramework.css" /> ' ; if ($theValidationFlag) { $theForm[] = sprintf('<script type="text/javascript" src="syntacticValidationFramework.js"></script> <script type="text/javascript" src="%s"></script> ', $theValidationFileName) ; } $theForm[] = sprintf('<form name=data method=post%saction="%s"%s> <table name=dataTable id=dataTable border=1 cols=2> <tr> <td colspan="2"> * Required Field </td> </tr> <tr id="errorRow" class="errorRow"> </tr> ', $theEncodeType, $theProcessFileName, ($theValidationFlag ? ' onSubmit="return validate(this) ;"' : "")) ; foreach ($theValidResults as $theResultArray) { $theForm[] = sprintf(' <tr> ') ; $theForm[] = sprintf(' <td><span id="%s" class="inline">%s%s</span></td> ', ("span" . ucfirst($theResultArray['Field'])), ($theResultArray['Null'] == "YES" ? "" : "* "), $theResultArray['Field']) ; $theForm[] = sprintf(' <td> ') ; if ((preg_match('/(^(var)?char)\((\d+)\)/', $theResultArray['Type'], $theSize)) || (preg_match('/(^character(\s+varying)?)\((\d+)\)/', $theResultArray['Type'], $theSize))) { $theFieldSize = $theSize[3] ; if ($theSize[0] == 'char') { $theForm[] = sprintf('<input name="%s" id="%s" type=text size=%d maxlength=%d required="%s" validate="return validate%s(what)" value="<?php print $the%s->get%s() ; ?>">', $theResultArray['Field'], $theResultArray['Field'], ($theFieldSize <= 40 ? $theFieldSize : 40), $theFieldSize, ($theResultArray['Null'] == "YES" ? "" : $theResultArray['Field'] . " is required"), ucfirst($theResultArray['Field']), ucfirst($theTableName), ucfirst($theResultArray['Field'])) ; } else { $theForm[] = sprintf('<input name="%s" id="%s" type=text size=%d maxlength=%d required="%s" validate="return validate%s(what)" value="<?php print $the%s->get%s() ; ?>">', $theResultArray['Field'], $theResultArray['Field'], ($theFieldSize <= 40 ? $theFieldSize : 40), $theFieldSize, ($theResultArray['Null'] == "YES" ? "" : $theResultArray['Field'] . " is required"), ucfirst($theResultArray['Field']), ucfirst($theTableName), ucfirst($theResultArray['Field'])) ; } } else if (preg_match('/^enum\(([^\)]+)\)$/', $theResultArray['Type'], $theEnumeration)) { $theEnumeration = $theEnumeration[1] ; $theEnumeration = explode("','", $theEnumeration) ; $xxx = count($theEnumeration) - 1 ; $theEnumeration[0] = substr($theEnumeration[0], 1) ; $theEnumeration[$xxx] = substr($theEnumeration[$xxx], 0, strlen($theEnumeration[$xxx]) - 1) ; if ($theResultArray['Null'] == "YES") { $theEnumeration = array_unshift($theEnumeration, "") ; } if (count($theEnumeration) == 1) { // // for "constant" enumerations, get rid of the field name // display. // unset($theForm[count($theForm) - 2]) ; $theForm[] = sprintf(' <input type=hidden name="%s" id="%s" value="%s"> ', $theResultArray['Field'], $theResultArray['Field'], $theEnumeration[0]) ; } else { $theForm[] = sprintf('<select name="%s" id="%s"> ', $theResultArray['Field'], $theResultArray['Field']) ; for ($xxx = 0 ; $xxx < count($theEnumeration); $xxx++) { $theForm[] = sprintf(' <option value="%s" <?php ($the%s->get%s() == "%s" ? print("selected") : print("") ) ?>>%s</option> ', $theEnumeration[$xxx], ucfirst($theTableName), ucfirst($theResultArray['Field']), $theEnumeration[$xxx], $theEnumeration[$xxx]) ; } $theForm[] = sprintf(' </select>') ; } } else if (preg_match('/text/', $theResultArray['Type'])) { $theFieldSize = 40 ; $theForm[] = sprintf('<textarea name="%s" id="%s" wrap=soft cols=%d rows=10 required="%s" validate="return validate%s(what)"><?php print $the%s->get%s() ; ?></textarea>', $theResultArray['Field'], $theResultArray['Field'], $theFieldSize, ($theResultArray['Null'] == "YES" ? "" : $theResultArray['Field'] . " is required"), ucfirst($theResultArray['Field']), ucfirst($theTableName), ucfirst($theResultArray['Field'])) ; } else if (preg_match('/blob/', $theResultArray['Type'])) { $theFieldSize = 60 ; $theForm[] = sprintf('<input type="file" name="%s" id="%s" size=%d>', $theResultArray['Field'], $theResultArray['Field'], $theFieldSize) ; } else { $theFieldSize = 10 ; $theForm[] = sprintf('<input name="%s" id="%s" type=text size=%d maxlength=%d required="%s" validate="return validate%s(what)" value="<?php print $the%s->get%s() ; ?>">', $theResultArray['Field'], $theResultArray['Field'], ($theFieldSize <= 40 ? $theFieldSize : 40), $theFieldSize, ($theResultArray['Null'] == "YES" ? "" : $theResultArray['Field'] . " is required"), ucfirst($theResultArray['Field']), ucfirst($theTableName), ucfirst($theResultArray['Field'])) ; } $theForm[] = sprintf(' </td> ') ; $theForm[] = sprintf(' </tr> ') ; } $theForm[] = sprintf(' <tr> <td colspan=2> &nbsp; </td> </tr> <tr> <td align=center colspan=2> <button name=save id=save type=submit>Save</button> <button name=new id=new type=button onClick="window.location.search=\'?action=new\'">New</button> <button name=reload id=reload type=button onClick="window.location.search=\'?action=reload\'">Reload</button> </td> </tr </table> </form> ') ; // // Preserve the outfile, if one already exists. // if (file_exists($theFileName)) { if (is_file($theFileName)) { if (!rename($theFileName, $theOldFileName)) { exit(1) ; } } else { exit(2) ; } } if (!($theStream = @fopen($theFileName, 'w'))) { exit(3) ; } if (fwrite($theStream,implode("", $theForm)) === FALSE) { exit(4) ; } exit(0) ; ?>