Perforce Chronicle 2012.2/486814
API Documentation

User_Form_Add Class Reference

This is the add user form. More...

Inheritance diagram for User_Form_Add:
P4Cms_Form User_Form_Edit

List of all members.

Public Member Functions

 __construct ($options=null)
 Overwrite construct to set form options.
 init ()
 Defines the elements that make up the edit form.
 isValid ($data)
 Override isValid to validate password confirmation and to ensure given username does not already exist.

Public Attributes

const E_PASSWORDS_MISMATCH = "The two passwords do not match"
const E_ROLE_REQUIRED = "'%s' role is required."
const E_USER_EXISTS = "User '%s' already exists."

Protected Attributes

 $_requireAdministrator = false
 $_uniqueIdRequired = true

Detailed Description

This is the add user form.

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Constructor & Destructor Documentation

User_Form_Add::__construct ( options = null)

Overwrite construct to set form options.

Parameters:
array | Zend_Config | null$optionsZend provides no documentation for this param.

Reimplemented from P4Cms_Form.

Reimplemented in User_Form_Edit.

    {
        if (isset($options['requireAdministrator'])) {
            $this->_requireAdministrator = (bool) $options['requireAdministrator'];
            unset($options['requireAdministrator']);
        }

        parent::__construct($options);
    }

Member Function Documentation

User_Form_Add::init ( )

Defines the elements that make up the edit form.

Called automatically when the form object is created.

Reimplemented in User_Form_Edit.

    {
        // form should use p4cms-ui styles.
        $this->setAttrib('class', 'p4cms-ui user-form user-add-form');

        // set the method for the form to POST
        $this->setMethod('post');

        // add a field to collect the user name.
        $this->addElement(
            'text',
            'id',
            array(
                'label'         => 'Username',
                'required'      => true,
                'filters'       => array('StringTrim'),
                'validators'    => array('UserName'),
                'size'          => 30,
                'order'         => 10
            )
        );

        // add a field to collect the user's email
        $this->addElement(
            'text',
            'email',
            array(
                'label'         => 'Email Address',
                'required'      => true,
                'filters'       => array('StringTrim'),
                'validators'    => array('EmailAddress'),
                'size'          => 30,
                'order'         => 20
            )
        );

        // add a field to collect the user's name
        $this->addElement(
            'text',
            'fullName',
            array(
                'label'         => 'Full Name',
                'required'      => true,
                'filters'       => array('StringTrim'),
                'size'          => 30,
                'order'         => 30
            )
        );

        // add a field to collect the user's password
        $this->addElement(
            'password',
            'password',
            array(
                'label'         => 'Password',
                'size'          => 30,
                'order'         => 40
            )
        );

        $this->addElement(
            'password',
            'passwordConfirm',
            array(
                'label'         => 'Confirm Password',
                'size'          => 30,
                'order'         => 45,
                'ignore'        => true
            )
        );

        // if user is allowed to manage roles, add a field to collect roles
        // (don't show virtual roles)
        if (P4Cms_User::hasActive() 
            && P4Cms_User::fetchActive()->isAllowed('users', 'manage-roles')
        ) {
            $roles = P4Cms_Acl_Role::fetchAll(
                array(P4Cms_Acl_Role::FETCH_HIDE_VIRTUAL => true),
                $this->getStorageAdapter()
            )->invoke('getId');

            $options = count($roles) ? array_combine($roles, $roles) : array();
            $this->addElement(
                'MultiCheckbox',
                'roles',
                array(
                    'multiOptions'  => $options,
                    'label'         => 'Roles',
                    'order'         => 50,
                    'ignore'        => true
                )
            );
        }

        // if security level > 0, strong passwords are required.
        $connection = $this->getStorageAdapter()->getConnection();
        if ($connection->getSecurityLevel() > 0) {
            $this->getElement('password')
                 ->addValidator('StrongPassword')
                 ->setRequired(true);
            $this->getElement('passwordConfirm')
                 ->setRequired(true);
        }

        // add the submit button
        $this->addElement(
            'SubmitButton',
            'save',
            array(
                'label'     => 'Save',
                'class'     => 'preferred',
                'required'  => false,
                'ignore'    => true
            )
        );
        
        // put the button in a fieldset.
        $this->addDisplayGroup(
            array('save'),
            'buttons',
            array(
                'class' => 'buttons',
                'order' => 100
            )
        );

    }
User_Form_Add::isValid ( data)

Override isValid to validate password confirmation and to ensure given username does not already exist.

Parameters:
array$datathe field values to validate.
Returns:
boolean true if the form values are valid.

Reimplemented from P4Cms_Form.

Reimplemented in User_Form_Edit.

    {
        $valid = parent::isValid($data);

        $password = isset($data['password']) ? $data['password'] : null;
        $confirm  = isset($data['passwordConfirm']) ? $data['passwordConfirm'] : null;
        if ($password != $confirm) {
            $this->getElement('passwordConfirm')->addError(
                self::E_PASSWORDS_MISMATCH
            );
            $valid = false;
        }

        if ($this->_uniqueIdRequired && isset($data['id'])) {
            if (P4_User::exists(($data['id']))) {
                $this->getElement('id')->addError(
                    sprintf(self::E_USER_EXISTS, $data['id'])
                );
                $valid = false;
            }
        }

        // if administrator role is required ensure that role is selected
        if ($this->_requireAdministrator
            && (!isset($data['roles'])
            || !in_array(P4Cms_Acl_Role::ROLE_ADMINISTRATOR, $data['roles']))
        ) {
            $this->getElement('roles')->addError(
                sprintf(self::E_ROLE_REQUIRED, P4Cms_Acl_Role::ROLE_ADMINISTRATOR)
            );
            $valid = false;
        }

        return $valid;
    }

Member Data Documentation

User_Form_Add::$_requireAdministrator = false [protected]
User_Form_Add::$_uniqueIdRequired = true [protected]

Reimplemented in User_Form_Edit.

const User_Form_Add::E_PASSWORDS_MISMATCH = "The two passwords do not match"
const User_Form_Add::E_ROLE_REQUIRED = "'%s' role is required."
const User_Form_Add::E_USER_EXISTS = "User '%s' already exists."

The documentation for this class was generated from the following file: