Perforce Chronicle 2012.2/486814
API Documentation

Content_Form_Element_ContentSelect Class Reference

Allows the user to select one or more content entries. More...

Inheritance diagram for Content_Form_Element_ContentSelect:
P4Cms_Content_EnhancedElementInterface Content_Form_Element_ImageSelect

List of all members.

Public Member Functions

 getContentRecord ()
 Get the associated content record (if set).
 getDefaultDisplayDecorators ()
 Get the default display decorators to use when rendering content elements of this type.
 isValid ($value, $context=null)
 Extends validate to ensure that value is not an array when multiple select is disabled (and vice-versa) and to verify that selected content ids exist.
 setContentRecord ($content)
 Set the associated content record for this element.

Public Attributes

 $helper = 'ContentSelect'

Protected Attributes

 $_contentRecord = null

Detailed Description

Allows the user to select one or more content entries.

The value is a single content id string, or an array of content ids. To allow for multiple selection, set the 'multiple' to true.

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

Content_Form_Element_ContentSelect::getContentRecord ( )

Get the associated content record (if set).

Returns:
null|P4Cms_Content the associated content record or null if none set.

Implements P4Cms_Content_EnhancedElementInterface.

    {
        return $this->_contentRecord;
    }
Content_Form_Element_ContentSelect::getDefaultDisplayDecorators ( )

Get the default display decorators to use when rendering content elements of this type.

Renders using the purpose built display selected content decorator (which is a wrapper of the content list view helper).

Configures it not to display a message when no content is selected. This allows placeholder values to work.

Returns:
array decorators configuration array suitable for passing to element setDecorators().

Implements P4Cms_Content_EnhancedElementInterface.

Reimplemented in Content_Form_Element_ImageSelect.

    {
        return array(
            array(
                'decorator' => 'DisplaySelectedContent',
                'options'   => array(
                    'emptyMessage' => ''
                )
            )
        );
    }
Content_Form_Element_ContentSelect::isValid ( value,
context = null 
)

Extends validate to ensure that value is not an array when multiple select is disabled (and vice-versa) and to verify that selected content ids exist.

Parameters:
string | array$valuethe selected content id(s).
mixed$contextoptional context
Returns:
bool
    {
        if (!$this->getValidator('P4Cms_Validate_Callback')) {
            $element   = $this;
            $validator = new P4Cms_Validate_Callback;
            $validator->setCallback(
                function ($value) use ($element, $validator)
                {
                    $multiple   = $element->getAttrib('multiple');
                    $validTypes = (array) $element->getAttrib('validTypes');

                    // ensure type is array or scalar as appropriate.
                    if (is_array($value) && !$multiple) {
                        $validator->setMessage(
                            "You may only select one content entry.",
                            $validator::INVALID_VALUE
                        );
                        return false;
                    }
                    if (!is_array($value) && $multiple) {
                        $validator->setMessage(
                            "The selected content must be in the form of a list.",
                            $validator::INVALID_VALUE
                        );
                        return false;
                    }

                    // validate the content id(s).
                    $invalidIds  = array();
                    $idValidator = new P4Cms_Validate_ContentId;
                    $idValidator->setAllowNonExistent(false)->setAllowEmpty(true);
                    foreach ((array) $value as $id) {
                        if (!$idValidator->isValid($id)) {
                            $invalidIds[] = $id;
                        }
                    }
                    if ($invalidIds) {
                        $validator->setMessage(
                            "One or more of the selected entries could not be found. " .
                            "Please review and clear these selections.",
                            $validator::INVALID_VALUE
                        );
                        return false;
                    }

                    // ensure that entries have valid content types
                    if ($validTypes) {
                        $entries = P4Cms_Content::fetchAll(
                            array(
                                'ids'         => array_filter((array) $value),
                                'limitFields' => array('contentType')
                            )
                        );
                        foreach ($entries as $entry) {
                            if (!in_array($entry->getContentTypeId(), $validTypes)) {
                                $validator->setMessage(
                                    "One or more entries have an invalid content type"
                                    . " (allowed types: " . implode(', ', $validTypes) . ').',
                                    $validator::INVALID_VALUE
                                );
                                return false;
                            }
                        }
                    }

                    return true;
                }
            );
            $this->addValidator($validator);
        }

        return parent::isValid($value, $context);
    }
Content_Form_Element_ContentSelect::setContentRecord ( content)

Set the associated content record for this element.

Parameters:
P4Cms_Content$contentthe associated content record for this element.

Implements P4Cms_Content_EnhancedElementInterface.

    {
        $this->_contentRecord = $content;
    }

Member Data Documentation

Content_Form_Element_ContentSelect::$_contentRecord = null [protected]
Content_Form_Element_ContentSelect::$helper = 'ContentSelect'

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