|
eZ Publish
[trunk]
|
00001 <?php 00002 /** 00003 * File containing the eZSubTreeHandler 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 eZSubTreeHandler ezsubtreehandler.php 00013 \brief The class eZSubTreeHandler does 00014 00015 */ 00016 00017 class eZSubTreeHandler extends eZNotificationEventHandler 00018 { 00019 const NOTIFICATION_HANDLER_ID = 'ezsubtree'; 00020 const TRANSPORT = 'ezmail'; 00021 00022 /*! 00023 Constructor 00024 */ 00025 function eZSubTreeHandler() 00026 { 00027 $this->eZNotificationEventHandler( self::NOTIFICATION_HANDLER_ID, "Subtree Handler" ); 00028 } 00029 00030 function attributes() 00031 { 00032 return array_merge( array( 'subscribed_nodes', 00033 'rules' ), 00034 eZNotificationEventHandler::attributes() ); 00035 } 00036 00037 function hasAttribute( $attr ) 00038 { 00039 return in_array( $attr, $this->attributes() ); 00040 } 00041 00042 function attribute( $attr ) 00043 { 00044 if ( $attr == 'subscribed_nodes' ) 00045 { 00046 $user = eZUser::currentUser(); 00047 return $this->subscribedNodes( $user ); 00048 } 00049 else if ( $attr == 'rules' ) 00050 { 00051 $user = eZUser::currentUser(); 00052 return $this->rules( $user ); 00053 } 00054 return eZNotificationEventHandler::attribute( $attr ); 00055 } 00056 00057 function handle( $event ) 00058 { 00059 eZDebugSetting::writeDebug( 'kernel-notification', $event, "trying to handle event" ); 00060 if ( $event->attribute( 'event_type_string' ) == 'ezpublish' ) 00061 { 00062 $parameters = array(); 00063 $status = $this->handlePublishEvent( $event, $parameters ); 00064 if ( $status == eZNotificationEventHandler::EVENT_HANDLED ) 00065 $this->sendMessage( $event, $parameters ); 00066 else 00067 return false; 00068 } 00069 return true; 00070 } 00071 00072 function handlePublishEvent( $event, &$parameters ) 00073 { 00074 $versionObject = $event->attribute( 'content' ); 00075 if ( !$versionObject ) 00076 return eZNotificationEventHandler::EVENT_SKIPPED; 00077 $contentObject = $versionObject->attribute( 'contentobject' ); 00078 if ( !$contentObject ) 00079 return eZNotificationEventHandler::EVENT_SKIPPED; 00080 $contentNode = $contentObject->attribute( 'main_node' ); 00081 if ( !$contentNode ) 00082 return eZNotificationEventHandler::EVENT_SKIPPED; 00083 00084 // Notification should only be sent out when the object is published (is visible) 00085 if ( $contentNode->attribute( 'is_invisible' ) == 1 ) 00086 return eZNotificationEventHandler::EVENT_SKIPPED; 00087 $contentClass = $contentObject->attribute( 'content_class' ); 00088 if ( !$contentClass ) 00089 return eZNotificationEventHandler::EVENT_SKIPPED; 00090 if ( // $versionObject->attribute( 'version' ) != 1 || 00091 $versionObject->attribute( 'version' ) != $contentObject->attribute( 'current_version' ) ) 00092 { 00093 return eZNotificationEventHandler::EVENT_SKIPPED; 00094 } 00095 $tpl = eZTemplate::factory(); 00096 $tpl->resetVariables(); 00097 00098 $parentNode = $contentNode->attribute( 'parent' ); 00099 if ( !$parentNode instanceof eZContentObjectTreeNode ) 00100 { 00101 eZDebug::writeError( 'DB corruption: Node id ' . $contentNode->attribute( 'node_id' ) . ' is missing parent node.', __METHOD__ ); 00102 return eZNotificationEventHandler::EVENT_SKIPPED; 00103 } 00104 00105 $parentContentObject = $parentNode->attribute( 'object' ); 00106 if ( !$parentContentObject instanceof eZContentObject ) 00107 { 00108 eZDebug::writeError( 'DB corruption: Node id ' . $parentNode->attribute( 'node_id' ) . ' is missing object.', __METHOD__ ); 00109 return eZNotificationEventHandler::EVENT_SKIPPED; 00110 } 00111 00112 $parentContentClass = $parentContentObject->attribute( 'content_class' ); 00113 if ( !$parentContentClass instanceof eZContentClass ) 00114 { 00115 eZDebug::writeError( 'DB corruption: Object id ' . $parentContentObject->attribute( 'id' ) . ' is missing class object.', __METHOD__ ); 00116 return eZNotificationEventHandler::EVENT_SKIPPED; 00117 } 00118 00119 $res = eZTemplateDesignResource::instance(); 00120 $res->setKeys( array( array( 'object', $contentObject->attribute( 'id' ) ), 00121 array( 'node', $contentNode->attribute( 'node_id' ) ), 00122 array( 'class', $contentObject->attribute( 'contentclass_id' ) ), 00123 array( 'class_identifier', $contentClass->attribute( 'identifier' ) ), 00124 array( 'parent_node', $contentNode->attribute( 'parent_node_id' ) ), 00125 array( 'parent_class', $parentContentObject->attribute( 'contentclass_id' ) ), 00126 array( 'parent_class_identifier', ( $parentContentClass != null ? $parentContentClass->attribute( 'identifier' ) : 0 ) ), 00127 array( 'depth', $contentNode->attribute( 'depth' ) ), 00128 array( 'url_alias', $contentNode->attribute( 'url_alias' ) ) 00129 ) ); 00130 00131 $tpl->setVariable( 'object', $contentObject ); 00132 00133 $notificationINI = eZINI::instance( 'notification.ini' ); 00134 $emailSender = $notificationINI->variable( 'MailSettings', 'EmailSender' ); 00135 $ini = eZINI::instance(); 00136 if ( !$emailSender ) 00137 $emailSender = $ini->variable( 'MailSettings', 'EmailSender' ); 00138 if ( !$emailSender ) 00139 $emailSender = $ini->variable( "MailSettings", "AdminEmail" ); 00140 $tpl->setVariable( 'sender', $emailSender ); 00141 00142 $result = $tpl->fetch( 'design:notification/handler/ezsubtree/view/plain.tpl' ); 00143 $subject = $tpl->variable( 'subject' ); 00144 if ( $tpl->hasVariable( 'message_id' ) ) 00145 $parameters['message_id'] = $tpl->variable( 'message_id' ); 00146 if ( $tpl->hasVariable( 'references' ) ) 00147 $parameters['references'] = $tpl->variable( 'references' ); 00148 if ( $tpl->hasVariable( 'reply_to' ) ) 00149 $parameters['reply_to'] = $tpl->variable( 'reply_to' ); 00150 if ( $tpl->hasVariable( 'from' ) ) 00151 $parameters['from'] = $tpl->variable( 'from' ); 00152 if ( $tpl->hasVariable( 'content_type' ) ) 00153 $parameters['content_type'] = $tpl->variable( 'content_type' ); 00154 00155 $collection = eZNotificationCollection::create( $event->attribute( 'id' ), 00156 self::NOTIFICATION_HANDLER_ID, 00157 self::TRANSPORT ); 00158 00159 $collection->setAttribute( 'data_subject', $subject ); 00160 $collection->setAttribute( 'data_text', $result ); 00161 $collection->store(); 00162 00163 $assignedNodes = $contentObject->parentNodes( true ); 00164 $nodeIDList = array(); 00165 foreach( $assignedNodes as $node ) 00166 { 00167 if ( $node ) 00168 { 00169 $pathString = $node->attribute( 'path_string' ); 00170 $pathString = ltrim( rtrim( $pathString, '/' ), '/' ); 00171 $nodeIDListPart = explode( '/', $pathString ); 00172 $nodeIDList = array_merge( $nodeIDList, $nodeIDListPart ); 00173 } 00174 } 00175 $nodeIDList[] = $contentNode->attribute( 'node_id' ); 00176 $nodeIDList = array_unique( $nodeIDList ); 00177 00178 $userList = eZSubtreeNotificationRule::fetchUserList( $nodeIDList, $contentObject ); 00179 00180 $locale = eZLocale::instance(); 00181 $weekDayNames = $locale->attribute( 'weekday_name_list' ); 00182 $weekDaysByName = array_flip( $weekDayNames ); 00183 00184 foreach( $userList as $subscriber ) 00185 { 00186 $item = $collection->addItem( $subscriber['address'] ); 00187 if ( $subscriber['use_digest'] == 0 ) 00188 { 00189 $settings = eZGeneralDigestUserSettings::fetchForUser( $subscriber['address'] ); 00190 if ( $settings !== null && $settings->attribute( 'receive_digest' ) == 1 ) 00191 { 00192 $time = $settings->attribute( 'time' ); 00193 $timeArray = explode( ':', $time ); 00194 $hour = $timeArray[0]; 00195 00196 if ( $settings->attribute( 'digest_type' ) == eZGeneralDigestUserSettings::TYPE_DAILY ) 00197 { 00198 eZNotificationSchedule::setDateForItem( $item, array( 'frequency' => 'day', 00199 'hour' => $hour ) ); 00200 } 00201 else if ( $settings->attribute( 'digest_type' ) == eZGeneralDigestUserSettings::TYPE_WEEKLY ) 00202 { 00203 $weekday = $weekDaysByName[ $settings->attribute( 'day' ) ]; 00204 eZNotificationSchedule::setDateForItem( $item, array( 'frequency' => 'week', 00205 'day' => $weekday, 00206 'hour' => $hour ) ); 00207 } 00208 else if ( $settings->attribute( 'digest_type' ) == eZGeneralDigestUserSettings::TYPE_MONTHLY ) 00209 { 00210 eZNotificationSchedule::setDateForItem( $item, 00211 array( 'frequency' => 'month', 00212 'day' => $settings->attribute( 'day' ), 00213 'hour' => $hour ) ); 00214 } 00215 $item->store(); 00216 } 00217 } 00218 } 00219 return eZNotificationEventHandler::EVENT_HANDLED; 00220 } 00221 00222 function sendMessage( $event, $parameters ) 00223 { 00224 $collection = eZNotificationCollection::fetchForHandler( self::NOTIFICATION_HANDLER_ID, 00225 $event->attribute( 'id' ), 00226 self::TRANSPORT ); 00227 00228 if ( !$collection ) 00229 return; 00230 00231 $items = $collection->attribute( 'items_to_send' ); 00232 00233 if ( !$items ) 00234 { 00235 eZDebugSetting::writeDebug( 'kernel-notification', "No items to send now" ); 00236 return; 00237 } 00238 $addressList = array(); 00239 foreach ( $items as $item ) 00240 { 00241 $addressList[] = $item->attribute( 'address' ); 00242 $item->remove(); 00243 } 00244 00245 $transport = eZNotificationTransport::instance( 'ezmail' ); 00246 $transport->send( $addressList, $collection->attribute( 'data_subject' ), $collection->attribute( 'data_text' ), null, 00247 $parameters ); 00248 if ( $collection->attribute( 'item_count' ) == 0 ) 00249 { 00250 $collection->remove(); 00251 } 00252 } 00253 00254 function subscribedNodes( $user = false ) 00255 { 00256 if ( $user === false ) 00257 { 00258 $user = eZUser::currentUser(); 00259 } 00260 $userID = $user->attribute( 'contentobject_id' ); 00261 00262 return eZSubtreeNotificationRule::fetchNodesForUserID( $userID ); 00263 } 00264 00265 static function rules( $user = false, $offset = false, $limit = false ) 00266 { 00267 if ( $user === false ) 00268 { 00269 $user = eZUser::currentUser(); 00270 } 00271 $userID = $user->attribute( 'contentobject_id' ); 00272 00273 return eZSubtreeNotificationRule::fetchList( $userID, true, $offset, $limit ); 00274 } 00275 00276 static function rulesCount( $user = false ) 00277 { 00278 if ( $user === false ) 00279 { 00280 $user = eZUser::currentUser(); 00281 } 00282 $userID = $user->attribute( 'contentobject_id' ); 00283 00284 return eZSubtreeNotificationRule::fetchListCount( $userID ); 00285 } 00286 00287 function fetchHttpInput( $http, $module ) 00288 { 00289 if ( $http->hasPostVariable( 'NewRule_' . self::NOTIFICATION_HANDLER_ID ) ) 00290 { 00291 eZContentBrowse::browse( array( 'action_name' => 'AddSubtreeSubscribingNode', 00292 'from_page' => '/notification/settings/' ), 00293 $module ); 00294 00295 } 00296 else if ( $http->hasPostVariable( 'RemoveRule_' . self::NOTIFICATION_HANDLER_ID ) and 00297 $http->hasPostVariable( 'SelectedRuleIDArray_' . self::NOTIFICATION_HANDLER_ID ) ) 00298 { 00299 $user = eZUser::currentUser(); 00300 $userList = eZSubtreeNotificationRule::fetchList( $user->attribute( 'contentobject_id' ), false ); 00301 foreach ( $userList as $userRow ) 00302 { 00303 $listID[] = $userRow['id']; 00304 } 00305 $ruleIDList = $http->postVariable( 'SelectedRuleIDArray_' . self::NOTIFICATION_HANDLER_ID ); 00306 foreach ( $ruleIDList as $ruleID ) 00307 { 00308 if ( in_array( $ruleID, $listID ) ) 00309 eZPersistentObject::removeObject( eZSubtreeNotificationRule::definition(), array( 'id' => $ruleID ) ); 00310 } 00311 } 00312 else if ( $http->hasPostVariable( "BrowseActionName" ) and 00313 $http->postVariable( "BrowseActionName" ) == "AddSubtreeSubscribingNode" and 00314 !$http->hasPostVariable( 'BrowseCancelButton' ) ) 00315 { 00316 $selectedNodeIDArray = $http->postVariable( "SelectedNodeIDArray" ); 00317 $user = eZUser::currentUser(); 00318 00319 $existingNodes = eZSubtreeNotificationRule::fetchNodesForUserID( $user->attribute( 'contentobject_id' ), false ); 00320 00321 foreach ( $selectedNodeIDArray as $nodeID ) 00322 { 00323 if ( ! in_array( $nodeID, $existingNodes ) ) 00324 { 00325 $rule = eZSubtreeNotificationRule::create( $nodeID, $user->attribute( 'contentobject_id' ) ); 00326 $rule->store(); 00327 } 00328 } 00329 // $Module->redirectTo( "//list/" ); 00330 } 00331 00332 } 00333 00334 function cleanup() 00335 { 00336 eZSubtreeNotificationRule::cleanup(); 00337 } 00338 } 00339 00340 ?>