Class dbForm

Description

dbForm - Create, validate and process HTML forms based on Database Tables

dbForm is a PHP class that will create a Web Form for a database table and handles all processing of the form including validation of data, inserts, updates and deletes. Very customisable and flexible. Uses ADODB to connect to any database. Uses a simple Template to display the form. Looks for the following tags

{:template=""}
Includes another template inside the current one.

{:action=""}
{:end action}
Includes a section of html only if the current form action matches the actions in the list. The action list is a comma seperated list of all of the actions e.g. "select,update". The actions match buttons. See the method list for details of the default buttons and adding/removing buttons.

{:each field=""}
Loads a new template file for each field on the form

{:each field}
{:end field}
Repeats the code between the tags for each field on the form

{:fieldname}
Displays the name of the field. Only usable inside {:each field} tags
{:field}
Displays the field. Only usable inside {:each field} tags
Can contain attributes that will be applied to each field
e.g. {:field style="color: blue;"} will add the attribute style="color: blue;" to each field

{:field=""}
Displays the selected Field
Can contain attributes that will be applied to the field
e.g. {:field="firstname" style="color: blue;"} will add the attribute style="color: blue;" to firstname field

{:#fields}
Returns the number of visible fields

{:each button=""}
Loads a new template file for each button on the form

{:each button}
{:end button}
Repeats the code between the tags for each button on the form

{:button}
Display the button. Only usable inside {:each button} tags
Can contain attributes that will be applied to each button
e.g. {:button style="color: blue;"} will add the attribute style="color: blue;" to each button

{:button=""}
Displays the selected button
Can contain attributes that will be applied to the button
e.g. {:button="update" style="color: blue;"} will add the attribute style="color: blue;" to update button

{:#buttons}
Returns the number of buttons

{:hidden}
Displays all of the hidden fields
Can contain attributes that will be applied to each hidden field
e.g. {:hidden style="color: blue;"} will add the attribute style="color: blue;" to each hidden field

{:formname}
Displays the name setup in the initial creation of the form

{:form}
Displays the opening form tag. You will have to put the closing tag yourself.
Can contain attributes that will be applied to the form tag
e.g. {:form style="color: blue;"} will add the attribute style="color: blue;" to the form tag

{:status}
Display any status messages from the form.

Located in Program_Root/dbform.inc (line 292)

template
   |
   --dbForm
Variable Summary
boolean $alwaysUpdate
boolean $hashPasswords
boolean $magicQuote
boolean $validateFields
string $ver
Method Summary
class dbForm (string $name, [object conn $conn = null], [string $table = null], [string $method = 'POST'], [string $action = null], [string $target = '_self'], [array $attributes = null])
void addButton (string $name, string $title, [string $type = "submit"])
void addField (string $name, [string $type = "hidden"], [string $dbType = null], [array $attributes = null])
void changeButtonType (string $name, [string $type = "submit"])
void changeFieldType (string $field, [string $type = "hidden"])
void clearButtons ()
void displayForm ()
string getFormAction ()
void getSelectFunction (string $name, string $function)
void getSelectValues (string $name, string $query, [string $fieldGlue = " "], [string $defaultText = null])
void orderButtons (array $orderArray)
void orderFields (array $orderArray)
void processForm ()
void processTemplate ()
void removeButton (string $name)
void removeField (string $name)
void setActionStatusMsg (string $action, [string $status = null])
void setAllButtonsAttributes (array $attributes)
void setAllFieldsAttributes (array $attributes)
void setAttributes (array $attributes)
void setButtonAttributes (string $button, array $attributes)
void setCheckbox (string $field, [mixed $checkedValue = true], [mixed $uncheckedValue = false])
void setFieldAttributes (string $fieldName, array $attributes)
void setFieldMaxLength (string $fieldName, [int $length = null])
void setFieldNotNull (string $fieldName, [boolean $notNull = true])
void setFieldTitle (string $field, string $title)
void setFieldTitles (array $titlesArray)
void setFilterGroup (string $field, string $filterField, array $filterGroup, string $filterReplace, [string $defaultText = null])
void setFilterPaging (string $field, int $pageSize, [string $defaultText = null])
void setFilterQuery (string $field, string $filterField, string $filterQuery, string $filterReplace, [string $glue = " "], [string $defaultText = null])
void setProcessingFunction (string $action, [string $function = null])
void setSearchField (string $name, string $query, [string $glue = " "], [string $defaultText = null], [boolean $autoSelect = false])
void setSearchFunction (string $name, string $function, [boolean $autoSelect = false])
void setSequence (string $sequence)
void setSequenceFunction (string $function)
void setStatusMsg ([string $status = null])
Variables
boolean $alwaysUpdate (line 493)

Whether to always update the current record before processing

Updates the record first if moving to another record.

  • access: public
boolean $forceUpdateNulls (line 522)

Boolean whether to force the update of empty fields

This is used to fix an issue where resetting populated fields to empty are not saved. suggested by Keith Powers

  • access: public
boolean $hashPasswords (line 502)

Boolean for hashing password fields

Requires the mhash extension to be loaded

  • access: public
boolean $magicQuote (line 531)

Boolean whether to use magic quoting (See PHP get_magic_quotes_gpc())

Used to determine how to handle quoted data for getUpdateSQL and getInsertSQL

  • access: public
boolean $validateFields (line 511)

Boolean for using validation

Currently validates not null and max lengths of fields

  • access: public
string $ver = '0.9.0' (line 300)

Version of dbForm

  • access: public
Methods
Constructor dbForm (line 584)

Class constructor

Create a dbForm based on a database table. If a table is set calls _prepareFields to determine the fields in the table Note however that no table is required and the form can be created manually Name will be stripped on any whitespace before being used so that javascript can find the form easily based on name

  • access: public
class dbForm (string $name, [object conn $conn = null], [string $table = null], [string $method = 'POST'], [string $action = null], [string $target = '_self'], [array $attributes = null])
  • string $name: name Name of the form
  • object conn $conn: ADODB Database Connection object
  • string $table: table Database table to be used for the form
  • string $method: method Form method either POST or GET
  • string $action: action Action url of the form
  • string $target: target Target for the form action
  • array $attributes: attributes Any extra form attributes
addButton (line 1347)

Adds a button to the buttons array

Type can be submit, button or image. Image buttons require the dbForm.js file or javascript errors will occur. Image buttons use a hidden field called dbform_imagerequesttype for processing

  • throws: E_USER_ERROR on validation errors
  • access: public
void addButton (string $name, string $title, [string $type = "submit"])
  • string $name: name Name of the button to add
  • string $title: title Title for the button
  • string $type: type Button type, either submit or button
addField (line 1004)

Adds a new field to the form at the end of the list

Creates a new fieldForm object and stores it in the class.

  • throws: E_USER_ERROR on validation errors
  • access: public
void addField (string $name, [string $type = "hidden"], [string $dbType = null], [array $attributes = null])
  • string $name: name Name of the field to add
  • string $type: type Field type
  • string $dbType: dbType metaType of the field
  • array $attributes: attributes Default field attributes in name value pair array
changeButtonType (line 1458)

Changes the button type

Can change the type to cubmit, button or image. Image buttons require the dbForm.js file or javascript errors will occur. Image buttons use a hidden field called dbform_imagerequesttype for processing.

  • throws: E_USER_ERROR on validation errors
  • access: public
void changeButtonType (string $name, [string $type = "submit"])
  • string $name: name Name of the button
  • string $type: type Type to change the button to
changeFieldType (line 1227)

Changes the Types of a field

Cannot set to radio or checkbox with this method. Radio is not supported and checkbox has its own method (See setCheckBox)

  • throws: E_USER_ERROR on validation errors
  • access: public
void changeFieldType (string $field, [string $type = "hidden"])
  • string $field: field Name of the field
  • string $type: type Type to set the field to
clearButtons (line 1528)

Removes all of the buttons and processingFunctions if set

  • throws: E_USER_ERROR on validation errors
  • access: public
void clearButtons ()
displayForm (line 3925)

Displays the Form and all of its fields

Only very simple at the moment but will eventually need to be able to display the form in a layout dependant on set properties and also be able to display only parts of the form depending on the previous actions

  • throws: E_USER_ERROR on validation errors
  • access: public
void displayForm ()
getFormAction (line 2130)

Sets and returns the form action based on the parameters passed

Created as a seperate function so that it can be processed early if the need should arise. Looks at the requesttypes passed and sets the _formAction property and returns this as a value. Can be used to selectively create parts of the form depending on the action Note: If called before all buttons are added then it may not return the correct action. Defaults to dbform_null.

  • access: public
string getFormAction ()
getSelectFunction (line 3541)

Calls the specified function to retrieve the select values

Select functions will be passed the following parameters. $value is the value of the current selected item. $action is the current action being processed.

Select functions should return an array of items in the following format: $options[0]["value"]=1 $options[0]["text"]="Some Text" $options[0]["selected"]=true // only if $value = 1 $options[0][any other attributes]=something;

  • throws: E_USER_ERROR on validation errors
  • access: public
void getSelectFunction (string $name, string $function)
  • string $name: name Name of field
  • string $function: function Function to call to retrieve values
getSelectValues (line 3429)

Retrieves the values for a select list based on a query

Primarily used for foreign key lists must be called after process form so that the value for the field already exists _conn must be set If using filtering then this is not used.

  • throws: E_USER_ERROR on validation errors
  • access: public
void getSelectValues (string $name, string $query, [string $fieldGlue = " "], [string $defaultText = null])
  • string $name: name Name of the field
  • string $query: query query used to retrieve the values
  • string $fieldGlue: fieldGlue Glue to join fields
  • string $defaultText: defaultText Default text to display at the start of the list
orderButtons (line 1553)

Orders the buttons incase they need to be displayed in order

Only easy way to do this is create a tempory array and add items to it in order

  • throws: E_USER_ERROR on validation errors
  • access: public
void orderButtons (array $orderArray)
  • array $orderArray: orderArray Array of button names used for ordering
orderFields (line 1259)

Orders the fields based on an array of field names

Any fields not placed in the order will be placed at the end of the list in the order they are in the current list

  • throws: E_USER_ERROR on validation errors
  • access: public
void orderFields (array $orderArray)
  • array $orderArray: orderArray Array of field names used for ordering
processForm (line 2222)

Determines what to do with any data passed thru the form

Does some preprocessing on any field data returned.
Determines the action to perform.
Calls the appropriate internal or external function to perform the action.
Processes any filters if required.

  • throws: E_USER_ERROR on validation errors
  • access: public
void processForm ()
processTemplate (line 3893)

Processes a template and returns the html for it

  • throws: E_USER_ERROR on validation errors
  • access: public
void processTemplate ()

Redefinition of:
template::processTemplate()
Process the Template
removeButton (line 1505)

Removes a button

Only easy way to remove a button is to add everything except the button to a tempory array and then assign the tempory array to _buttons

  • throws: E_USER_ERROR on validation errors
  • access: public
void removeButton (string $name)
  • string $name: name Name of the button to remove
removeField (line 1301)

Removes the indicated field from the field list

Field still exists but will not be displayed.

  • throws: E_USER_ERROR on validation errors
  • access: public
void removeField (string $name)
  • string $name: name Name of the field to remove
setActionStatusMsg (line 2084)

Sets the status message for a given action

Allows setting of the default status message to use for a specified action Can only be called before processForm
Note: Does not validate that the action is possible. i.e. actions usually equate to button names but this will NOT validate that a button of that name exists because to do so would not enable setting of the "null" action status. Also means that status messages could be set before the buttons are created.

  • throws: E_USER_ERROR on validation errors
  • access: public
void setActionStatusMsg (string $action, [string $status = null])
  • string $action: action Action or button name for the status message
  • string $status: status Status message to set for the action
setAllButtonsAttributes (line 1424)

Sets the Attributes for All buttons

  • throws: E_USER_ERROR on validation errors
  • access: public
void setAllButtonsAttributes (array $attributes)
  • array $attributes: attributes Array of name value pairs for the attributes
setAllFieldsAttributes (line 1198)

Updates Field Attributes of All fields

  • throws: E_USER_ERROR on validation errors
  • access: public
void setAllFieldsAttributes (array $attributes)
  • array $attributes: attributes Array of name value pairs for all attributes
setAttributes (line 903)

Sets Form Attributes

Sets any attributes required for the form, if not already set on class creation.

  • throws: E_USER_ERROR on validation errors
  • access: public
void setAttributes (array $attributes)
  • array $attributes: attributes Array of attribute name value pairs
setButtonAttributes (line 1398)

Sets the Attributes for a button

  • throws: E_USER_ERROR on validation errors
  • access: public
void setButtonAttributes (string $button, array $attributes)
  • string $button: button Name of the button
  • array $attributes: attributes Array of name value pairs for the attributes
setCheckbox (line 1940)

Sets a field to be a checkbox and defines the values for it

The checkValue and uncheckValue parameters are the values that will be stored in the database if the checkbox is checked or unchecked

  • throws: E_USER_ERROR on validation errors
  • access: public
void setCheckbox (string $field, [mixed $checkedValue = true], [mixed $uncheckedValue = false])
  • string $field: field Name of field
  • mixed $checkedValue: checkedValue Database value used to indicate checked
  • mixed $uncheckedValue: uncheckedValue Database value used to indicate unchecked
setFieldAttributes (line 1104)

Updates Field Attributes

  • throws: E_USER_ERROR on validation errors
  • access: public
void setFieldAttributes (string $fieldName, array $attributes)
  • string $fieldName: fieldName Name of field to set attributes for
  • array $attributes: attributes Array of name value pairs for attributes
setFieldMaxLength (line 1138)

Set the Max Length field validation property

Note: Max length is set but ignore during display for certain fields such as select and checkboxes Requires the template to include the dbForm.js file or javascript errors will occur

  • throws: E_USER_ERROR on validation errors
  • access: public
void setFieldMaxLength (string $fieldName, [int $length = null])
  • string $fieldName: fieldName Name of the field
  • int $length: length Max Length of the fields data
setFieldNotNull (line 1171)

Set the Not Null field validation property

Requires the template to include the dbForm.js file or javascript errors will occur

  • throws: E_USER_ERROR on validation errors
  • access: public
void setFieldNotNull (string $fieldName, [boolean $notNull = true])
  • string $fieldName: fieldName Name of the field
  • boolean $notNull: notNull Boolean Flag for whether the field is not null
setFieldTitle (line 1033)

Sets the title of the selected field

We are going to enforce that this be done before formProcess but this is not strictly necessay as it doesnt effect processing

  • throws: E_USER_ERROR on validation errors
  • access: public
void setFieldTitle (string $field, string $title)
  • string $field: field Name of the field to set the title for
  • string $title: title Title for the field
setFieldTitles (line 1068)

Sets the titles of many fields

Accepts an associative array of fields and titles. We are going to enforce that this be done before formProcess but this is not strictly necessay as it doesnt effect processing.

  • throws: E_USER_ERROR on validation errors
  • access: public
void setFieldTitles (array $titlesArray)
  • array $titlesArray: titlesArray Array of name title pairs for fields
setFilterGroup (line 1821)

Sets a filter group for a select list

The filter query will be run first and the data displayed in the search list in an attempt to limit the search records displayed. Builds the filter list based on a array of filter groups Currently only the earch field can be filtered. Only stores data in the selectList array. Processing is done later. NOTE: Hidden fields are added to the form to help with filter processing

  • throws: E_USER_ERROR on validation errors
  • since: 0.8.3
  • access: public
void setFilterGroup (string $field, string $filterField, array $filterGroup, string $filterReplace, [string $defaultText = null])
  • string $field: field Name of the field to filter
  • string $filterField: filterField Name of the field in the search query to filter by
  • array $filterGroup: filterGroup Array of filter groups
  • string $filterReplace: filterReplace String from the search query to replace with the where clause
  • string $defaultText: defaultText Default text to display at the start of the list
setFilterPaging (line 1885)

Sets a filter Page for a select list

The filter query will be run first and the data displayed in the search list in an attempt to limit the search records displayed. Build filter by creating groups of X records.
Uses SELECT LIMIT queries or the ADODB simulated equivilent.
Currently only the earch field can be filtered.
Only stores data in the selectList array. Processing is done later.
NOTE: Hidden fields are added to the form to help with filter processing

  • throws: E_USER_ERROR on validation errors
  • since: 0.8.3
  • access: public
void setFilterPaging (string $field, int $pageSize, [string $defaultText = null])
  • string $field: field Name of field to filter
  • int $pageSize: pageSize Number of records per filter group
  • string $defaultText: defaultText Default text to display at the start of the list
setFilterQuery (line 1756)

Sets a filter query for a select list

The filter query will be run first and the data displayed in the search list in an attempt to limit the search records displayed. Uses similar parameters to setSearchField. Currently only the search field can be filtered. Only stores data in the selectList array. Processing is done later. NOTE: Hidden fields are added to the form to help with filter processing

  • throws: E_USER_ERROR on validation errors
  • since: 0.8.3
  • access: public
void setFilterQuery (string $field, string $filterField, string $filterQuery, string $filterReplace, [string $glue = " "], [string $defaultText = null])
  • string $field: field Name of the field to filter
  • string $filterField: filterField Field in the search query to filter by
  • string $filterQuery: filterQuery Query to display the filter
  • string $filterReplace: filterReplace String from the search query to replace with the where clause
  • string $glue: glue Glue to join fields
  • string $defaultText: defaultText Default text to display at the start of the list
setProcessingFunction (line 1995)

Sets the function to be called to process an action

Validates the actions exists before setting Processing functions will be passed the following parameters.
&$conn
The database connection that can be used to process the action.
$table
The table that the form is built for.
$httpvars
Array of the _GET or _POST variables passed to the form.
$httpfiles
Array of the _FILES variables passed to the form. Used for file upload processing.
$searchField
The field used to search by. Set by the setSearchField() method.
$searchid
The value returned from the search field.
$action
The form action to process.

These parameters are general enough that it is possible to create a single function to process all form actions if desired.

Processing functions should return an array of values for the fields If no values are needed return an empty array e.g.
$returnValues["personid"]["value"]=2;
$returnValues["lastname"]["value"]="Smith";
$returnValues["firstname"]["value"]="John";

  • throws: E_USER_ERROR on validation errors
  • access: public
void setProcessingFunction (string $action, [string $function = null])
  • string $action: action Action to replace the processing for
  • string $function: function Function to perform the action
setSearchField (line 1611)

Sets the field to be used for searching (usually the primary key)

Requires a query to display the list. Query requires first field to be the index, any extra fields will be displayed concatenated together by the glue field. The default text if any is displayed as the first item in the list. Auto selection is used to simplify form processing, otherwise the select button must be used to selected the record. Changes the field to be a hidden field and creates a new field called dbform_searchid that the selection list will appear as. NOTE: This means you MUST have a field in your template called dbform_searchid for this to work

  • throws: E_USER_ERROR on validation errors
  • access: public
void setSearchField (string $name, string $query, [string $glue = " "], [string $defaultText = null], [boolean $autoSelect = false])
  • string $name: name Name of the field to use for searching
  • string $query: query Query to display in select list
  • string $glue: glue Glue to join fields
  • string $defaultText: defaultText Default text to display at the start of the list
  • boolean $autoSelect: autoSelect Whether to auto select records
setSearchFunction (line 1687)

Sets the field to be used for searching (usually the primary key)

Also sets the function to call to populate the field
Changes the field to be a hidden field and creates a new field called dbform_searchid that the selection list will appear as.
NOTE: This means you MUST have a field in your template called dbform_searchid for this to work

Search functions will be passed the following parameters.
$value is the value of the current selected item.

Search functions should return an array of items in the following format:
$options[0]["value"]=1
$options[0]["text"]="Some Text"
$options[0]["selected"]=true // only if $value = 1
$options[0][any other attributes]=something;

  • throws: E_USER_ERROR on validation errors
  • access: public
void setSearchFunction (string $name, string $function, [boolean $autoSelect = false])
  • string $name: name Name of the field to use for searching
  • string $function: function Name of the function to call
  • boolean $autoSelect: autoSelect Whether to auto select records
setSequence (line 929)

Sets the Sequence to use for the table in the form

Uses ADODB GenId which should create the sequence if it does not exist Emulates sequences for those databases that do not support them

  • throws: E_USER_ERROR on validation errors
  • access: public
void setSequence (string $sequence)
  • string $sequence: sequence Name of the sequence to use for the seach field
setSequenceFunction (line 966)

Sets the Sequence Function to use for the table in the form

Function will be called on insert to determine the new primary key for the table Really only useful if you need to produce a non standard key that cannot be generated by a sequence

  • throws: E_USER_ERROR on validation errors
  • access: public
void setSequenceFunction (string $function)
  • string $function: function Name of the function to call to retrieve the next sequence value
setStatusMsg (line 2106)

Sets the current Status Message

Hardcodes the status message reguardless of the current action. Can be called at any time.

  • access: public
void setStatusMsg ([string $status = null])
  • string $status: status Status message to set

Inherited Methods

Inherited From template

template::template()
template::display()
template::processTemplate()
template::replace()
template::setTemplateFile()

Documention generated on Sun, 14 Sep 2003 18:35:15 +1000 by phpDocumentor 1.2.1