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
00040 include_once( 'lib/ezutils/classes/ezini.php' );
00041 include_once( "lib/ezfile/classes/ezdir.php" );
00042
00043
00044
00045
00046 define( 'EZ_COLLABORATION_NOTIFICATION_COLLECTION_ONE_FOR_ALL', 1 );
00047 define( 'EZ_COLLABORATION_NOTIFICATION_COLLECTION_PER_USER', 2 );
00048 define( 'EZ_COLLABORATION_NOTIFICATION_COLLECTION_PER_PARTICIPATION_ROLE', 3 );
00049
00050 class eZCollaborationItemHandler
00051 {
00052
00053
00054
00055
00056 function eZCollaborationItemHandler( $typeIdentifier, $typeName, $parameters = array() )
00057 {
00058 $parameters = array_merge( array( 'use-messages' => false,
00059 'type-class-list' => array(),
00060 'notification-collection-handling' => EZ_COLLABORATION_NOTIFICATION_COLLECTION_ONE_FOR_ALL,
00061 'notification-types' => false ),
00062 $parameters );
00063 $typeClassList = $parameters['type-class-list'];
00064 $this->Info['type-identifier'] = $typeIdentifier;
00065 $this->Info['type-class-list'] = $typeClassList;
00066 $this->Info['type-name'] = $typeName;
00067 $this->Info['use-messages'] = $parameters['use-messages'];
00068 $this->Info['notification-collection-handling'] = $parameters['notification-collection-handling'];
00069 $this->Info['notification-types'] = $parameters['notification-types'];
00070 $this->NotificationCollectionHandling = $parameters['notification-collection-handling'];
00071 $this->NotificationTypes = $parameters['notification-types'];
00072 }
00073
00074 function attributes()
00075 {
00076 return array( 'info',
00077 'notification_types' );
00078 }
00079
00080
00081
00082
00083 function hasAttribute( $attr )
00084 {
00085 return in_array( $attr, $this->attributes() );
00086 }
00087
00088
00089
00090
00091 function &attribute( $attribute )
00092 {
00093 if ( $attribute == 'info' )
00094 return $this->Info;
00095 else if ( $attribute == 'notification_types' )
00096 return $this->notificationTypes();
00097 else
00098 {
00099 eZDebug::writeError( "Attribute '$attribute' does not exist", 'eZCollaborationItemHandler::attribute' );
00100 $retValue = null;
00101 return $retValue;
00102 }
00103 }
00104
00105
00106
00107
00108
00109
00110
00111 function ¬ificationTypes()
00112 {
00113 return $this->NotificationTypes;
00114 }
00115
00116
00117
00118
00119
00120 function notificationCollectionHandling()
00121 {
00122 return $this->NotificationCollectionHandling;
00123 }
00124
00125
00126
00127 function notificationParticipantTemplate( $participantRole )
00128 {
00129 return 'participant.tpl';
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139 function handleCollaborationEvent( &$event, &$item, &$parameters )
00140 {
00141 include_once( 'kernel/classes/ezcollaborationitemparticipantlink.php' );
00142 $participantList =& eZCollaborationItemParticipantLink::fetchParticipantList( array( 'item_id' => $item->attribute( 'id' ),
00143 'participant_type' => EZ_COLLABORATION_PARTICIPANT_TYPE_USER,
00144 'as_object' => false ) );
00145
00146 $userIDList = array();
00147 $participantMap = array();
00148 foreach ( $participantList as $participant )
00149 {
00150 $userIDList[] = $participant['participant_id'];
00151 $participantMap[$participant['participant_id']] = $participant;
00152 }
00153
00154
00155 $collaborationIdentifier = $event->attribute( 'data_text1' );
00156 $ruleList =& eZCollaborationNotificationRule::fetchItemTypeList( $collaborationIdentifier, $userIDList, false );
00157 $userIDList = array();
00158 foreach ( $ruleList as $rule )
00159 {
00160 $userIDList[] = $rule['user_id'];
00161 }
00162 $userList = array();
00163 if ( count( $userIDList ) > 0 )
00164 {
00165 $db =& eZDB::instance();
00166 $userIDListText = implode( "', '", $userIDList );
00167 $userIDListText = "'$userIDListText'";
00168 $userList = $db->arrayQuery( "SELECT contentobject_id, email FROM ezuser WHERE contentobject_id IN ( $userIDListText )" );
00169 }
00170 else
00171 return EZ_NOTIFICATIONEVENTHANDLER_EVENT_SKIPPED;
00172
00173 $itemHandler =& $item->attribute( 'handler' );
00174 $collectionHandling = $itemHandler->notificationCollectionHandling();
00175
00176 $db =& eZDB::instance();
00177 $db->begin();
00178 if ( $collectionHandling == EZ_COLLABORATION_NOTIFICATION_COLLECTION_ONE_FOR_ALL )
00179 {
00180 include_once( 'kernel/classes/notification/eznotificationcollection.php' );
00181 include_once( 'kernel/common/template.php' );
00182 $tpl =& templateInit();
00183 $tpl->resetVariables();
00184 $tpl->setVariable( 'collaboration_item', $item );
00185 $result = $tpl->fetch( 'design:notification/handler/ezcollaboration/view/plain.tpl' );
00186 $subject = $tpl->variable( 'subject' );
00187 if ( $tpl->hasVariable( 'message_id' ) )
00188 $parameters['message_id'] = $tpl->variable( 'message_id' );
00189 if ( $tpl->hasVariable( 'references' ) )
00190 $parameters['references'] = $tpl->variable( 'references' );
00191 if ( $tpl->hasVariable( 'reply_to' ) )
00192 $parameters['reply_to'] = $tpl->variable( 'reply_to' );
00193 if ( $tpl->hasVariable( 'from' ) )
00194 $parameters['from'] = $tpl->variable( 'from' );
00195
00196 $collection = eZNotificationCollection::create( $event->attribute( 'id' ),
00197 EZ_COLLABORATION_NOTIFICATION_HANDLER_ID,
00198 EZ_COLLABORATION_NOTIFICATION_HANDLER_TRANSPORT );
00199
00200 $collection->setAttribute( 'data_subject', $subject );
00201 $collection->setAttribute( 'data_text', $result );
00202 $collection->store();
00203
00204 foreach( $userList as $subscriber )
00205 {
00206 $collection->addItem( $subscriber['email'] );
00207 }
00208 }
00209 else if ( $collectionHandling == EZ_COLLABORATION_NOTIFICATION_COLLECTION_PER_PARTICIPATION_ROLE )
00210 {
00211 $userCollection = array();
00212 foreach( $userList as $subscriber )
00213 {
00214 $contentObjectID = $subscriber['contentobject_id'];
00215 $participant = $participantMap[$contentObjectID];
00216 $participantRole = $participant['participant_role'];
00217 $userItem = array( 'participant' => $participant,
00218 'email' => $subscriber['email'] );
00219 if ( !isset( $userCollection[$participantRole] ) )
00220 $userCollection[$participantRole] = array();
00221 $userCollection[$participantRole][] = $userItem;
00222 }
00223
00224 include_once( 'kernel/common/template.php' );
00225 $tpl =& templateInit();
00226 $tpl->resetVariables();
00227 foreach( $userCollection as $participantRole => $collectionItems )
00228 {
00229 $templateName = $itemHandler->notificationParticipantTemplate( $participantRole );
00230 if ( !$templateName )
00231 $templateName = eZCollaborationItemHandler::notificationParticipantTemplate( $participantRole );
00232
00233 $itemInfo = $itemHandler->attribute( 'info' );
00234 $typeIdentifier = $itemInfo['type-identifier'];
00235 include_once( 'kernel/classes/notification/eznotificationcollection.php' );
00236 $tpl->setVariable( 'collaboration_item', $item );
00237 $tpl->setVariable( 'collaboration_participant_role', $participantRole );
00238 $result = $tpl->fetch( 'design:notification/handler/ezcollaboration/view/' . $typeIdentifier . '/' . $templateName );
00239 $subject = $tpl->variable( 'subject' );
00240 if ( $tpl->hasVariable( 'message_id' ) )
00241 $parameters['message_id'] = $tpl->variable( 'message_id' );
00242 if ( $tpl->hasVariable( 'references' ) )
00243 $parameters['references'] = $tpl->variable( 'references' );
00244 if ( $tpl->hasVariable( 'reply_to' ) )
00245 $parameters['reply_to'] = $tpl->variable( 'reply_to' );
00246 if ( $tpl->hasVariable( 'from' ) )
00247 $parameters['from'] = $tpl->variable( 'from' );
00248
00249 $collection = eZNotificationCollection::create( $event->attribute( 'id' ),
00250 EZ_COLLABORATION_NOTIFICATION_HANDLER_ID,
00251 EZ_COLLABORATION_NOTIFICATION_HANDLER_TRANSPORT );
00252
00253 $collection->setAttribute( 'data_subject', $subject );
00254 $collection->setAttribute( 'data_text', $result );
00255 $collection->store();
00256 foreach ( $collectionItems as $collectionItem )
00257 {
00258 $collection->addItem( $collectionItem['email'] );
00259 }
00260 }
00261 }
00262 else if ( $collectionHandling == EZ_COLLABORATION_NOTIFICATION_COLLECTION_PER_USER )
00263 {
00264 }
00265 else
00266 {
00267 eZDebug::writeError( "Unknown collaboration notification collection handling type '$collectionHandling', skipping notification",
00268 'eZCollaborationItemHandler::handleCollaborationEvent' );
00269 }
00270 $db->commit();
00271
00272 return EZ_NOTIFICATIONEVENTHANDLER_EVENT_HANDLED;
00273 }
00274
00275
00276
00277
00278 function hasContentAttribute( &$collaborationItem, $attribute )
00279 {
00280 $content =& $collaborationItem->content();
00281 if ( is_array( $content ) )
00282 {
00283 return array_key_exists( $attribute, $content );
00284 }
00285 return false;
00286 }
00287
00288
00289
00290
00291 function &contentAttribute( &$collaborationItem, $attribute )
00292 {
00293 $content =& $collaborationItem->content();
00294 if ( is_array( $content ) )
00295 {
00296 if ( array_key_exists( $attribute, $content ) )
00297 return $content[$attribute];
00298 }
00299 $content = null;
00300 return $content;
00301 }
00302
00303
00304
00305
00306 function classes()
00307 {
00308 return $this->Info['type-class-list'];
00309 }
00310
00311
00312
00313
00314 function template( $viewMode )
00315 {
00316 $templateName = $this->templateName();
00317 return "design:collaboration/handlers/view/$viewMode/$templateName";
00318 }
00319
00320
00321
00322
00323
00324 function templateName()
00325 {
00326 return $this->Info['type-identifier'] . '.tpl';
00327 }
00328
00329
00330
00331
00332 function title( &$collaborationItem )
00333 {
00334 return $this->Info['type-name'];
00335 }
00336
00337
00338
00339
00340
00341
00342 function useMessages( &$collaborationItem )
00343 {
00344 return $this->Info['use-messages'];
00345 }
00346
00347
00348
00349
00350
00351
00352 function messageCount( &$collaborationItem )
00353 {
00354 return 0;
00355 }
00356
00357
00358
00359
00360
00361
00362 function unreadMessageCount( &$collaborationItem )
00363 {
00364 return 0;
00365 }
00366
00367
00368
00369
00370
00371
00372 function readItem( &$collaborationItem, $viewMode = false )
00373 {
00374 }
00375
00376
00377
00378
00379
00380
00381 function removeItem( &$collaborationItem )
00382 {
00383 }
00384
00385
00386
00387
00388
00389 function &ini()
00390 {
00391 return eZINI::instance( 'collaboration.ini' );
00392 }
00393
00394
00395
00396
00397
00398 function participantTypeString( $participantType )
00399 {
00400 return null;
00401 }
00402
00403
00404
00405
00406
00407 function participantRoleString( $participantRole )
00408 {
00409 return null;
00410 }
00411
00412
00413
00414
00415
00416 function roleName( $collaborationID, $roleID )
00417 {
00418 return null;
00419 }
00420
00421
00422
00423
00424
00425 function content( &$collaborationItem )
00426 {
00427 return null;
00428 }
00429
00430
00431
00432
00433
00434
00435
00436 function handleCustomAction( &$module, &$collaborationItem )
00437 {
00438 }
00439
00440
00441
00442
00443 function isCustomAction( $name )
00444 {
00445 $http =& eZHTTPTool::instance();
00446 $postVariable = 'CollaborationAction_' . $name;
00447 return $http->hasPostVariable( $postVariable );
00448 }
00449
00450
00451
00452
00453 function hasCustomInput( $name )
00454 {
00455 $http =& eZHTTPTool::instance();
00456 $postVariable = 'Collaboration_' . $name;
00457 return $http->hasPostVariable( $postVariable );
00458 }
00459
00460
00461
00462
00463 function customInput( $name )
00464 {
00465 $http =& eZHTTPTool::instance();
00466 $postVariable = 'Collaboration_' . $name;
00467 return $http->postVariable( $postVariable );
00468 }
00469
00470
00471
00472
00473
00474
00475 function defaultRepositories()
00476 {
00477 $collabINI =& eZCollaborationItemHandler::ini();
00478 return $collabINI->variable( 'HandlerSettings', 'Repositories' );
00479 }
00480
00481
00482
00483
00484
00485
00486 function extensionRepositories()
00487 {
00488 $collabINI =& eZCollaborationItemHandler::ini();
00489 return $collabINI->variable( 'HandlerSettings', 'Extensions' );
00490 }
00491
00492
00493
00494
00495
00496
00497 function handlerRepositories()
00498 {
00499 $extensions = eZCollaborationItemHandler::extensionRepositories();
00500 $repositories = eZCollaborationItemHandler::defaultRepositories();
00501 $extensionRoot = eZExtension::baseDirectory();
00502 foreach ( $extensions as $extension )
00503 {
00504 $handlerPath = eZDir::path( array( $extensionRoot, $extension, 'collaboration' ) );
00505 if ( file_exists( $handlerPath ) )
00506 $repositories[] = $handlerPath;
00507 }
00508 return $repositories;
00509 }
00510
00511
00512
00513
00514
00515 function activeHandlers()
00516 {
00517 $collabINI =& eZCollaborationItemHandler::ini();
00518 return $collabINI->variable( 'HandlerSettings', 'Active' );
00519 }
00520
00521
00522
00523
00524
00525
00526 function &instantiate( $handler, $repositories = false )
00527 {
00528 $objectCache =& $GLOBALS["eZCollaborationHandlerObjectCache"];
00529 if ( !isset( $objectCache ) )
00530 $objectCache = array();
00531 if ( isset( $objectCache[$handler] ) )
00532 return $objectCache[$handler];
00533 if ( $repositories === false )
00534 {
00535 $repositories = eZCollaborationItemHandler::handlerRepositories();
00536 }
00537 $handlerInstance = null;
00538 $foundHandlerFile = false;
00539 $foundHandler = false;
00540 foreach ( $repositories as $repository )
00541 {
00542 $handlerFile = $handler . 'collaborationhandler.php';
00543 $handlerClass = $handler . 'collaborationhandler';
00544 $handlerPath = eZDir::path( array( $repository, $handler, $handlerFile ) );
00545 if ( file_exists( $handlerPath ) )
00546 {
00547 $foundHandlerFile = true;
00548 include_once( $handlerPath );
00549 if ( class_exists( $handlerClass ) )
00550 {
00551 $foundHandler = true;
00552 $handlerInstance = new $handlerClass();
00553 $objectCache[$handler] =& $handlerInstance;
00554 $handlerClasses = $handlerInstance->classes();
00555 foreach ( $handlerClasses as $handlerClass )
00556 {
00557 }
00558 }
00559 }
00560 }
00561 if ( !$foundHandlerFile )
00562 eZDebug::writeWarning( "Collaboration file '$handlerFile' could not be found in " . implode( ', ', $repositories ), 'eZCollaborationItemHandler::fetchList' );
00563 else if ( !$foundHandler )
00564 eZDebug::writeWarning( "Collaboration class '$handlerClass' does not exist", 'eZCollaborationItemHandler::fetchList' );
00565 return $handlerInstance;
00566 }
00567
00568
00569
00570
00571
00572
00573 function &fetchList()
00574 {
00575 $list =& $GLOBALS['eZCollaborationList'];
00576 if ( isset( $list ) )
00577 return $list;
00578 $list = array();
00579 $activeHandlers = eZCollaborationItemHandler::activeHandlers();
00580 $repositories = eZCollaborationItemHandler::handlerRepositories();
00581 foreach ( $activeHandlers as $handler )
00582 {
00583 $handlerInstance =& eZCollaborationItemHandler::instantiate( $handler, $repositories );
00584 if ( $handlerInstance !== null )
00585 $list[] =& $handlerInstance;
00586 }
00587 return $list;
00588 }
00589
00590
00591 var $Info;
00592 }
00593
00594 ?>