Perforce Chronicle 2012.2/486814
API Documentation

Content_View_Helper_ContentEntry Class Reference

View helper that displays a content entry. More...

List of all members.

Public Member Functions

 __toString ()
 A magic method which calls through to render; see render for details.
 close ()
 Output the closing tag for a content entry dijit.
 contentEntry ($entry=null, array $options=null)
 The default method called for this helper.
 data (array $options=null)
 Return an array with data about the content entry.
 element ($name, array $options=null)
 Output the appropriate markup to display a content element.
 getDefaultEntry ()
 Returns the default entry or null.
 getDefaultOptions ()
 Returns the default options array or null.
 getEntry ()
 Return the entry being used for this helper instance.
 getOptions ()
 Return the options being used for this helper instance.
 getView ()
 Get the view object set on this helper.
 hasElement ($name)
 Indicate whether an element exists in the content entry.
 open ()
 Output the appropriate markup to open the active entry.
 render ()
 Output the appropriate markup to display a content entry and all of its associated fields.
 setDefaults (P4Cms_Content $entry=null, array $options=null)
 Set a default entry and options to be used if this helper is called without pasing arguments.

Public Attributes

const DATA_INCLUDE_CHANGE = 'change'
const DATA_INCLUDE_FIELDS = 'fields'
const DATA_INCLUDE_OPENED = 'opened'
const DATA_INCLUDE_STATUS = 'status'
const OPT_DISPLAY = 'display'
const OPT_FIELD_OPTIONS = 'fieldOptions'
const OPT_FIELDS = 'fields'
const OPT_PRELOAD_FORM = 'preloadForm'
const OPT_TAG_NAME = 'tagName'

Protected Member Functions

 _getTagName ($definition, array $options=null)
 Get the html tag name to use for this element.
 _shouldPreloadForm ($entry, array $privileges, array $options=null)
 Determine if the form should be preloaded.

Protected Attributes

 $_elementDefaultTag = 'div'
 $_elementDojoType = 'p4cms.content.Element'
 $_entry = null
 $_entryDojoType = 'p4cms.content.Entry'
 $_entryElementType = 'div'
 $_options = null

Static Protected Attributes

static $_defaultEntry = null
static $_defaultOptions = null

Detailed Description

View helper that displays a content entry.

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_View_Helper_ContentEntry::__toString ( )

A magic method which calls through to render; see render for details.

Returns:
string The output of our render method.
    {
        try {
            return $this->render();
        } catch (Exception $e) {
            return "";
        }
    }
Content_View_Helper_ContentEntry::_getTagName ( definition,
array $  options = null 
) [protected]

Get the html tag name to use for this element.

Parameters:
array$definitionthe element definition.
null | array$optionsoptions to influence entry markup.
Returns:
string the html tag name for this element.
    {
        if (is_array($options) && isset($options[static::OPT_TAG_NAME])) {
            return $options[static::OPT_TAG_NAME];
        }

        if (isset($definition['display'][static::OPT_TAG_NAME])) {
            return $definition['display'][static::OPT_TAG_NAME];
        }

        return $this->_elementDefaultTag;
    }
Content_View_Helper_ContentEntry::_shouldPreloadForm ( entry,
array $  privileges,
array $  options = null 
) [protected]

Determine if the form should be preloaded.

Form will only be preloaded if preload option is set and user has permission.

Parameters:
string | P4Cms_Content$entrya content entry id or instance.
array$privilegesprivileges current user has for this entry.
null | array$optionsoptions to influence entry markup.
Returns:
string the opening markup for a content entry.
    {
        // if user has no add/edit permission for this entry, return false.
        $privilege = $entry->hasId() ? 'edit' : 'add';
        if (!in_array($privilege, $privileges)) {
            return false;
        }

        $preloadOption = Content_View_Helper_ContentEntry::OPT_PRELOAD_FORM;
        if (isset($options[$preloadOption])) {
            return $options[$preloadOption];
        }

        return false;
    }
Content_View_Helper_ContentEntry::close ( )

Output the closing tag for a content entry dijit.

Returns:
string the closing tag for a content entry.

p4cms.content.render.close Return the passed HTML after applying any modifications. Allows customization of the closing HTML markup for a content entry. string $html The closing HTML markup for the content entry. Content_View_Helper_ContentEntry $helper The view helper rendering the content entry.

    {
        $html = "</" . $this->_entryElementType . ">";

        // allow third-parties to influence rendering.
        $html = P4Cms_PubSub::filter(
            'p4cms.content.render.close',
            $html,
            $this
        );

        return $html;
    }
Content_View_Helper_ContentEntry::contentEntry ( entry = null,
array $  options = null 
)

The default method called for this helper.

If no params are passed the default entry/options will be active. If an entry is passed the passed options are always used with it (even if null). If non null options are passed they will always be used.

Parameters:
P4Cms_Content | string | null$entryThe entry to use or null for default
array$optionsThe options to use or null for default when using the default entry
Returns:
Content_View_Helper_ContentEntry A helper instance with the correct options/entry
    {
        // if there is no entry and no options
        // return the active helper.
        if (is_null($entry) && is_null($options)) {
            return $this;
        }

        // if we made it here we must have a new entry and/or options.
        // create an instance and set the passed entry/options; if either
        // is invalid the new entry will fall back to the active entry
        // options which are stored statically
        $helper = new static;

        // normalize entry to an object for the case of a passed id
        if (!is_null($entry) && !$entry instanceof P4Cms_Content) {
            $entry = P4Cms_Content::fetch($entry);
        }

        $helper->view     = $this->view;
        $helper->_entry   = $entry;
        $helper->_options = $options;

        return $helper;
    }
Content_View_Helper_ContentEntry::data ( array $  options = null)

Return an array with data about the content entry.

Supports the options: DATA_INCLUDE_FIELDS => true/false/array of field ids (true is default) DATA_INCLUDE_CHANGE => true/false (false is default) DATA_INCLUDE_STATUS => true/false (false is default) DATA_INCLUDE_OPENED => true/false (false is default)

Parameters:
array | null$optionsAny custom options to use
Returns:
array The requested entry details
    {
        // normalize options to array and set defaults
        $options = ((array) $options) + array(
            static::DATA_INCLUDE_FIELDS => true,
            static::DATA_INCLUDE_CHANGE => false,
            static::DATA_INCLUDE_STATUS => false,
            static::DATA_INCLUDE_OPENED => false
        );

        // the data array will be our final return
        $data = array();

        // deal with the fields option
        $fields = $options[static::DATA_INCLUDE_FIELDS];
        if (!empty($fields) && (bool)$fields) {
            $data['fields'] = $this->getEntry()->getValues();

            // if we have an array of allowed fields enforce it
            if (is_array($fields)) {
                foreach ($data['fields'] as $key => $value) {
                    if (!in_array($key, $fields)) {
                        unset($data['fields'][$key]);
                    }
                }
            }
        }

        // deal with the opened option
        if ($options[static::DATA_INCLUDE_OPENED] && $this->getEntry()->getId()) {
            try {
                $data['opened'] = P4Cms_Content_Opened::fetch(
                    $this->getEntry()->getId(),
                    $this->getEntry()->getAdapter()
                )->getUsers();
            } catch (P4Cms_Record_NotFoundException $e) {
                // no data to add no-one has it open
            }
        }

        $file = $this->getEntry()->toP4File();

        // include the change details if requested
        if ((bool) $options[static::DATA_INCLUDE_CHANGE]) {
            $data['change'] = $file->getChange()->getValues();
        }

        // include the status if requested
        if ((bool) $options[static::DATA_INCLUDE_STATUS]) {
            $data['status'] = array(
                'Version'   => $file->getStatus('headRev'),
                'Action'    => $file->getStatus('headAction')
            );
        }

        return $data;
    }
Content_View_Helper_ContentEntry::element ( name,
array $  options = null 
)

Output the appropriate markup to display a content element.

Parameters:
string$namethe name of the element/field to display.
null | array$optionsoptions to influence entry markup.
Returns:
string|bool the markup for the content element or false
    {
        // get the entry and, defensive of null, the type
        $entry = $this->getEntry();
        $type  = $entry ? $entry->getContentType() : null;

        // return false if entry or element aren't known
        // to avoid throwing an exception
        if (!$entry || !$type->hasElement($name)) {
            return false;
        }

        // determine display options
        $displayOptions = isset($options[static::OPT_DISPLAY])
            ? $options[static::OPT_DISPLAY]
            : array();

        $element    = $type->getFormElement($name);
        $definition = $type->getElement($name);
        $label      = $element->getLabel() ? : $name;
        $value      = $entry->getDisplayValue($name, $displayOptions);

        // prepare html attributes.
        $attribs = array(
            "class"         => "content-element content-element-type-" . $definition['type']
                            .  " content-element-" . $name,
            "dojoType"      => $this->_elementDojoType,
            "elementName"   => $name,
            "elementLabel"  => $label,
            "contentId"     => $entry->getId(),
            "required"      => $element->isRequired() ? 'true' : 'false'
        );

        $tagName = $this->_getTagName($definition, $options);

        // _htmlAttribs() does HTML attribute escaping;
        // $value is escaped in P4Cms_Content::getDisplayValue() as long as
        // proper display filters are defined in element's definition
        return "<" . $tagName . $this->_htmlAttribs($attribs) . ">"
            . $value . "</" . $tagName . ">";
    }
Content_View_Helper_ContentEntry::getDefaultEntry ( )

Returns the default entry or null.

Returns:
P4Cms_Content|null The default entry if we have one.
Content_View_Helper_ContentEntry::getDefaultOptions ( )

Returns the default options array or null.

Returns:
array|null The default options array if we have one.
Content_View_Helper_ContentEntry::getEntry ( )

Return the entry being used for this helper instance.

Will return the entry passed to contentEntry() if valid falling back to the default entry.

Returns:
P4Cms_Content|null The active content entry or null
    {
        if ($this->_entry instanceof P4Cms_Content) {
            return $this->_entry;
        }

        if (!static::$_defaultEntry instanceof P4Cms_Content) {
            throw new P4Cms_Content_Exception(
                "No entry has been set on the Content Entry helper"
            );
        }

        return static::$_defaultEntry;
    }
Content_View_Helper_ContentEntry::getOptions ( )

Return the options being used for this helper instance.

The default options are returned if we are on the default entry and null options were passed to contentEntry(). If we are using a non-default content entry the passed options are always returned even when null.

We only ever use the default options with the default entry as settings such as include form are likely to be set here and are not generally advisable when rendering other content entries.

Returns:
P4Cms_Content|null The active content entry or null
    {
        if ($this->_options != null || $this->_entry instanceof P4Cms_Content) {
            return $this->_options;
        }

        return static::$_defaultOptions;
    }
Content_View_Helper_ContentEntry::getView ( )

Get the view object set on this helper.

Returns:
Zend_View_Interface the view object set on this helper.
Exceptions:
Content_Exceptionif no view object set.
    {
        if (!$this->view instanceof Zend_View_Interface) {
            throw new Content_Exception(
                "Cannot get view object. No valid view object has been set."
            );
        }

        return $this->view;
    }
Content_View_Helper_ContentEntry::hasElement ( name)

Indicate whether an element exists in the content entry.

Parameters:
string$namethe name of the element/field to display.
Returns:
bool true if the element exists in the entry, false otherwise
    {
        return $this->getEntry()->hasField($name) ? true : false;
    }
Content_View_Helper_ContentEntry::open ( )

Output the appropriate markup to open the active entry.

Produce dijit markup if user can edit this content entry or plain html otherwise.

Returns:
string the opening markup for a content entry.
    {
        $entry   = $this->getEntry();
        $options = $this->getOptions();

        // pull out the version and headVersion if possible; defaults to blank
        $version     = '';
        $headVersion = '';
        if ($entry->getId()) {
            $file = $entry->toP4File();
            if ($file->hasStatusField('headRev')) {
                $version     = $file->getStatus('headRev');
                $headVersion = P4_File::fetch(
                    P4_File::stripRevspec($file->getFilespec())
                )->getStatus('headRev');
            }
        }

        // prepare html attributes.
        $attribs = array(
            "id"                => "content-entry-" . $entry->getId(),
            "class"             => "content-entry content-entry-type-". $entry->getContentTypeId(),
            "dojoType"          => $this->_entryDojoType,
            "contentType"       => $entry->getContentTypeId(),
            "contentId"         => $entry->getId(),
            "contentTitle"      => $entry->getTitle(),
            "contentVersion"    => $version,
            "headVersion"       => $headVersion,
            "deleted"           => Zend_Json::encode($entry->isDeleted())
        );

        // add all privileges for which user has access to 'content/entryID' resource.
        $user       = P4Cms_User::fetchActive();
        $resource   = 'content/' . $entry->getId();
        $privileges = $user->getAllowedPrivileges($resource);

        // add list with privileges to the dijit attributes.
        $attribs["allowedPrivileges"] = implode(", ", $privileges);

        // declare entry markup (_htmlAttrib() does escaping).
        $html = "<" . $this->_entryElementType . $this->_htmlAttribs($attribs) . ">";

        // optionally preload the form.
        if ($this->_shouldPreloadForm($entry, $privileges, $options)) {
            $formOptions = array('entry' => $entry);

            // if request specifies an id prefix, add it to options.
            $request = Zend_Controller_Front::getInstance()->getRequest();
            $formOptions['idPrefix'] = $request->getParam('formIdPrefix');

            $form = new Content_Form_Content($formOptions);
            $form->populate();

            $iconUrl = $this->view->url(
                array(
                    'module'        => 'content',
                    'controller'    => 'type',
                    'action'        => 'icon',
                    'id'            => $entry->getContentType()->getId()
                )
            );

            // render form into markup.
            $html .= "<div class='p4cms-ui content-form-container' "
                  .  "dojoType='dojox.layout.ContentPane' "
                  .  "style='display: none'>"
                  .  "<div class=\"content-type-heading\">"
                  .  "<img src=\"$iconUrl\" width=\"64\" height=\"64\"/>"
                  .  "<span>" . $this->view->escape($entry->getContentType()->getLabel()) ."</span>"
                  .  "</div>\n"
                  .  $form->render()
                  .  "</div>";
        }

        return $html;
    }
Content_View_Helper_ContentEntry::render ( )

Output the appropriate markup to display a content entry and all of its associated fields.

Returns:
string the markup for a content entry.

p4cms.content.render Return the passed HTML after applying any modifications. Allows customization of the markup for a content entry. string $html The HTML markup for a content entry. P4Cms_Content $content The content entry being rendered. array $options An array of options for the view helper to influence rendering.

    {
        $entry   = $this->getEntry();
        $options = $this->getOptions();

        // if no fields specified, display all fields.
        $type   = $entry->getContentType();
        $fields = isset($options[static::OPT_FIELDS]) && is_array($options[static::OPT_FIELDS])
            ? $options[static::OPT_FIELDS]
            : null;
        if ($fields === null) {
            $fields = $type->getElementNames();
        }

        // start the content dijit.
        $html = $this->open();

        // show the selected fields of the content entry.
        $optionsKey = static::OPT_FIELD_OPTIONS;
        foreach ($fields as $field) {
            if ($type->hasElement($field)) {

                // extract options for this field.
                $fieldOptions = isset($options[$optionsKey], $options[$optionsKey][$field])
                    ? $options[$optionsKey][$field]
                    : null;

                // if rendering this field is disabled, skip it.
                $fieldDefinition = $type->getElement($field);
                if (isset($fieldDefinition['display']['render'])
                    && !$fieldDefinition['display']['render']
                ) {
                    continue;
                }

                $html .= $this->element($field, $fieldOptions);
            }
        }

        // close the content entry.
        $html .= $this->close();

        // allow third-parties to influence rendering.
        $html = P4Cms_PubSub::filter('p4cms.content.render', $html, $entry, $options);

        return $html;
    }
Content_View_Helper_ContentEntry::setDefaults ( P4Cms_Content entry = null,
array $  options = null 
)

Set a default entry and options to be used if this helper is called without pasing arguments.

This is normaly used by the controller to set the active entry and recommended options.

Parameters:
P4Cms_Content | null$entryentry to use or null for default
array | null$optionsoptions to use or null for default
    {
        // normalize entry to an object for the case of a passed id
        if (!is_null($entry) && !$entry instanceof P4Cms_Content) {
            $entry = P4Cms_Content::fetch($entry);
        }

        static::$_defaultEntry   = $entry;
        static::$_defaultOptions = $options;
    }

Member Data Documentation

Content_View_Helper_ContentEntry::$_defaultEntry = null [static, protected]
Content_View_Helper_ContentEntry::$_defaultOptions = null [static, protected]
Content_View_Helper_ContentEntry::$_elementDefaultTag = 'div' [protected]
Content_View_Helper_ContentEntry::$_elementDojoType = 'p4cms.content.Element' [protected]
Content_View_Helper_ContentEntry::$_entry = null [protected]
Content_View_Helper_ContentEntry::$_entryDojoType = 'p4cms.content.Entry' [protected]
Content_View_Helper_ContentEntry::$_entryElementType = 'div' [protected]
Content_View_Helper_ContentEntry::$_options = null [protected]

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