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