Perforce Chronicle 2012.2/486814
API Documentation

Menu_Form_MenuItemMvc Class Reference

This form is specialized for MVC menu items and provides a drop-down menu to select the module/controller/action. More...

Inheritance diagram for Menu_Form_MenuItemMvc:
Menu_Form_MenuItem P4Cms_Form

List of all members.

Public Member Functions

 getValues ($suppressArrayNotation=false)
 Extend parent to split action into discrete module/controller/action.
 init ()
 Defines the elements that make up the menu MVC item form.
 isValid ($data)
 Extends parent to combine combine route params (module/controller/action) into a single action field, and change the params array, if it exists, into an ini formatted string.
 setDefaults ($defaults)
 Extends parent to combine route params (module/controller/action) into a single action field.

Public Attributes

const ACTION_INTERFACE = 'Zend_Controller_Action_Interface'

Protected Member Functions

 _combineParamsArray ($values)
 If parameters are set, present them in INI format.
 _combineRouteParams ($values)
 Combine module/controller/action parameters to a single action value in given values array.
 _formatLabel ($input)
 Convert a camelCase formatted action or controller name into a human-friendly (space-separated) label.
 _formatRoute ($input)
 Convert a camelCase formatted action or controller name into a dash-separated version suitable for routing.
 _getActionOptions ()
 Get a multi-options formatted list of all actions across all modules.
 _splitParamsIniString ($values)
 Convert params INI string to an array using Zend_Config_Ini write to a temp file to facilitate Zend_Config_Ini parsing.
 _splitRouteParams ($values)
 Split action parameter into discrete module/controller/action values.

Detailed Description

This form is specialized for MVC menu items and provides a drop-down menu to select the module/controller/action.

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

Menu_Form_MenuItemMvc::_combineParamsArray ( values) [protected]

If parameters are set, present them in INI format.

Parameters:
array$valuesvalues that contain the array to convert
Returns:
array values with params converted to an INI formatted strings
    {
        if (isset($values['params']) && is_array($values['params'])) {
            $config = new Zend_Config($values['params']);
            $writer = new Zend_Config_Writer_Ini();
            $values['params'] = $writer->setConfig($config)->render();
        }
        
        return $values;
    }
Menu_Form_MenuItemMvc::_combineRouteParams ( values) [protected]

Combine module/controller/action parameters to a single action value in given values array.

If any parameters are missing, default values are used.

Parameters:
array$valuesvalues to combine route parameters in.
Returns:
array values with module/controller/action combined to action.
    {
        if (isset($values['module']) || isset($values['controller'])) {
            $front      = Zend_Controller_Front::getInstance();
            $module     = isset($values['module'])
                ? $values['module']
                : $front->getDefaultModule();
            $controller = isset($values['controller'])
                ? $values['controller']
                : $front->getDefaultControllerName();
            $action     = isset($values['action'])
                ? $values['action']
                : $front->getDefaultAction();
            
            $values['action'] = implode('/', array($module, $controller, $action));
            unset($values['module'], $values['controller']);
        }
        
        return $values;
    }
Menu_Form_MenuItemMvc::_formatLabel ( input) [protected]

Convert a camelCase formatted action or controller name into a human-friendly (space-separated) label.

Parameters:
string$inputthe camelCase formatted identifier.
Returns:
string the human-friendly version.
    {
        return trim(ucwords(preg_replace('/([A-Z])/', ' ${1}', $input)));
    }
Menu_Form_MenuItemMvc::_formatRoute ( input) [protected]

Convert a camelCase formatted action or controller name into a dash-separated version suitable for routing.

Parameters:
string$inputthe camelCase formatted identifier.
Returns:
string the route formatted (dash-separated) version.
Menu_Form_MenuItemMvc::_getActionOptions ( ) [protected]

Get a multi-options formatted list of all actions across all modules.

Returns:
array a list of all controller actions grouped by module.
    {
        // front controller gives us access to route defaults.
        $front = Zend_Controller_Front::getInstance();
        
        // fetch all actions from all controllers in all modules.
        $options = array();
        foreach (P4Cms_Module::fetchAllEnabled() as $module) {
            
            // get the module's particulars
            $path        = $module->getPath() . "/controllers";
            $moduleLabel = $module->getName();
            $moduleId    = $this->_formatRoute($module->getId());
            
            // skip if there is no controllers directory
            if (!is_dir($path)) {
                continue;
            }
            
            // add an opt-group for this module.
            $options[$moduleLabel] = array();
            
            // examine each file under the module's controllers directory
            // if it is a controller, fetch all of its action methods
            // always look for the default controller first so it appears 
            // first - all other controllers should list alphabetically.
            $files = array_merge(
                array($front->getDefaultControllerName() . 'Controller.php'),
                scandir($path)
            );
            foreach ($files as $file) {
                
                // only consider files ending in 'controller'.
                $matches = array();
                if (!is_file($path . '/' . $file)
                    || (!preg_match('/((.*)Controller).php$/', $file, $matches))
                ) {
                    continue;
                }
                
                // skip invalid controller classes
                $class = ucfirst($moduleId) . '_' . $matches[1]; 
                if (!class_exists($class) 
                    || !in_array(static::ACTION_INTERFACE, class_implements($class), true)
                ) {
                    continue;
                }

                // get the controller label and id
                $controllerName  = $matches[2];
                $controllerLabel = $this->_formatLabel($controllerName);
                $controllerId    = $this->_formatRoute($controllerName);
                    
                // collect actions from the controller.
                // sort the actions alphabetically, but always put the
                // default action first so that it appears first in options
                $methods = get_class_methods($class);
                natsort($methods);
                array_unshift($methods, $front->getDefaultAction() . 'Action');
                foreach ($methods as $method) {
                    
                    // only consider methods ending in 'action'.
                    $matches = array();
                    if (!method_exists($class, $method) 
                        || !preg_match('/(.*)Action$/', $method, $matches)
                    ) {
                        continue;
                    }
                    
                    $actionName  = $matches[1];
                    $actionLabel = $this->_formatLabel($actionName);
                    $actionId    = $this->_formatRoute($actionName);

                    // combine module/controller/action to form a
                    // qualified action id for the option value.
                    $value = implode('/', array($moduleId, $controllerId, $actionId));

                    // format the action label for the select option
                    $label  = $moduleLabel;
                    $label .= $controllerId !== $front->getDefaultControllerName()
                        ? '/'. $controllerLabel
                        : '';
                    $label .= $actionId !== $front->getDefaultAction()
                        ? '/' . $actionLabel
                        : ' (Default Action)';

                    $options[$moduleLabel][$value] = $label;
                }
            }
        }
        
        // remove any empty opt-groups and return.
        return array_filter($options);
    }
Menu_Form_MenuItemMvc::_splitParamsIniString ( values) [protected]

Convert params INI string to an array using Zend_Config_Ini write to a temp file to facilitate Zend_Config_Ini parsing.

Parameters:
array$valuesvalues that contain the string to convert
Returns:
array values with params converted to an array
    {
        $params = array();
        if (isset($values['params']) && strlen($values['params'])) {
            $tempFile = tempnam(sys_get_temp_dir(), 'menu-form');
            file_put_contents($tempFile, $values['params']);
            $config   = new Zend_Config_Ini($tempFile);
            $params   = $config->toArray();
            unlink($tempFile);
        }
        $values['params'] = is_array($params) ? $params : array();

        return $values;
    }
Menu_Form_MenuItemMvc::_splitRouteParams ( values) [protected]

Split action parameter into discrete module/controller/action values.

If any values are missing, default values are used.

Parameters:
array$valuesvalues array containing action to split.
Returns:
array values with action split into module/controller/action.
    {
        if (!isset($values['module']) && !isset($values['controller'])) {
            $front      = Zend_Controller_Front::getInstance();
            $action     = isset($values['action']) ? $values['action'] : '';
            $params     = explode('/', $action, 3);
            $module     = isset($params[0])
                ? $params[0]
                : $front->getDefaultModule();
            $controller = isset($params[1])
                ? $params[1]
                : $front->getDefaultControllerName();
            $action     = isset($params[2])
                ? $params[2]
                : $front->getDefaultAction();
            
            $values = array_merge(
                $values,
                array(
                    'module'     => $module,
                    'controller' => $controller,
                    'action'     => $action
                )
            );
        }
        
        return $values;
    }
Menu_Form_MenuItemMvc::getValues ( suppressArrayNotation = false)

Extend parent to split action into discrete module/controller/action.

Parameters:
bool$suppressArrayNotationsee parent
Returns:
array

Reimplemented from Menu_Form_MenuItem.

    {
        $values = parent::getValues($suppressArrayNotation);
        $values = $this->_splitRouteParams($values);
        $values = $this->_splitParamsIniString($values);

        return $values;
    }
Menu_Form_MenuItemMvc::init ( )

Defines the elements that make up the menu MVC item form.

Called automatically when the form object is created.

Reimplemented from Menu_Form_MenuItem.

    {
        parent::init();
        
        $this->addElement(
            'select',
            'action',
            array(
                'label'         => 'Action',
                'required'      => true,
                'multiOptions'  => $this->_getActionOptions()
            )
        );

        $this->addElement(
            'textarea',
            'params',
            array(
                'label'         => 'Parameters',
                'description'   => 'Enter additional action parameters in INI format.',
                'rows'          => 10,
                'cols'          => 80
            )
        );
    }
Menu_Form_MenuItemMvc::isValid ( data)

Extends parent to combine combine route params (module/controller/action) into a single action field, and change the params array, if it exists, into an ini formatted string.

isValid never calls setDefaults, which is why this needs to be done here as well

Parameters:
array$datasee parent
Returns:
boolean

Reimplemented from Menu_Form_MenuItem.

    {
        return parent::isValid($this->_combineParamsArray($this->_combineRouteParams($data)));
    }
Menu_Form_MenuItemMvc::setDefaults ( defaults)

Extends parent to combine route params (module/controller/action) into a single action field.

Parameters:
P4Cms_Record | array$defaultsthe default values to set on elements
Returns:
Zend_Form provides fluent interface

Reimplemented from Menu_Form_MenuItem.

    {
        $defaults = $this->_combineRouteParams($defaults);
        $defaults = $this->_combineParamsArray($defaults);
        
        return parent::setDefaults($defaults);
    }

Member Data Documentation

const Menu_Form_MenuItemMvc::ACTION_INTERFACE = 'Zend_Controller_Action_Interface'

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