summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
Diffstat (limited to 'modules')
-rw-r--r--modules/gallery/controllers/albums.php2
-rw-r--r--modules/gallery/controllers/movies.php4
-rw-r--r--modules/gallery/controllers/photos.php4
-rw-r--r--modules/gallery/helpers/item.php85
-rw-r--r--modules/gallery/models/item.php79
5 files changed, 94 insertions, 80 deletions
diff --git a/modules/gallery/controllers/albums.php b/modules/gallery/controllers/albums.php
index 25df0da7..3435465c 100644
--- a/modules/gallery/controllers/albums.php
+++ b/modules/gallery/controllers/albums.php
@@ -37,7 +37,7 @@ class Albums_Controller extends Items_Controller {
if ($show) {
$child = ORM::factory("item", $show);
- $index = $album->get_position($child);
+ $index = item::get_position($child);
if ($index) {
$page = ceil($index / $page_size);
if ($page == 1) {
diff --git a/modules/gallery/controllers/movies.php b/modules/gallery/controllers/movies.php
index bf50abd5..7c85dd98 100644
--- a/modules/gallery/controllers/movies.php
+++ b/modules/gallery/controllers/movies.php
@@ -28,10 +28,10 @@ class Movies_Controller extends Items_Controller {
access::required("view", $movie);
$where = array(array("type", "!=", "album"));
- $position = $movie->parent()->get_position($movie, $where);
+ $position = item::get_position($movie, $where);
if ($position > 1) {
list ($previous_item, $ignore, $next_item) =
- $movie->parent()->children(3, $position - 2, $where);
+ $movie->parent()->viewable()->children(3, $position - 2, $where);
} else {
$previous_item = null;
list ($next_item) = $movie->parent()->viewable()->children(1, $position, $where);
diff --git a/modules/gallery/controllers/photos.php b/modules/gallery/controllers/photos.php
index d500a92e..4578747d 100644
--- a/modules/gallery/controllers/photos.php
+++ b/modules/gallery/controllers/photos.php
@@ -28,10 +28,10 @@ class Photos_Controller extends Items_Controller {
access::required("view", $photo);
$where = array(array("type", "!=", "album"));
- $position = $photo->parent()->get_position($photo, $where);
+ $position = item::get_position($photo, $where);
if ($position > 1) {
list ($previous_item, $ignore, $next_item) =
- $photo->parent()->children(3, $position - 2, $where);
+ $photo->parent()->viewable()->children(3, $position - 2, $where);
} else {
$previous_item = null;
list ($next_item) = $photo->parent()->viewable()->children(1, $position, $where);
diff --git a/modules/gallery/helpers/item.php b/modules/gallery/helpers/item.php
index 29dd8603..a2d5f74d 100644
--- a/modules/gallery/helpers/item.php
+++ b/modules/gallery/helpers/item.php
@@ -304,4 +304,89 @@ class item_Core {
->where("rand_key", "<", random::percent())
->order_by("rand_key", "DESC");
}
+
+ /**
+ * Find the position of the given item in its parent album. The resulting
+ * value is 1-indexed, so the first child in the album is at position 1.
+ */
+ static function get_position($item, $where=array()) {
+ $album = $item->parent();
+
+ if (!strcasecmp($album->sort_order, "DESC")) {
+ $comp = ">";
+ } else {
+ $comp = "<";
+ }
+ $query_model = ORM::factory("item");
+
+ // If the comparison column has NULLs in it, we can't use comparators on it
+ // and will have to deal with it the hard way.
+ $count = $query_model->viewable()
+ ->where("parent_id", "=", $album->id)
+ ->where($album->sort_column, "IS", null)
+ ->merge_where($where)
+ ->count_all();
+
+ if (empty($count)) {
+ // There are no NULLs in the sort column, so we can just use it directly.
+ $sort_column = $album->sort_column;
+
+ $position = $query_model->viewable()
+ ->where("parent_id", "=", $album->id)
+ ->where($sort_column, $comp, $item->$sort_column)
+ ->merge_where($where)
+ ->count_all();
+
+ // 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.
+ foreach ($query_model->viewable()
+ ->select("id")
+ ->where("parent_id", "=", $album->id)
+ ->where($sort_column, "=", $item->$sort_column)
+ ->merge_where($where)
+ ->order_by(array("id" => "ASC"))
+ ->find_all() as $row) {
+ $position++;
+ if ($row->id == $item->id) {
+ break;
+ }
+ }
+ } else {
+ // There are NULLs in the sort column, so we can't use MySQL comparators.
+ // Fall back to iterating over every child row to get to the current one.
+ // This can be wildly inefficient for really large albums, but it should
+ // be a rare case that the user is sorting an album with null values in
+ // the sort column.
+ //
+ // Reproduce the children() functionality here using Database directly to
+ // avoid loading the whole ORM for each row.
+ $order_by = array($album->sort_column => $album->sort_order);
+ // Use id as a tie breaker
+ if ($album->sort_column != "id") {
+ $order_by["id"] = "ASC";
+ }
+
+ $position = 0;
+ foreach ($query_model->viewable()
+ ->select("id")
+ ->where("parent_id", "=", $album->id)
+ ->merge_where($where)
+ ->order_by($order_by)
+ ->find_all() as $row) {
+ $position++;
+ if ($row->id == $item->id) {
+ break;
+ }
+ }
+ }
+
+ return $position;
+ }
} \ No newline at end of file
diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php
index 88a444b4..47b062b8 100644
--- a/modules/gallery/models/item.php
+++ b/modules/gallery/models/item.php
@@ -546,83 +546,12 @@ class Item_Model_Core extends ORM_MPTT {
/**
* Find the position of the given child id in this album. The resulting value is 1-indexed, so
* the first child in the album is at position 1.
+ *
+ * This method stands as a backward compatibility for gallery 3.0, and will
+ * be deprecated in version 3.1.
*/
public function get_position($child, $where=array()) {
- if (!strcasecmp($this->sort_order, "DESC")) {
- $comp = ">";
- } else {
- $comp = "<";
- }
- $db = db::build();
-
- // If the comparison column has NULLs in it, we can't use comparators on it and will have to
- // deal with it the hard way.
- $count = $db->from("items")
- ->where("parent_id", "=", $this->id)
- ->where($this->sort_column, "IS", null)
- ->merge_where($where)
- ->count_records();
-
- if (empty($count)) {
- // There are no NULLs in the sort column, so we can just use it directly.
- $sort_column = $this->sort_column;
-
- $position = $db->from("items")
- ->where("parent_id", "=", $this->id)
- ->where($sort_column, $comp, $child->$sort_column)
- ->merge_where($where)
- ->count_records();
-
- // 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.
- foreach ($db
- ->select("id")
- ->from("items")
- ->where("parent_id", "=", $this->id)
- ->where($sort_column, "=", $child->$sort_column)
- ->merge_where($where)
- ->order_by(array("id" => "ASC"))
- ->execute() as $row) {
- $position++;
- if ($row->id == $child->id) {
- break;
- }
- }
- } else {
- // There are NULLs in the sort column, so we can't use MySQL comparators. Fall back to
- // iterating over every child row to get to the current one. This can be wildly inefficient
- // for really large albums, but it should be a rare case that the user is sorting an album
- // with null values in the sort column.
- //
- // Reproduce the children() functionality here using Database directly to avoid loading the
- // whole ORM for each row.
- $order_by = array($this->sort_column => $this->sort_order);
- // Use id as a tie breaker
- if ($this->sort_column != "id") {
- $order_by["id"] = "ASC";
- }
-
- $position = 0;
- foreach ($db->select("id")
- ->from("items")
- ->where("parent_id", "=", $this->id)
- ->merge_where($where)
- ->order_by($order_by)
- ->execute() as $row) {
- $position++;
- if ($row->id == $child->id) {
- break;
- }
- }
- }
-
- return $position;
+ return item::get_position($child, $where);
}
/**