Skip to content
Snippets Groups Projects
class.forms.php 162 KiB
Newer Older
  • Learn to ignore specific revisions
  • Peter Rotich's avatar
    Peter Rotich committed
            include $inc;
        }
    
    
        function setChoices($field, $choices, $prompt='') {
    
            if (!($f= $this->getField($field)))
               return;
    
            $f->set('choices', $choices);
    
            return $f;
    
        function getReferee() {
    
    Peter Rotich's avatar
    Peter Rotich committed
    
    
            $target = $this->getField('target')->getClean();
            if (!$target || !($f=$this->getField($target)))
                return null;
    
    Peter Rotich's avatar
    Peter Rotich committed
    
    
            $id = $f->getClean();
            switch($target) {
            case 'agent':
                return Staff::lookup($id);
            case 'team':
                return Team::lookup($id);
            case 'dept':
                return Dept::lookup($id);
            }
    
    Peter Rotich's avatar
    Peter Rotich committed
        }
    
        function getComments() {
            return $this->getField('comments')->getClean();
        }
    }
    
    
    
    Peter Rotich's avatar
    Peter Rotich committed
    class TransferForm extends Form {
    
        static $id = 'transfer';
        var $_dept = null;
    
        function __construct($source=null, $options=array()) {
            parent::__construct($source, $options);
    
    Peter Rotich's avatar
    Peter Rotich committed
        function getFields() {
    
            if ($this->fields)
                return $this->fields;
    
            $fields = array(
                'dept' => new DepartmentField(array(
                        'id'=>1,
                        'label' => __('Department'),
                        'flags' => hexdec(0X450F3),
                        'required' => true,
    
                        'validator-error' => __('Department selection is required'),
    
                'refer' => new BooleanField(array(
                    'id'=>2, 'label'=>'', 'required'=>false, 'default'=>false,
                    'configuration'=>array(
                        'desc' => 'Maintain referral access to current department')
                )),
    
    Peter Rotich's avatar
    Peter Rotich committed
                'comments' => new TextareaField(array(
    
    Peter Rotich's avatar
    Peter Rotich committed
                        'label'=> '',
                        'required'=>false,
                        'default'=>'',
                        'configuration' => array(
                            'html' => true,
    
    Peter Rotich's avatar
    Peter Rotich committed
                            'size' => 'small',
    
    Peter Rotich's avatar
    Peter Rotich committed
                            'placeholder' => __('Optional reason for the transfer'),
                            ),
                        )
                    ),
                );
    
            $this->setFields($fields);
    
            return $this->fields;
    
        function isValid($include=false) {
    
            if (!parent::isValid($include))
    
    Peter Rotich's avatar
    Peter Rotich committed
                return false;
    
            // Do additional validations
            if (!($dept = $this->getDept()))
                $this->getField('dept')->addError(
                        __('Unknown department'));
    
            return !$this->errors();
    
    Peter Rotich's avatar
    Peter Rotich committed
        function render($options) {
    
            switch(strtolower($options['template'])) {
            case 'simple':
                $inc = STAFFINC_DIR . 'templates/dynamic-form-simple.tmpl.php';
                break;
            default:
                throw new Exception(sprintf(__('%s: Unknown template style %s'),
                            get_class(), $options['template']));
            }
    
            $form = $this;
            include $inc;
    
    
        function refer() {
            return $this->getField('refer')->getClean();
        }
    
    
    Peter Rotich's avatar
    Peter Rotich committed
        function getDept() {
    
            if (!isset($this->_dept)) {
                if (($id = $this->getField('dept')->getClean()))
                    $this->_dept = Dept::lookup($id);
            }
    
            return $this->_dept;
    
    /**
     * FieldUnchanged
     *
     * Thrown in the to_database() method to indicate the value should not be
     * saved in the database (it wasn't changed in the request)
     */
    class FieldUnchanged extends Exception {}