PHP Demo Application - Source Code
/Application/Model/FormHandlers/Customer.php
<?php
/**
* Script Contents: Apeel_Application_Model_FormHandlers_Customer Class
* Extends Apeel_Framework_Model_FormHandlers_Abstract Class
* @package Apeel_Application_Model_FormHandlers
*/
/**
* Core functionality to handle displaying, populating, validating and saving
* data from forms.
*
* A form handler class in the Apeel Framework outputs the form, populated with
* data from a Data Object, it sets up the event handlers and both client side
* and server side validation, as well as handling the Save event to add/edit
* data to the database.
*
* It uses the Smarty templating engine to seperate design from coding and
* offer maximum flexibility.
*
* Form templates reside in Application/View/Templates/Forms
*
* @package Apeel_Application_Model_FormHandlers
* @version 1.1.0
* @author John W. King (email: contact@apeelframework.net)
* @copyright City Business Logic Limited 2001-2011
* @license Dual MIT / GNU Lesser General Public License Version 3
*/
class Apeel_Application_Model_FormHandlers_Customer extends Apeel_Framework_Model_FormHandlers_Abstract {
/**
* The unique code belonging to the data object linked to this class, for
* use in public areas such as javascript and hidden form fields.
*
* @var string
*/
public $dataObjectHash = 'd385a06aa02e0d3a61ad593bf19fa1e9';
/**
* The name of the base data object connected to this form.
*
* @var string
*/
public $dataObjectName = 'Customer';
/**
* Set default data values for Insert mode.
*
* e.g.
* $this->_data['FORMFIELD1'] = 'VALUE1';
* $this->_data['FORMFIELD2'] = 'VALUE2';
*
* @return void
*/
public function setDefaults() {
$this->_data['customer.customer_id'] = 'TBA';
$this->_data['customer.store_id'] = 0;
$this->_data['store_id__store.desc'] = '';
$this->_data['customer.first_name'] = '';
$this->_data['customer.last_name'] = '';
$this->_data['customer.email'] = '';
$this->_data['customer.address_id'] = 0;
$this->_data['address_id__address.desc'] = '';
$this->_data['customer.active'] = '1';
$this->_data['customer.create_date'] = '';
$this->_data['customer.last_update'] = '-';
}
/**
* Populate form fields with values from the Data Object.
*
* Abstract Base Class contains several methods to process values for
* different types of field such as:
*
* populateText(...)
* populateCheckbox(...)
* populateDropdown(...)
* populateDecimal(...)
* populateFileSystemImage(...)
* populateFileSystemFile(...)
* populateDatabaseImage(...)
* populateDatabaseFile(...)
* populateRichTextEditor(...)
* populateDuallist(...)
*
* e.g. $this->populateText('FORMFIELD', $this->_data['DBFIELD']);
*
* @return void
*/
public function populate() {
$this->populateText('customer__customer_id__6', $this->_data['customer.customer_id']);
$this->populateDropdown('customer__store_id__store__value__6_options', 'Store', 'Dropdown', $this->_data['customer.store_id']);
$this->populateText('customer__first_name__6', $this->_data['customer.first_name']);
$this->populateText('customer__last_name__6', $this->_data['customer.last_name']);
$this->populateText('customer__email__6', $this->_data['customer.email']);
$this->populateText('customer__address_id__6', $this->_data['customer.address_id']);
$this->populateText('address_id__address__desc__6', $this->_data['address_id__address.desc']);
$this->populateCheckbox('customer__active__6__checked', $this->_data['customer.active']);
$this->populateDate('customer__create_date__6__ID','customer__create_date__6__Display', $this->_data['customer.create_date'],1,1);
$this->populateText('customer__last_update__6', $this->_data['customer.last_update'], true);
}
/**
* Uses helper functions from base abstract class to generate Javascript for
* Form to handle widgets (AutoComplete, DataPicker etc) and Validation.
*
* @param string $mode
* @param integer $uniqueIndex
* @param string $returnId
* @param string $returnDisplay
* @return void
*/
public function generateClientScripts($mode, $uniqueIndex, $returnId, $returnDisplay) {
// Setup Header
$script = '
$(document).ready( function() {initEditPage();});
function initEditPage() {
$("#frmUpdate_d385a06aa02e0d3a61ad593bf19fa1e9").validate();
' . $this->getJsGeneral($uniqueIndex, $returnId, $returnDisplay);
// Setup Widgets
$script .= $this->getJsAutoComplete('Address__27_display', 'Address__27_id', '60053c13e608f70173c8336aebdae97b', 'CONTAINS', 2);
$script .= $this->getJsDateTimepicker('Create__Date__29__ID', 'Create__Date__29__Display', 'd MM yy');
// Setup Validation
$script .= $this->getJsRequired('Store__23');
$script .= $this->getJsRequired('First__Name__24');
$script .= $this->getJsRequired('Last__Name__25');
$script .= $this->getJsRequired('Address__27');
$script .= $this->getJsRequired('Create__Date__29');
// Setup Footer
$script .= '
}
';
// Add client scripts to page
$this->_smarty->assign('page_script', $script);
}
/**
* Server-side data validation method. saveData method should not commit
* changes until this method returns true to signify that all data is
* valid.
*
* @param mixed $data
* @return boolean
*/
public function validateData($data) {
$notValid = 0;
$message = '';
// Validation Functions
$this->validateRequired($data['customer.store_id'], 'Store', $notValid, $message);
$this->validateRequired($data['customer.first_name'], 'First Name', $notValid, $message);
$this->validateRequired($data['customer.last_name'], 'Last Name', $notValid, $message);
$this->validateRequired($data['customer.address_id'], 'Address', $notValid, $message);
$this->validateRequired($data['customer.create_date'], 'Create Date', $notValid, $message);
// Check validity
$validity['errors'] = $message;
$validity['isValid'] = ($notValid == 0);
return $validity;
}
/**
* Used to return the ID and Display value for the current record.
*
* It is used primarily when the user has brought up a data grid to select a
* value, and clicks the "Add" button to bring up this form. Upon saving
* the record, it needs to pass back the ID value to store in the hidden
* value field, and the display field to show to the user.
*
* These values should be returned in an array with indexes "id" and
* "display".
*
* If this form is unlikely to be used in such a scenario then simply
* return NULL.
*
* @param Data Object $dataObject
* @param string $mode
* @param array $data
* @return array
*/
public function getIdAndDisplayValues($dataObject, $mode, $data) {
if ($mode == 'insert') {
$values['id'] = $dataObject->getLastInsertId();
} else {
$values['id'] = Apeel_Framework_Controller_Libraries_Input::request('pk_' . $this->dataObjectHash);
}
$values['display'] = $data['customer.first_name'];
return $values;
}
/**
* Validate data and save changes to the database.
*
* @param array $data
* @param string $mode
* @return void
*/
public function saveData($primaryKeyValue, $mode) {
$data['customer.store_id'] = Apeel_Framework_Controller_Libraries_Input::request('Store__23', '', NULL);
$data['customer.first_name'] = Apeel_Framework_Controller_Libraries_Input::request('First__Name__24');
$data['customer.last_name'] = Apeel_Framework_Controller_Libraries_Input::request('Last__Name__25');
$data['customer.email'] = Apeel_Framework_Controller_Libraries_Input::request('Email__26');
$data['customer.address_id'] = Apeel_Framework_Controller_Libraries_Input::request('Address__27', '', NULL);
$data['customer.active'] = Apeel_Framework_Controller_Libraries_Input::request('Active__28', '', NULL);
$data['customer.create_date'] = Apeel_Framework_Controller_Libraries_Input::request('Create__Date__29', '', NULL);
// Finally, save data to database.
parent::saveData($data, $mode);
}
/**
* This method is used to save data in Data Objects other than the main
* one linked to this form.
*
* For example, a typical use is in a many-to-many relationship where
* you have a list of available values on one side and selected values
* on the other, using a "middle" table to connect the main table for
* this form with another table. In this scenario, the "middle" table
* should be written to in this method.
*
* If not applicable, simply return false.
*
* @param string | array $primaryKeyValue
* @param string $mode
* @return void | false
*/
public function saveAdditionalData($primaryKeyValue, $mode) {
return false;
}
/**
* Saves large binary objects to the database.
*
* The processUploadRequest method uses the manageBlob method which creates a seperate connection to
* the database as the main connection uses the UTF8 character set, which corrupts binary values.
*
* If not applicable, simply return false.
*
* @param string | array $primaryKeyValue
* @return void | boolean
*/
public function saveBinaryUploads($primaryKeyValue) {
}
}
?>