forked from ezsystems/ezpublish-legacy
/
ezsection.php
277 lines (254 loc) · 10.9 KB
/
ezsection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/**
* File containing the eZSection class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
* @package kernel
*/
/*!
\class eZSection ezsection.php
\brief eZSection handles grouping of content in eZ Publish
*/
class eZSection extends eZPersistentObject
{
public function __construct( $row )
{
if ( !isset( $row['id'] ) )
{
$row['id'] = null;
}
parent::__construct( $row );
}
/*!
\return the persistent object definition for the eZSection class.
*/
static function definition()
{
static $definition = array( "fields" => array( "id" => array( 'name' => 'ID',
'datatype' => 'integer',
'default' => 0,
'required' => true ),
"name" => array( 'name' => "Name",
'datatype' => 'string',
'default' => 0,
'required' => true ),
"navigation_part_identifier" => array( 'name' => "NavigationPartIdentifier",
'datatype' => 'string',
'default' => 'ezcontentnavigationpart',
'required' => true ),
"locale" => array( 'name' => "Locale",
'datatype' => 'string',
'default' => '',
'required' => true ),
"identifier" => array( 'name' => "Identifier",
'datatype' => 'string',
'default' => '',
'required' => true ) ),
"keys" => array( "id" ),
"increment_key" => "id",
"class_name" => "eZSection",
"sort" => array( "name" => "asc" ),
"name" => "ezsection" );
return $definition;
}
/*!
\return the section object with the given id.
*/
static function fetch( $sectionID, $asObject = true )
{
global $eZContentSectionObjectCache;
// If the object given by its id is not cached or should be returned as array
// then we fetch it from the DB (objects are always cached as arrays).
if ( !isset( $eZContentSectionObjectCache[$sectionID] ) or $asObject === false )
{
$section = eZPersistentObject::fetchObject( eZSection::definition(),
null,
array( "id" => $sectionID ),
$asObject );
if ( $asObject )
{
$eZContentSectionObjectCache[$sectionID] = $section;
}
}
else
{
$section = $eZContentSectionObjectCache[$sectionID];
}
return $section;
}
/**
* fetch object by section identifier
* @param string $sectionIdentifier
* @param boolean $asObject
* @return object|null
*/
static function fetchByIdentifier( $sectionIdentifier, $asObject = true )
{
global $eZContentSectionObjectCache;
if( !isset( $eZContentSectionObjectCache[$sectionIdentifier] ) || $asObject === false )
{
$sectionFetched = eZPersistentObject::fetchObject( eZSection::definition(),
null,
array( "identifier" => $sectionIdentifier ),
$asObject );
if( !$sectionFetched )
{
return null;
}
if( $asObject )
{
// the section identifier index refers to the id index object
$sectionID = $sectionFetched->attribute( 'id' );
if( !isset( $eZContentSectionObjectCache[$sectionID] ) )
{
$eZContentSectionObjectCache[$sectionID] = $sectionFetched;
}
$eZContentSectionObjectCache[$sectionIdentifier] = $eZContentSectionObjectCache[$sectionID];
}
else
{
return $sectionFetched;
}
}
$section = $eZContentSectionObjectCache[$sectionIdentifier];
return $section;
}
static function fetchFilteredList( $conditions = null, $offset = false, $limit = false, $asObject = true )
{
$limits = null;
if ( $offset or $limit )
$limits = array( 'offset' => $offset,
'length' => $limit );
return eZPersistentObject::fetchObjectList( eZSection::definition(),
null,
$conditions, null, $limits,
$asObject );
}
static function fetchList( $asObject = true )
{
return eZPersistentObject::fetchObjectList( eZSection::definition(),
null, null, null, null,
$asObject );
}
static function fetchByOffset( $offset, $limit, $asObject = true )
{
$sectionList = eZPersistentObject::fetchObjectList( eZSection::definition(),
null,
null,
array( 'name' => 'ASC' ),
array( 'offset' => $offset, 'length' => $limit ),
$asObject );
return $sectionList;
}
/*!
\return the number of active orders
*/
static function sectionCount()
{
$db = eZDB::instance();
$countArray = $db->arrayQuery( "SELECT count( * ) AS count FROM ezsection" );
return $countArray[0]['count'];
}
/*!
Will remove the current section from the database.
\note Transaction unsafe. If you call several transaction unsafe methods you must enclose
the calls within a db transaction; thus within db->begin and db->commit.
*/
function removeThis( $conditions = null, $extraConditions = null )
{
$this->remove( array( "id" => $this->ID ), $extraConditions );
}
/*
Check if this section is allowed to remove from the system
*/
function canBeRemoved( $sectionID = false )
{
if ( $sectionID === false )
{
$sectionID = $this->attribute( 'id' );
}
$objects = eZPersistentObject::fetchObjectList( eZContentObject::definition(), null,
array( 'section_id' => $sectionID ) );
$limitations = eZPolicyLimitation::findByType( 'Section', $sectionID, true, false );
$userRoles = eZRole::fetchRolesByLimitation( 'section', $sectionID );
if ( count( $objects ) > 0 or
count( $limitations ) > 0 or
count( $userRoles ) > 0 )
{
return false;
}
else
{
return true;
}
}
public function applyTo( eZContentObject $object )
{
$sectionID = $this->attribute( "id" );
$currentUser = eZUser::currentUser();
if ( !$currentUser->canAssignSectionToObject( $sectionID, $object ) )
{
eZDebug::writeError(
"You do not have permissions to assign the section <" . $this->attribute( "name" ) . "> to the object <" . $object->attribute( "name" ) . ">."
);
return false;
}
$db = eZDB::instance();
$db->begin();
$assignedNodes = $object->attribute( "assigned_nodes" );
if ( !empty( $assignedNodes ) )
{
if ( eZOperationHandler::operationIsAvailable( "content_updatesection" ) )
{
foreach ( $assignedNodes as $node )
{
eZOperationHandler::execute(
"content",
"updatesection",
array(
"node_id" => $node->attribute( "node_id" ),
"selected_section_id" => $sectionID
),
null,
true
);
}
}
else
{
foreach ( $assignedNodes as $node )
{
eZContentOperationCollection::updateSection( $node->attribute( "node_id" ), $sectionID, false );
}
}
}
else
{
// If there are no assigned nodes we should update db for the current object.
$objectID = $object->attribute( "id" );
$db->query( "UPDATE ezcontentobject SET section_id='$sectionID' WHERE id = '$objectID'" );
$db->query( "UPDATE ezsearch_object_word_link SET section_id='$sectionID' WHERE contentobject_id = '$objectID'" );
}
eZContentCacheManager::clearContentCacheIfNeeded( $object->attribute( "id" ) );
$object->expireAllViewCache();
$commitResult = $db->commit();
// Update search indexes AFTER transaction is completed, so changes are actually available for Search Engine.
if ( $commitResult ) {
if ( !empty( $assignedNodes ) ) {
foreach ( $assignedNodes as $node ) {
$objectIDArray = eZContentObjectTreeNode::getObjectIdsInNodeSubTree( $node );
foreach (array_chunk($objectIDArray, 100) as $pagedObjectIDs) {
// Clear cache of affected objects in sub tree so they are up-to-date when indexed.
eZContentCacheManager::clearObjectViewCacheArray($pagedObjectIDs);
eZSearch::updateObjectsSection($pagedObjectIDs, $sectionID);
}
}
} else {
eZSearch::updateObjectsSection(array($object->attribute("id")), $sectionID);
}
}
}
}
?>