eZ Publish  [4.0]
ezapprovecollaborationhandler.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZApproveCollaborationHandler class
00004 //
00005 // Created on: <23-Jan-2003 11:57:11 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 ezapprovecollaborationhandler.php
00032 */
00033 
00034 /*!
00035   \class eZApproveCollaborationHandler ezapprovecollaborationhandler.php
00036   \brief Handles approval communication using the collaboration system
00037 
00038   The handler uses the fields data_int1, data_int2 and data_int3 to store
00039   information on the contentobject and the approval status.
00040 
00041   - data_int1 - The content object ID
00042   - data_int2 - The content object version
00043   - data_int3 - The status of the approval, see defines.
00044 
00045 */
00046 
00047 //include_once( 'kernel/classes/ezcollaborationitemhandler.php' );
00048 //include_once( 'kernel/classes/ezcollaborationitem.php' );
00049 //include_once( 'kernel/classes/ezcollaborationitemmessagelink.php' );
00050 //include_once( 'kernel/classes/ezcollaborationitemparticipantlink.php' );
00051 //include_once( 'kernel/classes/ezcollaborationitemgrouplink.php' );
00052 //include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );
00053 //include_once( 'kernel/classes/ezcollaborationprofile.php' );
00054 //include_once( 'kernel/classes/ezcollaborationsimplemessage.php' );
00055 //include_once( 'kernel/classes/ezcontentobjectversion.php' );
00056 require_once( 'kernel/common/i18n.php' );
00057 
00058 class eZApproveCollaborationHandler extends eZCollaborationItemHandler
00059 {
00060     /// Approval message type
00061     const MESSAGE_TYPE_APPROVE = 1;
00062 
00063     /// Default status, no approval decision has been made
00064     const STATUS_WAITING = 0;
00065 
00066     /// The contentobject was approved and will be published.
00067     const STATUS_ACCEPTED = 1;
00068 
00069     /// The contentobject was denied and will be archived.
00070     const STATUS_DENIED = 2;
00071 
00072     /// The contentobject was deferred and will be a draft again for reediting.
00073     const STATUS_DEFERRED = 3;
00074 
00075     /*!
00076      Initializes the handler
00077     */
00078     function eZApproveCollaborationHandler()
00079     {
00080         $this->eZCollaborationItemHandler( 'ezapprove',
00081                                            ezi18n( 'kernel/classes', 'Approval' ),
00082                                            array( 'use-messages' => true,
00083                                                   'notification-types' => true,
00084                                                   'notification-collection-handling' => eZCollaborationItemHandler::NOTIFICATION_COLLECTION_PER_PARTICIPATION_ROLE ) );
00085     }
00086 
00087     /*!
00088      \reimp
00089     */
00090     function title( $collaborationItem )
00091     {
00092         return ezi18n( 'kernel/classes', 'Approval' );
00093     }
00094 
00095     /*!
00096      \reimp
00097     */
00098     function content( $collaborationItem )
00099     {
00100         return array( "content_object_id" => $collaborationItem->attribute( "data_int1" ),
00101                       "content_object_version" => $collaborationItem->attribute( "data_int2" ),
00102                       "approval_status" => $collaborationItem->attribute( "data_int3" ) );
00103     }
00104 
00105     function notificationParticipantTemplate( $participantRole )
00106     {
00107         if ( $participantRole == eZCollaborationItemParticipantLink::ROLE_APPROVER )
00108         {
00109             return 'approve.tpl';
00110         }
00111         else if ( $participantRole == eZCollaborationItemParticipantLink::ROLE_AUTHOR )
00112         {
00113             return 'author.tpl';
00114         }
00115         else
00116             return false;
00117     }
00118 
00119     /*!
00120      \return the content object version object for the collaboration item \a $collaborationItem
00121     */
00122     static function contentObjectVersion( $collaborationItem )
00123     {
00124         $contentObjectID = $collaborationItem->contentAttribute( 'content_object_id' );
00125         $contentObjectVersion = $collaborationItem->contentAttribute( 'content_object_version' );
00126         return eZContentObjectVersion::fetchVersion( $contentObjectVersion, $contentObjectID );
00127     }
00128 
00129     /*!
00130      \reimp
00131      Updates the last_read for the participant link.
00132     */
00133     function readItem( $collaborationItem, $viewMode = false )
00134     {
00135         $collaborationItem->setLastRead();
00136     }
00137 
00138     /*!
00139      \reimp
00140      \return the number of messages for the approve item.
00141     */
00142     function messageCount( $collaborationItem )
00143     {
00144         return eZCollaborationItemMessageLink::fetchItemCount( array( 'item_id' => $collaborationItem->attribute( 'id' ) ) );
00145     }
00146 
00147     /*!
00148      \reimp
00149      \return the number of unread messages for the approve item.
00150     */
00151     function unreadMessageCount( $collaborationItem )
00152     {
00153         $lastRead = 0;
00154         $status = $collaborationItem->attribute( 'user_status' );
00155         if ( $status )
00156             $lastRead = $status->attribute( 'last_read' );
00157         return eZCollaborationItemMessageLink::fetchItemCount( array( 'item_id' => $collaborationItem->attribute( 'id' ),
00158                                                                       'conditions' => array( 'modified' => array( '>', $lastRead ) ) ) );
00159     }
00160 
00161     /*!
00162      \static
00163      \return the status of the approval collaboration item \a $approvalID.
00164     */
00165     static function checkApproval( $approvalID )
00166     {
00167         $collaborationItem = eZCollaborationItem::fetch( $approvalID );
00168         if ( $collaborationItem !== null )
00169         {
00170             return $collaborationItem->attribute( 'data_int3' );
00171         }
00172         return false;
00173     }
00174 
00175     /*!
00176      \static
00177      \return makes sure the approval item is activated for all participants \a $approvalID.
00178     */
00179     static function activateApproval( $approvalID )
00180     {
00181         $collaborationItem = eZCollaborationItem::fetch( $approvalID );
00182         if ( $collaborationItem !== null )
00183         {
00184             $collaborationItem->setAttribute( 'data_int3', self::STATUS_WAITING );
00185             $collaborationItem->setAttribute( 'status', eZCollaborationItem::STATUS_ACTIVE );
00186             $timestamp = time();
00187             $collaborationItem->setAttribute( 'modified', $timestamp );
00188             $collaborationItem->store();
00189             $participantList = eZCollaborationItemParticipantLink::fetchParticipantList( array( 'item_id' => $approvalID ) );
00190             foreach( $participantList as $participantLink )
00191             {
00192                 $collaborationItem->setIsActive( true, $participantLink->attribute( 'participant_id' ) );
00193             }
00194             return true;
00195         }
00196         return false;
00197     }
00198 
00199     /*!
00200      Creates a new approval collaboration item which will approve the content object \a $contentObjectID
00201      with version \a $contentObjectVersion.
00202      The item will be added to the author \a $authorID and the approver array \a $approverIDArray.
00203      \return the collaboration item.
00204     */
00205     static function createApproval( $contentObjectID, $contentObjectVersion, $authorID, $approverIDArray )
00206     {
00207         $collaborationItem = eZCollaborationItem::create( 'ezapprove', $authorID );
00208         $collaborationItem->setAttribute( 'data_int1', $contentObjectID );
00209         $collaborationItem->setAttribute( 'data_int2', $contentObjectVersion );
00210         $collaborationItem->setAttribute( 'data_int3', false );
00211         $collaborationItem->store();
00212         $collaborationID = $collaborationItem->attribute( 'id' );
00213 
00214         $participantList = array( array( 'id' => array( $authorID ),
00215                                          'role' => eZCollaborationItemParticipantLink::ROLE_AUTHOR ),
00216                                   array( 'id' => $approverIDArray,
00217                                          'role' => eZCollaborationItemParticipantLink::ROLE_APPROVER ) );
00218         foreach ( $participantList as $participantItem )
00219         {
00220             foreach( $participantItem['id'] as $participantID )
00221             {
00222                 $participantRole = $participantItem['role'];
00223                 $link = eZCollaborationItemParticipantLink::create( $collaborationID, $participantID,
00224                                                                     $participantRole, eZCollaborationItemParticipantLink::TYPE_USER );
00225                 $link->store();
00226 
00227                 $profile = eZCollaborationProfile::instance( $participantID );
00228                 $groupID = $profile->attribute( 'main_group' );
00229                 eZCollaborationItemGroupLink::addItem( $groupID, $collaborationID, $participantID );
00230             }
00231         }
00232 
00233         // Create the notification
00234         $collaborationItem->createNotificationEvent();
00235         return $collaborationItem;
00236     }
00237 
00238     /*!
00239      \reimp
00240      Adds a new comment, approves the item or denies the item.
00241     */
00242     function handleCustomAction( $module, $collaborationItem )
00243     {
00244         $redirectView = 'item';
00245         $redirectParameters = array( 'full', $collaborationItem->attribute( 'id' ) );
00246         $addComment = false;
00247 
00248         if ( $this->isCustomAction( 'Comment' ) )
00249         {
00250             $addComment = true;
00251         }
00252         else if ( $this->isCustomAction( 'Accept' ) or
00253                   $this->isCustomAction( 'Deny' ) or
00254                   $this->isCustomAction( 'Defer' ) )
00255         {
00256             // check user's rights to approve
00257             $user = eZUser::currentUser();
00258             $userID = $user->attribute( 'contentobject_id' );
00259             $participantList = eZCollaborationItemParticipantLink::fetchParticipantList( array( 'item_id' => $collaborationItem->attribute( 'id' ) ) );
00260 
00261             $approveAllowed = false;
00262             foreach( $participantList as $participant )
00263             {
00264                 if ( $participant->ParticipantID == $userID &&
00265                      $participant->ParticipantRole == eZCollaborationItemParticipantLink::ROLE_APPROVER )
00266                 {
00267                     $approveAllowed = true;
00268                     break;
00269                 }
00270             }
00271             if ( !$approveAllowed )
00272             {
00273                 return $module->redirectToView( $redirectView, $redirectParameters );
00274             }
00275 
00276             $contentObjectVersion = $this->contentObjectVersion( $collaborationItem );
00277             $status = self::STATUS_DENIED;
00278             if ( $this->isCustomAction( 'Accept' ) )
00279                 $status = self::STATUS_ACCEPTED;
00280 //             else if ( $this->isCustomAction( 'Defer' ) )
00281 //                 $status = self::STATUS_DEFERRED;
00282 //             else if ( $this->isCustomAction( 'Deny' ) )
00283 //                 $status = self::STATUS_DENIED;
00284             else if ( $this->isCustomAction( 'Defer' ) or
00285                       $this->isCustomAction( 'Deny' ) )
00286                 $status = self::STATUS_DENIED;
00287             $collaborationItem->setAttribute( 'data_int3', $status );
00288             $collaborationItem->setAttribute( 'status', eZCollaborationItem::STATUS_INACTIVE );
00289             $timestamp = time();
00290             $collaborationItem->setAttribute( 'modified', $timestamp );
00291             $collaborationItem->setIsActive( false );
00292             $redirectView = 'view';
00293             $redirectParameters = array( 'summary' );
00294             $addComment = true;
00295         }
00296         if ( $addComment )
00297         {
00298             $messageText = $this->customInput( 'ApproveComment' );
00299             if ( trim( $messageText ) != '' )
00300             {
00301                 $message = eZCollaborationSimpleMessage::create( 'ezapprove_comment', $messageText );
00302                 $message->store();
00303                 eZCollaborationItemMessageLink::addMessage( $collaborationItem, $message, self::MESSAGE_TYPE_APPROVE );
00304             }
00305         }
00306         $collaborationItem->sync();
00307         return $module->redirectToView( $redirectView, $redirectParameters );
00308     }
00309 
00310 }
00311 
00312 ?>