eZ Publish  [4.0]
ezcollaborationnotificationhandler.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZCollaborationNotificationHandler class
00004 //
00005 // Created on: <09-Jul-2003 16:37:01 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 ezcollaborationnotificationhandler.php
00032 */
00033 
00034 /*!
00035   \class eZCollaborationNotificationHandler ezcollaborationnotificationhandler.php
00036   \brief The class eZCollaborationNotificationHandler does
00037 
00038 */
00039 
00040 //include_once( 'kernel/classes/notification/eznotificationeventhandler.php' );
00041 //include_once( 'kernel/classes/ezcollaborationitemhandler.php' );
00042 //include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );
00043 //include_once( 'kernel/classes/notification/eznotificationcollection.php' );
00044 //include_once( 'kernel/classes/notification/eznotificationschedule.php' );
00045 //include_once( 'kernel/classes/notification/handler/ezcollaborationnotification/ezcollaborationnotificationrule.php' );
00046 
00047 class eZCollaborationNotificationHandler extends eZNotificationEventHandler
00048 {
00049     const NOTIFICATION_HANDLER_ID = 'ezcollaboration';
00050     const TRANSPORT = 'ezmail';
00051 
00052     /*!
00053      Constructor
00054     */
00055     function eZCollaborationNotificationHandler()
00056     {
00057         $this->eZNotificationEventHandler( self::NOTIFICATION_HANDLER_ID, "Collaboration Handler" );
00058     }
00059 
00060     function attributes()
00061     {
00062         return array_merge( array( 'collaboration_handlers',
00063                                    'collaboration_selections' ),
00064                             eZNotificationEventHandler::attributes() );
00065     }
00066 
00067     function hasAttribute( $attr )
00068     {
00069         return in_array( $attr, $this->attributes() );
00070     }
00071 
00072     function attribute( $attr )
00073     {
00074         if ( $attr == 'collaboration_handlers' )
00075         {
00076             return $this->collaborationHandlers();
00077         }
00078         else if ( $attr == 'collaboration_selections' )
00079         {
00080             $selections = $this->collaborationSelections();
00081             return $selections;
00082         }
00083         return eZNotificationEventHandler::attribute( $attr );
00084     }
00085 
00086     /*!
00087      Returns the available collaboration handlers.
00088     */
00089     function collaborationHandlers()
00090     {
00091         return eZCollaborationItemHandler::fetchList();
00092     }
00093 
00094     /*!
00095     */
00096     function collaborationSelections()
00097     {
00098         $rules = eZCollaborationNotificationRule::fetchList();
00099         $selection = array();
00100         foreach( $rules as $rule )
00101         {
00102             $selection[] = $rule->attribute( 'collab_identifier' );
00103         }
00104         return $selection;
00105     }
00106 
00107     function handle( $event )
00108     {
00109         eZDebugSetting::writeDebug( 'kernel-notification', $event, "trying to handle event" );
00110         if ( $event->attribute( 'event_type_string' ) == 'ezcollaboration' )
00111         {
00112             $parameters = array();
00113             $status = $this->handleCollaborationEvent( $event, $parameters );
00114             if ( $status == eZNotificationEventHandler::EVENT_HANDLED )
00115                 $this->sendMessage( $event, $parameters );
00116             else
00117                 return false;
00118         }
00119         return true;
00120     }
00121 
00122     function handleCollaborationEvent( $event, &$parameters )
00123     {
00124         $collaborationItem = $event->attribute( 'content' );
00125         if ( !$collaborationItem )
00126             return eZNotificationEventHandler::EVENT_SKIPPED;
00127         $collaborationHandler = $collaborationItem->attribute( 'handler' );
00128         return $collaborationHandler->handleCollaborationEvent( $event, $collaborationItem, $parameters );
00129     }
00130 
00131     function sendMessage( $event, $parameters )
00132     {
00133         $collections = eZNotificationCollection::fetchListForHandler( self::NOTIFICATION_HANDLER_ID,
00134                                                                       $event->attribute( 'id' ),
00135                                                                       self::TRANSPORT );
00136         foreach ( $collections as $collection )
00137         {
00138             $items = $collection->attribute( 'items_to_send' );
00139             $addressList = array();
00140             foreach ( $items as $item )
00141             {
00142                 $addressList[] = $item->attribute( 'address' );
00143                 $item->remove();
00144             }
00145             $transport = eZNotificationTransport::instance( 'ezmail' );
00146             $transport->send( $addressList,
00147                               $collection->attribute( 'data_subject' ),
00148                               $collection->attribute( 'data_text' ),
00149                               null,
00150                               $parameters );
00151             if ( $collection->attribute( 'item_count' ) == 0 )
00152             {
00153                 $collection->remove();
00154             }
00155         }
00156     }
00157 
00158     function rules( $user = false )
00159     {
00160         if ( $user === false )
00161         {
00162             $user = eZUser::currentUser();
00163         }
00164         $email = $user->attribute( 'email' );
00165 
00166         return eZCollaborationNotificationRule::fetchList( $email );
00167     }
00168 
00169     function fetchHttpInput( $http, $module )
00170     {
00171         if ( $http->hasPostVariable( 'CollaborationHandlerSelection'  ) )
00172         {
00173             $oldSelection = $this->collaborationSelections();
00174             $selection = array();
00175             if ( $http->hasPostVariable( 'CollaborationHandlerSelection_' . self::NOTIFICATION_HANDLER_ID  ) )
00176                 $selection = $http->postVariable( 'CollaborationHandlerSelection_' . self::NOTIFICATION_HANDLER_ID );
00177             $createRules = array_diff( $selection, $oldSelection );
00178             $removeRules = array_diff( $oldSelection, $selection );
00179             if ( count( $removeRules ) > 0 )
00180                 eZCollaborationNotificationRule::removeByIdentifier( array( $removeRules ) );
00181             foreach ( $createRules as $createRule )
00182             {
00183                 $rule = eZCollaborationNotificationRule::create( $createRule );
00184                 $rule->store();
00185             }
00186         }
00187     }
00188 
00189     /*!
00190      \reimp
00191     */
00192     function cleanup()
00193     {
00194         eZCollaborationNotificationRule::cleanup();
00195     }
00196 }
00197 
00198 ?>