Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_Content_Opened Class Reference

Tracks which users have a given content record open for edit in their browser. More...

Inheritance diagram for P4Cms_Content_Opened:
P4Cms_Record_Volatile P4Cms_Record_Connected P4Cms_Model P4Cms_ModelInterface

List of all members.

Public Member Functions

 getUsers ()
 Use this accessor to get all of the keys grouped by user; e.g.
 setUserEditTime ($user, $time=true)
 Set the edit time for a specified user.
 setUserPingTime ($user, $time=true)
 Set the ping time for a specified user.
 setUserStartTime ($user, $time=true)
 Set the start time for a specified user.
 setUserTimeProperty ($user, $property, $time=true)
 Set the time for a specified user property.

Public Attributes

const PING_TIMEOUT = 70

Protected Attributes

 $_storageSubPath = 'opened'

Detailed Description

Tracks which users have a given content record open for edit in their browser.

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

P4Cms_Content_Opened::getUsers ( )

Use this accessor to get all of the keys grouped by user; e.g.

users[user][keyName]. This method will screen out any entries older than PING_TIMEOUT and adds in 'Offset' values for the start/ping/edit times.

Calling getValues on this model will return keys in the format user-keyName. We store the keys this way to allow setting a single user/key/value without knowing the other values (minimizes race conditions).

Returns:
array the current values organized by user
    {
        $users  = array();
        foreach ($this->getValues() as $key => $value) {
            $parts = explode('-', $key, 2);
            if (count($parts) !== 2) {
                continue;
            }

            list($user, $key) = $parts;
            if (!isset($users[$user])) {
                $users[$user] = array();
            }

            $users[$user][$key] = $value;
        }

        // do a second loop to remove expired entries
        $time = time();
        foreach ($users as $user => &$values) {
            // normalize array
            $values += array('pingTime' => null, 'editTime' => null, 'startTime' => null);
            
            // if start or ping time are missing, or the ping is expired remove entry
            if (!$values['startTime'] || !$values['pingTime'] 
                || ($time - $values['pingTime']) > static::PING_TIMEOUT
            ) {
                unset($users[$user]);
            }
        }

        // sort the users based on their last edit and start time
        uasort(
            $users, 
            function($a, $b) 
            {
                if ($a['editTime'] || $b['editTime']) {
                    return $a['editTime'] - $b['editTime'];
                }
                
                return $a['startTime'] - $b['startTime'];
            }
        );

        return $users;
    }
P4Cms_Content_Opened::setUserEditTime ( user,
time = true 
)

Set the edit time for a specified user.

The default time of true will automatically set the current time. Passing false will clear the time for the specified user and any other value will be used unchanged.

Parameters:
string | P4Cms_User$userThe user id to set this property on
mixed$timeThe time to use - optional
Returns:
P4Cms_Content_Opened To maintain a fluent interface
    {
        return $this->setUserTimeProperty($user, 'edit', $time);
    }
P4Cms_Content_Opened::setUserPingTime ( user,
time = true 
)

Set the ping time for a specified user.

The default time of true will automatically set the current time. Passing false will clear the time for the specified user and any other value will be used unchanged.

Parameters:
string | P4Cms_User$userThe user id to set this property on
mixed$timeThe time to use - optional
Returns:
P4Cms_Content_Opened To maintain a fluent interface
    {
        return $this->setUserTimeProperty($user, 'ping', $time);
    }
P4Cms_Content_Opened::setUserStartTime ( user,
time = true 
)

Set the start time for a specified user.

The default time of true will automatically set the current time. Passing false will clear the time for the specified user and any other value will be used unchanged.

Parameters:
string | P4Cms_User$userThe user id to set this property on
mixed$timeThe time to use - optional
Returns:
P4Cms_Content_Opened To maintain a fluent interface
    {
        return $this->setUserTimeProperty($user, 'start', $time);
    }
P4Cms_Content_Opened::setUserTimeProperty ( user,
property,
time = true 
)

Set the time for a specified user property.

The default time of true will automatically set the current time. Passing false will clear the time for the specified user and any other value will be used unchanged.

Parameters:
string | P4Cms_User$userThe user id to set this property on
string$propertyThe 'time' property to set (e.g. start/ping)
mixed$timeThe time to use - optional
Returns:
P4Cms_Content_Opened To maintain a fluent interface
    {
        // normalize user; like a boss
        $user = $user instanceof P4Cms_User ? $user->getId() : $user;

        // default case; use current time
        if ($time === true) {
            $time = time();
        }
        
        // normalize false values to null
        if (!$time) {
            $time = null;
        }

        return $this->setValue($user . '-' . $property . 'Time', $time);
    }

Member Data Documentation

P4Cms_Content_Opened::$_storageSubPath = 'opened' [protected]

Reimplemented from P4Cms_Record_Volatile.


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