Added: added attribute section_identifier to ezsection
[tinyz:tinyz.git] / kernel / classes / ezsection.php
1 <?php
2 //
3 // Definition of eZSection class
4 //
5 // Created on: <27-Aug-2002 15:55:18 bf>
6 //
7 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
8 // SOFTWARE NAME: eZ Publish
9 // SOFTWARE RELEASE: 4.1.x
10 // COPYRIGHT NOTICE: Copyright (C) 1999-2010 eZ Systems AS
11 // SOFTWARE LICENSE: GNU General Public License v2.0
12 // NOTICE: >
13 //   This program is free software; you can redistribute it and/or
14 //   modify it under the terms of version 2.0  of the GNU General
15 //   Public License as published by the Free Software Foundation.
16 //
17 //   This program is distributed in the hope that it will be useful,
18 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
19 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 //   GNU General Public License for more details.
21 //
22 //   You should have received a copy of version 2.0 of the GNU General
23 //   Public License along with this program; if not, write to the Free
24 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 //   MA 02110-1301, USA.
26 //
27 //
28 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
29 //
30
31 /*!
32   \class eZSection ezsection.php
33   \brief eZSection handles grouping of content in eZ Publish
34
35 */
36
37 class eZSection extends eZPersistentObject
38 {
39     function eZSection( $row )
40     {
41         if ( !isset( $row['id'] ) )
42         {
43             $row['id'] = null;
44         }
45         $this->eZPersistentObject( $row );
46     }
47
48     /*!
49      \return the persistent object definition for the eZSection class.
50     */
51     static function definition()
52     {
53         static $definition = array( "fields" => array( "id" => array( 'name' => 'ID',
54                                                         'datatype' => 'integer',
55                                                         'default' => 0,
56                                                         'required' => true ),
57                                          "name" => array( 'name' => "Name",
58                                                           'datatype' => 'string',
59                                                           'default' => 0,
60                                                           'required' => true ),
61                                          "navigation_part_identifier" => array( 'name' => "NavigationPartIdentifier",
62                                                                                 'datatype' => 'string',
63                                                                                 'default' => 'ezcontentnavigationpart',
64                                                                                 'required' => true ),
65                                          "locale" => array( 'name' => "Locale",
66                                                             'datatype' => 'string',
67                                                             'default' => '',
68                                                             'required' => true ),
69                                          "section_identifier" => array( 'name' => "SectionIdentifier",
70                                                             'datatype' => 'string',
71                                                             'default' => '',
72                                                             'required' => true ) ),
73                       "keys" => array( "id" ),
74                       "increment_key" => "id",
75                       "class_name" => "eZSection",
76                       "sort" => array( "name" => "asc" ),
77                       "name" => "ezsection" );
78         return $definition;
79     }
80
81     /*!
82      \return the section object with the given id.
83     */
84     static function fetch( $sectionID, $asObject = true )
85     {
86         global $eZContentSectionObjectCache;
87
88         // If the object given by its id is not cached or should be returned as array
89         // then we fetch it from the DB (objects are always cached as arrays).
90         if ( !isset( $eZContentSectionObjectCache[$sectionID] ) or $asObject === false )
91         {
92             $section = eZPersistentObject::fetchObject( eZSection::definition(),
93                                                 null,
94                                                 array( "id" => $sectionID ),
95                                                 $asObject );
96             if ( $asObject )
97             {
98                 $eZContentSectionObjectCache[$sectionID] = $section;
99             }
100         }
101         else
102         {
103             $section = $eZContentSectionObjectCache[$sectionID];
104         }
105         return $section;
106     }
107
108     static function fetchFilteredList( $conditions = null, $offset = false, $limit = false, $asObject = true )
109     {
110         $limits = null;
111         if ( $offset or $limit )
112             $limits = array( 'offset' => $offset,
113                              'length' => $limit );
114         return eZPersistentObject::fetchObjectList( eZSection::definition(),
115                                                     null,
116                                                     $conditions, null, $limits,
117                                                     $asObject );
118     }
119
120     static function fetchList( $asObject = true )
121     {
122         return eZPersistentObject::fetchObjectList( eZSection::definition(),
123                                                     null, null, null, null,
124                                                     $asObject );
125     }
126
127     static function fetchByOffset( $offset, $limit, $asObject = true )
128     {
129         $sectionList = eZPersistentObject::fetchObjectList( eZSection::definition(),
130                                                              null,
131                                                              null,
132                                                              array( 'name' => 'ASC' ),
133                                                              array( 'offset' => $offset, 'length' => $limit ),
134                                                              $asObject );
135         return $sectionList;
136     }
137
138      /*!
139      \return the number of active orders
140     */
141     static function sectionCount()
142     {
143         $db = eZDB::instance();
144
145         $countArray = $db->arrayQuery(  "SELECT count( * ) AS count FROM ezsection" );
146         return $countArray[0]['count'];
147     }
148
149     /*!
150      Makes sure the global section ID is propagated to the template override key.
151     */
152     static function initGlobalID()
153     {
154         global $eZSiteBasics;
155         $sessionRequired = $eZSiteBasics['session-required'];
156         $sectionID = false;
157         if ( $sessionRequired )
158         {
159             $http = eZHTTPTool::instance();
160             $sectionArray = array();
161             if ( $http->hasSessionVariable( 'eZGlobalSection' ) )
162                 $sectionArray = $http->sessionVariable( 'eZGlobalSection' );
163             if ( !isset( $sectionArray['id'] ) )
164             {
165                 return false;
166             }
167             $sectionID = $sectionArray['id'];
168         }
169
170         if ( $sectionID )
171         {
172             // eZTemplateDesignResource will read this global variable
173             $GLOBALS['eZDesignKeys']['section'] = $sectionID;
174             return true;
175         }
176         return false;
177     }
178
179     /*!
180      Sets the current global section ID to \a $sectionID in the session and
181      the template override key.
182     */
183     static function setGlobalID( $sectionID )
184     {
185         $http = eZHTTPTool::instance();
186         $sectionArray = array();
187         if ( $http->hasSessionVariable( 'eZGlobalSection' ) )
188             $sectionArray = $http->sessionVariable( 'eZGlobalSection' );
189         $sectionArray['id'] = $sectionID;
190         $http->setSessionVariable( 'eZGlobalSection', $sectionArray );
191
192         // eZTemplateDesignResource will read this global variable
193         $GLOBALS['eZDesignKeys']['section'] = $sectionID;
194     }
195
196     /*!
197      \return the global section ID or \c null if it is not set yet.
198     */
199     static function globalID()
200     {
201         $http = eZHTTPTool::instance();
202         if ( $http->hasSessionVariable( 'eZGlobalSection' ) )
203         {
204             $sectionArray = $http->sessionVariable( 'eZGlobalSection' );
205             if ( isset( $sectionArray['id'] ) )
206                 return $sectionArray['id'];
207         }
208         return null;
209     }
210
211     /*!
212      Will remove the current section from the database.
213      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
214      the calls within a db transaction; thus within db->begin and db->commit.
215     */
216     function removeThis( $conditions = null, $extraConditions = null )
217     {
218         eZPersistentObject::remove( array( "id" => $this->ID ), $extraConditions );
219     }
220
221     /*
222      Check if this section is allowed to remove from the system
223     */
224     function canBeRemoved( $sectionID = false )
225     {
226         if ( $sectionID === false )
227         {
228             $sectionID = $this->attribute( 'id' );
229         }
230
231         $objects = eZPersistentObject::fetchObjectList( eZContentObject::definition(), null,
232                                                         array( 'section_id' => $sectionID ) );
233         $limitations = eZPolicyLimitation::findByType( 'Section', $sectionID, true, false );
234         $userRoles = eZRole::fetchRolesByLimitation( 'section', $sectionID );
235
236         if ( count( $objects ) > 0 or
237              count( $limitations ) > 0 or
238              count( $userRoles ) > 0 )
239         {
240             return false;
241         }
242         else
243         {
244             return true;
245         }
246     }
247
248 }
249
250 ?>