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 include_once( 'kernel/classes/notification/eznotificationevent.php' );
00040 class eZNotificationEventFilter
00041 {
00042
00043
00044
00045 function eZNotificationEventFilter()
00046 {
00047 }
00048
00049
00050
00051
00052
00053 function process()
00054 {
00055 $eventList = eZNotificationEvent::fetchUnhandledList();
00056 $availableHandlers =& eZNotificationEventFilter::availableHandlers();
00057 foreach( array_keys( $eventList ) as $key )
00058 {
00059 $event =& $eventList[$key];
00060 foreach( array_keys( $availableHandlers ) as $handlerKey )
00061 {
00062 $handler =& $availableHandlers[$handlerKey];
00063 if ( $handler === false )
00064 {
00065 eZDebug::writeError( "Notification handler does not exist: $handlerKey", 'eZNotificationEventFilter::process()' );
00066 }
00067 else
00068 {
00069 $handler->handle( $event );
00070 }
00071 }
00072 $itemCountLeft = eZNotificationCollectionItem::fetchCountForEvent( $event->attribute( 'id' ) );
00073 if ( $itemCountLeft == 0 )
00074 {
00075 $event->remove();
00076 }
00077 else
00078 {
00079 $event->setAttribute( 'status', EZ_NOTIFICATIONEVENT_STATUS_HANDLED );
00080 $event->store();
00081 }
00082 }
00083 eZNotificationCollection::removeEmpty();
00084 }
00085
00086 function &availableHandlers()
00087 {
00088 include_once( 'lib/ezutils/classes/ezextension.php' );
00089 $baseDirectory = eZExtension::baseDirectory();
00090 $notificationINI =& eZINI::instance( 'notification.ini' );
00091 $availableHandlers = $notificationINI->variable( 'NotificationEventHandlerSettings', 'AvailableNotificationEventTypes' );
00092 $repositoryDirectories = array();
00093 $extensionDirectories = $notificationINI->variable( 'NotificationEventHandlerSettings', 'ExtensionDirectories' );
00094 foreach ( $extensionDirectories as $extensionDirectory )
00095 {
00096 $extensionPath = $baseDirectory . '/' . $extensionDirectory . '/notification/handler';
00097 if ( file_exists( $extensionPath ) )
00098 $repositoryDirectories[] = $extensionPath;
00099 }
00100 $handlers = array();
00101 foreach( $availableHandlers as $handlerString )
00102 {
00103 $eventHandler = eZNotificationEventFilter::loadHandler( $repositoryDirectories, $handlerString );
00104 if ( is_object( $eventHandler ) )
00105 $handlers[$handlerString] = $eventHandler;
00106 }
00107 return $handlers;
00108 }
00109
00110 function loadHandler( $directories, $handlerString )
00111 {
00112 $foundHandler = false;
00113 $includeFile = '';
00114
00115
00116 include_once( 'lib/ezutils/classes/ezextension.php' );
00117 $baseDirectory = eZExtension::baseDirectory();
00118 $notificationINI =& eZINI::instance( 'notification.ini' );
00119 $repositoryDirectories = $notificationINI->variable( 'NotificationEventHandlerSettings', 'RepositoryDirectories' );
00120 $extensionDirectories = $notificationINI->variable( 'NotificationEventHandlerSettings', 'ExtensionDirectories' );
00121 foreach ( $extensionDirectories as $extensionDirectory )
00122 {
00123 $extensionPath = "{$baseDirectory}/{$extensionDirectory}/notification/handler/";
00124 if ( file_exists( $extensionPath ) )
00125 $repositoryDirectories[] = $extensionPath;
00126 }
00127
00128 foreach ( $repositoryDirectories as $repositoryDirectory )
00129 {
00130 $repositoryDirectory = trim( $repositoryDirectory, '/' );
00131 $includeFile = "{$repositoryDirectory}/{$handlerString}/{$handlerString}handler.php";
00132 if ( file_exists( $includeFile ) )
00133 {
00134 $foundHandler = true;
00135 break;
00136 }
00137 }
00138 if ( !$foundHandler )
00139 {
00140 eZDebug::writeError( "Notification handler does not exist: $handlerString", 'eZNotificationEventFilter::loadHandler()' );
00141 return false;
00142 }
00143 include_once( $includeFile );
00144 $className = $handlerString . "handler";
00145 return new $className();
00146 }
00147
00148
00149
00150
00151
00152
00153
00154 function cleanup()
00155 {
00156 $availableHandlers =& eZNotificationEventFilter::availableHandlers();
00157
00158 $db =& eZDB::instance();
00159 $db->begin();
00160 foreach( array_keys( $availableHandlers ) as $handlerKey )
00161 {
00162 $handler =& $availableHandlers[$handlerKey];
00163 if ( $handler !== false )
00164 {
00165 $handler->cleanup();
00166 }
00167 }
00168 $db->commit();
00169 }
00170 }
00171
00172 ?>