CLICK HERE TO BUY IT TODAY! OR GET IT FREE VIA TRIALPAY  

PHP Demo Application - Source Code

/Application/Model/FormHandlers/Staff.php

<?php
/**
* Script Contents: Apeel_Application_Model_FormHandlers_Staff 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_Staff 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 '5d6b9c42a710130526aa1e6c302a7e8f';
    
    
    
/**
    * The name of the base data object connected to this form.  
    * 
    * @var string
    */    
    
public $dataObjectName 'Staff';
    
    
    
/**
    * Set default data values for Insert mode.  
    * 
    * e.g. 
    * $this->_data['FORMFIELD1'] = 'VALUE1';
    * $this->_data['FORMFIELD2'] = 'VALUE2';
    * 
    * @return void
    */
    
public function setDefaults() {
        
$this->_data['staff.staff_id'] = 'TBA';
        
$this->_data['staff.first_name'] = '';
        
$this->_data['staff.last_name'] = '';
        
$this->_data['staff.address_id'] = 0;
        
$this->_data['address_id__address.desc'] = '';
        
$this->_data['staff.picture'] = '';
        
$this->_data['staff.picture_length'] = 0;
        
$this->_data['staff.picture_filename'] = '';
        
$this->_data['staff.email'] = '';
        
$this->_data['staff.store_id'] = 0;
        
$this->_data['store_id__store.desc'] = '';
        
$this->_data['staff.active'] = '1';
        
$this->_data['staff.username'] = '';
        
$this->_data['staff.password'] = '';
        
$this->_data['staff.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('staff__staff_id__15'$this->_data['staff.staff_id']);
        
$this->populateText('staff__first_name__15'$this->_data['staff.first_name']);
        
$this->populateText('staff__last_name__15'$this->_data['staff.last_name']);
        
$this->populateText('staff__address_id__15'$this->_data['staff.address_id']);
        
$this->populateText('address_id__address__desc__15'$this->_data['address_id__address.desc']);
        
$this->populateDatabaseImage('staff__picture__15'0$this->_data['staff.picture_length'], $this->_data['staff.picture_filename']);
        
$this->populateText('staff__email__15'$this->_data['staff.email']);
        
$this->populateDropdown('staff__store_id__store__value__15_options''Store''Dropdown'$this->_data['staff.store_id']);
        
$this->populateCheckbox('staff__active__15__checked'$this->_data['staff.active']);
        
$this->populateText('staff__username__15'$this->_data['staff.username']);
        
$this->populateText('staff__password__15'$this->_data['staff.password']);
        
$this->populateText('staff__last_update__15'$this->_data['staff.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_5d6b9c42a710130526aa1e6c302a7e8f").validate();
        ' 
$this->getJsGeneral($uniqueIndex$returnId$returnDisplay);
        
        
// Setup Widgets        
        
$script .= $this->getJsAutoComplete('Address__77_display''Address__77_id''60053c13e608f70173c8336aebdae97b''CONTAINS'2);
        
$script .= $this->getJsPassword('Password__83');

        
// Setup Validation
        
$script .= $this->getJsRequired('First__Name__75');
        
$script .= $this->getJsRequired('Last__Name__76');
        
$script .= $this->getJsRequired('Address__77');
        
$script .= $this->getJsRequired('Store__80');
        
$script .= $this->getJsRequired('Username__82');
        
        
// 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['staff.first_name'], 'First Name'$notValid$message);
        
$this->validateRequired($data['staff.last_name'], 'Last Name'$notValid$message);
        
$this->validateRequired($data['staff.address_id'], 'Address'$notValid$message);
        
$this->validateRequired($data['staff.store_id'], 'Store'$notValid$message);
        
$this->validateRequired($data['staff.username'], 'Username'$notValid$message);
        
$this->validatePassword(Apeel_Framework_Controller_Libraries_Input::request('Password__83'), Apeel_Framework_Controller_Libraries_Input::request('Password__83_confirm'), 'Password'$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['staff.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['staff.first_name'] = Apeel_Framework_Controller_Libraries_Input::request('First__Name__75');
        
$data['staff.last_name'] = Apeel_Framework_Controller_Libraries_Input::request('Last__Name__76');
        
$data['staff.address_id'] = Apeel_Framework_Controller_Libraries_Input::request('Address__77'''NULL);
        
$data['staff.email'] = Apeel_Framework_Controller_Libraries_Input::request('Email__79');
        
$data['staff.store_id'] = Apeel_Framework_Controller_Libraries_Input::request('Store__80'''NULL);
        
$data['staff.active'] = Apeel_Framework_Controller_Libraries_Input::request('Active__81'''NULL);
        
$data['staff.username'] = Apeel_Framework_Controller_Libraries_Input::request('Username__82');
        if (
Apeel_Framework_Controller_Libraries_Input::request('Password__83') <> Apeel_Framework_Controller_Libraries_Input::request('Password__83_original')) {
                
$data['staff.password'] = SHA1('' Apeel_Framework_Controller_Libraries_Input::request('Password__83') . '');
        } else {
                
$data['staff.password'] = Apeel_Framework_Controller_Libraries_Input::request('Password__83');
        }        
        
        
// 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) {

        if (
$_FILES['Picture__78']['name'] != '') {
            
$validExtensions '.gif,.jpg,.jpeg,.png';
            
Apeel_Framework_Model_Libraries_File::processUploadRequest(
                            
'Picture__78'
                            
$validExtensions
                            
''
                            
false
                            
'Staff_Grid'
                            
$primaryKeyValue
                            
'staff.picture',
                            
''
            
);
        }   

    }    
}

?>

PHP Demo Source Code Index