PHP Demo Application - Source Code
/Application/Model/FormHandlers/Film.php
<?php
/**
* Script Contents: Apeel_Application_Model_FormHandlers_Film 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_Film 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 = '6cb555d4c4d37148b2590ef443605d95';
/**
* The name of the base data object connected to this form.
*
* @var string
*/
public $dataObjectName = 'Film';
/**
* Set default data values for Insert mode.
*
* e.g.
* $this->_data['FORMFIELD1'] = 'VALUE1';
* $this->_data['FORMFIELD2'] = 'VALUE2';
*
* @return void
*/
public function setDefaults() {
$this->_data['film.film_id'] = 'TBA';
$this->_data['film.title'] = '';
$this->_data['film.description'] = '';
$this->_data['film.release_year'] = '';
$this->_data['film.language_id'] = 0;
$this->_data['language_id__language.desc'] = '';
$this->_data['film.original_language_id'] = 0;
$this->_data['original_language_id__language.desc'] = '';
$this->_data['film.rental_duration'] = '3';
$this->_data['film.rental_rate'] = '4.99';
$this->_data['film.length'] = '';
$this->_data['film.replacement_cost'] = '19.99';
$this->_data['film.rating'] = 'G';
$this->_data['film.special_features'] = '';
$this->_data['film.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('film__film_id__7', $this->_data['film.film_id']);
$this->populateText('film__title__7', $this->_data['film.title']);
$this->populateRichTextEditor('film__description__7', 'Description__33', $this->_data['film.description']);
$this->populateText('film__release_year__7', $this->_data['film.release_year']);
$this->populateDropdown('film__language_id__language__value__7_options', 'Language', 'Dropdown', $this->_data['film.language_id']);
$this->populateDropdown('film__original_language_id__language__value__7_options', 'Language', 'Dropdown', $this->_data['film.original_language_id']);
$this->populateText('film__rental_duration__7', $this->_data['film.rental_duration']);
$this->populateDecimal('film__rental_rate__7', $this->_data['film.rental_rate'], 2);
$this->populateText('film__length__7', $this->_data['film.length']);
$this->populateDecimal('film__replacement_cost__7', $this->_data['film.replacement_cost'], 2);
$this->populateTextRadioButtons('film__rating__7', $this->_data['film.rating'], array(1 => 'G', 2 => 'PG', 3 => 'PG-13', 4 => 'R', 5 => 'NC-17'));
$this->populateSet('film__special_features__7', $this->_data['film.special_features'], array(1 => 'Trailers', 2 => 'Commentaries', 3 => 'Deleted Scenes', 4 => 'Behind the Scenes'));
$this->populateText('film__last_update__7', $this->_data['film.last_update'], true);
$filters__film__DualList_Actor__7 = new Apeel_Framework_Model_DataObjects_Parameters_Collections_Filters('film_actor.film_id', '=', $this->_data['film.film_id']);
$this->populateDuallistAutoComplete('film__DualList_Actor__7', 'film__DualList_Actor__7_current', 'film__DualList_Actor__7_existing', 'Film_actor__Actor', 'Duallist', '', $filters__film__DualList_Actor__7);
$filters__film__DualList_Category__7 = new Apeel_Framework_Model_DataObjects_Parameters_Collections_Filters('film_category.film_id', '=', $this->_data['film.film_id']);
$this->populateDuallistListAll('film__DualList_Category__7', 'film__DualList_Category__7_current', 'film__DualList_Category__7_existing', 'Film_category__Category', 'Duallist', '', $filters__film__DualList_Category__7,'Category','category.category_id');
}
/**
* 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_6cb555d4c4d37148b2590ef443605d95").validate();
' . $this->getJsGeneral($uniqueIndex, $returnId, $returnDisplay);
// Setup Widgets
$script .= $this->getJsNumeric('Release__Year__34', 0);
$script .= $this->getJsNumeric('Rental__Duration__37', 0);
$script .= $this->getJsNumeric('Rental__Rate__38', 2);
$script .= $this->getJsNumeric('Length__39', 0);
$script .= $this->getJsNumeric('Replacement__Cost__40', 2);
$script .= $this->getJsDuallistAuto('Actor__90', 'Actor__90_available', 'Actor__90_selected', 'Actor__90_selected_current', 'btnActor__90Add', 'btnActor__90Remove', 'Actor__90_auto', 'b13445e3bf2913f2c4947d89648a9b50', 'CONTAINS', true);
$script .= $this->getJsDuallistAuto('Category__91', 'Category__91_available', 'Category__91_selected', 'Category__91_selected_current', 'btnCategory__91Add', 'btnCategory__91Remove', 'Category__91_auto', '88893bb40539ed4aed6955b236ad0502', 'CONTAINS', false);
// Setup Validation
$script .= $this->getJsRequired('Title__32');
$script .= $this->getJsRequired('Language__35');
$script .= $this->getJsRequired('Rental__Duration__37');
$script .= $this->getJsRequired('Rental__Rate__38');
$script .= $this->getJsRequired('Replacement__Cost__40');
// 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['film.title'], 'Title', $notValid, $message);
$this->validateRequired($data['film.language_id'], 'Language', $notValid, $message);
$this->validateRequired($data['film.rental_duration'], 'Rental Duration', $notValid, $message);
$this->validateRequired($data['film.rental_rate'], 'Rental Rate', $notValid, $message);
$this->validateRequired($data['film.replacement_cost'], 'Replacement Cost', $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['film.title'];
return $values;
}
/**
* Validate data and save changes to the database.
*
* @param array $data
* @param string $mode
* @return void
*/
public function saveData($primaryKeyValue, $mode) {
$data['film.title'] = Apeel_Framework_Controller_Libraries_Input::request('Title__32');
$data['film.description'] = Apeel_Framework_Controller_Libraries_Input::request('Description__33', '', NULL);
$data['film.release_year'] = Apeel_Framework_Controller_Libraries_Input::request('Release__Year__34', '', NULL);
$data['film.language_id'] = Apeel_Framework_Controller_Libraries_Input::request('Language__35', '', NULL);
$data['film.original_language_id'] = Apeel_Framework_Controller_Libraries_Input::request('Original__Language__36', '', NULL);
$data['film.rental_duration'] = Apeel_Framework_Controller_Libraries_Input::request('Rental__Duration__37', '', NULL);
$data['film.rental_rate'] = Apeel_Framework_Controller_Libraries_Input::request('Rental__Rate__38', '', NULL);
$data['film.length'] = Apeel_Framework_Controller_Libraries_Input::request('Length__39', '', NULL);
$data['film.replacement_cost'] = Apeel_Framework_Controller_Libraries_Input::request('Replacement__Cost__40', '', NULL);
$data['film.rating'] = Apeel_Framework_Controller_Libraries_Input::request('Rating__41', '', NULL);
$data['film.special_features'] = Apeel_Framework_Controller_Libraries_Input::request('Special__Features__42_set', '', 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) {
$existing = Apeel_Framework_Controller_Libraries_Input::request('Actor__90_selected_existing');
$current = Apeel_Framework_Controller_Libraries_Input::request('Actor__90_selected_current');
$this->saveRemoteData(
'film_actor',
$existing,
$current,
$mode,
'film_actor.actor_id',
'film_actor.film_id',
$primaryKeyValue
);
$existing = Apeel_Framework_Controller_Libraries_Input::request('Category__91_selected_existing');
$current = Apeel_Framework_Controller_Libraries_Input::request('Category__91_selected_current');
$this->saveRemoteData(
'film_category',
$existing,
$current,
$mode,
'film_category.category_id',
'film_category.film_id',
$primaryKeyValue
);
}
/**
* 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) {
}
}
?>