eZ Publish  [4.0]
ezcollaborationitemparticipantlink.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZCollaborationItemParticipantLink class
00004 //
00005 // Created on: <22-Jan-2003 16:08:22 amos>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file ezcollaborationparticipantlink.php
00032 */
00033 
00034 /*!
00035   \class eZCollaborationItemParticipantLink ezcollaborationitemparticipantlink.php
00036   \brief The class eZCollaborationItemParticipantLink does
00037 
00038 */
00039 
00040 //include_once( 'kernel/classes/ezpersistentobject.php' );
00041 
00042 class eZCollaborationItemParticipantLink extends eZPersistentObject
00043 {
00044     const TYPE_USER = 1;
00045     const TYPE_USERGROUP = 2;
00046 
00047     // Everything from 1024 and above is considered custom and is specific per collaboration handler.
00048     const TYPE_CUSTOM = 1024;
00049 
00050     const ROLE_STANDARD = 1;
00051     const ROLE_OBSERVER = 2;
00052     const ROLE_OWNER = 3;
00053     const ROLE_APPROVER = 4;
00054     const ROLE_AUTHOR = 5;
00055 
00056     // Everything from 1024 and above is considered custom and is specific per collaboration handler.
00057     const ROLE_CUSTOM = 1024;
00058 
00059     /*!
00060      Constructor
00061     */
00062     function eZCollaborationItemParticipantLink( $row )
00063     {
00064         $this->eZPersistentObject( $row );
00065     }
00066 
00067     static function definition()
00068     {
00069         return array( 'fields' => array( 'collaboration_id' => array( 'name' => 'CollaborationID',
00070                                                                       'datatype' => 'integer',
00071                                                                       'default' => 0,
00072                                                                       'required' => true,
00073                                                                       'foreign_class' => 'eZCollaborationItem',
00074                                                                       'foreign_attribute' => 'id',
00075                                                                       'multiplicity' => '1..*' ),
00076                                          'participant_id' => array( 'name' => 'ParticipantID',
00077                                                                     'datatype' => 'integer',
00078                                                                     'default' => 0,
00079                                                                     'required' => true,
00080                                                                     'foreign_class' => 'eZContentObject',
00081                                                                     'foreign_attribute' => 'id',
00082                                                                     'multiplicity' => '1..*' ),
00083                                          'participant_type' => array( 'name' => 'ParticipantType',
00084                                                                       'datatype' => 'integer',
00085                                                                       'default' => 1,
00086                                                                       'required' => true ),
00087                                          'participant_role' => array( 'name' => 'ParticipantRole',
00088                                                                       'datatype' => 'integer',
00089                                                                       'default' => 1,
00090                                                                       'required' => true ),
00091                                          'last_read' => array( 'name' => 'LastRead',
00092                                                                'datatype' => 'integer',
00093                                                                'default' => 0,
00094                                                                'required' => true ),
00095                                          'created' => array( 'name' => 'Created',
00096                                                              'datatype' => 'integer',
00097                                                              'default' => 0,
00098                                                              'required' => true ),
00099                                          'modified' => array( 'name' => 'Modified',
00100                                                               'datatype' => 'integer',
00101                                                               'default' => 0,
00102                                                               'required' => true ) ),
00103                       'keys' => array( 'collaboration_id', 'participant_id' ),
00104                       'function_attributes' => array( 'collaboration_item' => 'collaborationItem',
00105                                                       'participant' => 'participant',
00106                                                       'participant_type_string' => 'participantTypeString',
00107                                                       'participant_role_string' => 'participantRoleString',
00108                                                       'is_builtin_type' => 'isBuiltinType',
00109                                                       'is_builtin_role' => 'isBuiltinRole' ),
00110                       'class_name' => 'eZCollaborationItemParticipantLink',
00111                       'name' => 'ezcollab_item_participant_link' );
00112     }
00113 
00114     static function create( $collaborationID, $participantID,
00115                       $participantRole = self::ROLE_STANDARD, $participantType = self::TYPE_USER )
00116     {
00117         $dateTime = time();
00118         $row = array(   'collaboration_id' => $collaborationID,
00119                         'participant_id' => $participantID,
00120                         'participant_role' => $participantRole,
00121                         'participant_type' => $participantType,
00122                         'created' => $dateTime,
00123                         'modified' => $dateTime );
00124         return new eZCollaborationItemParticipantLink( $row );
00125     }
00126 
00127     /*!
00128      \note transaction unsafe
00129      */
00130     static function setLastRead( $collaborationID, $userID = false, $timestamp = false )
00131     {
00132         if ( $userID === false )
00133         {
00134             //include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );
00135             $userID = eZUser::currentUserID();
00136         }
00137         if ( $timestamp === false )
00138         {
00139             $timestamp = time();
00140         }
00141         //include_once( 'lib/ezdb/classes/ezdb.php' );
00142         $db = eZDB::instance();
00143         $userID = (int) $userID;
00144         $timestamp = (int) $timestamp;
00145         $sql = "UPDATE ezcollab_item_participant_link set last_read='$timestamp'
00146                 WHERE  collaboration_id='$collaborationID' AND participant_id='$userID'";
00147         $db->query( $sql );
00148         if ( !empty( $GLOBALS["eZCollaborationItemParticipantLinkCache"][$collaborationID][$userID] ) )
00149             $GLOBALS["eZCollaborationItemParticipantLinkCache"][$collaborationID][$userID]->setAttribute( 'last_read', $timestamp );
00150     }
00151 
00152     static function fetch( $collaborationID, $participantID, $asObject = true )
00153     {
00154         if ( empty( $GLOBALS["eZCollaborationItemParticipantLinkCache"][$collaborationID][$participantID] ) )
00155         {
00156             $GLOBALS["eZCollaborationItemParticipantLinkCache"][$collaborationID][$participantID] =
00157                 eZPersistentObject::fetchObject( eZCollaborationItemParticipantLink::definition(),
00158                                                  null,
00159                                                  array( "collaboration_id" => $collaborationID,
00160                                                         'participant_id' => $participantID ),
00161                                                  $asObject );
00162         }
00163         return $GLOBALS["eZCollaborationItemParticipantLinkCache"][$collaborationID][$participantID];
00164     }
00165 
00166     static function fetchParticipantList( $parameters = array() )
00167     {
00168         $parameters = array_merge( array( 'as_object' => true,
00169                                           'item_id' => false,
00170                                           'offset' => false,
00171                                           'limit' => false,
00172                                           'sort_by' => false ),
00173                                    $parameters );
00174         $itemID = $parameters['item_id'];
00175         $asObject = $parameters['as_object'];
00176         $offset = $parameters['offset'];
00177         $limit = $parameters['limit'];
00178         $linkList = null;
00179         if ( !$offset and !$limit )
00180         {
00181             if ( !empty( $GLOBALS['eZCollaborationItemParticipantLinkListCache'] ) )
00182             {
00183                 return $GLOBALS['eZCollaborationItemParticipantLinkListCache'];
00184             }
00185         }
00186         $limitArray = null;
00187         if ( $offset and $limit )
00188             $limitArray = array( 'offset' => $offset, 'length' => $limit );
00189         $linkList = eZPersistentObject::fetchObjectList( eZCollaborationItemParticipantLink::definition(),
00190                                                           null,
00191                                                           array( "collaboration_id" => $itemID ),
00192                                                           null, $limitArray,
00193                                                           $asObject );
00194         foreach( $linkList as $linkItem )
00195         {
00196             if ( $asObject )
00197             {
00198                 $participantID = $linkItem->attribute( 'participant_id' );
00199             }
00200             else
00201             {
00202                 $participantID = $linkItem['participant_id'];
00203             }
00204             if ( empty( $GLOBALS["eZCollaborationItemParticipantLinkCache"][$itemID][$participantID] ) )
00205             {
00206                 $GLOBALS["eZCollaborationItemParticipantLinkCache"][$itemID][$participantID] = $linkItem;
00207             }
00208         }
00209         return $GLOBALS['eZCollaborationItemParticipantLinkListCache'] = $linkList;
00210     }
00211 
00212     static function fetchParticipantMap( $originalParameters = array() )
00213     {
00214         $parameters = array_merge( array( 'sort_field' => 'role' ),
00215                                    $originalParameters );
00216         $itemID = $parameters['item_id'];
00217         $sortField = $parameters['sort_field'];
00218         $list = eZCollaborationItemParticipantLink::fetchParticipantList( $originalParameters );
00219         if ( $list === null )
00220         {
00221             $listMap = null;
00222             return $listMap;
00223         }
00224 
00225         $listMap = array();
00226         foreach ( $list as $listItem )
00227         {
00228             $sortKey = null;
00229             if ( $sortField == 'role' )
00230             {
00231                 $sortKey = $listItem->attribute( 'participant_role' );
00232             }
00233             if ( $sortKey !== null )
00234             {
00235                 if ( !isset( $listMap[$sortKey] ) )
00236                 {
00237                     if ( $sortField == 'role' )
00238                     {
00239                         $sortName = eZCollaborationItemParticipantLink::roleName( $itemID, $sortKey );
00240                     }
00241                     $listMap[$sortKey] = array( 'name' => $sortName,
00242                                                 'items' => array() );
00243                 }
00244                 $listMap[$sortKey]['items'][] = $listItem;
00245             }
00246         }
00247         return $listMap;
00248     }
00249 
00250     static function typeString( $participantType )
00251     {
00252         if ( !isset( $GLOBALS['eZCollaborationParticipantTypeMap'] ) )
00253         {
00254             $GLOBALS['eZCollaborationParticipantTypeMap'] = array( self::TYPE_USER => 'user',
00255                                                                    self::TYPE_USERGROUP => 'usergroup' );
00256         }
00257         if ( isset( $GLOBALS['eZCollaborationParticipantTypeMap'][$participantType] ) )
00258         {
00259             return $GLOBALS['eZCollaborationParticipantTypeMap'][$participantType];
00260         }
00261         return null;
00262     }
00263 
00264     static function roleString( $participantRole )
00265     {
00266         if ( empty( $GLOBALS['eZCollaborationParticipantRoleMap'] ) )
00267         {
00268             $GLOBALS['eZCollaborationParticipantRoleMap'] =
00269                 array( self::ROLE_STANDARD => 'standard',
00270                        self::ROLE_OBSERVER => 'observer',
00271                        self::ROLE_OWNER => 'owner',
00272                        self::ROLE_APPROVER => 'approver',
00273                        self::ROLE_AUTHOR => 'author' );
00274         }
00275         $roleMap = $GLOBALS['eZCollaborationParticipantRoleMap'];
00276         if ( isset( $roleMap[$participantRole] ) )
00277         {
00278             return $roleMap[$participantRole];
00279         }
00280 
00281         return null;
00282     }
00283 
00284     static function roleName( $collaborationID, $roleID )
00285     {
00286         if ( $roleID < self::TYPE_CUSTOM )
00287         {
00288             if ( empty( $GLOBALS['eZCollaborationParticipantRoleNameMap'] ) )
00289             {
00290                 require_once( 'kernel/common/i18n.php' );
00291                 $GLOBALS['eZCollaborationParticipantRoleNameMap'] =
00292                     array( self::ROLE_STANDARD => ezi18n( 'kernel/classes', 'Standard' ),
00293                            self::ROLE_OBSERVER => ezi18n( 'kernel/classes', 'Observer' ),
00294                            self::ROLE_OWNER => ezi18n( 'kernel/classes', 'Owner' ),
00295                            self::ROLE_APPROVER => ezi18n( 'kernel/classes', 'Approver' ),
00296                            self::ROLE_AUTHOR => ezi18n( 'kernel/classes', 'Author' ) );
00297             }
00298             $roleNameMap = $GLOBALS['eZCollaborationParticipantRoleNameMap'];
00299             if ( isset( $roleNameMap[$roleID] ) )
00300             {
00301                 return $roleNameMap[$roleID];
00302             }
00303             return null;
00304         }
00305 
00306         $item = eZCollaborationItem::fetch( $collaborationID );
00307         return $item->handler()->roleName( $collaborationID, $roleID );
00308     }
00309 
00310     function collaborationItem()
00311     {
00312         //include_once( 'kernel/classes/ezcollaborationitem.php' );
00313         return eZCollaborationItem::fetch( $this->CollaborationID );
00314     }
00315 
00316     function participant()
00317     {
00318         if ( $this->ParticipantType == self::TYPE_USER )
00319         {
00320             //include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );
00321             return eZUser::fetch( $this->ParticipantID );
00322         }
00323         else if ( $this->ParticipantType == self::TYPE_USERGROUP )
00324         {
00325             //include_once( 'kernel/classes/ezcontentobject.php' );
00326             return eZContentObject::fetch( $this->ParticipantID );
00327         }
00328         return null;
00329     }
00330 
00331     function participantTypeString()
00332     {
00333         if ( $this->ParticipantType < self::TYPE_CUSTOM )
00334         {
00335             return  eZCollaborationItemParticipantLink::typeString( $this->ParticipantType );
00336         }
00337 
00338         $item = eZCollaborationItem::fetch( $this->CollaborationID );
00339         return $item->attribute( 'type_identifier' ) . '_' . $item->handler()->participantTypeString( $this->ParticipantType );
00340     }
00341 
00342     function participantRoleString()
00343     {
00344         if ( $this->ParticipantRole < self::ROLE_CUSTOM )
00345         {
00346             return  eZCollaborationItemParticipantLink::roleString( $this->ParticipantRole );
00347         }
00348 
00349         $item = eZCollaborationItem::fetch( $this->CollaborationID );
00350         return $item->attribute( 'type_identifier' ) . '_' . $item->handler()->participantRoleString( $this->ParticipantRole );
00351     }
00352 
00353     function isBuiltinType()
00354     {
00355         return $this->ParticipantType < self::TYPE_CUSTOM;
00356     }
00357 
00358     function isBuiltinRole()
00359     {
00360         return $this->ParticipantRole < self::ROLE_CUSTOM;
00361     }
00362 
00363     /// \privatesection
00364     public $CollaborationID;
00365     public $ParticipantID;
00366     public $ParticipantType;
00367     public $IsRead;
00368     public $IsActive;
00369     public $Created;
00370     public $Modified;
00371 }
00372 
00373 ?>