eZ Publish  [trunk]
ezcollaborationgroup.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZCollaborationGroup 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 eZCollaborationGroup ezcollaborationgroup.php
00013   \brief The class eZCollaborationGroup does
00014 
00015 */
00016 
00017 class eZCollaborationGroup extends eZPersistentObject
00018 {
00019     /*!
00020      Constructor
00021     */
00022     function eZCollaborationGroup( $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                                          'parent_group_id' => array( 'name' => 'ParentGroupID',
00034                                                                      'datatype' => 'integer',
00035                                                                      'default' => 0,
00036                                                                      'required' => true,
00037                                                                      'foreign_class' => 'eZCollaborationGroup',
00038                                                                      'foreign_attribute' => 'id',
00039                                                                      'multiplicity' => '1..*' ),
00040                                          'depth' => array( 'name' => 'Depth',
00041                                                            'datatype' => 'integer',
00042                                                            'default' => 0,
00043                                                            'required' => true ),
00044                                          'path_string' => array( 'name' => 'PathString',
00045                                                                  'datatype' => 'string',
00046                                                                  'default' => '',
00047                                                                  'required' => true ),
00048                                          'is_open' => array( 'name' => 'IsOpen',
00049                                                              'datatype' => 'integer',
00050                                                              'default' => '1',
00051                                                              'required' => true ),
00052                                          'user_id' => array( 'name' => 'UserID',
00053                                                              'datatype' => 'integer',
00054                                                              'default' => '0',
00055                                                              'required' => true,
00056                                                              'foreign_class' => 'eZUser',
00057                                                              'foreign_attribute' => 'contentobject_id',
00058                                                              'multiplicity' => '1..*' ),
00059                                          'title' => array( 'name' => 'Title',
00060                                                            'datatype' => 'string',
00061                                                            'default' => '',
00062                                                            'required' => true ),
00063                                          'created' => array( 'name' => 'Created',
00064                                                              'datatype' => 'integer',
00065                                                              'default' => '0',
00066                                                              'required' => true ),
00067                                          'modified' =>  array( 'name' => 'Modified',
00068                                                                'datatype' => 'integer',
00069                                                                'default' => '0',
00070                                                                'required' => true ) ),
00071                       'keys' => array( 'id' ),
00072                       "function_attributes" => array( 'user' => 'user',
00073                                                       'parent_group' => 'parentGroup',
00074                                                       'item_list' => 'itemList',
00075                                                       'item_count' => 'itemCount' ),
00076                       'increment_key' => 'id',
00077                       'class_name' => 'eZCollaborationGroup',
00078                       'sort' => array( 'title' => 'asc' ),
00079                       'name' => 'ezcollab_group' );
00080     }
00081 
00082     /*!
00083      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00084      the calls within a db transaction; thus within db->begin and db->commit.
00085      */
00086     function addChild( $group, $store = true )
00087     {
00088         $pathString = $this->PathString;
00089         if ( $pathString != '' )
00090             $pathString .= '/';
00091         $pathString .= $this->ID;
00092         $depth = $this->Depth + 1;
00093         $parentGroupID = $this->ID;
00094         $group->setAttribute( 'path_string', $pathString );
00095         $group->setAttribute( 'parent_group_id', $parentGroupID );
00096         $group->setAttribute( 'depth', $depth );
00097         if ( $store )
00098             $group->sync();
00099     }
00100 
00101     /*!
00102      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00103      the calls within a db transaction; thus within db->begin and db->commit.
00104      */
00105     static function instantiate( $userID, $title, $parentGroupID = 0, $isOpen = true )
00106     {
00107         $depth = 0;
00108         $pathString = '';
00109         if ( $parentGroupID > 0 )
00110         {
00111             $parentGroup = eZCollaborationGroup::fetch( $parentGroupID, $userID );
00112             $depth = $parentGroup->attribute( 'depth' ) + 1;
00113             $pathString = $parentGroup->attribute( 'path_string' );
00114         }
00115         $group = eZCollaborationGroup::create( $userID, $title, '', $depth, $parentGroupID, $isOpen );
00116 
00117         $db = eZDB::instance();
00118         $db->begin();
00119 
00120         $group->store();
00121         if ( $pathString == '' )
00122             $pathString = $group->attribute( 'id' );
00123         else
00124             $pathString .= '/' . $group->attribute( 'id' );
00125         $group->setAttribute( 'path_string', $pathString );
00126         $group->sync();
00127 
00128         $db->commit();
00129         return $group;
00130     }
00131 
00132     static function create( $userID, $title, $pathString = '', $depth = 0, $parentGroupID = 0, $isOpen = true )
00133     {
00134         $date_time = time();
00135         $row = array(
00136             'id' => null,
00137             'parent_group_id' => $parentGroupID,
00138             'path_string' => $pathString,
00139             'depth' => $depth,
00140             'is_open' => $isOpen,
00141             'user_id' => $userID,
00142             'title' => $title,
00143             'created' => $date_time,
00144             'modified' => $date_time );
00145         return new eZCollaborationGroup( $row );
00146     }
00147 
00148     static function fetch( $id, $userID = false, $asObject = true )
00149     {
00150         $conditions = array( "id" => $id );
00151         if ( $userID !== false )
00152             $conditions['user_id'] = $userID;
00153         return eZPersistentObject::fetchObject( eZCollaborationGroup::definition(),
00154                                                 null,
00155                                                 $conditions,
00156                                                 $asObject );
00157     }
00158 
00159     /*!
00160      \return an array with collaboration items which are in this group.
00161     */
00162     function itemList( $parameters = array() )
00163     {
00164         return eZCollaborationItem::fetchList( array_merge( array( 'parent_group_id' => $this->ID ),
00165                                                                  $parameters ) );
00166     }
00167 
00168     static function subTree( $parameters = array() )
00169     {
00170         $parameters = array_merge( array( 'parent_group_id' => false,
00171                                           'depth' => false,
00172                                           'sort_by' => false,
00173                                           'as_object' => true,
00174                                           'offset' => false,
00175                                           'limit' => false ),
00176                                    $parameters );
00177         $parentGroupID = $parameters['parent_group_id'];
00178         $depth = $parameters['depth'];
00179         $asObject = $parameters['as_object'];
00180         $offset = $parameters['offset'];
00181         $limit = $parameters['limit'];
00182 
00183         $group = null;
00184         if ( $parentGroupID > 0 )
00185             $group = eZCollaborationGroup::fetch( $parentGroupID );
00186 
00187         $sortCount = 0;
00188         $sortList = $parameters['sort_by'];
00189         if ( is_array( $sortList ) and
00190              count( $sortList ) > 0 )
00191         {
00192             if ( count( $sortList ) > 1 and
00193                  !is_array( $sortList[0] ) )
00194             {
00195                 $sortList = array( $sortList );
00196             }
00197         }
00198         if ( $sortList !== false )
00199         {
00200             $sortingFields = '';
00201             foreach ( $sortList as $sortBy )
00202             {
00203                 if ( is_array( $sortBy ) and count( $sortBy ) > 0 )
00204                 {
00205                     if ( $sortCount > 0 )
00206                         $sortingFields .= ', ';
00207                     $sortField = $sortBy[0];
00208                     switch ( $sortField )
00209                     {
00210                         case 'path':
00211                         {
00212                             $sortingFields .= 'path_string';
00213                         } break;
00214                         case 'created':
00215                         {
00216                             $sortingFields .= 'created';
00217                         } break;
00218                         case 'modified':
00219                         {
00220                             $sortingFields .= 'modified';
00221                         } break;
00222                         case 'depth':
00223                         {
00224                             $sortingFields .= 'depth';
00225                         } break;
00226                         case 'priority':
00227                         {
00228                             $sortingFields .= 'priority';
00229                         } break;
00230                         case 'title':
00231                         {
00232                             $sortingFields .= 'title';
00233                         } break;
00234                         default:
00235                         {
00236                             eZDebug::writeWarning( 'Unknown sort field: ' . $sortField, __METHOD__ );
00237                             continue;
00238                         }
00239                     }
00240                     $sortOrder = true; // true is ascending
00241                     if ( isset( $sortBy[1] ) )
00242                         $sortOrder = $sortBy[1];
00243                     $sortingFields .= $sortOrder ? " ASC" : " DESC";
00244                     ++$sortCount;
00245                 }
00246             }
00247         }
00248         if ( $sortCount == 0 )
00249         {
00250             $sortingFields = " path_string ASC";
00251         }
00252 
00253         $pathString = '';
00254         if ( $group !== null )
00255             $pathString = $group->attribute( 'path_string' );
00256 
00257         $depthSQL = "";
00258         if ( $depth !== false )
00259             $depthSQL = "depth <= '$depth' AND";
00260         $pathSQL = '';
00261         if ( $pathString != '' )
00262             $pathSQL = "path_string like '$pathString%' AND";
00263 
00264         $user = eZUser::currentUser();
00265         $userID = $user->attribute( 'contentobject_id' );
00266 
00267         $sql = "SELECT *
00268                 FROM
00269                       ezcollab_group
00270                 WHERE
00271                       $pathSQL
00272                       $depthSQL
00273                       id != '$parentGroupID' AND
00274                       user_id = '$userID'
00275                 ORDER BY $sortingFields";
00276 
00277         $db = eZDB::instance();
00278         $sqlParameters = array();
00279         if ( $offset !== false and $limit !== false )
00280         {
00281             $sqlParameters['offset'] = $offset;
00282             $sqlParameters['limit'] = $limit;
00283         }
00284         $groupListArray = $db->arrayQuery( $sql, $sqlParameters );
00285         $returnGroupList = eZPersistentObject::handleRows( $groupListArray, 'eZCollaborationGroup', $asObject );
00286         eZDebugSetting::writeDebug( 'collaboration-group-tree', $returnGroupList );
00287         return $returnGroupList;
00288     }
00289 
00290     function itemCount( $parameters = array() )
00291     {
00292         $parameters = array_merge( array( 'as_object' => true ),
00293                                    $parameters );
00294         $asObject = $parameters['as_object'];
00295 
00296         $user = eZUser::currentUser();
00297         $userID = $user->attribute( 'contentobject_id' );
00298 
00299         $groupID = $this->ID;
00300 
00301         $db = eZDB::instance();
00302         $sql = "SELECT   count( collaboration_id ) as count
00303                 FROM     ezcollab_item_group_link
00304                 WHERE    user_id = '$userID' AND
00305                          group_id = '$groupID'";
00306         $countArray = $db->arrayQuery( $sql );
00307         return $countArray[0]['count'];
00308     }
00309 
00310     function user()
00311     {
00312         if ( isset( $this->UserID ) and $this->UserID )
00313         {
00314             return eZUser::fetch( $this->UserID );
00315         }
00316         return null;
00317     }
00318 
00319     function parentGroup()
00320     {
00321         if ( isset( $this->ParentGroupID ) and $this->ParentGroupID )
00322         {
00323             return eZCollaborationGroup::fetch( $this->ParentGroupID );
00324         }
00325         return null;
00326     }
00327 
00328     /// \privatesection
00329     public $ID;
00330     public $ParentGroupID;
00331     public $UserID;
00332     public $Title;
00333     public $Created;
00334     public $Modified;
00335 }
00336 
00337 ?>