eZ Publish  [4.0]
ezcollaborationitemhandler.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZCollaborationItemHandler class
00004 //
00005 // Created on: <22-Jan-2003 16:24:33 amos>
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 ezcollaborationitemhandler.php
00032 */
00033 
00034 /*!
00035   \class eZCollaborationItemHandler ezcollaborationitemhandler.php
00036   \brief The class eZCollaborationItemHandler does
00037 
00038 */
00039 
00040 //include_once( 'lib/ezutils/classes/ezini.php' );
00041 //include_once( "lib/ezfile/classes/ezdir.php" );
00042 
00043 class eZCollaborationItemHandler
00044 {
00045     /*
00046      Definitions for notification handling for collaboration handlers.
00047     */
00048     const NOTIFICATION_COLLECTION_ONE_FOR_ALL = 1;
00049     const NOTIFICATION_COLLECTION_PER_USER = 2;
00050     const NOTIFICATION_COLLECTION_PER_PARTICIPATION_ROLE = 3;
00051 
00052     /*!
00053      Initializes the handler with identifier and name.
00054      Optional parameters can be placed in \a $parameters.
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' => self::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      \return true if the attribute \a $attribute exists.
00082     */
00083     function hasAttribute( $attr )
00084     {
00085         return in_array( $attr, $this->attributes() );
00086     }
00087 
00088     /*!
00089      \return the attribute \a $attribute if it exists or \c null.
00090     */
00091     function attribute( $attribute )
00092     {
00093         if ( $attribute == 'info' )
00094         {
00095             return $this->Info;
00096         }
00097         else if ( $attribute == 'notification_types' )
00098         {
00099             return $this->notificationTypes();
00100         }
00101 
00102         eZDebug::writeError( "Attribute '$attribute' does not exist", 'eZCollaborationItemHandler::attribute' );
00103         return null;
00104     }
00105 
00106     /*!
00107       \return what kind of notification types this handler supports. Can either return an array or a boolean.
00108       If it returns \c true the handler supports notification but does not have subnotifications.
00109       If it returns \c false the handler does not support notificiation.
00110       If it returns an array the array contains a list associative arrays each containing a \c name and \c value entry.
00111     */
00112     function notificationTypes()
00113     {
00114         return $this->NotificationTypes;
00115     }
00116 
00117     /*!
00118      \return how the handler wants collections to be made.
00119      \note The default is to create one collection for all participants.
00120     */
00121     function notificationCollectionHandling()
00122     {
00123         return $this->NotificationCollectionHandling;
00124     }
00125 
00126     /*!
00127     */
00128     function notificationParticipantTemplate( $participantRole )
00129     {
00130         return 'participant.tpl';
00131     }
00132 
00133     /*!
00134      \static
00135      Handles a notification event for collaboration items.
00136      \note The default implementation sends out a generic email.
00137      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00138      the calls within a db transaction; thus within db->begin and db->commit.
00139     */
00140     static function handleCollaborationEvent( $event, $item, &$parameters )
00141     {
00142         //include_once( 'kernel/classes/ezcollaborationitemparticipantlink.php' );
00143         $participantList = eZCollaborationItemParticipantLink::fetchParticipantList( array( 'item_id' => $item->attribute( 'id' ),
00144                                                                                              'participant_type' => eZCollaborationItemParticipantLink::TYPE_USER,
00145                                                                                              'as_object' => false ) );
00146 
00147         $userIDList = array();
00148         $participantMap = array();
00149         foreach ( $participantList as $participant )
00150         {
00151             $userIDList[] = $participant['participant_id'];
00152             $participantMap[$participant['participant_id']] = $participant;
00153         }
00154 
00155 //         $collaborationIdentifier = $event->attribute( 'collaboration_identifier' );
00156         $collaborationIdentifier = $event->attribute( 'data_text1' );
00157         $ruleList = eZCollaborationNotificationRule::fetchItemTypeList( $collaborationIdentifier, $userIDList, false );
00158         $userIDList = array();
00159         foreach ( $ruleList as $rule )
00160         {
00161             $userIDList[] = $rule['user_id'];
00162         }
00163         $userList = array();
00164         if ( count( $userIDList ) > 0 )
00165         {
00166             $db = eZDB::instance();
00167             $userIDListText = $db->generateSQLINStatement( $userIDList, 'contentobject_id', false, false, 'int' );
00168             $userList = $db->arrayQuery( "SELECT contentobject_id, email FROM ezuser WHERE $userIDListText" );
00169         }
00170         else
00171             return eZNotificationEventHandler::EVENT_SKIPPED;
00172 
00173         $itemHandler = $item->attribute( 'handler' );
00174         $collectionHandling = $itemHandler->notificationCollectionHandling();
00175 
00176         $db = eZDB::instance();
00177         $db->begin();
00178         if ( $collectionHandling == self::NOTIFICATION_COLLECTION_ONE_FOR_ALL )
00179         {
00180             //include_once( 'kernel/classes/notification/eznotificationcollection.php' );
00181             require_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                                                             eZCollaborationNotificationHandler::NOTIFICATION_HANDLER_ID,
00198                                                             eZCollaborationNotificationHandler::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 == self::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             require_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                                                                 eZCollaborationNotificationHandler::NOTIFICATION_HANDLER_ID,
00251                                                                 eZCollaborationNotificationHandler::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 == self::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 eZNotificationEventHandler::EVENT_HANDLED;
00273     }
00274 
00275     /*!
00276      \return true if the attribute \a $attribute exists in the content data.
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      \return the attribute \a $attribute if it exists in the content data or \c null.
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      \return a list of classes this handler supports.
00305     */
00306     function classes()
00307     {
00308         return $this->Info['type-class-list'];
00309     }
00310 
00311     /*!
00312      \return the template name for the viewmode \a $viewmode.
00313     */
00314     function template( $viewMode )
00315     {
00316         $templateName = $this->templateName();
00317         return "design:collaboration/handlers/view/$viewMode/$templateName";
00318     }
00319 
00320     /*!
00321      \return the name of the template file for this handler.
00322      Default is to append .tpl to the identifier.
00323     */
00324     function templateName()
00325     {
00326         return $this->Info['type-identifier'] . '.tpl';
00327     }
00328 
00329     /*!
00330      \return the title of the collaboration item.
00331     */
00332     function title( $collaborationItem )
00333     {
00334         return $this->Info['type-name'];
00335     }
00336 
00337     /*!
00338      \return true if the collaboration item \a $collaborationItem supports messages.
00339      \note The handler can either determine this by passing \a $useMessages to the constructor
00340            or by reimplementing this function to do it per item.
00341     */
00342     function useMessages( $collaborationItem )
00343     {
00344         return $this->Info['use-messages'];
00345     }
00346 
00347     /*!
00348      \return the number of messages for the collaboration item \a $collaborationItem.
00349      \note The default implementation returns 0, if you want real counts
00350            the handler must reimplement this function.
00351     */
00352     function messageCount( $collaborationItem )
00353     {
00354         return 0;
00355     }
00356 
00357     /*!
00358      \return the number of unread messages for the collaboration item \a $collaborationItem.
00359      \note The default implementation returns 0, if you want real counts
00360            the handler must reimplement this function.
00361     */
00362     function unreadMessageCount( $collaborationItem )
00363     {
00364         return 0;
00365     }
00366 
00367     /*!
00368      This is called whenever the item is considered to be read,
00369      it can be used by handlers to update when the item was last read.
00370      \note Default implementation does nothing.
00371     */
00372     function readItem( $collaborationItem, $viewMode = false )
00373     {
00374     }
00375 
00376     /*!
00377      This is called whenever a collaboration item is to be removed.
00378      Reimplementing this function can be used to cleanup external tables
00379      or other resources.
00380     */
00381     function removeItem( $collaborationItem )
00382     {
00383     }
00384 
00385     /*!
00386      \static
00387      \return the ini object which handles collaboration settings.
00388     */
00389     static function ini()
00390     {
00391         return eZINI::instance( 'collaboration.ini' );
00392     }
00393 
00394     /*!
00395      \return a textual representation of the participant type id \a $participantType
00396      \note It's up to the real handlers to implement this if they use custom participation types.
00397     */
00398     function participantTypeString( $participantType )
00399     {
00400         return null;
00401     }
00402 
00403     /*!
00404      \return a textual representation of the participant role id \a $participantRole
00405      \note It's up to the real handlers to implement this if they use custom participation roles.
00406     */
00407     function participantRoleString( $participantRole )
00408     {
00409         return null;
00410     }
00411 
00412     /*!
00413      \return a description of the role id \a $roleID in the current language.
00414      \note It's up to the real handlers to implement this if they use custom participation roles.
00415     */
00416     function roleName( $collaborationID, $roleID )
00417     {
00418         return null;
00419     }
00420 
00421     /*!
00422      \return the content of the collaborationitem.
00423      \note This is specific to the item type, some might return an array and others an object.
00424     */
00425     function content( $collaborationItem )
00426     {
00427         return null;
00428     }
00429 
00430     /*!
00431      This function is called when a custom action is executed for a specific collaboration item.
00432      The module object is available in \a $module and the item in \a $collaborationItem.
00433      \note The default does nothing, the function must be reimplemented in real handlers.
00434      \sa isCustomAction
00435     */
00436     function handleCustomAction( $module, $collaborationItem )
00437     {
00438     }
00439 
00440     /*!
00441      \return true if the current custom action is \a $name.
00442     */
00443     function isCustomAction( $name )
00444     {
00445         $http = eZHTTPTool::instance();
00446         $postVariable = 'CollaborationAction_' . $name;
00447         return $http->hasPostVariable( $postVariable );
00448     }
00449 
00450     /*!
00451      \return true if the custom input variable \a $name exists.
00452     */
00453     function hasCustomInput( $name )
00454     {
00455         $http = eZHTTPTool::instance();
00456         $postVariable = 'Collaboration_' . $name;
00457         return $http->hasPostVariable( $postVariable );
00458     }
00459 
00460     /*!
00461      \return value of the custom input variable \a $name.
00462     */
00463     function customInput( $name )
00464     {
00465         $http = eZHTTPTool::instance();
00466         $postVariable = 'Collaboration_' . $name;
00467         return $http->postVariable( $postVariable );
00468     }
00469 
00470     /*!
00471      \static
00472      \return an array with directories which acts as default collaboration repositories.
00473      \sa handlerRepositories
00474     */
00475     static function defaultRepositories()
00476     {
00477         $collabINI = eZCollaborationItemHandler::ini();
00478         return $collabINI->variable( 'HandlerSettings', 'Repositories' );
00479     }
00480 
00481     /*!
00482      \static
00483      \return an array with directories which acts as collaboration extension repositories.
00484      \sa handlerRepositories
00485     */
00486     static function extensionRepositories()
00487     {
00488         $collabINI = eZCollaborationItemHandler::ini();
00489         return $collabINI->variable( 'HandlerSettings', 'Extensions' );
00490     }
00491 
00492     /*!
00493      \static
00494      \return an array with directories which acts as collaboration repositories.
00495      \sa defaultRepositories, extensionRepositories
00496     */
00497     static 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      \static
00513      \return an array with handler identifiers that are considered active.
00514     */
00515     static function activeHandlers()
00516     {
00517         $collabINI = eZCollaborationItemHandler::ini();
00518         return $collabINI->variable( 'HandlerSettings', 'Active' );
00519     }
00520 
00521     /*!
00522      \static
00523      \return a unique instance of the handler for the identifier \a $handler.
00524      If \a $repositories is left out it will use the handlerRepositories.
00525     */
00526     static 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         {
00563             eZDebug::writeWarning( "Collaboration file '$handlerFile' could not be found in " . implode( ', ', $repositories ), 'eZCollaborationItemHandler::fetchList' );
00564         }
00565         else if ( !$foundHandler )
00566         {
00567             eZDebug::writeWarning( "Collaboration class '$handlerClass' does not exist", 'eZCollaborationItemHandler::fetchList' );
00568         }
00569         return $handlerInstance;
00570     }
00571 
00572     /*!
00573      \static
00574      \return a list of collaboration handler objects.
00575      \sa instantiate, activeHandlers
00576     */
00577     static function fetchList()
00578     {
00579         $list =& $GLOBALS['eZCollaborationList'];
00580         if ( isset( $list ) )
00581             return $list;
00582         $list = array();
00583         $activeHandlers = eZCollaborationItemHandler::activeHandlers();
00584         $repositories = eZCollaborationItemHandler::handlerRepositories();
00585         foreach ( $activeHandlers as $handler )
00586         {
00587             $handlerInstance = eZCollaborationItemHandler::instantiate( $handler, $repositories );
00588             if ( $handlerInstance !== null )
00589                 $list[] = $handlerInstance;
00590         }
00591         return $list;
00592     }
00593 
00594     /// \privatesection
00595     public $Info;
00596 }
00597 
00598 ?>