eZ Publish  [4.0]
ezgeneraldigesthandler.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZGeneralDigestHandler class
00004 //
00005 // Created on: <16-May-2003 10:55:24 sp>
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 ezgeneraldigesthandler.php
00032 */
00033 
00034 /*!
00035   \class eZGeneralDigestHandler ezgeneraldigesthandler.php
00036   \brief The class eZGeneralDigestHandler does
00037 
00038 */
00039 //include_once( 'kernel/classes/notification/eznotificationeventhandler.php' );
00040 //include_once( 'kernel/classes/notification/eznotificationcollection.php' );
00041 //include_once( 'kernel/classes/notification/handler/ezgeneraldigest/ezgeneraldigestusersettings.php' );
00042 
00043 class eZGeneralDigestHandler extends eZNotificationEventHandler
00044 {
00045     const NOTIFICATION_HANDLER_ID = 'ezgeneraldigest';
00046 
00047     /*!
00048      Constructor
00049     */
00050     function eZGeneralDigestHandler()
00051     {
00052         $this->eZNotificationEventHandler( self::NOTIFICATION_HANDLER_ID, "General Digest Handler" );
00053 
00054     }
00055 
00056     function attributes()
00057     {
00058         return array_merge( array( 'settings',
00059                                    'all_week_days',
00060                                    'all_month_days',
00061                                    'available_hours' ),
00062                             eZNotificationEventHandler::attributes() );
00063     }
00064 
00065     function hasAttribute( $attr )
00066     {
00067         return in_array( $attr, $this->attributes() );
00068     }
00069 
00070     function attribute( $attr )
00071     {
00072         if ( $attr == 'settings' )
00073         {
00074             return $this->settings( eZUser::currentUser() );
00075         }
00076         else if ( $attr == 'all_week_days' )
00077         {
00078             return eZLocale::instance()->attribute( 'weekday_name_list' );
00079         }
00080         else if ( $attr == 'all_month_days' )
00081         {
00082             return range( 1, 31 );
00083         }
00084         else if ( $attr == 'available_hours' )
00085         {
00086             return array( '0:00',
00087                           '1:00',
00088                           '2:00',
00089                           '3:00',
00090                           '4:00',
00091                           '5:00',
00092                           '6:00',
00093                           '7:00',
00094                           '8:00',
00095                           '9:00',
00096                           '10:00',
00097                           '11:00',
00098                           '12:00',
00099                           '13:00',
00100                           '14:00',
00101                           '15:00',
00102                           '16:00',
00103                           '17:00',
00104                           '18:00',
00105                           '19:00',
00106                           '20:00',
00107                           '21:00',
00108                           '22:00',
00109                           '23:00' );
00110         }
00111         return eZNotificationEventHandler::attribute( $attr );
00112     }
00113 
00114     function settings( $user = false )
00115     {
00116         if ( $user === false )
00117         {
00118             $user = eZUser::currentUser();
00119         }
00120         $address = $user->attribute( 'email' );
00121         $settings = eZGeneralDigestUserSettings::fetchForUser( $address );
00122         if ( $settings == null )
00123         {
00124             $settings = eZGeneralDigestUserSettings::create( $address );
00125             $settings->store();
00126         }
00127         return $settings;
00128     }
00129 
00130     function handle( $event )
00131     {
00132         eZDebugSetting::writeDebug( 'kernel-notification', $event, "trying to handle event" );
00133         if ( $event->attribute( 'event_type_string' ) == 'ezcurrenttime' )
00134         {
00135             $date = $event->content();
00136             $timestamp = $date->attribute( 'timestamp' );
00137 
00138             $addressArray = $this->fetchUsersForDigest( $timestamp );
00139 
00140             require_once( 'kernel/common/template.php' );
00141             $tpl = templateInit();
00142             $prevTplUsageStats = $tpl->setIsTemplatesUsageStatisticsEnabled( false );
00143 
00144             $transport = eZNotificationTransport::instance( 'ezmail' );
00145             foreach ( $addressArray as $address )
00146             {
00147                 $tpl->setVariable( 'date', $date );
00148                 $tpl->setVariable( 'address', $address['address'] );
00149                 $result = $tpl->fetch( 'design:notification/handler/ezgeneraldigest/view/plain.tpl' );
00150                 $subject = $tpl->variable( 'subject' );
00151                 $transport->send( $address, $subject, $result);
00152                 eZDebugSetting::writeDebug( 'kernel-notification', $result, "digest result" );
00153             }
00154 
00155             $collectionItemIDList = $tpl->variable( 'collection_item_id_list' );
00156             eZDebugSetting::writeDebug( 'kernel-notification', $collectionItemIDList, "handled items" );
00157 
00158             $tpl->setIsTemplatesUsageStatisticsEnabled( $prevTplUsageStats );
00159 
00160             if ( is_array( $collectionItemIDList ) && count( $collectionItemIDList ) > 0 )
00161             {
00162                 $ini = eZINI::instance( 'notification.ini' );
00163                 $countElements = $ini->variable( 'RuleSettings', 'LimitDeleteElements' );
00164                 if ( !$countElements )
00165                 {
00166                     $countElements = 50;
00167                 }
00168                 $splited = array_chunk( $collectionItemIDList, $countElements );
00169                 foreach ( $splited as $key => $value )
00170                 {
00171                     eZPersistentObject::removeObject( eZNotificationCollectionItem::definition(), array( 'id' => array( $value, '' ) ) );
00172                 }
00173             }
00174 
00175         }
00176         return true;
00177     }
00178 
00179 
00180     function fetchUsersForDigest( $timestamp )
00181     {
00182         return eZPersistentObject::fetchObjectList( eZNotificationCollectionItem::definition(),
00183                                                     array(), array( 'send_date' => array( '', array( 1, $timestamp ) ) ),
00184                                                     array( 'address' => 'asc' ),null,
00185                                                     false,false,array( array( 'operation' => 'distinct address' ) ) );
00186 
00187     }
00188 
00189     function fetchHandlersForUser( $time, $address )
00190     {
00191         $db = eZDB::instance();
00192 
00193         $time = (int)$time;
00194         $address = $db->escapeString( $address );
00195 
00196         $query = "select distinct handler
00197                   from eznotificationcollection,
00198                        eznotificationcollection_item
00199                   where eznotificationcollection_item.collection_id = eznotificationcollection.id and
00200                         address='$address' and
00201                         send_date != 0 and
00202                         send_date < $time";
00203         $handlerResult = $db->arrayQuery( $query );
00204         $handlers = array();
00205         $availableHandlers = eZNotificationEventFilter::availableHandlers();
00206         foreach ( $handlerResult as $handlerName )
00207         {
00208             $handlers[$handlerName['handler']] = $availableHandlers[$handlerName['handler']];
00209         }
00210         return $handlers;
00211     }
00212 
00213     function fetchItemsForUser( $time, $address, $handler )
00214     {
00215         $db = eZDB::instance();
00216 
00217         $time = (int)$time;
00218         $address = $db->escapeString( $address );
00219         $handler = $db->escapeString( $handler );
00220 
00221         $query = "select eznotificationcollection_item.*
00222                   from eznotificationcollection,
00223                        eznotificationcollection_item
00224                   where eznotificationcollection_item.collection_id = eznotificationcollection.id and
00225                         address='$address' and
00226                         send_date != 0 and
00227                         send_date < $time and
00228                         handler = '$handler'
00229                         order by eznotificationcollection_item.event_id";
00230         $itemResult = $db->arrayQuery( $query );
00231         $items = array();
00232         foreach ( $itemResult as $itemRow )
00233         {
00234             $items[] = new eZNotificationCollectionItem( $itemRow );
00235         }
00236         return $items;
00237     }
00238 
00239     function storeSettings( $http, $module )
00240     {
00241         $user = eZUser::currentUser();
00242         $address = $user->attribute( 'email' );
00243         $settings = eZGeneralDigestUserSettings::fetchForUser( $address );
00244 
00245         if ( $http->hasPostVariable( 'ReceiveDigest_' . self::NOTIFICATION_HANDLER_ID ) &&
00246              $http->hasPostVariable( 'ReceiveDigest_' . self::NOTIFICATION_HANDLER_ID ) == '1' )
00247         {
00248             $settings->setAttribute( 'receive_digest', 1 );
00249             $digestType = $http->postVariable( 'DigestType_' . self::NOTIFICATION_HANDLER_ID );
00250             $settings->setAttribute( 'digest_type', $digestType );
00251             if ( $digestType == 1 )
00252             {
00253                 $settings->setAttribute( 'day', $http->postVariable( 'Weekday_' . self::NOTIFICATION_HANDLER_ID ) );
00254             }
00255             else if ( $digestType == 2 )
00256             {
00257                 $settings->setAttribute( 'day', $http->postVariable( 'Monthday_' . self::NOTIFICATION_HANDLER_ID ) );
00258             }
00259             $settings->setAttribute( 'time', $http->postVariable( 'Time_' . self::NOTIFICATION_HANDLER_ID ) );
00260             $settings->store();
00261         }
00262         else
00263         {
00264             $settings->setAttribute( 'receive_digest', 0 );
00265             $settings->store();
00266         }
00267     }
00268 
00269     /*!
00270      \reimp
00271     */
00272     function cleanup()
00273     {
00274         eZGeneralDigestUserSettings::cleanup();
00275     }
00276 
00277 }
00278 
00279 ?>