diff options
author | Nathan Kinkade <nath@nkinka.de> | 2011-09-28 20:39:05 +0000 |
---|---|---|
committer | Nathan Kinkade <nath@nkinka.de> | 2011-09-28 20:39:05 +0000 |
commit | 47456c0de8a408bca9b5a02a0fbc835ff1c01a68 (patch) | |
tree | dca2ead5e7159b28aa323465a363f81813f75acd | |
parent | d8dffba94277c2179a59eb72d6927a5bdb879648 (diff) | |
parent | dfbfe090168ee4ed44e2c8208e79d3fe40f90437 (diff) |
Manually fixed a merge conflict after pulling.
-rw-r--r-- | .build_number | 2 | ||||
-rw-r--r-- | modules/gallery/controllers/albums.php | 21 | ||||
-rw-r--r-- | modules/gallery/controllers/movies.php | 25 | ||||
-rw-r--r-- | modules/gallery/controllers/photos.php | 25 | ||||
-rw-r--r-- | modules/gallery/helpers/item.php | 18 | ||||
-rw-r--r-- | modules/gallery/libraries/Theme_View.php | 19 | ||||
-rw-r--r-- | modules/search/controllers/search.php | 43 | ||||
-rw-r--r-- | modules/search/helpers/search.php | 45 | ||||
-rw-r--r-- | modules/tag/controllers/tag.php | 47 | ||||
-rw-r--r-- | modules/tag/helpers/tag.php | 19 | ||||
-rw-r--r-- | modules/tag/models/tag.php | 37 | ||||
-rw-r--r-- | themes/wind/views/page.html.php | 2 |
12 files changed, 221 insertions, 82 deletions
diff --git a/.build_number b/.build_number index a533fe1e..a8c3e6c9 100644 --- a/.build_number +++ b/.build_number @@ -3,4 +3,4 @@ ; process. You don't need to edit it. In fact.. ; ; DO NOT EDIT THIS FILE BY HAND! -build_number=170 +build_number=174 diff --git a/modules/gallery/controllers/albums.php b/modules/gallery/controllers/albums.php index 1c48c734..8aa3bb35 100644 --- a/modules/gallery/controllers/albums.php +++ b/modules/gallery/controllers/albums.php @@ -72,10 +72,29 @@ class Albums_Controller extends Items_Controller { "breadcrumbs" => Breadcrumb::array_from_item_parents($album), "children_count" => $children_count)); $template->content = new View("album.html"); - $album->increment_view_count(); print $template; + item::set_display_context_callback("Albums_Controller::get_display_context"); + } + + static function get_display_context($item) { + $where = array(array("type", "!=", "album")); + $position = item::get_position($item, $where); + if ($position > 1) { + list ($previous_item, $ignore, $next_item) = + $item->parent()->viewable()->children(3, $position - 2, $where); + } else { + $previous_item = null; + list ($next_item) = $item->parent()->viewable()->children(1, $position, $where); + } + + return array("position" => $position, + "previous_item" => $previous_item, + "next_item" => $next_item, + "sibling_count" => $item->parent()->viewable()->children_count($where), + "parents" => $item->parents()->as_array(), + "breadcrumbs" => Breadcrumb::array_from_item_parents($item)); } public function create($parent_id) { diff --git a/modules/gallery/controllers/movies.php b/modules/gallery/controllers/movies.php index 0f12c3fb..76263dc0 100644 --- a/modules/gallery/controllers/movies.php +++ b/modules/gallery/controllers/movies.php @@ -27,28 +27,11 @@ class Movies_Controller extends Items_Controller { access::required("view", $movie); - $where = array(array("type", "!=", "album")); - $position = item::get_position($movie, $where); - if ($position > 1) { - list ($previous_item, $ignore, $next_item) = - $movie->parent()->viewable()->children(3, $position - 2, $where); - } else { - $previous_item = null; - list ($next_item) = $movie->parent()->viewable()->children(1, $position, $where); - } - $template = new Theme_View("page.html", "item", "movie"); - $template->set_global( - array("item" => $movie, - "children" => array(), - "children_count" => 0, - "parents" => $movie->parents()->as_array(), - "breadcrumbs" => Breadcrumb::array_from_item_parents($movie), - "next_item" => $next_item, - "previous_item" => $previous_item, - "sibling_count" => $movie->parent()->viewable()->children_count($where), - "position" => $position)); - + $template->set_global(array("item" => $movie, + "children" => array(), + "children_count" => 0)); + $template->set_global(item::get_display_context($movie)); $template->content = new View("movie.html"); $movie->increment_view_count(); diff --git a/modules/gallery/controllers/photos.php b/modules/gallery/controllers/photos.php index af8aed16..7e78b205 100644 --- a/modules/gallery/controllers/photos.php +++ b/modules/gallery/controllers/photos.php @@ -27,28 +27,11 @@ class Photos_Controller extends Items_Controller { access::required("view", $photo); - $where = array(array("type", "!=", "album")); - $position = item::get_position($photo, $where); - if ($position > 1) { - list ($previous_item, $ignore, $next_item) = - $photo->parent()->viewable()->children(3, $position - 2, $where); - } else { - $previous_item = null; - list ($next_item) = $photo->parent()->viewable()->children(1, $position, $where); - } - $template = new Theme_View("page.html", "item", "photo"); - $template->set_global( - array("item" => $photo, - "children" => array(), - "children_count" => 0, - "parents" => $photo->parents()->as_array(), - "breadcrumbs" => Breadcrumb::array_from_item_parents($photo), - "next_item" => $next_item, - "previous_item" => $previous_item, - "sibling_count" => $photo->parent()->viewable()->children_count($where), - "position" => $position)); - + $template->set_global(array("item" => $photo, + "children" => array(), + "children_count" => 0)); + $template->set_global(item::get_display_context($photo)); $template->content = new View("photo.html"); $photo->increment_view_count(); diff --git a/modules/gallery/helpers/item.php b/modules/gallery/helpers/item.php index 7e779544..0bb45e49 100644 --- a/modules/gallery/helpers/item.php +++ b/modules/gallery/helpers/item.php @@ -402,4 +402,22 @@ class item_Core { return $position; } + + /** + * Set the display context callback for any future item renders. + */ + static function set_display_context_callback() { + $args = func_get_args(); + Cache::instance()->set("display_context_" . $sid = Session::instance()->id(), $args); + } + + /** + * Call the display context callback for the given item + */ + static function get_display_context($item) { + $args = Cache::instance()->get("display_context_" . $sid = Session::instance()->id()); + $callback = $args[0]; + $args[0] = $item; + return call_user_func_array($callback, $args); + } }
\ No newline at end of file diff --git a/modules/gallery/libraries/Theme_View.php b/modules/gallery/libraries/Theme_View.php index d6834464..389eace6 100644 --- a/modules/gallery/libraries/Theme_View.php +++ b/modules/gallery/libraries/Theme_View.php @@ -43,9 +43,6 @@ class Theme_View_Core extends Gallery_View { "page_type" => $page_type, "page_subtype" => $page_subtype, "page_title" => null)); - if ($page_type == "collection") { - $this->set_global("thumb_proportion", $this->thumb_proportion()); - } if (module::get_var("gallery", "maintenance_mode", 0)) { if (identity::active_user()->admin) { @@ -57,12 +54,20 @@ class Theme_View_Core extends Gallery_View { /** * Proportion of the current thumb_size's to default + * @param object Item_Model (optional) check the proportions for this item * @return int */ - public function thumb_proportion() { - // @TODO change the 200 to a theme supplied value when and if we come up with an - // API to allow the theme to set defaults. - return module::get_var("gallery", "thumb_size", 200) / 200; + public function thumb_proportion($item=null) { + // By default we have a globally fixed thumbnail size In core code, we just return a fixed + // proportion based on the global thumbnail size, but since modules can override that, we + // return the actual proportions when we have them. + if ($item && $item->has_thumb()) { + return max($item->thumb_width, $item->thumb_height) / 200; + } else { + // @TODO change the 200 to a theme supplied value when and if we come up with an + // API to allow the theme to set defaults. + return module::get_var("gallery", "thumb_size", 200) / 200; + } } public function item() { diff --git a/modules/search/controllers/search.php b/modules/search/controllers/search.php index 5db63ab0..e4ac6702 100644 --- a/modules/search/controllers/search.php +++ b/modules/search/controllers/search.php @@ -21,6 +21,18 @@ class Search_Controller extends Controller { public function index() { $page_size = module::get_var("gallery", "page_size", 9); $q = Input::instance()->get("q"); + $q_with_more_terms = search::add_query_terms($q); + $show = Input::instance()->get("show"); + + if ($show) { + $child = ORM::factory("item", $show); + $index = search::get_position($child, $q_with_more_terms); + if ($index) { + $page = ceil($index / $page_size); + url::redirect(url::abs_site("search?q=" . urlencode($q) . ($page == 1 ? "" : "&page=$page"))); + } + } + $page = Input::instance()->get("page", 1); // Make sure that the page references a valid offset @@ -30,9 +42,10 @@ class Search_Controller extends Controller { $offset = ($page - 1) * $page_size; - $q_with_more_terms = search::add_query_terms($q); list ($count, $result) = search::search($q_with_more_terms, $page_size, $offset); + $title = t("Search: %q", array("q" => $q_with_more_terms)); + $max_pages = max(ceil($count / $page_size), 1); $template = new Theme_View("page.html", "collection", "search"); @@ -52,5 +65,33 @@ class Search_Controller extends Controller { $template->content->q = $q; print $template; + + item::set_display_context_callback( + "Search_Controller::get_display_context", $title, $q_with_more_terms, $q); + } + + static function get_display_context($item, $title, $query_terms, $q) { + $position = search::get_position($item, $query_terms); + + if ($position > 1) { + list ($count, $result_data) = search::search($query_terms, 3, $position - 2); + list ($previous_item, $ignore, $next_item) = $result_data; + } else { + $previous_item = null; + list ($count, $result_data) = search::search($query_terms, 1, $position); + list ($next_item) = $result_data; + } + + $search_url = url::abs_site("search?q=" . urlencode($q) . "&show={$item->id}"); + $root = item::root(); + + return array("position" => $position, + "previous_item" => $previous_item, + "next_item" => $next_item, + "sibling_count" => $count, + "breadcrumbs" => array( + Breadcrumb::instance($root->title, "/", $root->id)->set_first(), + Breadcrumb::instance(t("Search: %q", array("q" => $q)), $search_url), + Breadcrumb::instance($item->title, $item->url())->set_last())); } } diff --git a/modules/search/helpers/search.php b/modules/search/helpers/search.php index a3fd795a..a77a2433 100644 --- a/modules/search/helpers/search.php +++ b/modules/search/helpers/search.php @@ -36,8 +36,19 @@ class search_Core { static function search($q, $limit, $offset) { $db = Database::instance(); - $q = $db->escape($q); + $query = self::_build_query_base($q) . + "ORDER BY `score` DESC " . + "LIMIT $limit OFFSET " . (int)$offset; + + $data = $db->query($query); + $count = $db->query("SELECT FOUND_ROWS() as c")->current()->c; + + return array($count, new ORM_Iterator(ORM::factory("item"), $data)); + } + + private static function _build_query_base($q, $where=array()) { + $q = Database::instance()->escape($q); if (!identity::active_user()->admin) { foreach (identity::group_ids_for_active_user() as $id) { $fields[] = "`view_$id` = TRUE"; // access::ALLOW @@ -47,18 +58,13 @@ class search_Core { $access_sql = ""; } - $query = + return "SELECT SQL_CALC_FOUND_ROWS {items}.*, " . " MATCH({search_records}.`data`) AGAINST ('$q') AS `score` " . "FROM {items} JOIN {search_records} ON ({items}.`id` = {search_records}.`item_id`) " . "WHERE MATCH({search_records}.`data`) AGAINST ('$q' IN BOOLEAN MODE) " . - $access_sql . - "ORDER BY `score` DESC " . - "LIMIT $limit OFFSET " . (int)$offset; - $data = $db->query($query); - $count = $db->query("SELECT FOUND_ROWS() as c")->current()->c; - - return array($count, new ORM_Iterator(ORM::factory("item"), $data)); + (empty($where) ? "" : " AND " . join(" AND ", $where)) . + $access_sql; } /** @@ -103,4 +109,25 @@ class search_Core { return array($remaining, $total, $percent); } + + static function get_position($item, $q) { + $page_size = module::get_var("gallery", "page_size", 9); + + $query = self::_build_query_base($q, array("{items}.id = " . $item->id)); + + $db = Database::instance(); + + // Truncate the score by two decimal places as this resolves the issues + // that arise due to in exact numeric conversions. + $score = $db->query($query)->current()->score; + $score = substr($score, 0, strlen($score) - 2); + + $data = $db->query(self::_build_query_base($q) . "having `score` >= " . $score); + + $data->seek($data->count() - 1); + + while ($data->get("id") != $item->id && $data->prev()->valid()); + + return $data->key() + 1; + } } diff --git a/modules/tag/controllers/tag.php b/modules/tag/controllers/tag.php index 7786daa1..559e2a5a 100644 --- a/modules/tag/controllers/tag.php +++ b/modules/tag/controllers/tag.php @@ -22,7 +22,22 @@ class Tag_Controller extends Controller { $tag_id = $function; $tag = ORM::factory("tag")->where("id", "=", $tag_id)->find(); $page_size = module::get_var("gallery", "page_size", 9); - $page = (int) Input::instance()->get("page", "1"); + + $input = Input::instance(); + $show = $input->get("show"); + + if ($show) { + $child = ORM::factory("item", $show); + $index = tag::get_position($tag, $child); + if ($index) { + $page = ceil($index / $page_size); + $uri = "tag/$tag_id/" . urlencode($tag->name); + url::redirect($uri . ($page == 1 ? "" : "?page=$page")); + } + } else { + $page = (int) $input->get("page", "1"); + } + $children_count = $tag->items_count(); $offset = ($page-1) * $page_size; $max_pages = max(ceil($children_count / $page_size), 1); @@ -44,11 +59,37 @@ class Tag_Controller extends Controller { "children" => $tag->items($page_size, $offset), "breadcrumbs" => array( Breadcrumb::instance($root->title, $root->url())->set_first(), - Breadcrumb::instance($tag->name, $tag->url())->set_last()), + Breadcrumb::instance(t("Tag: %tag_name", array("tag_name" => $tag->name)), + $tag->url())->set_last()), "children_count" => $children_count)); $template->content = new View("dynamic.html"); $template->content->title = t("Tag: %tag_name", array("tag_name" => $tag->name)); - print $template; + + item::set_display_context_callback("Tag_Controller::get_display_context", $tag->id); + } + + static function get_display_context($item, $tag_id) { + $tag = ORM::factory("tag", $tag_id); + $where = array(array("type", "!=", "album")); + + $position = tag::get_position($tag, $item, $where); + if ($position > 1) { + list ($previous_item, $ignore, $next_item) = $tag->items(3, $position - 2, $where); + } else { + $previous_item = null; + list ($next_item) = $tag->items(1, $position, $where); + } + + $root = item::root(); + return array("position" => $position, + "previous_item" => $previous_item, + "next_item" => $next_item, + "sibling_count" => $tag->items_count($where), + "breadcrumbs" => array( + Breadcrumb::instance($root->title, $root->url())->set_first(), + Breadcrumb::instance(t("Tag: %tag_name", array("tag_name" => $tag->name)), + $tag->url("show={$item->id}")), + Breadcrumb::instance($item->title, $item->url())->set_last())); } } diff --git a/modules/tag/helpers/tag.php b/modules/tag/helpers/tag.php index c21104ee..83a00080 100644 --- a/modules/tag/helpers/tag.php +++ b/modules/tag/helpers/tag.php @@ -136,4 +136,23 @@ class tag_Core { // extremely rare case. db::build()->delete("tags")->where("count", "=", 0)->execute(); } + + /** + * Find the position of the given item in the tag collection. The resulting + * value is 1-indexed, so the first child in the album is at position 1. + * + * @param Tag_Model $tag + * @param Item_Model $item + * @param array $where an array of arrays, each compatible with ORM::where() + */ + static function get_position($tag, $item, $where=array()) { + return ORM::factory("item") + ->viewable() + ->join("items_tags", "items.id", "items_tags.item_id") + ->where("items_tags.tag_id", "=", $tag->id) + ->where("items.id", "<=", $item->id) + ->merge_where($where) + ->order_by("items.id") + ->count_all(); + } }
\ No newline at end of file diff --git a/modules/tag/models/tag.php b/modules/tag/models/tag.php index 3dd71d8d..9ce091a2 100644 --- a/modules/tag/models/tag.php +++ b/modules/tag/models/tag.php @@ -33,36 +33,39 @@ class Tag_Model_Core extends ORM { * Return all viewable items associated with this tag. * @param integer $limit number of rows to limit result to * @param integer $offset offset in result to start returning rows from - * @param string $type the type of item (album, photo) + * @param string $where an array of arrays, each compatible with ORM::where() * @return ORM_Iterator */ - public function items($limit=null, $offset=null, $type=null) { - $model = ORM::factory("item") + public function items($limit=null, $offset=null, $where=array()) { + if (is_scalar($where)) { + // backwards compatibility + $where = array(array("items.type", "=", $where)); + } + return ORM::factory("item") ->viewable() ->join("items_tags", "items.id", "items_tags.item_id") ->where("items_tags.tag_id", "=", $this->id) - ->order_by("captured", "DESC"); - if ($type) { - $model->where("items.type", "=", $type); - } - return $model->find_all($limit, $offset); + ->merge_where($where) + ->order_by("items.id") + ->find_all($limit, $offset); } /** * Return the count of all viewable items associated with this tag. - * @param string $type the type of item (album, photo) + * @param string $where an array of arrays, each compatible with ORM::where() * @return integer */ - public function items_count($type=null) { - $model = ORM::factory("item") + public function items_count($where=array()) { + if (is_scalar($where)) { + // backwards compatibility + $where = array(array("items.type", "=", $where)); + } + return $model = ORM::factory("item") ->viewable() ->join("items_tags", "items.id", "items_tags.item_id") - ->where("items_tags.tag_id", "=", $this->id); - - if ($type) { - $model->where("items.type", "=", $type); - } - return $model->count_all(); + ->where("items_tags.tag_id", "=", $this->id) + ->merge_where($where) + ->count_all(); } /** diff --git a/themes/wind/views/page.html.php b/themes/wind/views/page.html.php index 534b7de4..ca6d2bb1 100644 --- a/themes/wind/views/page.html.php +++ b/themes/wind/views/page.html.php @@ -24,7 +24,7 @@ <link rel="apple-touch-icon-precomposed" href="<?= url::file(module::get_var("gallery", "apple_touch_icon_url")) ?>" /> <? if ($theme->page_type == "collection"): ?> - <? if ($thumb_proportion != 1): ?> + <? if (($thumb_proportion = $theme->thumb_proportion($theme->item())) != 1): ?> <? $new_width = round($thumb_proportion * 213) ?> <? $new_height = round($thumb_proportion * 240) ?> <style type="text/css"> |