DB_DataObject_FormBuilder
[ class tree: DB_DataObject_FormBuilder ] [ index: DB_DataObject_FormBuilder ] [ all elements ]

Source for file index.php

Documentation is available at index.php

  1. <?php
  2.  
  3. /*************************************************************************
  4.  * As all DataObject-derived classes can be used in exactly the same way,
  5.  * we will use only a single, dynamic script for all the tables
  6.  *************************************************************************/
  7.  
  8. // Be unforgiving about ANY errors, warnings or notices!
  9.  
  10. // Read DataObject and FormBuilder configurations
  11. require_once './config_inc.php';
  12.  
  13. // Which class to use?
  14. $class = isset($_GET['class']$_GET['class''Product';
  15.  
  16. // make the object
  17. require_once 'DB/DataObject.php';
  18. $obj = DB_DataObject::factory($class);
  19.  
  20. // Maybe an existing record was chosen? If yes, fetch it before making the form!
  21. // (If you don't, a new one will be created on form submit)
  22. if (isset($_GET['id']&& is_numeric($_GET['id'])) {
  23.     $obj->get($_GET['id']);
  24. }
  25.  
  26. // Include the FormBuilder class definition
  27. require_once 'DB/DataObject/FormBuilder.php';
  28.  
  29. // Create the FormBuilder object BY REFERENCE and pass the DataObject
  30. $formBuilder =DB_DataObject_FormBuilder::create($obj);
  31.  
  32. // Create the form, make sure to always make a REFERENCE to the FormBuilder object!
  33. $form =$formBuilder->getForm($_SERVER['REQUEST_URI']);
  34.  
  35. // If the form was posted and the data has passed all rules,
  36. // apply the changes to the database
  37. if ($form->validate()) {
  38.     $form->process(array($formBuilder'processForm')false);
  39. }
  40.  
  41. /**
  42.  * Although not essential for this example, let's make a quick list of the things
  43.  * that already are in the database. All entries will have a link that enables
  44.  * you to update the records by passing the appropriate ID.
  45.  */
  46. $listObj = new $class;
  47. $list = null;
  48. // Do we have enough info on the object to list all records? We need a field to use for displaying the entry...
  49. if (isset($_DB_DATAOBJECT_FORMBUILDER['CONFIG']['select_display_field']|| isset($listObj->select_display_field)) {
  50.     // look if there are already records in this table
  51.     $num $listObj->find();
  52.     if ($num > 0{
  53.         // yes, make a list for later output
  54.         $list '';
  55.         while ($listObj->fetch()) {
  56.             // look for the display field in the object
  57.             if (isset($listObj->select_display_field)) {
  58.                 $titleField $listObj->select_display_field;
  59.                 $title $listObj->$titleField;
  60.             else {
  61.                 $title $listObj->$_DB_DATAOBJECT_FORMBUILDER['CONFIG']['select_display_field'];
  62.             }
  63.             // get the primary key
  64.             $keyField strtolower($class.'_id');
  65.             $id $listObj->$keyField;
  66.             // make the list entry
  67.             $list .= sprintf("<a href='?class=%s&id=%s'>%s</a><br />"$class$id$title);
  68.         }
  69.     }
  70. }
  71.  
  72.  
  73.  
  74. // Some HTML for nice output, inline for simplicity
  75. ?>
  76. <html>
  77.   <head>
  78.     <title>DB_DataObject_FormBuilder Example</title>
  79.     <style type="text/css">
  80.       <!--
  81.         body { font-family: Georgia, Verdana, Arial, Helvetica, Sans-Serif; font-size: 1em; }
  82.         h1 { font-size: 1.5em; font-weight: bold; }
  83.         .classes { background: #EFEFEF; padding: 5px; margin: 5px; border: 1px solid black; width: 150px; float: left; }
  84.         .form { margin-left: 200px; width: 300px; }
  85.         .list { margin-left: 200px; background: #EFEFEF; border: 1px dotted grey; width: 300px; }
  86.       -->
  87.     </style>
  88.   </head>
  89.   <body>
  90.     <h1>Table/Class: <?php echo($class)?></h1>
  91.     <div class='classes'>
  92.       <a href='?class=Category'>Categories</a><br />
  93.       <a href='?class=Manufacturer'>Manufacturers</a><br />
  94.       <a href='?class=Product'>Products</a><br />
  95.     </div>
  96.     <div class='form'><?php $form->display()?></div>
  97.     <div class='list'><?php echo($list)?></div>
  98.   </body>
  99. </html>

Documentation generated on Mon, 11 Mar 2019 15:48:18 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.