Perforce Chronicle 2012.2/486814
API Documentation

Workflow_Form_EditContent Class Reference

This is the workflow form to display while editing content. More...

Inheritance diagram for Workflow_Form_EditContent:
P4Cms_Form_SubForm Workflow_Form_ManageContent

List of all members.

Public Member Functions

 getCurrentState ()
 Return current state of the form entry.
 getEntry ()
 Get the content entry this form is for.
 getStateOptions ($data=null)
 Get the valid state options for the associated entry.
 getWorkflow ()
 Get the workflow model this form is for.
 init ()
 Defines the elements that make up the workflow form.
 isValid ($data)
 Extend parent method to provide additional checks for date and time elements as they are required only if transition is scheduled.
 render (Zend_View_Interface $view=null)
 Extend parent to set event attributes on some elements.
 setDefaults (array $defaults)
 Extend parent to hide schedule display group if current state is selected initially.
 setOptions (array $options)
 Set content entry and workflow options.

Public Attributes

const E_INVALID_DATE = "Date is invalid or unset."
const E_INVALID_TIME = "Time is invalid or unset."
const E_INVALID_TIMEDATE = "Scheduled status changes must be in the future."

Protected Attributes

 $_entry
 $_workflow

Detailed Description

This is the workflow form to display while editing content.

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

Member Function Documentation

Workflow_Form_EditContent::getCurrentState ( )

Return current state of the form entry.

Returns:
string id of the form's current state

Reimplemented in Workflow_Form_ManageContent.

    {
        return $this->getWorkflow()->getStateOf($this->getEntry())->getId();
    }
Workflow_Form_EditContent::getEntry ( )

Get the content entry this form is for.

Returns:
P4Cms_Content the content entry associated with this workflow.
Exceptions:
Workflow_Exceptionif no content entry has been set
    {
        if (!$this->_entry instanceof P4Cms_Content) {
            throw new Workflow_Exception(
                "Cannot get content entry for workflow form. No content entry has been set."
            );
        }

        return $this->_entry;
    }
Workflow_Form_EditContent::getStateOptions ( data = null)

Get the valid state options for the associated entry.

If data is given, pass it along so that workflow conditions can evaluate against the latest (pending) values.

Parameters:
array | null$dataoptional - pending values to pass to condition
Returns:
array the valid state options for the current entry/data
    {
        // get the entry and workflow that this form is for.
        $entry    = $this->getEntry();
        $workflow = $this->getWorkflow();

        // get the current state of the content entry.
        $state = $workflow->getStateOf($entry);

        // always include the current state.
        $options = array($state->getId() => $state->getLabel() . " (Current)");

        // get options for status radio element, passing data
        // so conditions can evaluate against pending values.
        $transitions = $state->getValidTransitionsFor($entry, $data);
        if ($transitions->count()) {
            $options += array_combine(
                $transitions->invoke('getId'),
                $transitions->invoke('getLabel')
            );
        }

        // mark scheduled transition if there is one
        $scheduledState = $workflow->getScheduledStateOf($entry);
        if ($scheduledState && array_key_exists($scheduledState->getId(), $options)) {
            $options[$scheduledState->getId()] .= " (Scheduled)";
        }

        // put state options in the same order they are defined in the
        // workflow - if the workflow is roughly linear (e.g. draft, review,
        // published) and defined in that order, this will likely be a more
        // intuitive order than if we used the order transitions are defined.
        $stateOrder = array_flip(array_keys($workflow->getStates()));
        uksort(
            $options,
            function($a, $b) use ($stateOrder)
            {
                return $stateOrder[$a] - $stateOrder[$b];
            }
        );

        return $options;
    }
Workflow_Form_EditContent::getWorkflow ( )

Get the workflow model this form is for.

Returns:
Workflow_Model_Workflow the workflow model instance.
Exceptions:
Workflow_Exceptionif no workflow has been set
    {
        if (!$this->_workflow instanceof Workflow_Model_Workflow) {
            throw new Workflow_Exception(
                "Cannot get workflow for workflow form. No workflow has been set."
            );
        }

        return $this->_workflow;
    }
Workflow_Form_EditContent::init ( )

Defines the elements that make up the workflow form.

Called automatically when the form object is created.

Reimplemented in Workflow_Form_ManageContent.

    {
        // set the title of this form.
        $this->setLegend('Workflow');

        $this->addElement(
            'hidden',
            'currentState',
            array(
                'value'  => $this->getCurrentState(),
                'ignore' => true
            )
        );

        $this->addElement(
            'radio',
            'state',
            array(
                'label'         => 'Status',
                'multiOptions'  => $this->getStateOptions()
            )
        );

        $this->addElement(
            'radio',
            'scheduled',
            array(
                'label'         => 'Schedule Status Change',
                'multiOptions'  => array(
                    'false'     => 'Now',
                    'true'      => 'Specify Server Date and Time'
                )
            )
        );

        $this->addElement(
            'text',
            'currentTime',
            array(
                'helper'    => 'FormNote'
            )
        );
        $this->getElement('currentTime')
             ->getDecorator('htmlTag')
             ->setOption('class', 'current-time');

        $sheduledElementName = $this->getElement('scheduled')->getFullyQualifiedName();
        $this->addElement(
            'dateTextBox',
            'scheduledDate',
            array(
                'datePattern'   => 'MMM d, yyyy',
                'constraints'   => array(
                    'min'       => date('Y-m-d')
                )
            )
        );

        // prepare time options
        $options = array();
        for ($h = 0; $h < 24; $h++) {
            $amPm          = $h < 12 ? ' AM' : ' PM';
            $key           = sprintf('%02d:00', $h);
            $options[$key] = ($h % 12 ?: 12) . $amPm;
        }

        $this->addElement(
            'select',
            'scheduledTime',
            array(
                'multiOptions'  => $options
            )
        );

        // put time/date elements together
        $this->addDisplayGroup(
            array('scheduled', 'scheduledDate', 'scheduledTime', 'currentTime'),
            'scheduleGroup',
            array(
                'class' => 'scheduleGroup'
            )
        );
    }
Workflow_Form_EditContent::isValid ( data)

Extend parent method to provide additional checks for date and time elements as they are required only if transition is scheduled.

Also ensures that date and time values (if required) refer to the time in the future.

Parameters:
array$datavalues to check against
Returns:
boolean true if form is valid, false otherwise
    {
        $valid = parent::isValid($data);

        // extract workflow field values from data
        // in some contexts, fields are under a 'workflow' array
        // in other contexts they are at the top level.
        $belongTo = $this->getElementsBelongTo();
        $workflow = $belongTo
            ? isset($data[$belongTo]) ? $data[$belongTo] : array()
            : $data;

        // ensure date and time fields are filled if transition is scheduled
        if (isset($workflow['scheduled']) && $workflow['scheduled'] === 'true') {
            $date = $workflow['scheduledDate'];
            $time = $workflow['scheduledTime'];

            // ensure time and date fields are not blank
            if (!$date) {
                $this->getElement('currentTime')->addError(self::E_INVALID_DATE);
                $valid = false;
            }
            if (!$time) {
                $this->getElement('currentTime')->addError(self::E_INVALID_TIME);
                $valid = false;
            }

            // ensure that timedate refers to the future
            if ($valid) {
                $timestamp = strtotime($date . ' ' . $time);
                if ($timestamp < time()) {
                    $this->getElement('currentTime')->addError(self::E_INVALID_TIMEDATE);
                    $valid = false;
                }
            }
        }

        return $valid;
    }
Workflow_Form_EditContent::render ( Zend_View_Interface $  view = null)

Extend parent to set event attributes on some elements.

This cannot be done during the init as elements' fully qualified names may not be known at that time. In some cases (e.g. when this is a sub-form of a non-array sub-form), getFullyQualifiedName() method of Zend_Form_Element doesn't work.

Parameters:
Zend_View_Interface$viewZend provides no documentation for this
Returns:
string rendered form markup

Reimplemented from P4Cms_Form_SubForm.

    {
        $currentState = $this->getElement('currentState')->getName();
        $scheduled    = $this->getElement('scheduled')->getName();
        $belongsTo    = $this->getElementsBelongTo();
        if ($belongsTo) {
            $currentState = $belongsTo . '[' . $currentState . ']';
            $scheduled    = $belongsTo . '[' . $scheduled    . ']';
        }

        $this->getElement('state')->setAttrib(
            "onClick",
            "
                var currentState = this.form.elements['$currentState'];
                if (currentState && this.value === currentState.value) {
                    p4cms.ui.hide('fieldset-scheduleGroup');
                    dojo.forEach(this.form.elements['$scheduled'], function(node){
                        if (node.value === 'false') {
                            node.checked = true;
                        }
                    });
                } else {
                    p4cms.ui.show('fieldset-scheduleGroup');
                }
            "
        );

        $this->getElement('scheduledDate')->setAttrib(
            "onClick",
            "
                var inputNode = this.valueNode || this.focusNode;
                dojo.forEach(inputNode.form.elements['$scheduled'], function(node){
                    if (node.value === 'true') {
                        node.checked = true;
                    }
                });
            "
        );

        $this->getElement('scheduledTime')->setAttrib(
            "onClick",
            "
                dojo.forEach(this.form.elements['$scheduled'], function(node){
                    if (node.value === 'true') {
                        node.checked = true;
                    }
                });
            "
        );

        return parent::render($view);
    }
Workflow_Form_EditContent::setDefaults ( array $  defaults)

Extend parent to hide schedule display group if current state is selected initially.

Parameters:
array$defaultsZend provides no documentation for this.
Returns:
Zend_Form provides fluent interface.
    {
        // hide schedule group if state is current
        $currentState = $this->getCurrentState();
        if (array_key_exists('state', $defaults) && $defaults['state'] === $currentState) {
            $this->getDisplayGroup('scheduleGroup')->setAttrib('class', 'hidden');
        }

        return parent::setDefaults($defaults);
    }
Workflow_Form_EditContent::setOptions ( array $  options)

Set content entry and workflow options.

Parameters:
array$optionsZend provides no documentation for this.
Returns:
Workflow_Form_EditContent provides fluent interface.

Reimplemented in Workflow_Form_ManageContent.

    {
        if (isset($options['workflow'])) {
            $this->_workflow = $options['workflow'];
            unset($options['workflow']);
        }

        if (isset($options['entry'])) {
            $this->_entry = $options['entry'];
            unset($options['entry']);
        }

        return parent::setOptions($options);
    }

Member Data Documentation

Workflow_Form_EditContent::$_entry [protected]
Workflow_Form_EditContent::$_workflow [protected]
const Workflow_Form_EditContent::E_INVALID_DATE = "Date is invalid or unset."
const Workflow_Form_EditContent::E_INVALID_TIME = "Time is invalid or unset."
const Workflow_Form_EditContent::E_INVALID_TIMEDATE = "Scheduled status changes must be in the future."

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