~ubuntu-branches/ubuntu/quantal/gallery2/quantal

« back to all changes in this revision

Viewing changes to modules/core/classes/helpers/GalleryItemAttributesHelper_advanced.class

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2005-11-29 15:50:12 UTC
  • Revision ID: james.westby@ubuntu.com-20051129155012-wtophp03lu01kdgl
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
 * $RCSfile: GalleryItemAttributesHelper_advanced.class,v $
 
4
 *
 
5
 * Gallery - a web based photo album viewer and editor
 
6
 * Copyright (C) 2000-2005 Bharat Mediratta
 
7
 *
 
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.
 
12
 *
 
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.
 
17
 *
 
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.
 
21
 */
 
22
/**
 
23
 * @version $Revision: 1.13 $ $Date: 2005/08/23 03:49:04 $
 
24
 * @package GalleryCore
 
25
 * @author Bharat Mediratta <bharat@menalto.com>
 
26
 */
 
27
 
 
28
/**
 
29
 *
 
30
 * @package GalleryCore
 
31
 * @subpackage Helpers
 
32
 */
 
33
class GalleryItemAttributesHelper_advanced {
 
34
    /**
 
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.
 
39
     *
 
40
     * @param int the parent id
 
41
     * @param int the order spacing
 
42
     * @return object GalleryStatus a status code
 
43
     * @static
 
44
     */
 
45
    function rebalanceChildOrderWeights($parentItemId, $spacing=1000) {
 
46
        global $gallery;
 
47
 
 
48
        list ($ret, $parentItem) = GalleryCoreApi::loadEntitiesById($parentItemId);
 
49
        if ($ret->isError()) {
 
50
            return $ret->wrap(__FILE__, __LINE__);
 
51
        }
 
52
 
 
53
        list ($ret, $ids) = GalleryCoreApi::fetchChildItemIdsIgnorePermissions($parentItem);
 
54
        if ($ret->isError()) {
 
55
            return $ret->wrap(__FILE__, __LINE__);
 
56
        }
 
57
 
 
58
        $current = $spacing;
 
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__);
 
64
            }
 
65
            $current += $spacing;
 
66
        }
 
67
 
 
68
        return GalleryStatus::success();
 
69
    }
 
70
 
 
71
    /**
 
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
 
76
     *               int a weight
 
77
     * @static
 
78
     */
 
79
    function fetchExtremeChildWeight($itemId, $direction) {
 
80
        global $gallery;
 
81
 
 
82
        if ($direction == LOWER_WEIGHT) {
 
83
            $aggregate = 'MIN';
 
84
        } else {
 
85
            $aggregate = 'MAX';
 
86
        }
 
87
 
 
88
        $query = '
 
89
        SELECT
 
90
          ' . $aggregate . '([GalleryItemAttributesMap::orderWeight])
 
91
        FROM
 
92
          [GalleryItemAttributesMap], [GalleryChildEntity]
 
93
        WHERE
 
94
          [GalleryChildEntity::id] = [GalleryItemAttributesMap::itemId]
 
95
          AND
 
96
          [GalleryChildEntity::parentId] = ?
 
97
        ';
 
98
        list ($ret, $searchResults) = $gallery->search($query, array($itemId));
 
99
        if ($ret->isError()) {
 
100
            return array($ret->wrap(__FILE__, __LINE__), null);
 
101
        }
 
102
 
 
103
        $result = $searchResults->nextResult();
 
104
        if (isset($result[0])) {
 
105
            $weight = (int)$result[0];
 
106
        } else {
 
107
            $weight = 0;
 
108
        }
 
109
 
 
110
        return array(GalleryStatus::success(), $weight);
 
111
    }
 
112
 
 
113
    /**
 
114
     * Fetch the weight of the next peer in line (higher or lower, as specified)
 
115
     *
 
116
     * @param int the item id
 
117
     * @param int the direction (HIGHER_WEIGHT, LOWER_WEIGHT)
 
118
     * @return array object GalleryStatus a status code
 
119
     *               int a weight
 
120
     * @static
 
121
     */
 
122
    function fetchNextWeight($itemId, $direction) {
 
123
        global $gallery;
 
124
 
 
125
        if ($direction == LOWER_WEIGHT) {
 
126
            $aggregate = 'MAX';
 
127
            $comparison = '<';
 
128
        } else {
 
129
            $aggregate = 'MIN';
 
130
            $comparison = '>';
 
131
        }
 
132
 
 
133
        $query = '
 
134
        SELECT
 
135
          ' . $aggregate . '([GalleryItemAttributesMap=2::orderWeight])
 
136
        FROM
 
137
          [GalleryItemAttributesMap=1], [GalleryItemAttributesMap=2],
 
138
          [GalleryChildEntity=1], [GalleryChildEntity=2]
 
139
        WHERE
 
140
          [GalleryChildEntity=1::id] = ?
 
141
          AND
 
142
          [GalleryChildEntity=1::parentId] = [GalleryChildEntity=2::parentId]
 
143
          AND
 
144
          [GalleryChildEntity=1::id] = [GalleryItemAttributesMap=1::itemId]
 
145
          AND
 
146
          [GalleryChildEntity=2::id] = [GalleryItemAttributesMap=2::itemId]
 
147
          AND
 
148
          [GalleryItemAttributesMap=2::orderWeight] ' .
 
149
                $comparison . ' [GalleryItemAttributesMap=1::orderWeight]
 
150
        ';
 
151
        list ($ret, $searchResults) = $gallery->search($query, array($itemId));
 
152
        if ($ret->isError()) {
 
153
            return array($ret->wrap(__FILE__, __LINE__), null);
 
154
        }
 
155
 
 
156
        $result = $searchResults->nextResult();
 
157
        if (isset($result[0])) {
 
158
            $weight = (int)$result[0];
 
159
        } else {
 
160
            $weight = null;
 
161
        }
 
162
 
 
163
        return array(GalleryStatus::success(), $weight);
 
164
    }
 
165
 
 
166
    /**
 
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
 
171
     * @static
 
172
     */
 
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__);
 
179
        }
 
180
 
 
181
        return GalleryStatus::success();
 
182
    }
 
183
 
 
184
    /**
 
185
     * Set the parent id sequence for an item id
 
186
     *
 
187
     * @param int id the item id
 
188
     * @param array the parent sequence (ids)
 
189
     * @return object GalleryStatus a status code
 
190
     * @static
 
191
     */
 
192
    function setParentSequence($itemId, $parentSequence) {
 
193
        if (empty($parentSequence)) {
 
194
            $parentSequence = '';
 
195
        } else {
 
196
            $parentSequence = join('/', $parentSequence) . '/';
 
197
        }
 
198
 
 
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__);
 
204
        }
 
205
 
 
206
        return GalleryStatus::success();
 
207
    }
 
208
 
 
209
    /**
 
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
 
214
     * @static
 
215
     */
 
216
    function createItemAttributes($itemId, $parentSequence) {
 
217
        if (empty($parentSequence)) {
 
218
            $parentSequence = '';
 
219
        } else {
 
220
            $parentSequence = join('/', $parentSequence) . '/';
 
221
        }
 
222
        GalleryCoreApi::relativeRequireOnce('modules/core/classes/GalleryItemAttributesMap.class');
 
223
        $ret = GalleryItemAttributesMap::addMapEntry(array('itemId' => $itemId,
 
224
                                                           'viewCount' => 0,
 
225
                                                           'orderWeight' => 0,
 
226
                                                           'parentSequence' => $parentSequence));
 
227
        if ($ret->isError()) {
 
228
            return $ret->wrap(__FILE__, __LINE__);
 
229
        }
 
230
 
 
231
        return GalleryStatus::success();
 
232
    }
 
233
 
 
234
    /**
 
235
     * Remove the attributes for the given item
 
236
     * @param int the item id
 
237
     * @return object GalleryStatus a status code
 
238
     * @static
 
239
     */
 
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__);
 
245
        }
 
246
 
 
247
        return GalleryStatus::success();
 
248
    }
 
249
 
 
250
    /**
 
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
 
255
     * @static
 
256
     */
 
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__);
 
263
        }
 
264
 
 
265
        return GalleryStatus::success();
 
266
    }
 
267
 
 
268
    /**
 
269
     * Get the view count for this item id
 
270
     * @param int the item id
 
271
     * @return array object GalleryStatus a status code
 
272
     *               int view count
 
273
     * @static
 
274
     */
 
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);
 
282
        }
 
283
 
 
284
        return array(GalleryStatus::success(), $viewCounts[$itemId]);
 
285
    }
 
286
 
 
287
    /**
 
288
     * Update all items containing the source parent sequence to the new parent sequence
 
289
     *
 
290
     * @param int id the item id
 
291
     * @param array the parent sequence (ids)
 
292
     * @return object GalleryStatus a status code
 
293
     * @static
 
294
     */
 
295
    function updateParentSequence($oldParentSequence, $newParentSequence) {
 
296
        GalleryCoreApi::relativeRequireOnce(
 
297
            'modules/core/classes/GalleryStorage/DatabaseSqlFragment.class');
 
298
        global $gallery;
 
299
 
 
300
        $oldParentSequence = join('/', $oldParentSequence) . '/';
 
301
        $newParentSequence = join('/', $newParentSequence) . '/';
 
302
 
 
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__);
 
308
        }
 
309
        list ($ret, $newSequenceSql) = $storage->getFunctionSql('CONCAT', array('?', $substring));
 
310
        if ($ret->isError()) {
 
311
            return $ret->wrap(__FILE__, __LINE__);
 
312
        }
 
313
 
 
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__);
 
322
        }
 
323
 
 
324
        return GalleryStatus::success();
 
325
    }
 
326
}
 
327
?>