GENERAL INFORMATION:

DO NOT MODIFY ANY FILES IN THE "generated" folder created in "/path/to/lib/". These files will be completely overwritten the next time the program is run. Add any custom functions to the main classes generated directly within "/path/to/lib/". All code written under the specified line will be saved if the program is re-run.

You can also add any custom "SITE WIDE" variables/arrays/functions to the "/path/to/lib.php" file to be used throughout your site. This could include things like the web path to the directory you upload pictures to as well as the absolute path of the directory you upload pictures to. Since this file should be included on every page anyway, it makes it logical to use it for these variables as well.


Initial Setup:
First, create the database. When creating the tables, the table name cannot equal "class_functions". Make sure to set the tables primary key(s) and foreign key(s) if they exist. The keys are used in numerous places. Tables with no primary key and tables with multi-column primary keys are handled differently from tables with single column primary keys. Tables with foreign keys have additional functions as well.

To create the code, you need to create a setup file that includes some basic parameters. An example setup file will look something like this:

(filename: ddataobjects.php)

<?php
error_reporting
(0);
// do not allow this page to be loaded from a browser
if (php_sapi_name() != 'cli'  && strstr($_SERVER['REQUEST_URI'], '/ddataobjects.php')) { Header('Location: '.(($_SERVER['HTTPS'] == 'on') ? 'https' 'http')."://$_SERVER[HTTP_HOST]"); return false; }


//setup database connection fields
$database['dbname'] = 'database_name';
$database['host'] = 'localhost';
$database['port'] = '5432';
$database['user'] = 'username';
$database['password'] = 'password';

//optional - list tables from the database to include, if not set, all tables are included
//$include_tables = array("table1", "table3", "table5");

//optional - list schemas from the database to include, if not set, all schemas are included
//$include_schemas = array("schema1", "schema3", "schema5");

//optional - set namespace
//$namespace = 'database_name';

//setup library path
$setup['lib_path'] = '/path/to/lib';

$setup['pathinfo'] = 'absolute';
#$setup['pathinfo'] = 'relative';

// This requires that php have allow_url_include set to on
require_once('http://www.ddataobjects.com/2.7/prog/setup_GET.php');

// if you download ddataobjects to use locally
//$path_to_local_directory = ''; // entered into box when downloaded, e.g. 'http://domain.com/ddataobjects'
//require_once("$path_to_local_directory/setup_GET.php");

// if you want to use a proxy
// start proxy connection first: ssh -qTfnN2 -D 8080 user@proxy.server.com
#$setup['useproxy'] = true;
#$URL = 'http://www.ddataobjects.com/2.7/prog/setup_GET.php';
#$ch = curl_init();
#curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
#curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8080');
#curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
#curl_setopt($ch, CURLOPT_URL, $URL);
#$data = curl_exec($ch) or die(curl_error($ch));
#if (strlen(trim($data)) == 0) echo 'Invalid Proxy, initialize proxy first';
#curl_close($ch);
#$file = fopen('/tmp/dd-setup.php', 'w');
#fwrite($file, $data);
#fclose($file);
#require_once('/tmp/dd-setup.php');

?>


then, from the command line, run setup:
root@localhost [/var/www/html]# php ddataobjects.php

This will create a folder called "/path/to/lib" with all of the library files included. It will also create "/path/to/lib.php" which is the file that should be included on every page of your site. All paths within the generated files are ABSOLUTE (unless you change pathinfo to relative). You cannot move this folder without modifying the setup file and re-running the script.


Variables Created:
The php code generated from this program will be based on the database being read. A class will be created for each table of the database with one variable for each column of the database. An example table named "person" with columns "person_id" "name" "address" "city" "state" "age" "birthdate" "person_created":

###################################################

<?php
class person
{
  var 
$person_id//-PRIMARY KEY COLUMN--integer--Null NOT Allowed--Default: nextval('person_person_id_seq'::regclass)--Comments: -
  
var $person_id_label 'Person Id';
  var 
$name//-character varying(20)--Null NOT Allowed--Comments: -
  
var $name_label 'Name';
  var 
$address//-character varying(50)--Null Allowed--Comments: -
  
var $address_label 'Address';
  var 
$city//-character varying(30)--Null Allowed--Comments: -
  
var $city_label 'City';
  var 
$state//-character varying(2)--Null Allowed--Comments: -
  
var $state_label 'State';
  var 
$zip//-character varying(1)--Null Allowed--Comments:  -
  
var $zip_label 'Zip';
  var 
$phone//-character varying(14)--Null Allowed--Comments:  -
  
var $phone_label 'Phone';
  var 
$email//-character varying(50)--Null Allowed--Comments:  -
  
var $email_label 'Email';
  var 
$age//-integer--Null Allowed--Comments: -
  
var $age_label 'Age';
  var 
$birthdate//-date--Null Allowed--Comments: -
  
var $birthdate_display;
  var 
$birthdate_label 'Birthdate';
  var 
$salary//-double precision--Null Allowed--Comments:  -
  
var $salary_label 'Salary';
  var 
$person_created//-timestamp without time zone--Null NOT Allowed--Default: now()--Comments: -
  
var $person_created_display;
  var 
$person_created_label 'Person Created';

  ...

}
?>


*note - if a variable is a timestamp, a class variable will be created with the variable name to store the
	timestamp.  Another variable will be created as the variable name appended by "_display" which will
	be a display friendly version of the timestamp in "mm/dd/yyyy hh:mm" format.
      - if a variable is a date, a class variable will be created with the variable name to store the date.
	Another variable will be created as the variable name appended by "_display" which will be a display
	friendly version of the date in "mm/dd/yyyy" format.
      - if a variable is a time, a class variable will be created with the variable name to store the time.
	Another variable will be created as the variable name appended by "_display" which will be a display
	friendly version of the time in "hh:mm am/pm" format

* "_lookup" variables - if any fields in this table are foreign keys in another table (and this table links
	to the other table exactly 1 time), another variable can be created as the name of the other table
	appended by "_lookup"  which will store an array of arrays containing the values of the other table.
	A label variable will also be created.  If the linked table is a lookup table, the value of the label
	variable with be the table name of the column referenced from the lookup table, otherwise, it will be
	the name of the table linked to.  This feature is enabled by adding the text "create_linked_variables"
	somewhere in the comments section of the table.
*THERE IS A CHANCE OF A CONFLICT HERE if this table links to a lookup table that references a table, and another
	element of this table references the referenced table directly.  I consider the chance of this
	happenening remote enough to not work around, but I do want to mention it.



Loading Classes
By default classes are loaded with PHP's __autoload() function in lib.php. Two additional functions are created if this doesn't work. The "load_all()" function will load all classes, and the "load($class) function can load the individual classes, for example, "load('person')"


Constructor:
A class constructor will be created which (optionally) takes as input the primary key of the table (if one exists, does not work with tables with multiple primary key fields). If the primary key is entered, the record will be selected from the table and the values will be set in the class.
<?php
include('lib.php');
load('person');
$person = new person(12);
?>


Also, there is an optional query string that can be entered to get a single record from the database. This is most useful when a primary key doesn't exist, but has been relevant in other situations as well. If more than one record is returned by the search, no records will be set. Search must return exactly one record. IMPORTANT: YOU MUST USE pg_escape_string ON USER GENERATED CONTENT HERE

<?php
include('lib.php');
load('person');
$person = new person(null"address = 'PO Box 1299' AND city = 'Saint Louis' AND state = 'MO'");
?>


If there is no primary key (or multiple primary keys):.

<?php
include('lib.php');
load('abstract_table');
$class = new abstract_class("table_name = 'value'");
?>





--------------------------------------------------------------------------------------------------------
Parameters that can be passed through Postgresql comments are:
regexp, label (l), field_type (ft), input_type (it), size (s), default_value (dv),
rows (r), columns(c), elements (e), spacer, multiple, additional_params (ap),
foreign_DISPLAY_columns (fdc), foreign_SORT_columns (fsc), foreign_DISPLAY_columns_comma (fdcc)


Comments are passed through with the following syntax: "regular comments|||param1|value1||param2|value2||....
--------------------------------------------------------------------------------------------------------

The "regular comments" value will create the class variable ${field_name}_regular_comments that can be returned on error.

The regexp (regular expression) will be added to the variables setter to validate the variable

The label will create the class variable ${field_name}_label with the value of "Label". If "Label" is not set, the value will be the value of the field with the first letter of each word capitalized and "_"'s replaced by " "'s.

The field_type can be used to denote special fields that have their own unique error checking and formatting. This
only works with "text" type fields.
Current Field Types:
    phone_number: all non-digit chars will be stripped, 9 digit chars must exist and will be formatted as (xxx) xxx-xxxx
    zip_code: must be 5 digits or 9 digits
    email: validated against the regexp ^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,3})$
    currency: creates a "_display" variable and formats it as number_format($value, 2, '.', ',')
    ssn: all non-digit chars will be stripped. must be 9 digits, formatted xxx-xx-xxxx
    mmdd: A date without the year. Internally the year will be set to 9999, but will display as mm/dd
    not setup yet credit_card_number: all non-digit chars will be stripped. then must be 16 digits. formatted as xxxx-xxxx-xxxx-xxxx
    not setup yet month_year: must be 2 sets of digits seperated by - or /. must pass a date check for m/1/y.

The input_type can be used to change the default input element of the field from textbox to something else. (use the size parameter to set default textbox size)
Current Input Types:
    password displayed value will be ********
    textarea use this with the rows and columns parameters
    select use this with the elements and multiple parameters
    radio use this with the elements and spacer parameters
Use additional_params and default_value with any of the input_types

The foreign_DISPLAY_columns are used when the field is a foreign key to another table. This tells internal functions what to link to
The foreign_DISPLAY_columns_comma is used to tell internal functions to put a comma between columns when auto-pulling data out of linked tables

The foreign_SORT_columns are used when the field is a foreign key to another table. This tells internal functions how to sort the DISPLAY columns
NOTE ON FOREIGN KEYS: A setter method will also be generated for the "_lookup" variables. Input will be a
one-dimensional array, either the referenced column from the lookup table will be used, or the primary key
of the table will be set if it isn't a lookup table will be used.