eZ Publish  [trunk]
eznotificationeventfilter.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZNotificationEventFilter class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package kernel
00009  */
00010 
00011 /*!
00012   \class eZNotificationEventFilter eznotificationeventfilter.php
00013   \brief The class eZNotificationEventFilter does
00014 
00015 */
00016 class eZNotificationEventFilter
00017 {
00018     /*!
00019      Constructor
00020     */
00021     function eZNotificationEventFilter()
00022     {
00023     }
00024 
00025     /*!
00026      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00027      the calls within a db transaction; thus within db->begin and db->commit.
00028      */
00029     static function process()
00030     {
00031         $limit = 100;
00032         $offset = 0;
00033         $availableHandlers = eZNotificationEventFilter::availableHandlers();
00034         do
00035         {
00036             $eventList = eZNotificationEvent::fetchUnhandledList( array( 'offset' => $offset, 'length' => $limit ) );
00037             foreach( $eventList as $event )
00038             {
00039                 $db = eZDB::instance();
00040                 $db->begin();
00041 
00042                 foreach( $availableHandlers as $handler )
00043                 {
00044                     if ( $handler === false )
00045                     {
00046                         eZDebug::writeError( "Notification handler does not exist: $handlerKey", __METHOD__ );
00047                     }
00048                     else
00049                     {
00050                         $handler->handle( $event );
00051                     }
00052                 }
00053                 $itemCountLeft = eZNotificationCollectionItem::fetchCountForEvent( $event->attribute( 'id' ) );
00054                 if ( $itemCountLeft == 0 )
00055                 {
00056                     $event->remove();
00057                 }
00058                 else
00059                 {
00060                     $event->setAttribute( 'status', eZNotificationEvent::STATUS_HANDLED );
00061                     $event->store();
00062                 }
00063 
00064                 $db->commit();
00065             }
00066             eZContentObject::clearCache();
00067         } while ( count( $eventList ) == $limit ); // If less than limit, we're on the last iteration
00068 
00069         eZNotificationCollection::removeEmpty();
00070     }
00071 
00072     static function availableHandlers()
00073     {
00074         $baseDirectory = eZExtension::baseDirectory();
00075         $notificationINI = eZINI::instance( 'notification.ini' );
00076         $availableHandlers = $notificationINI->variable( 'NotificationEventHandlerSettings', 'AvailableNotificationEventTypes' );
00077         $repositoryDirectories = array();
00078         $extensionDirectories = $notificationINI->variable( 'NotificationEventHandlerSettings', 'ExtensionDirectories' );
00079         foreach ( $extensionDirectories as $extensionDirectory )
00080         {
00081             $extensionPath = $baseDirectory . '/' . $extensionDirectory . '/notification/handler';
00082             if ( file_exists( $extensionPath ) )
00083                 $repositoryDirectories[] = $extensionPath;
00084         }
00085         $handlers = array();
00086         foreach( $availableHandlers as $handlerString )
00087         {
00088             $eventHandler = eZNotificationEventFilter::loadHandler( $repositoryDirectories, $handlerString );
00089             if ( is_object( $eventHandler ) )
00090                 $handlers[$handlerString] = $eventHandler;
00091         }
00092         return $handlers;
00093     }
00094 
00095     static function loadHandler( $directories, $handlerString )
00096     {
00097         $foundHandler = false;
00098         $includeFile = '';
00099 
00100         $baseDirectory = eZExtension::baseDirectory();
00101         $notificationINI = eZINI::instance( 'notification.ini' );
00102         $repositoryDirectories = $notificationINI->variable( 'NotificationEventHandlerSettings', 'RepositoryDirectories' );
00103         $extensionDirectories = $notificationINI->variable( 'NotificationEventHandlerSettings', 'ExtensionDirectories' );
00104         foreach ( $extensionDirectories as $extensionDirectory )
00105         {
00106             $extensionPath = "{$baseDirectory}/{$extensionDirectory}/notification/handler/";
00107             if ( file_exists( $extensionPath ) )
00108                 $repositoryDirectories[] = $extensionPath;
00109         }
00110 
00111         foreach ( $repositoryDirectories as $repositoryDirectory )
00112         {
00113             $repositoryDirectory = trim( $repositoryDirectory, '/' );
00114             $includeFile = "{$repositoryDirectory}/{$handlerString}/{$handlerString}handler.php";
00115             if ( file_exists( $includeFile ) )
00116             {
00117                 $foundHandler = true;
00118                 break;
00119             }
00120         }
00121         if ( !$foundHandler  )
00122         {
00123             eZDebug::writeError( "Notification handler does not exist: $handlerString", __METHOD__ );
00124             return false;
00125         }
00126         include_once( $includeFile );
00127         $className = $handlerString . "handler";
00128         return new $className();
00129     }
00130 
00131     /*!
00132      \static
00133      Goes through all event handlers and tells them to cleanup.
00134      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00135      the calls within a db transaction; thus within db->begin and db->commit.
00136     */
00137     static function cleanup()
00138     {
00139         $availableHandlers = eZNotificationEventFilter::availableHandlers();
00140 
00141         $db = eZDB::instance();
00142         $db->begin();
00143         foreach( $availableHandlers as $handler )
00144         {
00145             if ( $handler !== false )
00146             {
00147                 $handler->cleanup();
00148             }
00149         }
00150         $db->commit();
00151     }
00152 }
00153 
00154 ?>