summaryrefslogtreecommitdiff
path: root/modules/gallery/models
diff options
context:
space:
mode:
Diffstat (limited to 'modules/gallery/models')
-rw-r--r--modules/gallery/models/cache.php20
-rw-r--r--modules/gallery/models/item.php39
2 files changed, 53 insertions, 6 deletions
diff --git a/modules/gallery/models/cache.php b/modules/gallery/models/cache.php
new file mode 100644
index 00000000..99e19a6e
--- /dev/null
+++ b/modules/gallery/models/cache.php
@@ -0,0 +1,20 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Cache_Model extends ORM {}
diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php
index 7dce9e51..430119b5 100644
--- a/modules/gallery/models/item.php
+++ b/modules/gallery/models/item.php
@@ -378,14 +378,41 @@ class Item_Model extends ORM_MPTT {
* the first child in the album is at position 1.
*/
public function get_position($child_id) {
- $result = Database::instance()->query("
+ if ($this->sort_order == "DESC") {
+ $comp = ">";
+ } else {
+ $comp = "<";
+ }
+
+ $db = Database::instance();
+ $position = $db->query("
SELECT COUNT(*) AS position FROM {items}
- WHERE parent_id = {$this->id}
- AND {$this->sort_column} <= (SELECT {$this->sort_column}
- FROM {items} WHERE id = $child_id)
- ORDER BY {$this->sort_column} {$this->sort_order}");
+ WHERE parent_id = {$this->id}
+ AND `{$this->sort_column}` $comp (SELECT `{$this->sort_column}`
+ FROM {items} WHERE id = $child_id)
+ ORDER BY `{$this->sort_column}` {$this->sort_order}")->current()->position;
+
+ // We stopped short of our target value in the sort (notice that we're using a < comparator
+ // above) because it's possible that we have duplicate values in the sort column. An
+ // equality check would just arbitrarily pick one of those multiple possible equivalent
+ // columns, which would mean that if you choose a sort order that has duplicates, it'd pick
+ // any one of them as the child's "position".
+ //
+ // Fix this by doing a 2nd query where we iterate over the equivalent columns and add them to
+ // our base value.
+ $result = $db->query("
+ SELECT id FROM {items}
+ WHERE parent_id = {$this->id}
+ AND `{$this->sort_column}` = (SELECT `{$this->sort_column}`
+ FROM {items} WHERE id = $child_id)");
+ foreach ($result as $row) {
+ $position++;
+ if ($row->id == $child_id) {
+ break;
+ }
+ }
- return $result->current()->position;
+ return $position;
}
/**