Skip to content
Snippets Groups Projects
class.forms.php 130 KiB
Newer Older
  • Learn to ignore specific revisions
  • 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);
        }
    
        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 required'),
                        )
                    ),
                'comments' => new TextareaField(array(
                        'id' => 2,
                        '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() {
    
            if (!parent::isValid())
                return false;
    
            // Do additional validations
            if (!($dept = $this->getDept()))
                $this->getField('dept')->addError(
                        __('Unknown department'));
    
            return !$this->errors();
        }
    
        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 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 {}