eZ Publish  [4.0]
ezcollaborationitem.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZCollaborationItem class
00004 //
00005 // Created on: <22-Jan-2003 15:44:48 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 ezcollaborationitem.php
00032 */
00033 
00034 /*!
00035   \class eZCollaborationItem ezcollaborationitem.php
00036   \brief The class eZCollaborationItem does
00037 
00038 */
00039 
00040 //include_once( 'kernel/classes/ezpersistentobject.php' );
00041 //include_once( 'kernel/classes/ezcollaborationitemstatus.php' );
00042 
00043 class eZCollaborationItem extends eZPersistentObject
00044 {
00045     const STATUS_ACTIVE = 1;
00046     const STATUS_INACTIVE = 2;
00047     const STATUS_ARCHIVE = 3;
00048 
00049     /*!
00050      Constructor
00051     */
00052     function eZCollaborationItem( $row )
00053     {
00054         $this->eZPersistentObject( $row );
00055     }
00056 
00057     static function definition()
00058     {
00059         return array( 'fields' => array( 'id' => array( 'name' => 'ID',
00060                                                         'datatype' => 'integer',
00061                                                         'default' => 0,
00062                                                         'required' => true ),
00063                                          'type_identifier' => array( 'name' => 'TypeIdentifier',
00064                                                                      'datatype' => 'string',
00065                                                                      'default' => '',
00066                                                                      'required' => true ),
00067                                          'creator_id' =>  array( 'name' => 'CreatorID',
00068                                                                  'datatype' => 'integer',
00069                                                                  'default' => 0,
00070                                                                  'required' => true,
00071                                                                  'foreign_class' => 'eZUser',
00072                                                                  'foreign_attribute' => 'contentobject_id',
00073                                                                  'multiplicity' => '1..*' ),
00074                                          'status' => array( 'name' => 'Status',
00075                                                             'datatype' => 'integer',
00076                                                             'default' => 1,
00077                                                             'required' => true ),
00078                                          'data_text1' => array( 'name' => 'DataText1',
00079                                                                 'datatype' => 'text',
00080                                                                 'default' => '',
00081                                                                 'required' => true ),
00082                                          'data_text2' => array( 'name' => 'DataText2',
00083                                                                 'datatype' => 'text',
00084                                                                 'default' => '',
00085                                                                 'required' => true ),
00086                                          'data_text3' => array( 'name' => 'DataText3',
00087                                                                 'datatype' => 'text',
00088                                                                 'default' => '',
00089                                                                 'required' => true ),
00090                                          'data_int1' => array( 'name' => 'DataInt1',
00091                                                                'datatype' => 'integer',
00092                                                                'default' => 0,
00093                                                                'required' => true ),
00094                                          'data_int2' => array( 'name' => 'DataInt2',
00095                                                                'datatype' => 'integer',
00096                                                                'default' => 0,
00097                                                                'required' => true ),
00098                                          'data_int3' => array( 'name' => 'DataInt3',
00099                                                                'datatype' => 'integer',
00100                                                                'default' => 0,
00101                                                                'required' => true ),
00102                                          'data_float1' => array( 'name' => 'DataFloat1',
00103                                                                  'datatype' => 'float',
00104                                                                  'default' => 0,
00105                                                                  'required' => true ),
00106                                          'data_float2' => array( 'name' => 'DataFloat2',
00107                                                                  'datatype' => 'float',
00108                                                                  'default' => 0,
00109                                                                  'required' => true ),
00110                                          'data_float3' => array( 'name' => 'DataFloat3',
00111                                                                  'datatype' => 'float',
00112                                                                  'default' => 0,
00113                                                                  'required' => true ),
00114                                          'created' => array( 'name' => 'Created',
00115                                                              'datatype' => 'integer',
00116                                                              'default' => 0,
00117                                                              'required' => true ),
00118                                          'modified' => array( 'name' => 'Modified',
00119                                                               'datatype' => 'integer',
00120                                                               'default' => 0,
00121                                                               'required' => true ) ),
00122                       'keys' => array( 'id' ),
00123                       'function_attributes' => array( 'creator' => 'creator',
00124                                                       'is_creator' => 'isCreator',
00125                                                       'participant_list' => 'participantList',
00126                                                       'user_status' => 'userStatus',
00127                                                       'handler' => 'handler',
00128                                                       'use_messages' => 'useMessages',
00129                                                       'message_count' => 'messageCount',
00130                                                       'unread_message_count' => 'unreadMessageCount',
00131                                                       'content' => 'content',
00132                                                       'title' => 'title' ),
00133                       'increment_key' => 'id',
00134                       'class_name' => 'eZCollaborationItem',
00135                       'sort' => array( 'modified' => 'asc' ),
00136                       'name' => 'ezcollab_item' );
00137     }
00138 
00139     static function create( $typeIdentifier, $creatorID, $status = self::STATUS_ACTIVE )
00140     {
00141         $date_time = time();
00142         $row = array(
00143             'id' => null,
00144             'type_identifier' => $typeIdentifier,
00145             'creator_id' => $creatorID,
00146             'status' => $status,
00147             'created' => $date_time,
00148             'modified' => $date_time );
00149         return new eZCollaborationItem( $row );
00150     }
00151 
00152     /*!
00153      Creates a collaboration notification event and stores it.
00154      \a subType can be used to specify a sub type of this collaboration item.
00155     */
00156     function createNotificationEvent( $subType = false )
00157     {
00158         $handler = $this->attribute( 'handler' );
00159         $info = $handler->attribute( 'info' );
00160         $type = $info['type-identifier'];
00161         if ( $subType )
00162             $type .= '_' . $subType;
00163         //include_once( 'kernel/classes/notification/eznotificationevent.php' );
00164         $event = eZNotificationEvent::create( 'ezcollaboration', array( 'collaboration_id' => $this->attribute( 'id' ),
00165                                                                          'collaboration_identifier' => $type ) );
00166         $event->store();
00167         return $event;
00168     }
00169 
00170     static function fetch( $id, $creatorID = false, $asObject = true )
00171     {
00172         $conditions = array( 'id' => $id );
00173         if ( $creatorID !== false )
00174             $conditions['creator_id'] = $creatorID;
00175         return eZPersistentObject::fetchObject( eZCollaborationItem::definition(),
00176                                                 null,
00177                                                 $conditions,
00178                                                 $asObject );
00179     }
00180 
00181     function creator()
00182     {
00183         if ( isset( $this->CreatorID ) and $this->CreatorID )
00184         {
00185             //include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );
00186             return eZUser::fetch( $this->CreatorID );
00187         }
00188         return null;
00189     }
00190 
00191     function isCreator()
00192     {
00193         if ( isset( $this->CreatorID ) and $this->CreatorID )
00194         {
00195             //include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );
00196             return ( eZUser::currentUserID() == $this->CreatorID );
00197         }
00198         return false;
00199     }
00200 
00201     function participantList()
00202     {
00203         //include_once( 'kernel/classes/ezcollaborationitemparticipantlink.php' );
00204         return eZCollaborationItemParticipantLink::fetchParticipantList( array('item_id' => $this->ID ) );
00205     }
00206 
00207     function userStatus()
00208     {
00209         //include_once( 'kernel/classes/ezcollaborationitemstatus.php' );
00210         //include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );
00211         $userID = eZUser::currentUserID();
00212         return eZCollaborationItemStatus::fetch( $this->ID, $userID );
00213     }
00214 
00215     function handler()
00216     {
00217         //include_once( 'kernel/classes/ezcollaborationitemhandler.php' );
00218         return eZCollaborationItemHandler::instantiate( $this->attribute( 'type_identifier' ) );
00219     }
00220 
00221     /*!
00222      \return true if the item uses messages.
00223      \note It's up to each handler to control this.
00224     */
00225     function useMessages()
00226     {
00227         $handler = $this->handler();
00228         if ( $handler )
00229         {
00230             return $handler->useMessages( $this );
00231         }
00232         return null;
00233     }
00234 
00235     /*!
00236      \return the number of messages in this item.
00237      \note The message count is purely abstract and it's up to each handler to return a valid count.
00238     */
00239     function messageCount()
00240     {
00241         $handler = $this->handler();
00242         if ( $handler )
00243         {
00244             return $handler->messageCount( $this );
00245         }
00246         return 0;
00247     }
00248 
00249     /*!
00250      \return the number of unread messages in this item.
00251      \note The message count is purely abstract and it's up to each handler to return a valid count.
00252            It's also up the handler to keep track of which messages are read or not.
00253     */
00254     function unreadMessageCount()
00255     {
00256         $handler = $this->handler();
00257         if ( $handler )
00258         {
00259             return $handler->unreadMessageCount( $this );
00260         }
00261         return 0;
00262     }
00263 
00264     function content()
00265     {
00266         $handler = $this->handler();
00267         if ( $handler )
00268         {
00269             return $handler->content( $this );
00270         }
00271         return null;
00272     }
00273 
00274     function title()
00275     {
00276         $handler = $this->handler();
00277         if ( $handler )
00278         {
00279             return $handler->title( $this );
00280         }
00281         return null;
00282     }
00283 
00284 
00285     function hasContentAttribute( $attribute )
00286     {
00287         $handler =& $this->handler();
00288         if ( !$handler )
00289             $hasContentAttribute = null;
00290         else
00291             $hasContentAttribute = $handler->hasContentAttribute( $this, $attribute );
00292         return $hasContentAttribute;
00293     }
00294 
00295     function contentAttribute( $attribute )
00296     {
00297         $handler = $this->handler();
00298         if ( $handler )
00299         {
00300             return $handler->contentAttribute( $this, $attribute );
00301         }
00302         return null;
00303     }
00304 
00305     function setIsActive( $active, $userID = false )
00306     {
00307         $active = intval($active);
00308         eZCollaborationItemStatus::updateFields( $this->attribute( 'id' ), $userID, array( 'is_active' => $active ) );
00309     }
00310 
00311     static function fetchListCount( $parameters = array() )
00312     {
00313         return eZCollaborationItem::fetchListTool( $parameters, true );
00314 //         $parameters = array_merge( array( 'status' => false
00315 //                                           'is_active' => null,
00316 //                                           'is_read' => null ),
00317 //                                    $parameters );
00318 //         $statusTypes = $parameters['status'];
00319 //         $isRead = $parameters['is_read'];
00320 //         $isActive = $parameters['is_active'];
00321 
00322 //         $user = eZUser::currentUser();
00323 //         $userID =& $user->attribute( 'contentobject_id' );
00324 
00325 //         $isReadText = '';
00326 //         if ( $isRead !== null )
00327 //         {
00328 //             $isReadValue = $isRead ? 1 : 0;
00329 //             $isReadText = "ezcollab_item_group_link.is_read = '$isReadValue' AND";
00330 //         }
00331 
00332 //         $isActiveText = '';
00333 //         if ( $isActive !== null )
00334 //         {
00335 //             $isActiveValue = $isActive ? 1 : 0;
00336 //             $isActiveText = "ezcollab_item_group_link.is_active = '$isActiveValue' AND";
00337 //         }
00338 
00339 //         $statusText = '';
00340 //         if ( $statusTypes === false )
00341 //             $statusTypes = array( self::STATUS_ACTIVE,
00342 //                                   self::STATUS_INACTIVE );
00343 //         $statusText = implode( ', ', $statusTypes );
00344 
00345 //         $sql = "SELECT count( ezcollab_item.id ) as count
00346 //                 FROM
00347 //                        ezcollab_item,
00348 //                        ezcollab_item_group_link
00349 //                 WHERE  ezcollab_item.status IN ( $statusText ) AND
00350 //                        $isReadText
00351 //                        $isActiveText
00352 //                        ezcollab_item.id = ezcollab_item_group_link.collaboration_id AND
00353 //                        ezcollab_item_group_link.user_id=$userID";
00354 
00355 //         $db = eZDB::instance();
00356 //         $itemCount = $db->arrayQuery( $sql );
00357 //         return $itemCount[0]['count'];
00358     }
00359 
00360     function setLastRead( $userID = false, $timestamp = false )
00361     {
00362         if ( $userID === false )
00363             $userID = eZUser::currentUserID();
00364         if ( $timestamp === false )
00365             $timestamp = time();
00366         $collaborationID = $this->attribute( 'id' );
00367 
00368         eZCollaborationItemStatus::setLastRead( $collaborationID, $userID, $timestamp );
00369         eZCollaborationItemParticipantLink::setLastRead( $collaborationID, $userID, $timestamp );
00370     }
00371 
00372     static function fetchList( $parameters = array() )
00373     {
00374         return eZCollaborationItem::fetchListTool( $parameters, false );
00375     }
00376 
00377     static function fetchListTool( $parameters = array(), $asCount )
00378     {
00379         $parameters = array_merge( array( 'as_object' => true,
00380                                           'offset' => false,
00381                                           'parent_group_id' => false,
00382                                           'limit' => false,
00383                                           'is_active' => null,
00384                                           'is_read' => null,
00385                                           'status' => false,
00386                                           'sort_by' => false ),
00387                                    $parameters );
00388         $asObject = $parameters['as_object'];
00389         $offset = $parameters['offset'];
00390         $limit = $parameters['limit'];
00391         $statusTypes = $parameters['status'];
00392         $isRead = $parameters['is_read'];
00393         $isActive = $parameters['is_active'];
00394         $parentGroupID = $parameters['parent_group_id'];
00395 
00396         $sortText = '';
00397         if ( !$asCount )
00398         {
00399             $sortCount = 0;
00400             $sortList = $parameters['sort_by'];
00401             if ( is_array( $sortList ) and
00402                  count( $sortList ) > 0 )
00403             {
00404                 if ( count( $sortList ) > 1 and
00405                      !is_array( $sortList[0] ) )
00406                 {
00407                     $sortList = array( $sortList );
00408                 }
00409             }
00410             if ( $sortList !== false )
00411             {
00412                 $sortingFields = '';
00413                 foreach ( $sortList as $sortBy )
00414                 {
00415                     if ( is_array( $sortBy ) and count( $sortBy ) > 0 )
00416                     {
00417                         if ( $sortCount > 0 )
00418                             $sortingFields .= ', ';
00419                         $sortField = $sortBy[0];
00420                         switch ( $sortField )
00421                         {
00422                             case 'created':
00423                             {
00424                                 $sortingFields .= 'ezcollab_item_group_link.created';
00425                             } break;
00426                             case 'modified':
00427                             {
00428                                 $sortingFields .= 'ezcollab_item_group_link.modified';
00429                             } break;
00430                             default:
00431                             {
00432                                 eZDebug::writeWarning( 'Unknown sort field: ' . $sortField, 'eZCollaborationItem::fetchList' );
00433                                 continue;
00434                             };
00435                         }
00436                         $sortOrder = true; // true is ascending
00437                         if ( isset( $sortBy[1] ) )
00438                             $sortOrder = $sortBy[1];
00439                         $sortingFields .= $sortOrder ? ' ASC' : ' DESC';
00440                         ++$sortCount;
00441                     }
00442                 }
00443             }
00444             if ( $sortCount == 0 )
00445             {
00446                 $sortingFields = ' ezcollab_item_group_link.modified DESC';
00447             }
00448             $sortText = "ORDER BY $sortingFields";
00449         }
00450 
00451         $parentGroupText = '';
00452         if ( $parentGroupID > 0 )
00453         {
00454             $parentGroupText = "ezcollab_item_group_link.group_id = '$parentGroupID' AND";
00455         }
00456 
00457         $isReadText = '';
00458         if ( $isRead !== null )
00459         {
00460             $isReadValue = $isRead ? 1 : 0;
00461             $isReadText = "ezcollab_item_status.is_read = '$isReadValue' AND";
00462         }
00463 
00464         $isActiveText = '';
00465         if ( $isActive !== null )
00466         {
00467             $isActiveValue = $isActive ? 1 : 0;
00468             $isActiveText = "ezcollab_item_status.is_active = '$isActiveValue' AND";
00469         }
00470 
00471         $userID = eZUser::currentUserID();
00472 
00473         $statusText = '';
00474         if ( $statusTypes === false )
00475             $statusTypes = array( self::STATUS_ACTIVE,
00476                                   self::STATUS_INACTIVE );
00477         $statusText = implode( ', ', $statusTypes );
00478 
00479         if ( $asCount )
00480             $selectText = 'count( ezcollab_item.id ) as count';
00481         else
00482             $selectText = 'ezcollab_item.*, ezcollab_item_status.is_read, ezcollab_item_status.is_active, ezcollab_item_status.last_read';
00483 
00484         $sql = "SELECT $selectText
00485                 FROM
00486                        ezcollab_item,
00487                        ezcollab_item_status,
00488                        ezcollab_item_group_link
00489                 WHERE  ezcollab_item.status IN ( $statusText ) AND
00490                        $isReadText
00491                        $isActiveText
00492                        ezcollab_item.id = ezcollab_item_status.collaboration_id AND
00493                        ezcollab_item.id = ezcollab_item_group_link.collaboration_id AND
00494                        $parentGroupText
00495                        ezcollab_item_status.user_id = '$userID' AND
00496                        ezcollab_item_group_link.user_id = '$userID'
00497                 $sortText";
00498 
00499         $db = eZDB::instance();
00500         if ( !$asCount )
00501         {
00502             $sqlParameters = array();
00503             if ( $offset !== false and $limit !== false )
00504             {
00505                 $sqlParameters['offset'] = $offset;
00506                 $sqlParameters['limit'] = $limit;
00507             }
00508             $itemListArray = $db->arrayQuery( $sql, $sqlParameters );
00509 
00510             foreach( $itemListArray as $key => $value )
00511             {
00512                 $itemData =& $itemListArray[$key];
00513                 $statusObject = eZCollaborationItemStatus::create( $itemData['id'], $userID );
00514                 $statusObject->setAttribute( 'is_read', $itemData['is_read'] );
00515                 $statusObject->setAttribute( 'is_active', $itemData['is_active'] );
00516                 $statusObject->setAttribute( 'last_read', $itemData['last_read'] );
00517                 $statusObject->updateCache();
00518             }
00519             $returnItemList = eZPersistentObject::handleRows( $itemListArray, 'eZCollaborationItem', $asObject );
00520             eZDebugSetting::writeDebug( 'collaboration-item-list', $returnItemList );
00521             return $returnItemList;
00522         }
00523         else
00524         {
00525             $itemCount = $db->arrayQuery( $sql );
00526             return $itemCount[0]['count'];
00527         }
00528     }
00529 
00530     function handleView( $viewMode )
00531     {
00532         $handler = $this->handler();
00533         $handler->readItem( $this, $viewMode );
00534         return true;
00535     }
00536 
00537     /*!
00538      \static
00539      Removes all collaboration items by fetching them and calling remove on them.
00540      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00541      the calls within a db transaction; thus within db->begin and db->commit.
00542     */
00543     static function cleanup()
00544     {
00545         $db = eZDB::instance();
00546         $db->begin();
00547         $db->query( "DELETE FROM ezcollab_item" );
00548         $db->query( "DELETE FROM ezcollab_item_group_link" );
00549         $db->query( "DELETE FROM ezcollab_item_message_link" );
00550         $db->query( "DELETE FROM ezcollab_item_participant_link" );
00551         $db->query( "DELETE FROM ezcollab_item_status" );
00552         $db->query( "DELETE FROM ezcollab_notification_rule" );
00553         $db->query( "DELETE FROM ezcollab_profile" );
00554         $db->query( "DELETE FROM ezcollab_simple_message" );
00555         $db->commit();
00556     }
00557 
00558     /// \privatesection
00559     public $ID;
00560     public $TypeIdentifier;
00561     public $CreatorID;
00562     public $Status;
00563     public $Created;
00564     public $Modified;
00565     public $DataText1;
00566     public $DataText2;
00567     public $DataText3;
00568     public $DataInt1;
00569     public $DataInt2;
00570     public $DataInt3;
00571     public $DataFloat1;
00572     public $DataFloat2;
00573     public $DataFloat3;
00574 }
00575 
00576 ?>