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