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 define( 'EZ_SUBTREE_NOTIFICATION_HANDLER_ID', 'ezsubtree' );
00040 define( 'EZ_SUBTREE_NOTIFICATION_HANDLER_TRANSPORT', 'ezmail' );
00041 include_once( 'kernel/classes/notification/eznotificationeventhandler.php' );
00042 include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );
00043 include_once( 'kernel/classes/notification/eznotificationcollection.php' );
00044 include_once( 'kernel/classes/notification/eznotificationschedule.php' );
00045 include_once( 'kernel/classes/notification/handler/ezsubtree/ezsubtreenotificationrule.php' );
00046 include_once( 'kernel/classes/notification/handler/ezgeneraldigest/ezgeneraldigestusersettings.php' );
00047
00048 class eZSubTreeHandler extends eZNotificationEventHandler
00049 {
00050
00051
00052
00053 function eZSubTreeHandler()
00054 {
00055 $this->eZNotificationEventHandler( EZ_SUBTREE_NOTIFICATION_HANDLER_ID, "Subtree Handler" );
00056 }
00057
00058 function attributes()
00059 {
00060 return array_merge( array( 'subscribed_nodes',
00061 'rules' ),
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 == 'subscribed_nodes' )
00073 {
00074 $user =& eZUser::currentUser();
00075 return $this->subscribedNodes( $user );
00076 }
00077 else if ( $attr == 'rules' )
00078 {
00079 $user =& eZUser::currentUser();
00080 return $this->rules( $user );
00081 }
00082 return eZNotificationEventHandler::attribute( $attr );
00083 }
00084
00085 function handle( &$event )
00086 {
00087 eZDebugSetting::writeDebug( 'kernel-notification', $event, "trying to handle event" );
00088 if ( $event->attribute( 'event_type_string' ) == 'ezpublish' )
00089 {
00090 $parameters = array();
00091 $status = $this->handlePublishEvent( $event, $parameters );
00092 if ( $status == EZ_NOTIFICATIONEVENTHANDLER_EVENT_HANDLED )
00093 $this->sendMessage( $event, $parameters );
00094 else
00095 return false;
00096 }
00097 return true;
00098 }
00099
00100 function handlePublishEvent( &$event, &$parameters )
00101 {
00102 $versionObject =& $event->attribute( 'content' );
00103 if ( !$versionObject )
00104 return EZ_NOTIFICATIONEVENTHANDLER_EVENT_SKIPPED;
00105 $contentObject = $versionObject->attribute( 'contentobject' );
00106 if ( !$contentObject )
00107 return EZ_NOTIFICATIONEVENTHANDLER_EVENT_SKIPPED;
00108 $contentNode =& $contentObject->attribute( 'main_node' );
00109 if ( !$contentNode )
00110 return EZ_NOTIFICATIONEVENTHANDLER_EVENT_SKIPPED;
00111
00112
00113 if ( $contentNode->attribute( 'is_invisible' ) == 1 )
00114 return EZ_NOTIFICATIONEVENTHANDLER_EVENT_SKIPPED;
00115 $contentClass =& $contentObject->attribute( 'content_class' );
00116 if ( !$contentClass )
00117 return EZ_NOTIFICATIONEVENTHANDLER_EVENT_SKIPPED;
00118 if (
00119 $versionObject->attribute( 'version' ) != $contentObject->attribute( 'current_version' ) )
00120 {
00121 return EZ_NOTIFICATIONEVENTHANDLER_EVENT_SKIPPED;
00122 }
00123 include_once( 'kernel/common/template.php' );
00124 $tpl =& templateInit();
00125 $tpl->resetVariables();
00126
00127 $parentNode =& $contentNode->attribute( 'parent' );
00128 $parentContentObject =& $parentNode->attribute( 'object' );
00129 $parentContentClass =& $parentContentObject->attribute( 'content_class' );
00130
00131 $res =& eZTemplateDesignResource::instance();
00132 $res->setKeys( array( array( 'object', $contentObject->attribute( 'id' ) ),
00133 array( 'node', $contentNode->attribute( 'node_id' ) ),
00134 array( 'class', $contentObject->attribute( 'contentclass_id' ) ),
00135 array( 'class_identifier', $contentClass->attribute( 'identifier' ) ),
00136 array( 'parent_node', $contentNode->attribute( 'parent_node_id' ) ),
00137 array( 'parent_class', $parentContentObject->attribute( 'contentclass_id' ) ),
00138 array( 'parent_class_identifier', ( $parentContentClass != null ? $parentContentClass->attribute( 'identifier' ) : 0 ) ),
00139 array( 'depth', $contentNode->attribute( 'depth' ) ),
00140 array( 'url_alias', $contentNode->attribute( 'url_alias' ) )
00141 ) );
00142
00143 $tpl->setVariable( 'object', $contentObject );
00144
00145 $notificationINI =& eZINI::instance( 'notification.ini' );
00146 $emailSender = $notificationINI->variable( 'MailSettings', 'EmailSender' );
00147 $ini =& eZINI::instance();
00148 if ( !$emailSender )
00149 $emailSender = $ini->variable( 'MailSettings', 'EmailSender' );
00150 if ( !$emailSender )
00151 $emailSender = $ini->variable( "MailSettings", "AdminEmail" );
00152 $tpl->setVariable( 'sender', $emailSender );
00153
00154 $result = $tpl->fetch( 'design:notification/handler/ezsubtree/view/plain.tpl' );
00155 $subject = $tpl->variable( 'subject' );
00156 if ( $tpl->hasVariable( 'message_id' ) )
00157 $parameters['message_id'] = $tpl->variable( 'message_id' );
00158 if ( $tpl->hasVariable( 'references' ) )
00159 $parameters['references'] = $tpl->variable( 'references' );
00160 if ( $tpl->hasVariable( 'reply_to' ) )
00161 $parameters['reply_to'] = $tpl->variable( 'reply_to' );
00162 if ( $tpl->hasVariable( 'from' ) )
00163 $parameters['from'] = $tpl->variable( 'from' );
00164
00165 $collection = eZNotificationCollection::create( $event->attribute( 'id' ),
00166 EZ_SUBTREE_NOTIFICATION_HANDLER_ID,
00167 EZ_SUBTREE_NOTIFICATION_HANDLER_TRANSPORT );
00168
00169 $collection->setAttribute( 'data_subject', $subject );
00170 $collection->setAttribute( 'data_text', $result );
00171 $collection->store();
00172
00173 $assignedNodes =& $contentObject->parentNodes( true );
00174 $nodeIDList = array();
00175 foreach( array_keys( $assignedNodes ) as $key )
00176 {
00177 $node =& $assignedNodes[$key];
00178 if ( $node )
00179 {
00180 $pathString = $node->attribute( 'path_string' );
00181 $pathString = ltrim( rtrim( $pathString, '/' ), '/' );
00182 $nodeIDListPart = explode( '/', $pathString );
00183 $nodeIDList = array_merge( $nodeIDList, $nodeIDListPart );
00184 }
00185 }
00186 $nodeIDList[] = $contentNode->attribute( 'node_id' );
00187 $nodeIDList = array_unique( $nodeIDList );
00188
00189 $userList = eZSubtreeNotificationRule::fetchUserList( $nodeIDList, $contentObject );
00190
00191 $locale =& eZLocale::instance();
00192 $weekDayNames = $locale->attribute( 'weekday_name_list' );
00193 $weekDaysByName = array_flip( $weekDayNames );
00194
00195 foreach( $userList as $subscriber )
00196 {
00197 $item = $collection->addItem( $subscriber['address'] );
00198 if ( $subscriber['use_digest'] == 0 )
00199 {
00200 $settings = eZGeneralDigestUserSettings::fetchForUser( $subscriber['address'] );
00201 if ( !is_null( $settings ) && $settings->attribute( 'receive_digest' ) == 1 )
00202 {
00203 $time = $settings->attribute( 'time' );
00204 $timeArray = explode( ':', $time );
00205 $hour = $timeArray[0];
00206
00207 if ( $settings->attribute( 'digest_type' ) == EZ_DIGEST_SETTINGS_TYPE_DAILY )
00208 {
00209 eZNotificationSchedule::setDateForItem( $item, array( 'frequency' => 'day',
00210 'hour' => $hour ) );
00211 }
00212 else if ( $settings->attribute( 'digest_type' ) == EZ_DIGEST_SETTINGS_TYPE_WEEKLY )
00213 {
00214 $weekday = $weekDaysByName[ $settings->attribute( 'day' ) ];
00215 eZNotificationSchedule::setDateForItem( $item, array( 'frequency' => 'week',
00216 'day' => $weekday,
00217 'hour' => $hour ) );
00218 }
00219 else if ( $settings->attribute( 'digest_type' ) == EZ_DIGEST_SETTINGS_TYPE_MONTHLY )
00220 {
00221 eZNotificationSchedule::setDateForItem( $item,
00222 array( 'frequency' => 'month',
00223 'day' => $settings->attribute( 'day' ),
00224 'hour' => $hour ) );
00225 }
00226 $item->store();
00227 }
00228 }
00229 }
00230 return EZ_NOTIFICATIONEVENTHANDLER_EVENT_HANDLED;
00231 }
00232
00233 function sendMessage( &$event, $parameters )
00234 {
00235 $collection = eZNotificationCollection::fetchForHandler( EZ_SUBTREE_NOTIFICATION_HANDLER_ID,
00236 $event->attribute( 'id' ),
00237 EZ_SUBTREE_NOTIFICATION_HANDLER_TRANSPORT );
00238
00239 if ( !$collection )
00240 return;
00241
00242 $items =& $collection->attribute( 'items_to_send' );
00243
00244 if ( !$items )
00245 {
00246 eZDebugSetting::writeDebug( 'kernel-notification', "No items to send now" );
00247 return;
00248 }
00249 $addressList = array();
00250 foreach ( array_keys( $items ) as $key )
00251 {
00252 $addressList[] = $items[$key]->attribute( 'address' );
00253 $items[$key]->remove();
00254 }
00255
00256 $transport =& eZNotificationTransport::instance( 'ezmail' );
00257 $transport->send( $addressList, $collection->attribute( 'data_subject' ), $collection->attribute( 'data_text' ), null,
00258 $parameters );
00259 if ( $collection->attribute( 'item_count' ) == 0 )
00260 {
00261 $collection->remove();
00262 }
00263 }
00264
00265 function &subscribedNodes( $user = false )
00266 {
00267 if ( $user === false )
00268 {
00269 $user =& eZUser::currentUser();
00270 }
00271 $userID = $user->attribute( 'contentobject_id' );
00272
00273 $nodeList = eZSubtreeNotificationRule::fetchNodesForUserID( $userID );
00274 return $nodeList;
00275 }
00276
00277 function &rules( $user = false, $offset = false, $limit = false )
00278 {
00279 if ( $user === false )
00280 {
00281 $user =& eZUser::currentUser();
00282 }
00283 $userID = $user->attribute( 'contentobject_id' );
00284
00285 $ruleList = eZSubtreeNotificationRule::fetchList( $userID, true, $offset, $limit );
00286 return $ruleList;
00287 }
00288
00289 function rulesCount( $user = false )
00290 {
00291 if ( $user === false )
00292 {
00293 $user =& eZUser::currentUser();
00294 }
00295 $userID = $user->attribute( 'contentobject_id' );
00296
00297 return eZSubtreeNotificationRule::fetchListCount( $userID );
00298 }
00299
00300 function fetchHttpInput( &$http, &$module )
00301 {
00302 if ( $http->hasPostVariable( 'NewRule_' . EZ_SUBTREE_NOTIFICATION_HANDLER_ID ) )
00303 {
00304 include_once( "kernel/classes/ezcontentbrowse.php" );
00305 eZContentBrowse::browse( array( 'action_name' => 'AddSubtreeSubscribingNode',
00306 'from_page' => '/notification/settings/' ),
00307 $module );
00308
00309 }
00310 else if ( $http->hasPostVariable( 'RemoveRule_' . EZ_SUBTREE_NOTIFICATION_HANDLER_ID ) and
00311 $http->hasPostVariable( 'SelectedRuleIDArray_' . EZ_SUBTREE_NOTIFICATION_HANDLER_ID ) )
00312 {
00313 $user =& eZUser::currentUser();
00314 $userList = eZSubtreeNotificationRule::fetchList( $user->attribute( 'contentobject_id' ), false );
00315 foreach ( $userList as $userRow )
00316 {
00317 $listID[] = $userRow['id'];
00318 }
00319 $ruleIDList = $http->postVariable( 'SelectedRuleIDArray_' . EZ_SUBTREE_NOTIFICATION_HANDLER_ID );
00320 foreach ( $ruleIDList as $ruleID )
00321 {
00322 if ( in_array( $ruleID, $listID ) )
00323 eZPersistentObject::removeObject( eZSubtreeNotificationRule::definition(), array( 'id' => $ruleID ) );
00324 }
00325 }
00326 else if ( $http->hasPostVariable( "BrowseActionName" ) and
00327 $http->postVariable( "BrowseActionName" ) == "AddSubtreeSubscribingNode" and
00328 !$http->hasPostVariable( 'BrowseCancelButton' ) )
00329 {
00330 $selectedNodeIDArray = $http->postVariable( "SelectedNodeIDArray" );
00331 $user =& eZUser::currentUser();
00332
00333 $existingNodes = eZSubtreeNotificationRule::fetchNodesForUserID( $user->attribute( 'contentobject_id' ), false );
00334
00335 foreach ( $selectedNodeIDArray as $nodeID )
00336 {
00337 if ( ! in_array( $nodeID, $existingNodes ) )
00338 {
00339 $rule = eZSubtreeNotificationRule::create( $nodeID, $user->attribute( 'contentobject_id' ) );
00340 $rule->store();
00341 }
00342 }
00343
00344 }
00345
00346 }
00347
00348
00349
00350
00351 function cleanup()
00352 {
00353 eZSubtreeNotificationRule::cleanup();
00354 }
00355 }
00356
00357 ?>