Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_Widget_Type Class Reference

Encapsulates information about widget types. More...

Inheritance diagram for P4Cms_Widget_Type:
P4Cms_Model P4Cms_ModelInterface

List of all members.

Public Member Functions

 getControllerClassName ()
 Get the widget controller class name.
 getDefaults ()
 Get the default options for this widget type.
 getIconUrl ()
 Get the URI to the widget type icon file.
 getModule ()
 Get an instance of the module that provides this widget.
 getModulePackageName ()
 Get the package name of the module that provides this widget.
 getRouteParams ()
 Get the widget module/controller/action route parameters to invoke this widget.
 hasIcon ()
 Determine if there is an icon for this widget type.
 isValid ()
 Determine if this widget type is valid (e.g.

Static Public Member Functions

static addType ($type)
 Programatically add a type to the list of types.
static clearCache ()
 Clear the types cache.
static exists ($id)
 Determine if a given widget type exists.
static fetch ($id)
 Get the requested widget type.
static fetchAll ()
 Get all of the valid widget types that are available across all modules.

Static Protected Member Functions

static _typesFromPackage ($module)
 Extract the widget types from the given module.

Static Protected Attributes

static $_types = null

Detailed Description

Encapsulates information about widget types.

Widgets are just configurable action controllers.

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

static P4Cms_Widget_Type::_typesFromPackage ( module) [static, protected]

Extract the widget types from the given module.

Parameters:
P4Cms_Module$modulethe module to get types from.
Returns:
array all of the widget types in package info.
    {
        $info = $module->getPackageInfo();

        // if no widgets, early exit.
        if (!isset($info['widgets']) || !is_array($info['widgets'])) {
            return array();
        }

        // turn widget info into type instances.
        $types = array();
        foreach ($info['widgets'] as $typeId => $typeInfo) {

            // qualify type id with module name.
            $typeId = $module->getRouteFormattedName() . "/" . $typeId;

            // add module route param to type info.
            $typeInfo['module'] = $module->getRouteFormattedName();
            
            $type = new static;
            $type->setId($typeId);
            $type->setValues($typeInfo);
            $types[$typeId] = $type;
        }

        return $types;
    }
static P4Cms_Widget_Type::addType ( type) [static]

Programatically add a type to the list of types.

Note: there is no persistent storage of these types - they survive only for the duration of the process (or until they are explicitly cleared).

Parameters:
P4Cms_Widget_Type$typethe widget type to add.
Exceptions:
InvalidArgumentExceptionif the given type is invalid.
    {
        if (!$type instanceof P4Cms_Widget_Type) {
            throw new InvalidArgumentException(
                "Cannot add widget type. Type is not a valid widget type instance."
            );
        }

        $types = static::fetchAll();
        $types[$type->getId()] = $type;

        static::$_types = $types;
    }
static P4Cms_Widget_Type::clearCache ( ) [static]

Clear the types cache.

    {
        static::$_types = null;
    }
static P4Cms_Widget_Type::exists ( id) [static]

Determine if a given widget type exists.

Parameters:
string$idthe id of the widget type to check.
Returns:
bool true if the widget type exists.
    {
        try {
            static::fetch($id);
        } catch (P4Cms_Model_NotFoundException $e) {
            return false;
        }

        return true;
    }
static P4Cms_Widget_Type::fetch ( id) [static]

Get the requested widget type.

Parameters:
string$idthe id of the widget type to get.
Returns:
P4Cms_Widget_Type the requested widget type.
    {
        $types = static::fetchAll();

        if (!isset($types[$id])) {
            // unable to find the requested type.
            throw new P4Cms_Model_NotFoundException(
                'Cannot fetch widget type. The requested type does not exist.'
            );
        }

        return $types[$id];
    }
static P4Cms_Widget_Type::fetchAll ( ) [static]

Get all of the valid widget types that are available across all modules.

Returns:
P4Cms_Model_Iterator all widget types in the system.
    {
        // only collect types once.
        if (static::$_types instanceof P4Cms_Model_Iterator) {
            return static::$_types;
        }

        $types = new P4Cms_Model_Iterator;
        foreach (P4Cms_Module::fetchAllEnabled() as $module) {
            foreach (static::_typesFromPackage($module) as $type) {
                $types[$type->getId()] = $type;
            }
        }

        // keep it!
        static::$_types = $types;
        return $types;
    }
P4Cms_Widget_Type::getControllerClassName ( )

Get the widget controller class name.

Returns:
string the full name of the widget controller class.
    {
        // convert controller from route to class name format.
        $dispatcher = new Zend_Controller_Dispatcher_Standard;
        $controller = $this->getValue('controller');
        $controller = $dispatcher->formatControllerName($controller);

        return $this->getModulePackageName() . "_" . $controller;
    }
P4Cms_Widget_Type::getDefaults ( )

Get the default options for this widget type.

Returns:
array widget defaults.
    {
        $defaults = $this->_getValue('defaults');
        return is_array($defaults) ? $defaults : array();
    }
P4Cms_Widget_Type::getIconUrl ( )

Get the URI to the widget type icon file.

Returns:
string the URI of the type icon.
Exceptions:
P4Cms_Widget_Exceptionif there is no icon.
    {
        if (!$this->hasIcon()) {
            throw new P4Cms_Package_Exception(
                "Cannot get icon URI. This widget type has no icon."
            );
        }

        $icon = $this->_getValue('icon');

        return (P4Cms_Uri::isRelativeUri($icon))
            ? $this->getModule()->getBaseUrl() . '/' . $icon
            : $icon;
    }
P4Cms_Widget_Type::getModule ( )

Get an instance of the module that provides this widget.

Returns:
P4Cms_Module the module package that provides this widget.
    {
        return P4Cms_Module::fetch($this->getValue('module'));
    }
P4Cms_Widget_Type::getModulePackageName ( )

Get the package name of the module that provides this widget.

Returns:
string the package name of the widget's module.
    {
        // convert module from route to class name format.
        $dispatcher = new Zend_Controller_Dispatcher_Standard;
        return $dispatcher->formatModuleName($this->getValue('module'));
    }
P4Cms_Widget_Type::getRouteParams ( )

Get the widget module/controller/action route parameters to invoke this widget.

Returns:
array the module, controller and action route parameters.
    {
        return array(
            'module'        => $this->_getValue('module'),
            'controller'    => $this->_getValue('controller'),
            'action'        => $this->_getValue('action')
        );
    }
P4Cms_Widget_Type::hasIcon ( )

Determine if there is an icon for this widget type.

Returns:
bool true if this type has an icon.
    {
        $icon = $this->_getValue('icon');
        return isset($icon) && is_string($icon);
    }
P4Cms_Widget_Type::isValid ( )

Determine if this widget type is valid (e.g.

does the controller class exist?).

Returns:
bool true if this type is valid.
    {
        // ensure type has an id.
        if (!strlen($this->getId())) {
            return false;
        }

        // ensure controller class is valid.
        $controller = $this->getControllerClassName();
        if (!class_exists($controller)
            || !is_subclass_of($controller, 'P4Cms_Widget_ControllerAbstract')
        ) {
            return false;
        }

        return true;
    }

Member Data Documentation

P4Cms_Widget_Type::$_types = null [static, protected]

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