Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Fix item_Model::get_position() and item controllers #5

Closed
wants to merge 2 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+12 −8
Split
@@ -37,7 +37,7 @@ public function show($album) {
if ($show) {
$child = ORM::factory("item", $show);
- $index = $album->get_position($child);
+ $index = $album->viewable()->get_position($child);
if ($index) {
$page = ceil($index / $page_size);
if ($page == 1) {
@@ -28,10 +28,10 @@ public function show($movie) {
access::required("view", $movie);
$where = array(array("type", "!=", "album"));
- $position = $movie->parent()->get_position($movie, $where);
+ $position = $movie->parent()->viewable()->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);
@@ -28,10 +28,10 @@ public function show($photo) {
access::required("view", $photo);
$where = array(array("type", "!=", "album"));
- $position = $photo->parent()->get_position($photo, $where);
+ $position = $photo->parent()->viewable()->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);
@@ -546,17 +546,20 @@ public function album_cover() {
/**
* 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 executes a query on the model, so the database builder is reset
+ * after a call.
*/
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.
+ $db = clone $this->db_builder;
$count = $db->from("items")
->where("parent_id", "=", $this->id)
->where($this->sort_column, "IS", null)
@@ -567,6 +570,7 @@ public function get_position($child, $where=array()) {
// There are no NULLs in the sort column, so we can just use it directly.
$sort_column = $this->sort_column;
+ $db = clone $this->db_builder;
$position = $db->from("items")
->where("parent_id", "=", $this->id)
->where($sort_column, $comp, $child->$sort_column)
@@ -581,7 +585,7 @@ public function get_position($child, $where=array()) {
//
// Fix this by doing a 2nd query where we iterate over the equivalent columns and add them to
// our base value.
- foreach ($db
+ foreach ($this->db_builder
->select("id")
->from("items")
->where("parent_id", "=", $this->id)
@@ -609,7 +613,7 @@ public function get_position($child, $where=array()) {
}
$position = 0;
- foreach ($db->select("id")
+ foreach ($this->db_builder->select("id")
->from("items")
->where("parent_id", "=", $this->id)
->merge_where($where)