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