Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_Notifications Class Reference

Adds a notification facility which will ultimately be exposed in the UI. More...

List of all members.

Static Public Member Functions

static add ($message, $severity=self::SEVERITY_INFO)
 Add a message to the collection of notifications of the specified priority.
static exist ($severity=null)
 Reports whether there are any notifications.
static fetch ($severity=null, $clear=true)
 Retrieve current notifications.
static getCount ($severity=null)
 Returns the count of current notifications.

Public Attributes

const SEVERITY_ERROR = 'error'
const SEVERITY_INFO = 'info'
const SEVERITY_SUCCESS = 'success'
 Define known severities.
const SEVERITY_WARNING = 'warning'

Static Protected Member Functions

static _getSession ()
 Return the static session object, initializing if necessary.
static _resetNotifications ($severity=null)
 Reset/initialize the notifications.

Detailed Description

Adds a notification facility which will ultimately be exposed in the UI.

Example usage: P4Cms_Notifications::add('An error message', 'error'); P4Cms_Notifications::add('Other users are editing this document', 'warn'); P4Cms_Notifications::add('Changes saved.');

if (P4Cms_Notifications::exist('error')) { echo P4Cms_Notifications::getCount('error') .' error notifications exist.'; }

Notifications are intended to be transient; users should only be presented with a notifications once. Internally, Zend_Session is used to persist notifications across web requests, but once retrieved, by default, each notification is destroyed. Calling code can opt to retain notifications; please see the documentation for fetch() below.

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_Notifications::_getSession ( ) [static, protected]

Return the static session object, initializing if necessary.

Returns:
Zend_Session_Namespace
    {
        if (!static::$_session instanceof Zend_Session_Namespace) {
            static::$_session = new Zend_Session_Namespace('Notifications');
        }

        return static::$_session;
    }
static P4Cms_Notifications::_resetNotifications ( severity = null) [static, protected]

Reset/initialize the notifications.

If a severity is provided, reset/initialize only that severity.

Parameters:
string$severityOptional severity to reset/initialize.
Returns:
void
    {
        // initialize notification storage, if necessary
        if (!isset(static::_getSession()->notifications)) {
            static::_getSession()->notifications = array();
        }

        if (is_null($severity)) {
            static::_getSession()->notifications = array();
        } elseif (is_string($severity)) {
            static::_getSession()->notifications[$severity] = array();
        }
    }
static P4Cms_Notifications::add ( message,
severity = self::SEVERITY_INFO 
) [static]

Add a message to the collection of notifications of the specified priority.

Parameters:
string | array$messageMessage(s) to add.
string$severitySeverity of the message(s) - default: SEVERITY_INFO
Returns:
void
Exceptions:
InvalidArgumentExceptionWhen severity is passed as an array.
    {
        $session = static::_getSession();
        // Initialize the notifications, if necessary
        if (!isset($session->notifications)) {
            $session->notifications = array();
        }

        // make sure severity has a reasonable default
        if (is_null($severity)) {
            $severity = static::SEVERITY_INFO;
        }

        // severity passed as an array is not acceptable.
        if (is_array($severity)) {
            throw new InvalidArgumentException('Severity cannot be an array.');
        }

        // initialize notifications for the current severity, if necessary
        if (!array_key_exists($severity, $session->notifications)) {
            $session->notifications[$severity] = array();
        }

        // handle messages as an array or single item, as appropriate
        if (is_array($message)) {
            foreach ($message as $msg) {
                $session->notifications[$severity][] = $msg;
            }
        } else {
            $session->notifications[$severity][] = $message;
        }
    }
static P4Cms_Notifications::exist ( severity = null) [static]

Reports whether there are any notifications.

If severity is specified, identifies whether any notifications exist at that severity.

Parameters:
string$severityOptional severity to test.
Returns:
bool Indicates whether any notifications exist.
    {
        return (bool) static::getCount($severity) > 0;
    }
static P4Cms_Notifications::fetch ( severity = null,
clear = true 
) [static]

Retrieve current notifications.

If severity is specified, retrieve only the notifications at that severity.

The default behaviour is to clear the notifications upon retrieval. Set $clear to false to prohibit that.

Parameters:
string$severityOptional severity to retrieve.
bool$clearSet to false to prohibit notification clearing. Defaults to true.
Returns:
array Retrieved notifications.
    {
        if (!isset(static::_getSession()->notifications)) {
            static::_getSession()->notifications = array();
        }

        $notifications = static::_getSession()->notifications;
        if (!is_null($severity)) {
            if (!array_key_exists($severity, $notifications)) {
                $notifications = array();
            } else {
                $notifications = $notifications[$severity];
            }
        }

        if ($clear) {
            static::_resetNotifications($severity);
        }

        return $notifications;
    }
static P4Cms_Notifications::getCount ( severity = null) [static]

Returns the count of current notifications.

If severity is specified, only count notifications at that severity.

Parameters:
string$severityOptional severity for notification counting.
Returns:
int Total of defined notifications.
    {
        $notifications = static::_getSession()->notifications;
        // if we are not yet initialized, the count is 0.
        if (!isset($notifications)) {
            return 0;
        }

        $total = 0;
        if (is_null($severity)) {
            foreach ($notifications as $aSeverity => $list) {
                $total += count($list);
            }
        } elseif (is_string($severity)) {
            if (array_key_exists($severity, $notifications)) {
                $total = count($notifications[$severity]);
            }
        }

        return $total;
    }

Member Data Documentation

Define known severities.


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