diff options
author | Bharat Mediratta <bharat@menalto.com> | 2009-06-23 14:50:41 -0700 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2009-06-23 14:50:41 -0700 |
commit | ba7163bd30939f87aa3786dd8583a361bc638baf (patch) | |
tree | 298ba53cd3230c8044fe7ce843b86a8fcc515baf /modules | |
parent | 4e3b84eb391de177d94fd1c4189d456280fd7fdd (diff) |
Fix get_position() to properly deal with duplicate values in the sort column. Fixes #389
Diffstat (limited to 'modules')
-rw-r--r-- | modules/gallery/models/item.php | 39 |
1 files changed, 33 insertions, 6 deletions
diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index 07c305d8..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; } /** |