Subversion Repositories ezapprove2

[/] [ezapprove2/] [ezp4/] [trunk/] [extension/] [ezapprove2/] [collaboration/] [ezapprove2/] [ezapprove2collaborationhandler.php] - Blame information for rev 2508

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2487 tw
<?php
2
//
3
// Definition of eZApprove2CollaborationHandler class
4
//
5
// Created on: <13-Dec-2005 11:24:05 hovik>
6
//
7
// ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
8 2507 Cyberwolf
// SOFTWARE NAME: eZ Approve 2
9
// SOFTWARE RELEASE: 0.8.0
10
// COPYRIGHT NOTICE: Copyright (C) 2006-2008 eZ Systems AS
11 2487 tw
// 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 ezapprove2collaborationhandler.php
32
*/
33
 
34
/*!
35
  \class eZApprove2CollaborationHandler ezapprove2collaborationhandler.php
36
  \brief The class eZApprove2CollaborationHandler does
37
 
38
*/
39
 
40
/*!
41
  \class eZApprove2CollaborationHandler ezapprove2collaborationhandler.php
42
  \brief Handles approval communication using the collaboration system
43
 
44
  The handler uses the fields data_int1, data_int2 and data_int3 to store
45
  information on the contentobject and the approval status.
46
 
47
  - data_int1 - The content object ID
48
  - data_int2 - The content object version
49
  - data_int3 - The status of the approval, see defines.
50
 
51
*/
52
 
53
#include_once( 'kernel/classes/ezcollaborationitemhandler.php' );
54
#include_once( 'kernel/classes/ezcollaborationitem.php' );
55
#include_once( 'kernel/classes/ezcollaborationitemmessagelink.php' );
56
#include_once( 'kernel/classes/ezcollaborationitemparticipantlink.php' );
57
#include_once( 'kernel/classes/ezcollaborationitemgrouplink.php' );
58
#include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );
59
#include_once( 'kernel/classes/ezcollaborationprofile.php' );
60
#include_once( 'kernel/classes/ezcollaborationsimplemessage.php' );
61
#include_once( 'kernel/classes/ezcontentobjectversion.php' );
62
#include_once( 'kernel/common/i18n.php' );
63
 
64
#define( "EZ_COLLABORATION_MESSAGE_TYPE_APPROVE2", 1 );
65
/// Default status, no approval decision has been made
66
#define( "EZ_COLLABORATION_APPROVE2_STATUS_WAITING", 0 );
67
/// The contentobject was approved and will be published.
68
#define( "EZ_COLLABORATION_APPROVE2_STATUS_ACCEPTED", 1 );
69
/// The contentobject was denied and will be archived.
70
#define( "EZ_COLLABORATION_APPROVE2_STATUS_DENIED", 2 );
71
/// The contentobject was deferred and will be a draft again for reediting.
72
#define( "EZ_COLLABORATION_APPROVE2_STATUS_DEFERRED", 3 );
73
 
74
class eZApprove2CollaborationHandler extends eZCollaborationItemHandler
75
{
76
    /// Approval message type
77
    const MESSAGE_TYPE_APPROVE2 = 1;
78
 
79
    /// Default status, no approval decision has been made
80
    const STATUS_WAITING = 0;
81
 
82
    /// The contentobject was approved and will be published.
83
    const STATUS_ACCEPTED = 1;
84
 
85
    /// The contentobject was denied and will be archived.
86
    const STATUS_DENIED = 2;
87
 
88
    /// The contentobject was deferred and will be a draft again for reediting.
89
    const STATUS_DEFERRED = 3;
90 2507 Cyberwolf
 
91 2487 tw
    /*!
92
     Initializes the handler
93
    */
94
    function __construct()
95
    {
96
        $this->eZCollaborationItemHandler( 'ezapprove2',
97
                                           ezi18n( 'kernel/classes', 'Approval2' ),
98
                                           array( 'use-messages' => true,
99
                                                  'notification-types' => true,
100
                                                  'notification-collection-handling' => eZCollaborationItemHandler::NOTIFICATION_COLLECTION_PER_PARTICIPATION_ROLE ) );
101
    }
102
 
103
    /*!
104
     \reimp
105
    */
106 2508 Cyberwolf
    function title( $collaborationItem )
107 2487 tw
    {
108
        return ezi18n( 'kernel/classes', 'Approval v.2' );
109
    }
110
 
111
    /*!
112
     \reimp
113
    */
114
    function content( $collaborationItem )
115
    {
116
        $content = array( "content_object_id" => $collaborationItem->attribute( "data_int1" ),
117
                          "content_object_version" => $collaborationItem->attribute( "data_int2" ),
118
                          "approval_status" => $collaborationItem->attribute( "data_int3" ) );
119
        return $content;
120
    }
121
 
122
    function notificationParticipantTemplate( $participantRole )
123
    {
124
        if ( $participantRole == eZCollaborationItemParticipantLink::ROLE_APPROVER )
125
        {
126
            return 'approve.tpl';
127
        }
128
        else if ( $participantRole == eZCollaborationItemParticipantLink::ROLE_AUTHOR )
129
        {
130
            return 'author.tpl';
131
        }
132
        else
133
            return false;
134
    }
135
 
136
    /*!
137
     \return the content object version object for the collaboration item \a $collaborationItem
138
    */
139
    function contentObjectVersion( $collaborationItem )
140
    {
141
        $contentObjectID = $collaborationItem->contentAttribute( 'content_object_id' );
142
        $contentObjectVersion = $collaborationItem->contentAttribute( 'content_object_version' );
143
        return eZContentObjectVersion::fetchVersion( $contentObjectVersion, $contentObjectID );
144
    }
145
 
146
    /*!
147
     \reimp
148
     Updates the last_read for the participant link.
149
    */
150
    function readItem( $collaborationItem, $viewMode = false )
151
    {
152
        $collaborationItem->setLastRead();
153
    }
154
 
155
    /*!
156
     \reimp
157
     \return the number of messages for the approve item.
158
    */
159 2508 Cyberwolf
    function messageCount( $collaborationItem )
160 2487 tw
    {
161
        return eZCollaborationItemMessageLink::fetchItemCount( array( 'item_id' => $collaborationItem->attribute( 'id' ) ) );
162
    }
163
 
164
    /*!
165
     \reimp
166
     \return the number of unread messages for the approve item.
167
    */
168 2508 Cyberwolf
    function unreadMessageCount( $collaborationItem )
169 2487 tw
    {
170
        $lastRead = 0;
171
        $status = $collaborationItem->attribute( 'user_status' );
172
        if ( $status )
173
            $lastRead = $status->attribute( 'last_read' );
174
        return eZCollaborationItemMessageLink::fetchItemCount( array( 'item_id' => $collaborationItem->attribute( 'id' ),
175
                                                                      'conditions' => array( 'modified' => array( '>', $lastRead ) ) ) );
176
    }
177
 
178
    /*!
179
     \static
180
     \return the status of the approval collaboration item \a $approvalID.
181
    */
182
    static function checkApproval( $approvalID )
183
    {
184
        $collaborationItem = eZCollaborationItem::fetch( $approvalID );
185
        if ( $collaborationItem !== null )
186
        {
187
            return $collaborationItem->attribute( 'data_int3' );
188
        }
189
        return false;
190
    }
191
 
192
    /*!
193
     \static
194
     \return makes sure the approval item is activated for all participants \a $approvalID.
195
    */
196
    static function activateApproval( $approvalID )
197
    {
198
        $collaborationItem = eZCollaborationItem::fetch( $approvalID );
199
        if ( $collaborationItem !== null )
200
        {
201
            $collaborationItem->setAttribute( 'data_int3', eZCollaborationApprove2::STATUS_WAITING );
202
            $collaborationItem->setAttribute( 'status', eZCollaborationItem::STATUS_ACTIVE );
203
            $timestamp = time();
204
            $collaborationItem->setAttribute( 'modified', $timestamp );
205
            $collaborationItem->store();
206
            $participantList = eZCollaborationItemParticipantLink::fetchParticipantList( array( 'item_id' => $approvalID ) );
207
            foreach( $participantList as $participantLink )
208
            {
209
                $collaborationItem->setIsActive( true, $participantLink->attribute( 'participant_id' ) );
210
            }
211
            return true;
212
        }
213
        return false;
214
    }
215
 
216
    /*!
217
     Creates a new approval collaboration item which will approve the content object \a $contentObjectID
218
     with version \a $contentObjectVersion.
219
     The item will be added to the author \a $authorID and the approver \a $approverID.
220
     \return the collaboration item.
221
    */
222
    static function createApproval( $contentObjectID, $contentObjectVersion, $authorID, $approverIDList, $collaborationID = false )
223
    {
224
        $createNotification = false;
225
        $collaborationItem = false;
226
        $participantList = array();
227 2507 Cyberwolf
 
228 2487 tw
        if ( $collaborationID === false )
229
        {
230
            $collaborationItem = eZCollaborationItem::create( 'ezapprove2', $authorID );
231
            $collaborationItem->setAttribute( 'data_int1', $contentObjectID );
232
            $collaborationItem->setAttribute( 'data_int2', $contentObjectVersion );
233
            $collaborationItem->setAttribute( 'data_int3', false );
234
            $collaborationItem->store();
235
            $createNotification = true;
236
            $collaborationID = $collaborationItem->attribute( 'id' );
237
 
238
            $participantList[] = array( 'id'   => $authorID,
239
                                        'role' => eZCollaborationItemParticipantLink::ROLE_AUTHOR );
240
        }
241
 
242
        foreach( $approverIDList as $approverID )
243
        {
244
            $participantList[] = array( 'id'   => $approverID,
245
                                        'role' => eZCollaborationItemParticipantLink::ROLE_APPROVER );
246
        }
247
 
248
        // #HACK# ???
249
        foreach ( $participantList as $participantItem )
250
        {
251
            $participantID = $participantItem['id'];
252
            $participantRole = $participantItem['role'];
253
            $link = eZCollaborationItemParticipantLink::create( $collaborationID, $participantID,
254
                                                                $participantRole, eZCollaborationItemParticipantLink::TYPE_USER );
255
            $link->store();
256
 
257
            $profile = eZCollaborationProfile::instance( $participantID );
258
            $groupID = $profile->attribute( 'main_group' );
259
            eZCollaborationItemGroupLink::addItem( $groupID, $collaborationID, $participantID );
260
        }
261
 
262
        // Create the notification
263
        if ( $createNotification )
264
        {
265
            $collaborationItem->createNotificationEvent();
266
        }
267
        return $collaborationItem;
268
    }
269
 
270
    /*!
271
     \reimp
272
     Adds a new comment, approves the item or denies the item.
273
    */
274 2508 Cyberwolf
    function handleCustomAction( $module, $collaborationItem )
275 2487 tw
    {
276
        $redirectView = 'item';
277
        $redirectParameters = array( 'full', $collaborationItem->attribute( 'id' ) );
278
        $addComment = false;
279
 
280
        if ( $this->isCustomAction( 'Comment' ) )
281
        {
282
            $addComment = true;
283
        }
284
        else if ( $this->isCustomAction( 'Accept' ) ||
285
                  $this->isCustomAction( 'Deny' ) )
286
        {
287
            #include_once( eZExtension::baseDirectory() . '/ezapprove2/classes/ezxapprovestatus.php' );
288
 
289
            $approveStatus = eZXApproveStatus::fetchByCollaborationItem( $collaborationItem->attribute( 'id' ) );
290
            if ( !$approveStatus )
291
            {
292
                eZDebug::writeError( 'Could not fetch approve status for collaboration id : ' . $collaborationItem->attribute( 'id' ) );
293
                return false;
294
            }
295
            $approveStatusUserLink = eZXApproveStatusUserLink::fetchByUserID( eZUser::currentUserID(),
296
                                                                              $approveStatus->attribute( 'id' ),
297
                                                                              eZXApproveStatusUserLink::RoleApprover );
298
            if ( !$approveStatusUserLink )
299
            {
300
                eZDebug::writeDebug( 'User is not approver for approve status : ' . $approveStatus->attribute( 'id' ) . ', user ID : ' . eZUser::currentUserID() );
301
                return false;
302
            }
303
 
304
            $contentObjectVersion = $this->contentObjectVersion( $collaborationItem );
305
            if ( $this->isCustomAction( 'Accept' ) )
306
            {
307
                $approveStatusUserLink->setAttribute( 'approve_status', eZXApproveStatusUserLink::StatusApproved );
308
            }
309
            else if ( $this->isCustomAction( 'Deny' ) )
310
            {
311
                $collaborationItem->setIsActive( false );
312
                $collaborationItem->setAttribute( 'status', eZCollaborationItem::STATUS_INACTIVE );
313
                $approveStatusUserLink->setAttribute( 'approve_status', eZXApproveStatusUserLink::StatusDiscarded );
314
            }
315
            $approveStatusUserLink->sync();
316
 
317
            $redirectView = 'view';
318
            $redirectParameters = array( 'summary' );
319
            $addComment = true;
320
        }
321
        else if ( $this->isCustomAction( 'Edit' ) )
322
        {
323
            #include_once( eZExtension::baseDirectory() . '/ezapprove2/classes/ezxapprovestatus.php' );
324
 
325
            $approveStatus = eZXApproveStatus::fetchByCollaborationItem( $collaborationItem->attribute( 'id' ) );
326
            $contentObject = $approveStatus->attribute( 'contentobject' );
327
 
328
            // Check if user can edit object
329
            if ( !$contentObject ||
330
                 !$contentObject->attribute( 'can_edit' ) )
331
            {
332
                eZDebug::writeError( 'insufficient access to edit object : ' . $contentObject->attribute( 'id' ) );
333
                return false;
334
            }
335
 
336
            // 1. Lock workflow, and abort all previous pending elements.
337
            $db = eZDB::instance();
338
            if ( !$approveStatus )
339
            {
340
                eZDebug::writeError( 'Could not fetch approve status for collaboration id : ' . $collaborationItem->attribute( 'id' ) );
341
                return false;
342
            }
343
            $db->begin();
344
            $approveStatus->cancel();
345
 
346
            // 2. Create new version based in the pending one.
347
            $newVersion = $contentObject->createNewVersion( $approveStatus->attribute( 'active_version' ) );
348 2505 zabbie
            // If the Object has never been published we have to copy the node
349
            // assignment(s) acorss from the initial version to avoid the user
350
            // being prompted for a location when publishing.
351
            //
352
            // It would appear that isn't done in the model for non published items.
353
            if ($contentObject->attribute('published') == 0)
354
            {
355
              $version = $contentObject->attribute('current');
356
              $nodeAssignmentList = $version->attribute( 'node_assignments' );
357
              $contentObjectID = $contentObject->attribute('id');
358
              $newVersionNumber = $newVersion->attribute('version');
359
              foreach ( array_keys( $nodeAssignmentList ) as $key )
360
              {
361
                  $nodeAssignment = $nodeAssignmentList[$key];
362
                  $clonedAssignment = $nodeAssignment->cloneNodeAssignment( $newVersionNumber, $contentObjectID );
363
                  $clonedAssignment->store();
364
              }
365
            }
366 2487 tw
 
367
            // 3. Set pending version to rejected.
368
            $oldVersion = $approveStatus->attribute( 'object_version' );
369
            $oldVersion->setAttribute( 'status', eZContentObjectVersion::STATUS_REJECTED );
370
            $oldVersion->sync();
371
 
372
            // Abort collaboration item. Added by KK
373
            $collaborationItem->setIsActive( false );
374
            $collaborationItem->setAttribute( 'status', eZCollaborationItem::STATUS_INACTIVE );
375
 
376
            $db->commit();
377
 
378
            // 4. Redirect user to new object.
379
            return $module->redirect( 'content', 'edit', array( $contentObject->attribute( 'id' ),
380
                                                                $newVersion->attribute( 'version' ) ) );
381
        }
382
        else if ( $this->isCustomAction( 'AddApprover' ) )
383
        {
384
            #include_once( eZExtension::baseDirectory() . '/ezapprove2/classes/ezxapprovestatus.php' );
385
 
386
            $approveStatus = eZXApproveStatus::fetchByCollaborationItem( $collaborationItem->attribute( 'id' ) );
387
            if ( !$approveStatus )
388
            {
389
                eZDebug::writeError( 'Could not find eZXApproveStatus, ' . $collaborationItem->attribute( 'id' ) );
390
                return $Module->handleError( eZError::KERNEL_NOT_AVAILABLE, 'kernel' );
391
            }
392
 
393
            return $module->redirect( 'ezapprove2', 'add_approver', array( $approveStatus->attribute( 'id' ) ) );
394
        }
395
 
396
        if ( $addComment )
397
        {
398
            $messageText = $this->customInput( 'ApproveComment' );
399
            if ( trim( $messageText ) != '' )
400
            {
401
                $message = eZCollaborationSimpleMessage::create( 'ezapprove2_comment', $messageText );
402
                $message->store();
403
                eZCollaborationItemMessageLink::addMessage( $collaborationItem, $message, eZApprove2CollaborationHandler::MESSAGE_TYPE_APPROVE2 );
404
            }
405
        }
406
 
407
        $collaborationItem->setAttribute( 'modified', time() );
408
        $collaborationItem->sync();
409
        return $module->redirectToView( $redirectView, $redirectParameters );
410
    }
411
}
412
?>