- Fixed #16160: Call to undefined function ezi18n()
[tinyz:tinyz.git] / kernel / classes / ezcollaborationitemparticipantlink.php
1 <?php
2 //
3 // Definition of eZCollaborationItemParticipantLink class
4 //
5 // Created on: <22-Jan-2003 16:08:22 amos>
6 //
7 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
8 // SOFTWARE NAME: eZ Publish
9 // SOFTWARE RELEASE: 4.1.x
10 // COPYRIGHT NOTICE: Copyright (C) 1999-2010 eZ Systems AS
11 // SOFTWARE LICENSE: GNU General Public License v2.0
12 // NOTICE: >
13 //   This program is free software; you can redistribute it and/or
14 //   modify it under the terms of version 2.0  of the GNU General
15 //   Public License as published by the Free Software Foundation.
16 //
17 //   This program is distributed in the hope that it will be useful,
18 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
19 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 //   GNU General Public License for more details.
21 //
22 //   You should have received a copy of version 2.0 of the GNU General
23 //   Public License along with this program; if not, write to the Free
24 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 //   MA 02110-1301, USA.
26 //
27 //
28 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
29 //
30
31 /*! \file
32 */
33
34 /*!
35   \class eZCollaborationItemParticipantLink ezcollaborationitemparticipantlink.php
36   \brief The class eZCollaborationItemParticipantLink does
37
38 */
39
40 class eZCollaborationItemParticipantLink extends eZPersistentObject
41 {
42     const TYPE_USER = 1;
43     const TYPE_USERGROUP = 2;
44
45     // Everything from 1024 and above is considered custom and is specific per collaboration handler.
46     const TYPE_CUSTOM = 1024;
47
48     const ROLE_STANDARD = 1;
49     const ROLE_OBSERVER = 2;
50     const ROLE_OWNER = 3;
51     const ROLE_APPROVER = 4;
52     const ROLE_AUTHOR = 5;
53
54     // Everything from 1024 and above is considered custom and is specific per collaboration handler.
55     const ROLE_CUSTOM = 1024;
56
57     /*!
58      Constructor
59     */
60     function eZCollaborationItemParticipantLink( $row )
61     {
62         $this->eZPersistentObject( $row );
63     }
64
65     static function definition()
66     {
67         return array( 'fields' => array( 'collaboration_id' => array( 'name' => 'CollaborationID',
68                                                                       'datatype' => 'integer',
69                                                                       'default' => 0,
70                                                                       'required' => true,
71                                                                       'foreign_class' => 'eZCollaborationItem',
72                                                                       'foreign_attribute' => 'id',
73                                                                       'multiplicity' => '1..*' ),
74                                          'participant_id' => array( 'name' => 'ParticipantID',
75                                                                     'datatype' => 'integer',
76                                                                     'default' => 0,
77                                                                     'required' => true,
78                                                                     'foreign_class' => 'eZContentObject',
79                                                                     'foreign_attribute' => 'id',
80                                                                     'multiplicity' => '1..*' ),
81                                          'participant_type' => array( 'name' => 'ParticipantType',
82                                                                       'datatype' => 'integer',
83                                                                       'default' => 1,
84                                                                       'required' => true ),
85                                          'participant_role' => array( 'name' => 'ParticipantRole',
86                                                                       'datatype' => 'integer',
87                                                                       'default' => 1,
88                                                                       'required' => true ),
89                                          'last_read' => array( 'name' => 'LastRead',
90                                                                'datatype' => 'integer',
91                                                                'default' => 0,
92                                                                'required' => true ),
93                                          'created' => array( 'name' => 'Created',
94                                                              'datatype' => 'integer',
95                                                              'default' => 0,
96                                                              'required' => true ),
97                                          'modified' => array( 'name' => 'Modified',
98                                                               'datatype' => 'integer',
99                                                               'default' => 0,
100                                                               'required' => true ) ),
101                       'keys' => array( 'collaboration_id', 'participant_id' ),
102                       'function_attributes' => array( 'collaboration_item' => 'collaborationItem',
103                                                       'participant' => 'participant',
104                                                       'participant_type_string' => 'participantTypeString',
105                                                       'participant_role_string' => 'participantRoleString',
106                                                       'is_builtin_type' => 'isBuiltinType',
107                                                       'is_builtin_role' => 'isBuiltinRole' ),
108                       'class_name' => 'eZCollaborationItemParticipantLink',
109                       'name' => 'ezcollab_item_participant_link' );
110     }
111
112     static function create( $collaborationID, $participantID,
113                       $participantRole = self::ROLE_STANDARD, $participantType = self::TYPE_USER )
114     {
115         $dateTime = time();
116         $row = array(   'collaboration_id' => $collaborationID,
117                         'participant_id' => $participantID,
118                         'participant_role' => $participantRole,
119                         'participant_type' => $participantType,
120                         'created' => $dateTime,
121                         'modified' => $dateTime );
122         return new eZCollaborationItemParticipantLink( $row );
123     }
124
125     /*!
126      \note transaction unsafe
127      */
128     static function setLastRead( $collaborationID, $userID = false, $timestamp = false )
129     {
130         if ( $userID === false )
131         {
132             $userID = eZUser::currentUserID();
133         }
134         if ( $timestamp === false )
135         {
136             $timestamp = time();
137         }
138         $db = eZDB::instance();
139         $userID = (int) $userID;
140         $timestamp = (int) $timestamp;
141         $sql = "UPDATE ezcollab_item_participant_link set last_read='$timestamp'
142                 WHERE  collaboration_id='$collaborationID' AND participant_id='$userID'";
143         $db->query( $sql );
144         if ( !empty( $GLOBALS["eZCollaborationItemParticipantLinkCache"][$collaborationID][$userID] ) )
145             $GLOBALS["eZCollaborationItemParticipantLinkCache"][$collaborationID][$userID]->setAttribute( 'last_read', $timestamp );
146     }
147
148     static function fetch( $collaborationID, $participantID, $asObject = true )
149     {
150         if ( empty( $GLOBALS["eZCollaborationItemParticipantLinkCache"][$collaborationID][$participantID] ) )
151         {
152             $GLOBALS["eZCollaborationItemParticipantLinkCache"][$collaborationID][$participantID] =
153                 eZPersistentObject::fetchObject( eZCollaborationItemParticipantLink::definition(),
154                                                  null,
155                                                  array( "collaboration_id" => $collaborationID,
156                                                         'participant_id' => $participantID ),
157                                                  $asObject );
158         }
159         return $GLOBALS["eZCollaborationItemParticipantLinkCache"][$collaborationID][$participantID];
160     }
161
162     static function fetchParticipantList( $parameters = array() )
163     {
164         $parameters = array_merge( array( 'as_object' => true,
165                                           'item_id' => false,
166                                           'offset' => false,
167                                           'limit' => false,
168                                           'sort_by' => false ),
169                                    $parameters );
170         $itemID = $parameters['item_id'];
171         $asObject = $parameters['as_object'];
172         $offset = $parameters['offset'];
173         $limit = $parameters['limit'];
174         $linkList = null;
175         if ( !$offset and !$limit )
176         {
177             if ( !empty( $GLOBALS['eZCollaborationItemParticipantLinkListCache'] ) )
178             {
179                 return $GLOBALS['eZCollaborationItemParticipantLinkListCache'];
180             }
181         }
182         $limitArray = null;
183         if ( $offset and $limit )
184             $limitArray = array( 'offset' => $offset, 'length' => $limit );
185         $linkList = eZPersistentObject::fetchObjectList( eZCollaborationItemParticipantLink::definition(),
186                                                           null,
187                                                           array( "collaboration_id" => $itemID ),
188                                                           null, $limitArray,
189                                                           $asObject );
190         foreach( $linkList as $linkItem )
191         {
192             if ( $asObject )
193             {
194                 $participantID = $linkItem->attribute( 'participant_id' );
195             }
196             else
197             {
198                 $participantID = $linkItem['participant_id'];
199             }
200             if ( empty( $GLOBALS["eZCollaborationItemParticipantLinkCache"][$itemID][$participantID] ) )
201             {
202                 $GLOBALS["eZCollaborationItemParticipantLinkCache"][$itemID][$participantID] = $linkItem;
203             }
204         }
205         return $GLOBALS['eZCollaborationItemParticipantLinkListCache'] = $linkList;
206     }
207
208     static function fetchParticipantMap( $originalParameters = array() )
209     {
210         $parameters = array_merge( array( 'sort_field' => 'role' ),
211                                    $originalParameters );
212         $itemID = $parameters['item_id'];
213         $sortField = $parameters['sort_field'];
214         $list = eZCollaborationItemParticipantLink::fetchParticipantList( $originalParameters );
215         if ( $list === null )
216         {
217             $listMap = null;
218             return $listMap;
219         }
220
221         $listMap = array();
222         foreach ( $list as $listItem )
223         {
224             $sortKey = null;
225             if ( $sortField == 'role' )
226             {
227                 $sortKey = $listItem->attribute( 'participant_role' );
228             }
229             if ( $sortKey !== null )
230             {
231                 if ( !isset( $listMap[$sortKey] ) )
232                 {
233                     if ( $sortField == 'role' )
234                     {
235                         $sortName = eZCollaborationItemParticipantLink::roleName( $itemID, $sortKey );
236                     }
237                     $listMap[$sortKey] = array( 'name' => $sortName,
238                                                 'items' => array() );
239                 }
240                 $listMap[$sortKey]['items'][] = $listItem;
241             }
242         }
243         return $listMap;
244     }
245
246     static function typeString( $participantType )
247     {
248         if ( !isset( $GLOBALS['eZCollaborationParticipantTypeMap'] ) )
249         {
250             $GLOBALS['eZCollaborationParticipantTypeMap'] = array( self::TYPE_USER => 'user',
251                                                                    self::TYPE_USERGROUP => 'usergroup' );
252         }
253         if ( isset( $GLOBALS['eZCollaborationParticipantTypeMap'][$participantType] ) )
254         {
255             return $GLOBALS['eZCollaborationParticipantTypeMap'][$participantType];
256         }
257         return null;
258     }
259
260     static function roleString( $participantRole )
261     {
262         if ( empty( $GLOBALS['eZCollaborationParticipantRoleMap'] ) )
263         {
264             $GLOBALS['eZCollaborationParticipantRoleMap'] =
265                 array( self::ROLE_STANDARD => 'standard',
266                        self::ROLE_OBSERVER => 'observer',
267                        self::ROLE_OWNER => 'owner',
268                        self::ROLE_APPROVER => 'approver',
269                        self::ROLE_AUTHOR => 'author' );
270         }
271         $roleMap = $GLOBALS['eZCollaborationParticipantRoleMap'];
272         if ( isset( $roleMap[$participantRole] ) )
273         {
274             return $roleMap[$participantRole];
275         }
276
277         return null;
278     }
279
280     static function roleName( $collaborationID, $roleID )
281     {
282         if ( $roleID < self::TYPE_CUSTOM )
283         {
284             if ( empty( $GLOBALS['eZCollaborationParticipantRoleNameMap'] ) )
285             {
286                 
287                 $GLOBALS['eZCollaborationParticipantRoleNameMap'] =
288                     array( self::ROLE_STANDARD => ezpI18n::translate( 'kernel/classes', 'Standard' ),
289                            self::ROLE_OBSERVER => ezpI18n::translate( 'kernel/classes', 'Observer' ),
290                            self::ROLE_OWNER => ezpI18n::translate( 'kernel/classes', 'Owner' ),
291                            self::ROLE_APPROVER => ezpI18n::translate( 'kernel/classes', 'Approver' ),
292                            self::ROLE_AUTHOR => ezpI18n::translate( 'kernel/classes', 'Author' ) );
293             }
294             $roleNameMap = $GLOBALS['eZCollaborationParticipantRoleNameMap'];
295             if ( isset( $roleNameMap[$roleID] ) )
296             {
297                 return $roleNameMap[$roleID];
298             }
299             return null;
300         }
301
302         $item = eZCollaborationItem::fetch( $collaborationID );
303         return $item->handler()->roleName( $collaborationID, $roleID );
304     }
305
306     function collaborationItem()
307     {
308         return eZCollaborationItem::fetch( $this->CollaborationID );
309     }
310
311     function participant()
312     {
313         if ( $this->ParticipantType == self::TYPE_USER )
314         {
315             return eZUser::fetch( $this->ParticipantID );
316         }
317         else if ( $this->ParticipantType == self::TYPE_USERGROUP )
318         {
319             return eZContentObject::fetch( $this->ParticipantID );
320         }
321         return null;
322     }
323
324     function participantTypeString()
325     {
326         if ( $this->ParticipantType < self::TYPE_CUSTOM )
327         {
328             return  eZCollaborationItemParticipantLink::typeString( $this->ParticipantType );
329         }
330
331         $item = eZCollaborationItem::fetch( $this->CollaborationID );
332         return $item->attribute( 'type_identifier' ) . '_' . $item->handler()->participantTypeString( $this->ParticipantType );
333     }
334
335     function participantRoleString()
336     {
337         if ( $this->ParticipantRole < self::ROLE_CUSTOM )
338         {
339             return  eZCollaborationItemParticipantLink::roleString( $this->ParticipantRole );
340         }
341
342         $item = eZCollaborationItem::fetch( $this->CollaborationID );
343         return $item->attribute( 'type_identifier' ) . '_' . $item->handler()->participantRoleString( $this->ParticipantRole );
344     }
345
346     function isBuiltinType()
347     {
348         return $this->ParticipantType < self::TYPE_CUSTOM;
349     }
350
351     function isBuiltinRole()
352     {
353         return $this->ParticipantRole < self::ROLE_CUSTOM;
354     }
355
356     /// \privatesection
357     public $CollaborationID;
358     public $ParticipantID;
359     public $ParticipantType;
360     public $IsRead;
361     public $IsActive;
362     public $Created;
363     public $Modified;
364 }
365
366 ?>