3
* $RCSfile: GalleryItemAttributesHelper_advanced.class,v $
5
* Gallery - a web based photo album viewer and editor
6
* Copyright (C) 2000-2005 Bharat Mediratta
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or (at
11
* your option) any later version.
13
* This program is distributed in the hope that it will be useful, but
14
* WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
23
* @version $Revision: 1.13 $ $Date: 2005/08/23 03:49:04 $
24
* @package GalleryCore
25
* @author Bharat Mediratta <bharat@menalto.com>
30
* @package GalleryCore
33
class GalleryItemAttributesHelper_advanced {
35
* Rebalance the order weights associated with this item's children.
36
* When this method is complete, the child item ids should still have the
37
* same order as they have now, but their order weights should be spaced
38
* out to exactly the spacing value specified in the arguments.
40
* @param int the parent id
41
* @param int the order spacing
42
* @return object GalleryStatus a status code
45
function rebalanceChildOrderWeights($parentItemId, $spacing=1000) {
48
list ($ret, $parentItem) = GalleryCoreApi::loadEntitiesById($parentItemId);
49
if ($ret->isError()) {
50
return $ret->wrap(__FILE__, __LINE__);
53
list ($ret, $ids) = GalleryCoreApi::fetchChildItemIdsIgnorePermissions($parentItem);
54
if ($ret->isError()) {
55
return $ret->wrap(__FILE__, __LINE__);
59
foreach ($ids as $id) {
60
$gallery->guaranteeTimeLimit(5);
61
$ret = GalleryItemAttributesHelper_advanced::setOrderWeight($id, $current);
62
if ($ret->isError()) {
63
return $ret->wrap(__FILE__, __LINE__);
68
return GalleryStatus::success();
72
* Fetch the highest or lowest weight of all children
73
* @param int the parent item id
74
* @param int the direction (HIGHER_WEIGHT, LOWER_WEIGHT)
75
* @return array object GalleryStatus a status code
79
function fetchExtremeChildWeight($itemId, $direction) {
82
if ($direction == LOWER_WEIGHT) {
90
' . $aggregate . '([GalleryItemAttributesMap::orderWeight])
92
[GalleryItemAttributesMap], [GalleryChildEntity]
94
[GalleryChildEntity::id] = [GalleryItemAttributesMap::itemId]
96
[GalleryChildEntity::parentId] = ?
98
list ($ret, $searchResults) = $gallery->search($query, array($itemId));
99
if ($ret->isError()) {
100
return array($ret->wrap(__FILE__, __LINE__), null);
103
$result = $searchResults->nextResult();
104
if (isset($result[0])) {
105
$weight = (int)$result[0];
110
return array(GalleryStatus::success(), $weight);
114
* Fetch the weight of the next peer in line (higher or lower, as specified)
116
* @param int the item id
117
* @param int the direction (HIGHER_WEIGHT, LOWER_WEIGHT)
118
* @return array object GalleryStatus a status code
122
function fetchNextWeight($itemId, $direction) {
125
if ($direction == LOWER_WEIGHT) {
135
' . $aggregate . '([GalleryItemAttributesMap=2::orderWeight])
137
[GalleryItemAttributesMap=1], [GalleryItemAttributesMap=2],
138
[GalleryChildEntity=1], [GalleryChildEntity=2]
140
[GalleryChildEntity=1::id] = ?
142
[GalleryChildEntity=1::parentId] = [GalleryChildEntity=2::parentId]
144
[GalleryChildEntity=1::id] = [GalleryItemAttributesMap=1::itemId]
146
[GalleryChildEntity=2::id] = [GalleryItemAttributesMap=2::itemId]
148
[GalleryItemAttributesMap=2::orderWeight] ' .
149
$comparison . ' [GalleryItemAttributesMap=1::orderWeight]
151
list ($ret, $searchResults) = $gallery->search($query, array($itemId));
152
if ($ret->isError()) {
153
return array($ret->wrap(__FILE__, __LINE__), null);
156
$result = $searchResults->nextResult();
157
if (isset($result[0])) {
158
$weight = (int)$result[0];
163
return array(GalleryStatus::success(), $weight);
167
* Update the view count for this item id
168
* @param int the item id
169
* @param int the new count
170
* @return object GalleryStatus a status code
173
function setViewCount($itemId, $count) {
174
GalleryCoreApi::relativeRequireOnce('modules/core/classes/GalleryItemAttributesMap.class');
175
$ret = GalleryItemAttributesMap::updateMapEntry(array('itemId' => $itemId),
176
array('viewCount' => $count));
177
if ($ret->isError()) {
178
return $ret->wrap(__FILE__, __LINE__);
181
return GalleryStatus::success();
185
* Set the parent id sequence for an item id
187
* @param int id the item id
188
* @param array the parent sequence (ids)
189
* @return object GalleryStatus a status code
192
function setParentSequence($itemId, $parentSequence) {
193
if (empty($parentSequence)) {
194
$parentSequence = '';
196
$parentSequence = join('/', $parentSequence) . '/';
199
GalleryCoreApi::relativeRequireOnce('modules/core/classes/GalleryItemAttributesMap.class');
200
$ret = GalleryItemAttributesMap::updateMapEntry(array('itemId' => $itemId),
201
array('parentSequence' => $parentSequence));
202
if ($ret->isError()) {
203
return $ret->wrap(__FILE__, __LINE__);
206
return GalleryStatus::success();
210
* Create a new set of attributes for an item
211
* @param int the item id
212
* @param array the sequence of parent ids
213
* @return object GalleryStatus a status code
216
function createItemAttributes($itemId, $parentSequence) {
217
if (empty($parentSequence)) {
218
$parentSequence = '';
220
$parentSequence = join('/', $parentSequence) . '/';
222
GalleryCoreApi::relativeRequireOnce('modules/core/classes/GalleryItemAttributesMap.class');
223
$ret = GalleryItemAttributesMap::addMapEntry(array('itemId' => $itemId,
226
'parentSequence' => $parentSequence));
227
if ($ret->isError()) {
228
return $ret->wrap(__FILE__, __LINE__);
231
return GalleryStatus::success();
235
* Remove the attributes for the given item
236
* @param int the item id
237
* @return object GalleryStatus a status code
240
function removeItemAttributes($itemId) {
241
GalleryCoreApi::relativeRequireOnce('modules/core/classes/GalleryItemAttributesMap.class');
242
$ret = GalleryItemAttributesMap::removeMapEntry(array('itemId' => $itemId));
243
if ($ret->isError()) {
244
return $ret->wrap(__FILE__, __LINE__);
247
return GalleryStatus::success();
251
* Set the order weight for an item id
252
* @param int the item id
253
* @param int the new order weight
254
* @return object GalleryStatus a status code
257
function setOrderWeight($itemId, $orderWeight) {
258
GalleryCoreApi::relativeRequireOnce('modules/core/classes/GalleryItemAttributesMap.class');
259
$ret = GalleryItemAttributesMap::updateMapEntry(array('itemId' => $itemId),
260
array('orderWeight' => $orderWeight));
261
if ($ret->isError()) {
262
return $ret->wrap(__FILE__, __LINE__);
265
return GalleryStatus::success();
269
* Get the view count for this item id
270
* @param int the item id
271
* @return array object GalleryStatus a status code
275
function fetchViewCount($itemId) {
276
GalleryCoreApi::relativeRequireOnce(
277
'modules/core/classes/helpers/GalleryItemAttributesHelper_medium.class');
278
list ($ret, $viewCounts) =
279
GalleryItemAttributesHelper_medium::fetchViewCounts(array($itemId));
280
if ($ret->isError()) {
281
return array($ret->wrap(__FILE__, __LINE__), null);
284
return array(GalleryStatus::success(), $viewCounts[$itemId]);
288
* Update all items containing the source parent sequence to the new parent sequence
290
* @param int id the item id
291
* @param array the parent sequence (ids)
292
* @return object GalleryStatus a status code
295
function updateParentSequence($oldParentSequence, $newParentSequence) {
296
GalleryCoreApi::relativeRequireOnce(
297
'modules/core/classes/GalleryStorage/DatabaseSqlFragment.class');
300
$oldParentSequence = join('/', $oldParentSequence) . '/';
301
$newParentSequence = join('/', $newParentSequence) . '/';
303
$storage =& $gallery->getStorage();
304
list ($ret, $substring) = $storage->getFunctionSql('SUBSTRING', array(
305
'[GalleryItemAttributesMap::parentSequence]', strlen($oldParentSequence) + 1));
306
if ($ret->isError()) {
307
return $ret->wrap(__FILE__, __LINE__);
309
list ($ret, $newSequenceSql) = $storage->getFunctionSql('CONCAT', array('?', $substring));
310
if ($ret->isError()) {
311
return $ret->wrap(__FILE__, __LINE__);
314
GalleryCoreApi::relativeRequireOnce('modules/core/classes/GalleryItemAttributesMap.class');
315
$ret = GalleryItemAttributesMap::updateMapEntry(
316
array('parentSequence' =>
317
new DatabaseSqlFragment('LIKE ?', $oldParentSequence . '%')),
318
array('parentSequence' =>
319
new DatabaseSqlFragment('=' . $newSequenceSql, $newParentSequence)));
320
if ($ret->isError()) {
321
return $ret->wrap(__FILE__, __LINE__);
324
return GalleryStatus::success();