Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_Controller_Action_Helper_WidgetContext Class Reference

Provides a context facility to inform widgets (which can be asynchronously loaded/operated) of arbitrary data. More...

List of all members.

Public Member Functions

 clearContext ()
 Clear the context.
 getEncodedValues ()
 Provide the context in encoded form, suitable for placing in HTML.
 getValues ($key=null)
 Provide the values from the context, or if specified, a named value within.
 init ()
 Initialize by making ourself available to the controller and cleaning out any cached initial dispatch values.
 setEncodedValues ($encoded)
 Set the context from its encoded form.
 setValue ($key, $value=null)
 Set a value in the context.
 setValues ($values)
 Set a number of values in the context at once.

Static Protected Attributes

static $_context = array()
static $_initialDispatch = true
 Flag indicating whether the current invocation is the initial invocation (as opposed to a forward, redirect, or other action manipulation).

Detailed Description

Provides a context facility to inform widgets (which can be asynchronously loaded/operated) of arbitrary data.

A 'data' controller can use this facility to convey information to widgets like so: $this->widgetContext->setValue('foo', 'bar');

A 'widget' controller can use this facility to use information provided like so: $context = $this->widgetContext->getValues(); if (array_key_exists('foo', $context) { ... } or if ($this->widgetContext->getValues('foo')) { ... }

The widget views and supporting javascript maintain the context across requests to support async creation/configuration/reload.

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

P4Cms_Controller_Action_Helper_WidgetContext::clearContext ( )

Clear the context.

Returns:
P4Cms_Controller_Action_Helper_WidgetContext Provide a fluent interface.
    {
        static::$_context = array();
        return $this;
    }
P4Cms_Controller_Action_Helper_WidgetContext::getEncodedValues ( )

Provide the context in encoded form, suitable for placing in HTML.

Returns:
string Encoded form of the context.
    {
        $encoded = '';
        $values = $this->getValues();
        if (count($values)) {
            $encoded = Zend_Json::encode($values);
        }
        return $encoded;
    }
P4Cms_Controller_Action_Helper_WidgetContext::getValues ( key = null)

Provide the values from the context, or if specified, a named value within.

Parameters:
string$keyOptional key to retrieve from context.
Returns:
mixed The context, or value within the context.
    {
        if (isset($key)) {
            return array_key_exists($key, static::$_context)
                ? static::$_context[$key]
                : null;
        }
        return static::$_context;
    }
P4Cms_Controller_Action_Helper_WidgetContext::init ( )

Initialize by making ourself available to the controller and cleaning out any cached initial dispatch values.

    {
        // make this action helper available to the controller
        $this->getActionController()->widgetContext = $this;

        // clear context for initial dispatch
        if (static::$_initialDispatch) {
            static::$_initialDispatch = false;
            $this->clearContext();
        }
    }
P4Cms_Controller_Action_Helper_WidgetContext::setEncodedValues ( encoded)

Set the context from its encoded form.

Parameters:
string$encodedA JSON-encoded context.
Returns:
P4Cms_Controller_Action_Helper_WidgetContext Provide a fluent interface.
    {
        if (isset($encoded) and strlen($encoded)) {
            $values = Zend_Json::decode($encoded);
            if ($values) {
                $this->setValues($values);
            }
        }
        return $this;
    }
P4Cms_Controller_Action_Helper_WidgetContext::setValue ( key,
value = null 
)

Set a value in the context.

Parameters:
string$keyThe key to set in the context.
mixed$valueThe (optional) value to set. If null, the value will be removed from the context.
Returns:
P4Cms_Controller_Action_Helper_WidgetContext Provide a fluent interface.
    {
        if (!isset($value)) {
            unset(static::$_context[$key]);
            return $this;
        }

        static::$_context[$key] = $value;
        return $this;
    }
P4Cms_Controller_Action_Helper_WidgetContext::setValues ( values)

Set a number of values in the context at once.

Parameters:
array$valuesThe key/value pairs to set in the context.
Returns:
P4Cms_Controller_Action_Helper_WidgetContext Provide a fluent interface.
    {
        foreach ($values as $key => $value) {
            $this->setValue($key, $value);
        }

        return $this;
    }

Member Data Documentation

P4Cms_Controller_Action_Helper_WidgetContext::$_context = array() [static, protected]
P4Cms_Controller_Action_Helper_WidgetContext::$_initialDispatch = true [static, protected]

Flag indicating whether the current invocation is the initial invocation (as opposed to a forward, redirect, or other action manipulation).

This is useful to ensure a fresh context.


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