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