Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezsubtreehandler.php
Go to the documentation of this file.
1 <?php
17 class eZSubTreeHandler extends eZNotificationEventHandler
18 {
19 const NOTIFICATION_HANDLER_ID = 'ezsubtree';
20 const TRANSPORT = 'ezmail';
21
22 public function __construct()
23 {
24 parent::__construct( self::NOTIFICATION_HANDLER_ID, "Subtree Handler" );
25 }
26
27 function attributes()
28 {
29 return array_merge( array( 'subscribed_nodes',
30 'rules' ),
31 eZNotificationEventHandler::attributes() );
32 }
33
34 function hasAttribute( $attr )
35 {
36 return in_array( $attr, $this->attributes() );
37 }
38
39 function attribute( $attr )
40 {
41 if ( $attr == 'subscribed_nodes' )
42 {
43 $user = eZUser::currentUser();
44 return $this->subscribedNodes( $user );
45 }
46 else if ( $attr == 'rules' )
47 {
48 $user = eZUser::currentUser();
49 return $this->rules( $user );
50 }
51 return eZNotificationEventHandler::attribute( $attr );
52 }
53
54 function handle( $event )
55 {
56 eZDebugSetting::writeDebug( 'kernel-notification', $event, "trying to handle event" );
57 if ( $event->attribute( 'event_type_string' ) == 'ezpublish' )
58 {
59 $parameters = array();
60 $status = $this->handlePublishEvent( $event, $parameters );
61 if ( $status == eZNotificationEventHandler::EVENT_HANDLED )
62 $this->sendMessage( $event, $parameters );
63 else
64 return false;
65 }
66 return true;
67 }
68
69 function handlePublishEvent( $event, &$parameters )
70 {
71 $versionObject = $event->attribute( 'content' );
72 if ( !$versionObject )
73 return eZNotificationEventHandler::EVENT_SKIPPED;
74 $contentObject = $versionObject->attribute( 'contentobject' );
75 if ( !$contentObject )
76 return eZNotificationEventHandler::EVENT_SKIPPED;
77 $contentNode = $contentObject->attribute( 'main_node' );
78 if ( !$contentNode )
79 return eZNotificationEventHandler::EVENT_SKIPPED;
80
81 // Notification should only be sent out when the object is published (is visible)
82 if ( $contentNode->attribute( 'is_invisible' ) == 1 )
83 return eZNotificationEventHandler::EVENT_SKIPPED;
84 $contentClass = $contentObject->attribute( 'content_class' );
85 if ( !$contentClass )
86 return eZNotificationEventHandler::EVENT_SKIPPED;
87 if ( // $versionObject->attribute( 'version' ) != 1 ||
88 $versionObject->attribute( 'version' ) != $contentObject->attribute( 'current_version' ) )
89 {
90 return eZNotificationEventHandler::EVENT_SKIPPED;
91 }
92 $tpl = eZTemplate::factory();
93 $tpl->resetVariables();
94
95 $parentNode = $contentNode->attribute( 'parent' );
96 if ( !$parentNode instanceof eZContentObjectTreeNode )
97 {
98 eZDebug::writeError( 'DB corruption: Node id ' . $contentNode->attribute( 'node_id' ) . ' is missing parent node.', __METHOD__ );
99 return eZNotificationEventHandler::EVENT_SKIPPED;
100 }
101
102 $parentContentObject = $parentNode->attribute( 'object' );
103 if ( !$parentContentObject instanceof eZContentObject )
104 {
105 eZDebug::writeError( 'DB corruption: Node id ' . $parentNode->attribute( 'node_id' ) . ' is missing object.', __METHOD__ );
106 return eZNotificationEventHandler::EVENT_SKIPPED;
107 }
108
109 $parentContentClass = $parentContentObject->attribute( 'content_class' );
110 if ( !$parentContentClass instanceof eZContentClass )
111 {
112 eZDebug::writeError( 'DB corruption: Object id ' . $parentContentObject->attribute( 'id' ) . ' is missing class object.', __METHOD__ );
113 return eZNotificationEventHandler::EVENT_SKIPPED;
114 }
115
116 $res = eZTemplateDesignResource::instance();
117 $res->setKeys( array( array( 'object', $contentObject->attribute( 'id' ) ),
118 array( 'node', $contentNode->attribute( 'node_id' ) ),
119 array( 'class', $contentObject->attribute( 'contentclass_id' ) ),
120 array( 'class_identifier', $contentClass->attribute( 'identifier' ) ),
121 array( 'parent_node', $contentNode->attribute( 'parent_node_id' ) ),
122 array( 'parent_class', $parentContentObject->attribute( 'contentclass_id' ) ),
123 array( 'parent_class_identifier', ( $parentContentClass != null ? $parentContentClass->attribute( 'identifier' ) : 0 ) ),
124 array( 'depth', $contentNode->attribute( 'depth' ) ),
125 array( 'url_alias', $contentNode->attribute( 'url_alias' ) )
126 ) );
127
128 $tpl->setVariable( 'object', $contentObject );
129
130 $notificationINI = eZINI::instance( 'notification.ini' );
131 $emailSender = $notificationINI->variable( 'MailSettings', 'EmailSender' );
132 $ini = eZINI::instance();
133 if ( !$emailSender )
134 $emailSender = $ini->variable( 'MailSettings', 'EmailSender' );
135 if ( !$emailSender )
136 $emailSender = $ini->variable( "MailSettings", "AdminEmail" );
137 $tpl->setVariable( 'sender', $emailSender );
138
139 $result = $tpl->fetch( 'design:notification/handler/ezsubtree/view/plain.tpl' );
140 $subject = $tpl->variable( 'subject' );
141 if ( $tpl->hasVariable( 'message_id' ) )
142 $parameters['message_id'] = $tpl->variable( 'message_id' );
143 if ( $tpl->hasVariable( 'references' ) )
144 $parameters['references'] = $tpl->variable( 'references' );
145 if ( $tpl->hasVariable( 'reply_to' ) )
146 $parameters['reply_to'] = $tpl->variable( 'reply_to' );
147 if ( $tpl->hasVariable( 'from' ) )
148 $parameters['from'] = $tpl->variable( 'from' );
149 if ( $tpl->hasVariable( 'content_type' ) )
150 $parameters['content_type'] = $tpl->variable( 'content_type' );
151
152 $collection = eZNotificationCollection::create( $event->attribute( 'id' ),
153 self::NOTIFICATION_HANDLER_ID,
154 self::TRANSPORT );
155
156 $collection->setAttribute( 'data_subject', $subject );
157 $collection->setAttribute( 'data_text', $result );
158 $collection->store();
159
160 $assignedNodes = $contentObject->parentNodes( true );
161 $nodeIDList = array();
162 foreach( $assignedNodes as $node )
163 {
164 if ( $node )
165 {
166 $pathString = $node->attribute( 'path_string' );
167 $pathString = ltrim( rtrim( $pathString, '/' ), '/' );
168 $nodeIDListPart = explode( '/', $pathString );
169 $nodeIDList = array_merge( $nodeIDList, $nodeIDListPart );
170 }
171 }
172 $nodeIDList[] = $contentNode->attribute( 'node_id' );
173 $nodeIDList = array_unique( $nodeIDList );
174
175 $userList = eZSubtreeNotificationRule::fetchUserList( $nodeIDList, $contentObject );
176
177 $locale = eZLocale::instance();
178 $weekDayNames = $locale->attribute( 'weekday_name_list' );
179 $weekDaysByName = array_flip( $weekDayNames );
180
181 foreach( $userList as $subscriber )
182 {
183 $item = $collection->addItem( $subscriber['address'] );
184 if ( $subscriber['use_digest'] == 0 )
185 {
186 $settings = eZGeneralDigestUserSettings::fetchByUserId( $subscriber['user_id'] );
187 if ( $settings !== null && $settings->attribute( 'receive_digest' ) == 1 )
188 {
189 $time = $settings->attribute( 'time' );
190 $timeArray = explode( ':', $time );
191 $hour = $timeArray[0];
192
193 if ( $settings->attribute( 'digest_type' ) == eZGeneralDigestUserSettings::TYPE_DAILY )
194 {
195 eZNotificationSchedule::setDateForItem( $item, array( 'frequency' => 'day',
196 'hour' => $hour ) );
197 }
198 else if ( $settings->attribute( 'digest_type' ) == eZGeneralDigestUserSettings::TYPE_WEEKLY )
199 {
200 $weekday = $weekDaysByName[ $settings->attribute( 'day' ) ];
201 eZNotificationSchedule::setDateForItem( $item, array( 'frequency' => 'week',
202 'day' => $weekday,
203 'hour' => $hour ) );
204 }
205 else if ( $settings->attribute( 'digest_type' ) == eZGeneralDigestUserSettings::TYPE_MONTHLY )
206 {
207 eZNotificationSchedule::setDateForItem( $item,
208 array( 'frequency' => 'month',
209 'day' => $settings->attribute( 'day' ),
210 'hour' => $hour ) );
211 }
212 $item->store();
213 }
214 }
215 }
216 return eZNotificationEventHandler::EVENT_HANDLED;
217 }
218
219 function sendMessage( $event, $parameters )
220 {
221 $collection = eZNotificationCollection::fetchForHandler( self::NOTIFICATION_HANDLER_ID,
222 $event->attribute( 'id' ),
223 self::TRANSPORT );
224
225 if ( !$collection )
226 return;
227
228 $items = $collection->attribute( 'items_to_send' );
229
230 if ( !$items )
231 {
232 eZDebugSetting::writeDebug( 'kernel-notification', "No items to send now" );
233 return;
234 }
235 $addressList = array();
236 foreach ( $items as $item )
237 {
238 $addressList[] = $item->attribute( 'address' );
239 $item->remove();
240 }
241
242 $transport = eZNotificationTransport::instance( 'ezmail' );
243 $transport->send( $addressList, $collection->attribute( 'data_subject' ), $collection->attribute( 'data_text' ), null,
244 $parameters );
245 if ( $collection->attribute( 'item_count' ) == 0 )
246 {
247 $collection->remove();
248 }
249 }
250
251 function subscribedNodes( $user = false )
252 {
253 if ( $user === false )
254 {
255 $user = eZUser::currentUser();
256 }
257 $userID = $user->attribute( 'contentobject_id' );
258
259 return eZSubtreeNotificationRule::fetchNodesForUserID( $userID );
260 }
261
262 static function rules( $user = false, $offset = false, $limit = false )
263 {
264 if ( $user === false )
265 {
266 $user = eZUser::currentUser();
267 }
268 $userID = $user->attribute( 'contentobject_id' );
269
270 return eZSubtreeNotificationRule::fetchList( $userID, true, $offset, $limit );
271 }
272
273 static function rulesCount( $user = false )
274 {
275 if ( $user === false )
276 {
277 $user = eZUser::currentUser();
278 }
279 $userID = $user->attribute( 'contentobject_id' );
280
281 return eZSubtreeNotificationRule::fetchListCount( $userID );
282 }
283
284 function fetchHttpInput( $http, $module )
285 {
286 if ( $http->hasPostVariable( 'NewRule_' . self::NOTIFICATION_HANDLER_ID ) )
287 {
288 eZContentBrowse::browse( array( 'action_name' => 'AddSubtreeSubscribingNode',
289 'from_page' => '/notification/settings/' ),
290 $module );
291
292 }
293 else if ( $http->hasPostVariable( 'RemoveRule_' . self::NOTIFICATION_HANDLER_ID ) and
294 $http->hasPostVariable( 'SelectedRuleIDArray_' . self::NOTIFICATION_HANDLER_ID ) )
295 {
296 $user = eZUser::currentUser();
297 $userList = eZSubtreeNotificationRule::fetchList( $user->attribute( 'contentobject_id' ), false );
298 foreach ( $userList as $userRow )
299 {
300 $listID[] = $userRow['id'];
301 }
302 $ruleIDList = $http->postVariable( 'SelectedRuleIDArray_' . self::NOTIFICATION_HANDLER_ID );
303 foreach ( $ruleIDList as $ruleID )
304 {
305 if ( in_array( $ruleID, $listID ) )
306 eZPersistentObject::removeObject( eZSubtreeNotificationRule::definition(), array( 'id' => $ruleID ) );
307 }
308 }
309 else if ( $http->hasPostVariable( "BrowseActionName" ) and
310 $http->postVariable( "BrowseActionName" ) == "AddSubtreeSubscribingNode" and
311 !$http->hasPostVariable( 'BrowseCancelButton' ) )
312 {
313 $selectedNodeIDArray = $http->postVariable( "SelectedNodeIDArray" );
314 $user = eZUser::currentUser();
315
316 $existingNodes = eZSubtreeNotificationRule::fetchNodesForUserID( $user->attribute( 'contentobject_id' ), false );
317
318 foreach ( $selectedNodeIDArray as $nodeID )
319 {
320 if ( ! in_array( $nodeID, $existingNodes ) )
321 {
322 $rule = eZSubtreeNotificationRule::create( $nodeID, $user->attribute( 'contentobject_id' ) );
323 $rule->store();
324 }
325 }
326 // $Module->redirectTo( "//list/" );
327 }
328
329 }
330
331 function cleanup()
332 {
333 eZSubtreeNotificationRule::cleanup();
334 }
335 }
336
337 ?>
eZContentBrowse\browse
static browse( $parameters=array(), &$module)
Definition: ezcontentbrowse.php:89
eZContentClass
Handles eZ Publish content classes.
Definition: ezcontentclass.php:20
eZContentObjectTreeNode
Definition: ezcontentobjecttreenode.php:17
eZContentObject
Definition: ezcontentobject.php:17
eZDebugSetting\writeDebug
static writeDebug( $conditionName, $string, $label="")
Definition: ezdebugsetting.php:127
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZGeneralDigestUserSettings\TYPE_WEEKLY
const TYPE_WEEKLY
Definition: ezgeneraldigestusersettings.php:20
eZGeneralDigestUserSettings\TYPE_DAILY
const TYPE_DAILY
Definition: ezgeneraldigestusersettings.php:22
eZGeneralDigestUserSettings\TYPE_MONTHLY
const TYPE_MONTHLY
Definition: ezgeneraldigestusersettings.php:21
eZGeneralDigestUserSettings\fetchByUserId
static fetchByUserId( $userId, $asObject=true)
Definition: ezgeneraldigestusersettings.php:115
eZLocale\instance
static instance( $localeString=false)
Definition: ezlocale.php:1536
eZNotificationCollection\fetchForHandler
static fetchForHandler( $handler, $eventID, $transport)
Definition: eznotificationcollection.php:101
eZNotificationCollection\create
static create( $eventID, $handler, $transport)
Definition: eznotificationcollection.php:58
eZNotificationEventHandler
The class eZNotificationEventHandler does.
Definition: eznotificationeventhandler.php:18
eZNotificationEventHandler\EVENT_SKIPPED
const EVENT_SKIPPED
Definition: eznotificationeventhandler.php:20
eZNotificationEventHandler\EVENT_HANDLED
const EVENT_HANDLED
Definition: eznotificationeventhandler.php:19
eZNotificationEventHandler\attributes
attributes()
Definition: eznotificationeventhandler.php:36
eZNotificationEventHandler\attribute
attribute( $attr)
Definition: eznotificationeventhandler.php:47
eZNotificationSchedule\setDateForItem
static setDateForItem( $item, $settings)
Definition: eznotificationschedule.php:19
eZNotificationTransport\instance
static instance( $transport=false, $forceNewInstance=false)
Definition: eznotificationtransport.php:26
eZPersistentObject\removeObject
static removeObject( $def, $conditions=null, $extraConditions=null)
Definition: ezpersistentobject.php:271
eZSubTreeHandler
The class eZSubTreeHandler does.
Definition: ezsubtreehandler.php:18
eZSubTreeHandler\attributes
attributes()
Definition: ezsubtreehandler.php:27
eZSubTreeHandler\__construct
__construct()
Definition: ezsubtreehandler.php:22
eZSubTreeHandler\handle
handle( $event)
Definition: ezsubtreehandler.php:54
eZSubTreeHandler\TRANSPORT
const TRANSPORT
Definition: ezsubtreehandler.php:20
eZSubTreeHandler\rulesCount
static rulesCount( $user=false)
Definition: ezsubtreehandler.php:273
eZSubTreeHandler\subscribedNodes
subscribedNodes( $user=false)
Definition: ezsubtreehandler.php:251
eZSubTreeHandler\handlePublishEvent
handlePublishEvent( $event, &$parameters)
Definition: ezsubtreehandler.php:69
eZSubTreeHandler\sendMessage
sendMessage( $event, $parameters)
Definition: ezsubtreehandler.php:219
eZSubTreeHandler\rules
static rules( $user=false, $offset=false, $limit=false)
Definition: ezsubtreehandler.php:262
eZSubTreeHandler\NOTIFICATION_HANDLER_ID
const NOTIFICATION_HANDLER_ID
Definition: ezsubtreehandler.php:19
eZSubTreeHandler\attribute
attribute( $attr)
Definition: ezsubtreehandler.php:39
eZSubTreeHandler\cleanup
cleanup()
Definition: ezsubtreehandler.php:331
eZSubTreeHandler\fetchHttpInput
fetchHttpInput( $http, $module)
Definition: ezsubtreehandler.php:284
eZSubTreeHandler\hasAttribute
hasAttribute( $attr)
Definition: ezsubtreehandler.php:34
eZSubtreeNotificationRule\fetchUserList
static fetchUserList( $nodeIDList, $contentObject)
Definition: ezsubtreenotificationrule.php:113
eZSubtreeNotificationRule\definition
static definition()
Definition: ezsubtreenotificationrule.php:18
eZSubtreeNotificationRule\cleanup
static cleanup()
Definition: ezsubtreenotificationrule.php:547
eZSubtreeNotificationRule\fetchListCount
static fetchListCount( $userID)
Definition: ezsubtreenotificationrule.php:90
eZSubtreeNotificationRule\create
static create( $nodeID, $userID, $useDigest=0)
Definition: ezsubtreenotificationrule.php:51
eZSubtreeNotificationRule\fetchNodesForUserID
static fetchNodesForUserID( $userID, $asObject=true)
Definition: ezsubtreenotificationrule.php:59
eZSubtreeNotificationRule\fetchList
static fetchList( $userID, $asObject=true, $offset=false, $limit=false)
Definition: ezsubtreenotificationrule.php:82
eZTemplateDesignResource\instance
static instance()
Definition: eztemplatedesignresource.php:1026
eZTemplate\factory
static factory()
Definition: eztemplate.php:2380
eZUser\currentUser
static currentUser()
Definition: ezuser.php:1716
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.