eZ Publish  [trunk]
ezcollaborationitemmessagelink.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZCollaborationItemMessageLink 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 eZCollaborationItemMessageLink ezcollaborationitemmessagelink.php
00013   \brief The class eZCollaborationItemMessageLink does
00014 
00015 */
00016 
00017 class eZCollaborationItemMessageLink extends eZPersistentObject
00018 {
00019     /*!
00020      Constructor
00021     */
00022     function eZCollaborationItemMessageLink( $row )
00023     {
00024         $this->eZPersistentObject( $row );
00025     }
00026 
00027     static function definition()
00028     {
00029         return array( 'fields' => array( 'id' => array( 'name' => 'ID',
00030                                                         'datatype' => 'integer',
00031                                                         'default' => 0,
00032                                                         'required' => true ),
00033                                          'collaboration_id' => array( 'name' => 'CollaborationID',
00034                                                                       'datatype' => 'integer',
00035                                                                       'default' => 0,
00036                                                                       'required' => true,
00037                                                                       'foreign_class' => 'eZCollaborationItem',
00038                                                                       'foreign_attribute' => 'id',
00039                                                                       'multiplicity' => '1..*' ),
00040                                          'message_id' => array( 'name' => 'MessageID',
00041                                                                 'datatype' => 'integer',
00042                                                                 'default' => 0,
00043                                                                 'required' => true,
00044                                                                 'foreign_class' => 'eZCollaborationSimpleMessage',
00045                                                                 'foreign_attribute' => 'id',
00046                                                                 'multiplicity' => '1..*' ),
00047                                          'message_type' => array( 'name' => 'MessageType',
00048                                                                   'datatype' => 'integer',
00049                                                                   'default' => 0,
00050                                                                   'required' => true ),
00051                                          'participant_id' => array( 'name' => 'ParticipantID',
00052                                                                     'datatype' => 'integer',
00053                                                                     'default' => 0,
00054                                                                     'required' => true,
00055                                                                     'foreign_class' => 'eZUser',
00056                                                                     'foreign_attribute' => 'contentobject_id',
00057                                                                     'multiplicity' => '1..*' ),
00058                                          'created' => array( 'name' => 'Created',
00059                                                              'datatype' => 'integer',
00060                                                              'default' => 0,
00061                                                              'required' => true ),
00062                                          'modified' => array( 'name' => 'Modified',
00063                                                               'datatype' => 'integer',
00064                                                               'default' => 0,
00065                                                               'required' => true ) ),
00066                       'keys' => array( 'id' ),
00067                       'function_attributes' => array( 'collaboration_item' => 'collaborationItem',
00068                                                       'participant' => 'participant',
00069                                                       'simple_message' => 'simpleMessage' ),
00070                       'increment_key' => 'id',
00071                       'class_name' => 'eZCollaborationItemMessageLink',
00072                       'name' => 'ezcollab_item_message_link' );
00073     }
00074 
00075     static function create( $collaborationID, $messageID, $messageType, $participantID )
00076     {
00077         $dateTime = time();
00078         $row = array(
00079             'collaboration_id' => $collaborationID,
00080             'message_id' => $messageID,
00081             'message_type' => $messageType,
00082             'participant_id' => $participantID,
00083             'created' => $dateTime,
00084             'modified' => $dateTime );
00085         return new eZCollaborationItemMessageLink( $row );
00086     }
00087 
00088     /*!
00089      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00090      the calls within a db transaction; thus within db->begin and db->commit.
00091      */
00092     static function addMessage( $collaborationItem, $message, $messageType, $participantID = false )
00093     {
00094         $messageID = $message->attribute( 'id' );
00095 
00096         if ( !$messageID )
00097         {
00098             eZDebug::writeError( 'No message ID, cannot create link', __METHOD__ );
00099             $retValue = null;
00100             return $retValue;
00101         }
00102         if ( $participantID === false )
00103         {
00104             $user = eZUser::currentUser();
00105             $participantID = $user->attribute( 'contentobject_id' );
00106         }
00107         $collaborationID = $collaborationItem->attribute( 'id' );
00108         $timestamp = time();
00109         $collaborationItem->setAttribute( 'modified', $timestamp );
00110 
00111         $db = eZDB::instance();
00112         $db->begin();
00113         $collaborationItem->sync();
00114         $link = eZCollaborationItemMessageLink::create( $collaborationID, $messageID, $messageType, $participantID );
00115         $link->store();
00116         $db->commit();
00117 
00118         return $link;
00119     }
00120 
00121     static function fetch( $id, $asObject = true )
00122     {
00123         return eZPersistentObject::fetchObject( eZCollaborationItemMessageLink::definition(),
00124                                                 null,
00125                                                 array( "id" => $id ),
00126                                                 null, null,
00127                                                 $asObject );
00128     }
00129 
00130     static function fetchItemCount( $parameters )
00131     {
00132         $parameters = array_merge( array( 'item_id' => false,
00133                                           'conditions' => null ),
00134                                    $parameters );
00135         $itemID = $parameters['item_id'];
00136         $conditions = $parameters['conditions'];
00137         if ( $conditions === null )
00138             $conditions = array();
00139         $conditions['collaboration_id'] = $itemID;
00140 
00141         $objectList = eZPersistentObject::fetchObjectList( eZCollaborationItemMessageLink::definition(),
00142                                                            array(),
00143                                                            $conditions,
00144                                                            false,
00145                                                            null,
00146                                                            false,
00147                                                            false,
00148                                                            array( array( 'operation' => 'count( id )',
00149                                                                          'name' => 'count' ) ) );
00150         return $objectList[0]['count'];
00151     }
00152 
00153     static function fetchItemList( $parameters )
00154     {
00155         $parameters = array_merge( array( 'as_object' => true,
00156                                           'item_id' => false,
00157                                           'offset' => false,
00158                                           'limit' => false,
00159                                           'sort_by' => false ),
00160                                    $parameters );
00161         $itemID = $parameters['item_id'];
00162         $asObject = $parameters['as_object'];
00163         $offset = $parameters['offset'];
00164         $limit = $parameters['limit'];
00165         $limitArray = null;
00166         if ( $offset and $limit )
00167         {
00168             $limitArray = array( 'offset' => $offset,
00169                                  'limit' => $limit );
00170         }
00171 
00172         return eZPersistentObject::fetchObjectList( eZCollaborationItemMessageLink::definition(),
00173                                                     null,
00174                                                     array( "collaboration_id" => $itemID ),
00175                                                     null, $limitArray,
00176                                                     $asObject );
00177     }
00178 
00179 
00180     function collaborationItem()
00181     {
00182         if ( isset( $this->CollaborationID ) and $this->CollaborationID )
00183         {
00184             return eZCollaborationItem::fetch( $this->CollaborationID );
00185         }
00186 
00187         return null;
00188     }
00189 
00190     function participant()
00191     {
00192         return eZCollaborationItemParticipantLink::fetch( $this->CollaborationID, $this->ParticipantID );
00193     }
00194 
00195     function simpleMessage()
00196     {
00197         if ( isset( $this->MessageID ) and $this->MessageID )
00198         {
00199             return eZCollaborationSimpleMessage::fetch( $this->MessageID );
00200         }
00201 
00202         return null;
00203     }
00204 
00205     /// \privatesection
00206     public $CollaborationID;
00207     public $MessageID;
00208     public $ParticipantID;
00209     public $Created;
00210     public $Modified;
00211 }
00212 
00213 ?>