00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 define( 'EZ_COLLABORATION_NOTIFICATION_HANDLER_ID', 'ezcollaboration' );
00041 define( 'EZ_COLLABORATION_NOTIFICATION_HANDLER_TRANSPORT', 'ezmail' );
00042
00043 include_once( 'kernel/classes/notification/eznotificationeventhandler.php' );
00044 include_once( 'kernel/classes/ezcollaborationitemhandler.php' );
00045 include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );
00046 include_once( 'kernel/classes/notification/eznotificationcollection.php' );
00047 include_once( 'kernel/classes/notification/eznotificationschedule.php' );
00048 include_once( 'kernel/classes/notification/handler/ezcollaborationnotification/ezcollaborationnotificationrule.php' );
00049
00050 class eZCollaborationNotificationHandler extends eZNotificationEventHandler
00051 {
00052
00053
00054
00055 function eZCollaborationNotificationHandler()
00056 {
00057 $this->eZNotificationEventHandler( EZ_COLLABORATION_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
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 for ( $i = 0; $i < count( $rules ); ++$i )
00101 {
00102 $rule =& $rules[$i];
00103 $selection[] = $rule->attribute( 'collab_identifier' );
00104 }
00105 return $selection;
00106 }
00107
00108 function handle( &$event )
00109 {
00110 eZDebugSetting::writeDebug( 'kernel-notification', $event, "trying to handle event" );
00111 if ( $event->attribute( 'event_type_string' ) == 'ezcollaboration' )
00112 {
00113 $parameters = array();
00114 $status = $this->handleCollaborationEvent( $event, $parameters );
00115 if ( $status == EZ_NOTIFICATIONEVENTHANDLER_EVENT_HANDLED )
00116 $this->sendMessage( $event, $parameters );
00117 else
00118 return false;
00119 }
00120 return true;
00121 }
00122
00123 function handleCollaborationEvent( &$event, &$parameters )
00124 {
00125 $collaborationItem =& $event->attribute( 'content' );
00126 if ( !$collaborationItem )
00127 return EZ_NOTIFICATIONEVENTHANDLER_EVENT_SKIPPED;
00128 $collaborationHandler =& $collaborationItem->attribute( 'handler' );
00129 return $collaborationHandler->handleCollaborationEvent( $event, $collaborationItem, $parameters );
00130 }
00131
00132 function sendMessage( &$event, $parameters )
00133 {
00134 $collections = eZNotificationCollection::fetchListForHandler( EZ_COLLABORATION_NOTIFICATION_HANDLER_ID,
00135 $event->attribute( 'id' ),
00136 EZ_COLLABORATION_NOTIFICATION_HANDLER_TRANSPORT );
00137 foreach ( array_keys( $collections ) as $collectionKey )
00138 {
00139 $collection =& $collections[$collectionKey];
00140 $items =& $collection->attribute( 'items_to_send' );
00141 $addressList = array();
00142 foreach ( array_keys( $items ) as $key )
00143 {
00144 $addressList[] = $items[$key]->attribute( 'address' );
00145 $items[$key]->remove();
00146 }
00147 $transport =& eZNotificationTransport::instance( 'ezmail' );
00148 $transport->send( $addressList, $collection->attribute( 'data_subject' ), $collection->attribute( 'data_text' ), null,
00149 $parameters );
00150 if ( $collection->attribute( 'item_count' ) == 0 )
00151 {
00152 $collection->remove();
00153 }
00154 }
00155 }
00156
00157 function &rules( $user = false )
00158 {
00159 if ( $user === false )
00160 {
00161 $user =& eZUser::currentUser();
00162 }
00163 $email = $user->attribute( 'email' );
00164
00165 $ruleList = eZCollaborationNotificationRule::fetchList( $email );
00166 return $ruleList;
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_' . EZ_COLLABORATION_NOTIFICATION_HANDLER_ID ) )
00176 $selection = $http->postVariable( 'CollaborationHandlerSelection_' . EZ_COLLABORATION_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
00191
00192 function cleanup()
00193 {
00194 eZCollaborationNotificationRule::cleanup();
00195 }
00196 }
00197
00198 ?>