From 5bc0da365221eea08b1525d4cf71371853aa4d15 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Thu, 21 Apr 2011 14:33:08 -0700 Subject: Create before_combine and after_combine events to allow modules and themes to interact with the combine list ahead of time, and to be able to do things like minification after it's combined. Fixes #1653. --- modules/gallery/libraries/Gallery_View.php | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'modules/gallery/libraries') diff --git a/modules/gallery/libraries/Gallery_View.php b/modules/gallery/libraries/Gallery_View.php index 562f7929..77e3d204 100644 --- a/modules/gallery/libraries/Gallery_View.php +++ b/modules/gallery/libraries/Gallery_View.php @@ -111,6 +111,8 @@ class Gallery_View_Core extends View { $contents = $cache->get($key); if (empty($contents)) { + module::event("before_combine", $type, $this->combine_queue[$type][$group]); + $contents = ""; foreach (array_keys($this->combine_queue[$type][$group]) as $path) { if ($type == "css") { @@ -120,6 +122,8 @@ class Gallery_View_Core extends View { } } + module::event("after_combine", $type, $contents); + $cache->set($key, $contents, array($type), 30 * 84600); $use_gzip = function_exists("gzencode") && @@ -128,6 +132,7 @@ class Gallery_View_Core extends View { $cache->set("{$key}_gz", gzencode($contents, 9, FORCE_GZIP), array($type, "gzip"), 30 * 84600); } + } unset($this->combine_queue[$type][$group]); @@ -158,6 +163,7 @@ class Gallery_View_Core extends View { $replace[] = "url('" . url::abs_file($relative) . "')"; } else { Kohana_Log::add("error", "Missing URL reference '{$match[1]}' in CSS file '$css_file'"); + } } $replace = str_replace(DIRECTORY_SEPARATOR, "/", $replace); -- cgit v1.2.3 From fa4fb20f8036745a4999d05ccb2fe285a891f3a8 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sat, 23 Apr 2011 06:50:47 -0700 Subject: If we've cleared out the last group in a combine_queue for a given type, unset the combine_queue for that type entirely. This way future calls to css() and script() emit an element until there's a new call to start_combining(). Fixes 1685. --- modules/gallery/libraries/Gallery_View.php | 3 +++ 1 file changed, 3 insertions(+) (limited to 'modules/gallery/libraries') diff --git a/modules/gallery/libraries/Gallery_View.php b/modules/gallery/libraries/Gallery_View.php index 77e3d204..1395686c 100644 --- a/modules/gallery/libraries/Gallery_View.php +++ b/modules/gallery/libraries/Gallery_View.php @@ -136,6 +136,9 @@ class Gallery_View_Core extends View { } unset($this->combine_queue[$type][$group]); + if (empty($this->combine_queue[$type])) { + unset($this->combine_queue[$type]); + } if ($type == "css") { return html::stylesheet("combined/css/$key", "screen,print,projection", true); -- cgit v1.2.3 From 4b01676f323fb280ebaa4c041e6894cbb464d8fe Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sat, 23 Apr 2011 07:25:33 -0700 Subject: Fix for ticket #541 Added a theme_info variable to the theme globals. The properties in the theme.info file are now contained in this theme_info structure. Access is: author ?> will display the theme author. --- modules/gallery/libraries/Theme_View.php | 1 + 1 file changed, 1 insertion(+) (limited to 'modules/gallery/libraries') diff --git a/modules/gallery/libraries/Theme_View.php b/modules/gallery/libraries/Theme_View.php index a507e9c8..152efc37 100644 --- a/modules/gallery/libraries/Theme_View.php +++ b/modules/gallery/libraries/Theme_View.php @@ -38,6 +38,7 @@ class Theme_View_Core extends Gallery_View { $this->item = null; $this->tag = null; $this->set_global(array("theme" => $this, + "theme_info" => theme::get_info($this->theme_name), "user" => identity::active_user(), "page_type" => $page_type, "page_subtype" => $page_subtype, -- cgit v1.2.3 From c101151616033d53587d1435881dae0fa45aeefa Mon Sep 17 00:00:00 2001 From: Andy Lindeman Date: Sat, 23 Apr 2011 12:04:12 -0400 Subject: Allow tags to be merged by renaming * Fixes #1628 --- modules/gallery/libraries/InPlaceEdit.php | 8 +++-- modules/tag/controllers/admin_tags.php | 11 +------ modules/tag/models/tag.php | 24 ++++++++++----- modules/tag/tests/Tag_Test.php | 50 ++++++++++++++++++++++++++++--- 4 files changed, 70 insertions(+), 23 deletions(-) (limited to 'modules/gallery/libraries') diff --git a/modules/gallery/libraries/InPlaceEdit.php b/modules/gallery/libraries/InPlaceEdit.php index 88c30494..739cbb61 100644 --- a/modules/gallery/libraries/InPlaceEdit.php +++ b/modules/gallery/libraries/InPlaceEdit.php @@ -56,8 +56,12 @@ class InPlaceEdit_Core { } public function validate() { - $post = Validation::factory($_POST) - ->add_callbacks("input", $this->callback); + $post = Validation::factory($_POST); + + if (!empty($this->callback)) { + $post->add_callbacks("input", $this->callback); + } + foreach ($this->rules as $rule) { $post->add_rules("input", $rule); } diff --git a/modules/tag/controllers/admin_tags.php b/modules/tag/controllers/admin_tags.php index 73042a55..fd82bc92 100644 --- a/modules/tag/controllers/admin_tags.php +++ b/modules/tag/controllers/admin_tags.php @@ -81,9 +81,7 @@ class Admin_Tags_Controller extends Admin_Controller { $in_place_edit = InPlaceEdit::factory($tag->name) ->action("admin/tags/rename/$tag->id") - ->rules(array("required", "length[1,64]")) - ->messages(array("in_use" => t("There is already a tag with that name"))) - ->callback(array($this, "check_for_duplicate")); + ->rules(array("required", "length[1,64]")); if ($in_place_edit->validate()) { $old_name = $tag->name; @@ -101,12 +99,5 @@ class Admin_Tags_Controller extends Admin_Controller { } } - public function check_for_duplicate(Validation $post_data, $field) { - $tag_exists = ORM::factory("tag")->where("name", "=", $post_data[$field])->count_all(); - if ($tag_exists) { - $post_data->add_error($field, "in_use"); - } - } - } diff --git a/modules/tag/models/tag.php b/modules/tag/models/tag.php index bd665667..bb79e707 100644 --- a/modules/tag/models/tag.php +++ b/modules/tag/models/tag.php @@ -69,13 +69,23 @@ class Tag_Model_Core extends ORM { * to this tag. */ public function save() { - $related_item_ids = array(); - foreach (db::build() - ->select("item_id") - ->from("items_tags") - ->where("tag_id", "=", $this->id) - ->execute() as $row) { - $related_item_ids[$row->item_id] = 1; + // Check to see if another tag exists with the same name + $duplicate_tag = ORM::factory("tag") + ->where("name", "=", $this->name) + ->where("id", "!=", $this->id) + ->find(); + if ($duplicate_tag->loaded()) { + // If so, tag its items with this tag so as to merge it + $duplicate_tag_items = ORM::factory("item") + ->join("items_tags", "items.id", "items_tags.item_id") + ->where("items_tags.tag_id", "=", $duplicate_tag->id) + ->find_all(); + foreach ($duplicate_tag_items as $item) { + $this->add($item); + } + + // ... and remove the duplicate tag + $duplicate_tag->delete(); } if (isset($this->object_relations["items"])) { diff --git a/modules/tag/tests/Tag_Test.php b/modules/tag/tests/Tag_Test.php index f5ccb3a2..9e10fa4a 100644 --- a/modules/tag/tests/Tag_Test.php +++ b/modules/tag/tests/Tag_Test.php @@ -18,18 +18,60 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class Tag_Test extends Gallery_Unit_Test_Case { + public function teardown() { + ORM::factory("tag")->delete_all(); + } + public function create_tag_test() { $album = test::random_album(); tag::add($album, "tag1"); $tag = ORM::factory("tag")->where("name", "=", "tag1")->find(); - $this->assert_true(1, $tag->count); + $this->assert_equal(1, $tag->count); // Make sure adding the tag again doesn't increase the count tag::add($album, "tag1"); - $this->assert_true(1, $tag->reload()->count); + $this->assert_equal(1, $tag->reload()->count); tag::add(test::random_album(), "tag1"); - $this->assert_true(2, $tag->reload()->count); + $this->assert_equal(2, $tag->reload()->count); + } + + public function rename_merge_tag_test() { + $album1 = test::random_album(); + $album2 = test::random_album(); + + tag::add($album1, "tag1"); + tag::add($album2, "tag2"); + + $tag1 = ORM::factory("tag")->where("name", "=", "tag1")->find(); + $tag1->name = "tag2"; + $tag1->save(); + + // Tags should be merged; $tag2 should be deleted + $tag1->reload(); + + $this->assert_equal(2, $tag1->count); + $this->assert_true($tag1->has($album1)); + $this->assert_true($tag1->has($album2)); + $this->assert_equal(1, ORM::factory("tag")->count_all()); + } + + public function rename_merge_tag_with_same_items_test() { + $album = test::random_album(); + + tag::add($album, "tag1"); + tag::add($album, "tag2"); + + $tag1 = ORM::factory("tag")->where("name", "=", "tag1")->find(); + $tag1->name = "tag2"; + $tag1->save(); + + // Tags should be merged + $tag1->reload(); + + $this->assert_equal(1, $tag1->count); + $this->assert_true($tag1->has($album)); + $this->assert_equal(1, ORM::factory("tag")->count_all()); } -} \ No newline at end of file +} -- cgit v1.2.3 From 76a7ad3161be0994d7ba98e9dff9b317b2430bb3 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sat, 23 Apr 2011 10:07:32 -0700 Subject: Overhaul of the uploader code: - Propagate size limits (including detecting memory limits from GD) down to the Flash so that we don't even start uploads that won't work - Improve the error messages to be more user meaningful and provide links to the codex for errors - Tell the user up front what the file size limit is. Fixes #1638 --- modules/gallery/css/gallery.css | 6 ++--- modules/gallery/helpers/graphics.php | 19 ++++++++++++++ modules/gallery/libraries/Form_Uploadify.php | 14 +++++++++++ modules/gallery/views/form_uploadify.html.php | 36 ++++++++++++++------------- 4 files changed, 55 insertions(+), 20 deletions(-) (limited to 'modules/gallery/libraries') diff --git a/modules/gallery/css/gallery.css b/modules/gallery/css/gallery.css index 275a3d7d..97d09454 100644 --- a/modules/gallery/css/gallery.css +++ b/modules/gallery/css/gallery.css @@ -29,12 +29,12 @@ #g-add-photos-canvas object, #g-add-photos-button { - left: 137px; - margin: .5em 0; + left: 93px; + margin: .5em 0; padding: .4em 1em; position: absolute; top: 0; - width: 175px; + width: auto; } #g-add-photos-canvas object { diff --git a/modules/gallery/helpers/graphics.php b/modules/gallery/helpers/graphics.php index 04501132..d19392cf 100644 --- a/modules/gallery/helpers/graphics.php +++ b/modules/gallery/helpers/graphics.php @@ -423,4 +423,23 @@ class graphics_Core { return true; } + + /** + * Return the max file size that this graphics toolkit can handle. + */ + static function max_filesize() { + if (module::get_var("gallery", "graphics_toolkit") == "gd") { + $memory_limit = trim(ini_get("memory_limit")); + $memory_limit_bytes = num::convert_to_bytes($memory_limit); + + // GD expands images in memory and uses 4 bytes of RAM for every byte + // in the file. + $max_filesize = $memory_limit_bytes / 4; + $max_filesize_human_readable = num::convert_to_human_readable($max_filesize); + return array($max_filesize, $max_filesize_human_readable); + } + + // Some arbitrarily large size + return array(1000000000, "1G"); + } } diff --git a/modules/gallery/libraries/Form_Uploadify.php b/modules/gallery/libraries/Form_Uploadify.php index 27ab9684..3e35e380 100644 --- a/modules/gallery/libraries/Form_Uploadify.php +++ b/modules/gallery/libraries/Form_Uploadify.php @@ -48,6 +48,20 @@ class Form_Uploadify_Core extends Form_Input { $v->simultaneous_upload_limit = module::get_var("gallery", "simultaneous_upload_limit"); $v->movies_allowed = (bool) movie::find_ffmpeg(); $v->suhosin_session_encrypt = (bool) ini_get("suhosin.session.encrypt"); + + list ($toolkit_max_filesize_bytes, $toolkit_max_filesize) = graphics::max_filesize(); + + $upload_max_filesize = trim(ini_get("upload_max_filesize")); + $upload_max_filesize_bytes = num::convert_to_bytes($upload_max_filesize); + + if ($upload_max_filesize_bytes < $toolkit_max_filesize_bytes) { + $v->size_limit_bytes = $upload_max_filesize_bytes; + $v->size_limit = $upload_max_filesize; + } else { + $v->size_limit_bytes = $toolkit_max_filesize_bytes; + $v->size_limit = $toolkit_max_filesize; + } + return $v; } diff --git a/modules/gallery/views/form_uploadify.html.php b/modules/gallery/views/form_uploadify.html.php index 77b6d493..83dfcc68 100644 --- a/modules/gallery/views/form_uploadify.html.php +++ b/modules/gallery/views/form_uploadify.html.php @@ -32,6 +32,7 @@ fileDesc: for_js() ?>, cancelImg: "", simUploadLimit: , + sizeLimit: , wmode: "transparent", hideButton: true, /* should be true */ auto: true, @@ -66,26 +67,30 @@ return true; }, onError: function(event, queueID, fileObj, errorObj) { - var msg = " - "; if (errorObj.type == "HTTP") { if (errorObj.info == "500") { - msg += for_js() ?>; - // Server error - check server logs + error_msg = for_js() ?>; } else if (errorObj.info == "404") { - msg += for_js() ?>; - // Server script not found + error_msg = for_js() ?>; + } else if (errorObj.info == "400") { + error_msg = $size_limit))->for_js() ?>; } else { - // Server Error: status: errorObj.info - msg += (for_js() ?>.replace("__INFO__", errorObj.info)); + msg += (for_js() ?> + .replace("__INFO__", errorObj.info) + .replace("__TYPE__", errorObj.type)); } } else if (errorObj.type == "File Size") { - var sizelimit = $("#g-uploadify").uploadifySettings(sizeLimit); - msg += fileObj.name+' '+errorObj.type+' Limit: '+Math.round(d.sizeLimit/1024)+'KB'; + error_msg = $size_limit))->for_js() ?>; } else { - msg += (for_js() ?> - .replace("__INFO__", errorObj.info) - .replace("__TYPE__", errorObj.type)); + error_msg = for_js() ?> + .replace("__INFO__", errorObj.info) + .replace("__TYPE__", errorObj.type); } + msg = " - " + + error_msg + ""; + $("#g-add-photos-status ul").append( "
  • " + fileObj.name + msg + "
  • "); $("#g-uploadify").uploadifyCancel(queueID); @@ -131,10 +136,7 @@
    -

    - -

    -
      +
        parents() as $i => $parent): ?> > title) ?> @@ -143,7 +145,7 @@
    - +
    -- cgit v1.2.3 From 5cf38ed816006af52fa08475d2957a6f18846887 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Tue, 26 Apr 2011 09:48:21 -0700 Subject: Stop using Pagination() and instead use $theme->pager() in views. Move the pager() function up to Gallery_View and replace themes/admin_wind/views/pager.html.php (Pagination based) with a modified version from the wind theme in themes/admin_wind/views/paginator.html.php. Fixes #1718. --- .../comment/controllers/admin_manage_comments.php | 13 ++-- .../comment/views/admin_manage_comments.html.php | 2 +- modules/gallery/libraries/Gallery_View.php | 46 +++++++++++ modules/gallery/libraries/MY_Pagination.php | 35 --------- modules/gallery/libraries/Theme_View.php | 46 ----------- modules/user/controllers/admin_users.php | 8 ++ modules/user/views/admin_users.html.php | 2 +- themes/admin_wind/views/pager.html.php | 44 ----------- themes/admin_wind/views/paginator.html.php | 88 ++++++++++++++++++++++ 9 files changed, 151 insertions(+), 133 deletions(-) delete mode 100644 modules/gallery/libraries/MY_Pagination.php delete mode 100644 themes/admin_wind/views/pager.html.php create mode 100644 themes/admin_wind/views/paginator.html.php (limited to 'modules/gallery/libraries') diff --git a/modules/comment/controllers/admin_manage_comments.php b/modules/comment/controllers/admin_manage_comments.php index 9bd1d7f6..effefcbb 100644 --- a/modules/comment/controllers/admin_manage_comments.php +++ b/modules/comment/controllers/admin_manage_comments.php @@ -45,6 +45,8 @@ class Admin_Manage_Comments_Controller extends Admin_Controller { $view = new Admin_View("admin.html"); $view->page_title = t("Manage comments"); + $view->page_type = "collection"; + $view->page_subtype = "admin_comments"; $view->content = new View("admin_manage_comments.html"); $view->content->counts = $this->_counts(); $view->content->menu = $this->_menu($view->content->counts); @@ -56,13 +58,12 @@ class Admin_Manage_Comments_Controller extends Admin_Controller { ->limit(self::$items_per_page) ->offset(($page - 1) * self::$items_per_page) ->find_all(); - $view->content->pager = new Pagination(); - $view->content->pager->initialize( - array("query_string" => "page", - "total_items" => $view->content->counts->$state, - "items_per_page" => self::$items_per_page, - "style" => "classic")); + // Pagination info + $view->page = $page; + $view->page_size = self::$items_per_page; + $view->children_count = $this->_counts()->$state; + $view->max_pages = ceil($view->children_count / $view->page_size); print $view; } diff --git a/modules/comment/views/admin_manage_comments.html.php b/modules/comment/views/admin_manage_comments.html.php index 34a28986..e7a61837 100644 --- a/modules/comment/views/admin_manage_comments.html.php +++ b/modules/comment/views/admin_manage_comments.html.php @@ -194,7 +194,7 @@
    - + paginator() ?>
    diff --git a/modules/gallery/libraries/Gallery_View.php b/modules/gallery/libraries/Gallery_View.php index 1395686c..e04b9169 100644 --- a/modules/gallery/libraries/Gallery_View.php +++ b/modules/gallery/libraries/Gallery_View.php @@ -30,6 +30,52 @@ class Gallery_View_Core extends View { return $absolute_url ? url::abs_file($arg) : url::file($arg); } + /** + * Set up the data and render a pager. + * + * See themes/wind/views/pager.html for documentation on the variables generated here. + */ + public function paginator() { + $v = new View("paginator.html"); + $v->page_type = $this->page_type; + $v->page_subtype = $this->page_subtype; + $v->first_page_url = null; + $v->previous_page_url = null; + $v->next_page_url = null; + $v->last_page_url = null; + + if ($this->page_type == "collection") { + $v->page = $this->page; + $v->max_pages = $this->max_pages; + $v->total = $this->children_count; + + if ($this->page != 1) { + $v->first_page_url = url::site(url::merge(array("page" => 1))); + $v->previous_page_url = url::site(url::merge(array("page" => $this->page - 1))); + } + + if ($this->page != $this->max_pages) { + $v->next_page_url = url::site(url::merge(array("page" => $this->page + 1))); + $v->last_page_url = url::site(url::merge(array("page" => $this->max_pages))); + } + + $v->first_visible_position = ($this->page - 1) * $this->page_size + 1; + $v->last_visible_position = min($this->page * $this->page_size, $v->total); + } else if ($this->page_type == "item") { + $v->position = $this->position; + $v->total = $this->sibling_count; + if ($this->previous_item) { + $v->previous_page_url = $this->previous_item->url(); + } + + if ($this->next_item) { + $v->next_page_url = $this->next_item->url(); + } + } + + return $v; + } + /** * Begin gather up scripts or css files so that they can be combined into a single request. * diff --git a/modules/gallery/libraries/MY_Pagination.php b/modules/gallery/libraries/MY_Pagination.php deleted file mode 100644 index e697c0bd..00000000 --- a/modules/gallery/libraries/MY_Pagination.php +++ /dev/null @@ -1,35 +0,0 @@ -auto_hide === TRUE AND $this->total_pages <= 1) { - return ""; - } - - if ($style === NULL) { - // Use default style - $style = $this->style; - } - - // Return rendered pagination view - return View::factory("pager.html", get_object_vars($this))->render(); - } -} diff --git a/modules/gallery/libraries/Theme_View.php b/modules/gallery/libraries/Theme_View.php index 152efc37..d6834464 100644 --- a/modules/gallery/libraries/Theme_View.php +++ b/modules/gallery/libraries/Theme_View.php @@ -138,52 +138,6 @@ class Theme_View_Core extends Gallery_View { return $menu->render(); } - /** - * Set up the data and render a pager. - * - * See themes/wind/views/pager.html for documentation on the variables generated here. - */ - public function paginator() { - $v = new View("paginator.html"); - $v->page_type = $this->page_type; - $v->page_subtype = $this->page_subtype; - $v->first_page_url = null; - $v->previous_page_url = null; - $v->next_page_url = null; - $v->last_page_url = null; - - if ($this->page_type == "collection") { - $v->page = $this->page; - $v->max_pages = $this->max_pages; - $v->total = $this->children_count; - - if ($this->page != 1) { - $v->first_page_url = url::site(url::merge(array("page" => 1))); - $v->previous_page_url = url::site(url::merge(array("page" => $this->page - 1))); - } - - if ($this->page != $this->max_pages) { - $v->next_page_url = url::site(url::merge(array("page" => $this->page + 1))); - $v->last_page_url = url::site(url::merge(array("page" => $this->max_pages))); - } - - $v->first_visible_position = ($this->page - 1) * $this->page_size + 1; - $v->last_visible_position = min($this->page * $this->page_size, $v->total); - } else if ($this->page_type == "item") { - $v->position = $this->position; - $v->total = $this->sibling_count; - if ($this->previous_item) { - $v->previous_page_url = $this->previous_item->url(); - } - - if ($this->next_item) { - $v->next_page_url = $this->next_item->url(); - } - } - - return $v; - } - /** * Print out any site wide status information. */ diff --git a/modules/user/controllers/admin_users.php b/modules/user/controllers/admin_users.php index a3633b52..41be6c03 100644 --- a/modules/user/controllers/admin_users.php +++ b/modules/user/controllers/admin_users.php @@ -21,6 +21,8 @@ class Admin_Users_Controller extends Admin_Controller { public function index() { $view = new Admin_View("admin.html"); $view->page_title = t("Users and groups"); + $view->page_type = "collection"; + $view->page_subtype = "admin_users"; $view->content = new View("admin_users.html"); // @todo: add this as a config option @@ -29,6 +31,12 @@ class Admin_Users_Controller extends Admin_Controller { $builder = db::build(); $user_count = $builder->from("users")->count_records(); + // Pagination info + $view->page = $page; + $view->page_size = $page_size; + $view->children_count = $user_count; + $view->max_pages = ceil($view->children_count / $view->page_size); + $view->content->pager = new Pagination(); $view->content->pager->initialize( array("query_string" => "page", diff --git a/modules/user/views/admin_users.html.php b/modules/user/views/admin_users.html.php index a7bd6b27..033c9dae 100644 --- a/modules/user/views/admin_users.html.php +++ b/modules/user/views/admin_users.html.php @@ -110,7 +110,7 @@
    - + paginator() ?>
    diff --git a/themes/admin_wind/views/pager.html.php b/themes/admin_wind/views/pager.html.php deleted file mode 100644 index 5fff5845..00000000 --- a/themes/admin_wind/views/pager.html.php +++ /dev/null @@ -1,44 +0,0 @@ - - -
      - $current_first_item, - "to_number" => $current_last_item, - "count" => $total_items)) ?> -
    • - - - - - - - - - - - - - - -
    • -
    • -
    • - - - - - - - - - - - - - - -
    • -
    diff --git a/themes/admin_wind/views/paginator.html.php b/themes/admin_wind/views/paginator.html.php new file mode 100644 index 00000000..3cb0223d --- /dev/null +++ b/themes/admin_wind/views/paginator.html.php @@ -0,0 +1,88 @@ + + + +
      +
    • + + + + + + + + + + + + + + + + + +
    • + +
    • + + + $first_visible_position, + "to_number" => $last_visible_position, + "count" => $total)) ?> + + $position, "total" => $total)) ?> + + + + +
    • + +
    • + + + + + + + + + + + + + + + + + +
    • +
    -- cgit v1.2.3 From b875368167658fd7992812504674afeb61c64831 Mon Sep 17 00:00:00 2001 From: Chad Parry Date: Thu, 28 Apr 2011 19:41:44 -0600 Subject: This patch helps provide raw photo support with some small changes to the framework. Items can now change their extension and MIME type. Squashed commit of the following: commit 4c2b2ebd3f2052898fbfb175650ed4cf49c8006e Author: Chad Parry Date: Wed Apr 27 20:52:35 2011 -0600 Remove a newline at the end of the file that I accidentally introduced. commit 6d564f185e5279d6cca9a7385066514ff18a2455 Merge: 7ff485f 4060640 Author: Chad Parry Date: Wed Apr 27 20:35:58 2011 -0600 Merge branch 'master' of https://github.com/gallery/gallery3 into rawphoto commit 7ff485fa48c392bbbb0370f67cb1bd6fcc00c2a4 Author: Chad Parry Date: Wed Apr 27 20:29:06 2011 -0600 Move the extensions helpers out of the Kohana system directory and into their own Gallery Extensions class. commit 26585fed03236f0f70a75959e1d3002025f4e15e Merge: 809567f c8f90e8 Author: Chad Parry Date: Sun Apr 24 08:28:39 2011 -0600 Merge branch 'master' of https://github.com/gallery/gallery3 into rawphoto commit 809567f12850f59bdeb47a2963f6968b99b5a201 Author: Chad Parry Date: Sun Apr 24 08:10:04 2011 -0600 Expose the data file field. commit fcb06bf175bb9eeff36d9c294e97ace9374ef0f3 Author: Chad Parry Date: Sun Apr 24 00:45:12 2011 -0600 Don't assign to the item->name field if the name is unchanged, because the save method will crash. commit c6ef706d70c7e48bea1145eec1b13fb5683e023f Author: Chad Parry Date: Sat Apr 23 22:55:59 2011 -0600 Preserve old data files long enough for them to be available to event handlers. commit 0d6a3a3cfc4f38f450db9e18da47a5e2ad826af8 Author: Chad Parry Date: Sat Apr 23 21:19:47 2011 -0600 Create a tempnam substitute that safely creates files with a given extension. commit e149cf7238a1f8eaddfc68580f2d636dd8255795 Author: Chad Parry Date: Sat Apr 23 16:39:25 2011 -0600 Support data files that change their extension and MIME type. commit 6702104f571413e4d57db3515b2070c48d3e9b55 Author: Chad Parry Date: Sat Apr 23 16:35:00 2011 -0600 Resolve an infinite recursion that happens when the path caches are updated during saving. commit 944cb72eea946f4c45a04b7e4c7c33929fa8b9f3 Merge: 567522b 5af74d4 Author: Chad Parry Date: Fri Apr 22 14:10:42 2011 -0600 Merge remote branch 'origin/master' into rawphoto commit 567522bfa08c370bb5baf8454afc5b04bc9e49b4 Author: Chad Parry Date: Thu Apr 21 20:12:32 2011 -0600 Add an event for when a new graphics toolkit is chosen. commit 31ba081b793141ca36866a6dd349cd2eac5af68e Author: Chad Parry Date: Thu Apr 21 02:06:53 2011 -0600 Add an event that will collect all valid filename extensions. --- modules/gallery/controllers/admin_graphics.php | 2 + modules/gallery/controllers/quick.php | 4 +- modules/gallery/controllers/uploader.php | 2 +- modules/gallery/helpers/extensions.php | 39 ++++++++++++++++ modules/gallery/helpers/system.php | 25 ++++++++++ modules/gallery/libraries/Form_Uploadify.php | 1 + modules/gallery/models/item.php | 63 ++++++++++++++------------ modules/gallery/tests/Mock_Built_In.php | 39 ++++++++++++++++ modules/gallery/tests/System_Helper_Test.php | 49 ++++++++++++++++++++ modules/gallery/views/form_uploadify.html.php | 2 +- 10 files changed, 194 insertions(+), 32 deletions(-) create mode 100644 modules/gallery/helpers/extensions.php create mode 100644 modules/gallery/tests/Mock_Built_In.php create mode 100644 modules/gallery/tests/System_Helper_Test.php (limited to 'modules/gallery/libraries') diff --git a/modules/gallery/controllers/admin_graphics.php b/modules/gallery/controllers/admin_graphics.php index a2d19d4a..a8a7cdc0 100644 --- a/modules/gallery/controllers/admin_graphics.php +++ b/modules/gallery/controllers/admin_graphics.php @@ -40,6 +40,8 @@ class Admin_Graphics_Controller extends Admin_Controller { $msg = t("Changed graphics toolkit to: %toolkit", array("toolkit" => $tk->$toolkit_id->name)); message::success($msg); log::success("graphics", $msg); + + module::event("graphics_toolkit_change", $toolkit_id); } url::redirect("admin/graphics"); diff --git a/modules/gallery/controllers/quick.php b/modules/gallery/controllers/quick.php index da4768fd..ce52cb8d 100644 --- a/modules/gallery/controllers/quick.php +++ b/modules/gallery/controllers/quick.php @@ -36,8 +36,8 @@ class Quick_Controller extends Controller { } if ($degrees) { - $tmpfile = tempnam(TMPPATH, "rotate") . "." . - pathinfo($item->file_path(), PATHINFO_EXTENSION); + $tmpfile = system::tempnam(TMPPATH, "rotate", + "." . pathinfo($item->file_path(), PATHINFO_EXTENSION)); gallery_graphics::rotate($item->file_path(), $tmpfile, array("degrees" => $degrees), $item); $item->set_data_file($tmpfile); $item->save(); diff --git a/modules/gallery/controllers/uploader.php b/modules/gallery/controllers/uploader.php index 6b1455e4..5f3e9ca4 100644 --- a/modules/gallery/controllers/uploader.php +++ b/modules/gallery/controllers/uploader.php @@ -51,7 +51,7 @@ class Uploader_Controller extends Controller { $file_validation = new Validation($_FILES); $file_validation->add_rules( "Filedata", "upload::valid", "upload::required", - "upload::type[gif,jpg,jpeg,png,flv,mp4,m4v]"); + "upload::type[" . implode(",", extensions::get_upload_extensions()) . "]"); if ($form->validate() && $file_validation->validate()) { $temp_filename = upload::save("Filedata"); diff --git a/modules/gallery/helpers/extensions.php b/modules/gallery/helpers/extensions.php new file mode 100644 index 00000000..bccbfc41 --- /dev/null +++ b/modules/gallery/helpers/extensions.php @@ -0,0 +1,39 @@ +extensions = array("gif", "jpg", "jpeg", "png"); + if (movie::find_ffmpeg()) { + array_push($extensions_wrapper->extensions, "flv", "mp4", "m4v"); + } + module::event("upload_extensions", $extensions_wrapper); + return $extensions_wrapper->extensions; + } + + static function get_upload_filters() { + $filters = array(); + foreach (self::get_upload_extensions() as $extension) { + array_push($filters, "*." . $extension, "*." . strtoupper($extension)); + } + return $filters; + } +} diff --git a/modules/gallery/helpers/system.php b/modules/gallery/helpers/system.php index c39c7227..31ecafa7 100644 --- a/modules/gallery/helpers/system.php +++ b/modules/gallery/helpers/system.php @@ -40,4 +40,29 @@ class system_Core { } return null; } + + /** + * Create a file with a unique file name. + * This helper is similar to the built-in tempnam, except that it supports an optional postfix. + */ + static function tempnam($dir = TMPPATH, $prefix = "", $postfix = "") { + return self::_tempnam($dir, $prefix, $postfix, "tempnam"); + } + + // This helper provides a dependency-injected implementation of tempnam. + static function _tempnam($dir, $prefix, $postfix, $builtin) { + $success = false; + do { + $basename = call_user_func($builtin, $dir, $prefix); + if (!$basename) { + return false; + } + $filename = $basename . $postfix; + $success = !file_exists($filename) && @rename($basename, $filename); + if (!$success) { + @unlink($basename); + } + } while (!$success); + return $filename; + } } \ No newline at end of file diff --git a/modules/gallery/libraries/Form_Uploadify.php b/modules/gallery/libraries/Form_Uploadify.php index 3e35e380..884653e2 100644 --- a/modules/gallery/libraries/Form_Uploadify.php +++ b/modules/gallery/libraries/Form_Uploadify.php @@ -47,6 +47,7 @@ class Form_Uploadify_Core extends Form_Input { $v->script_data = $this->data["script_data"]; $v->simultaneous_upload_limit = module::get_var("gallery", "simultaneous_upload_limit"); $v->movies_allowed = (bool) movie::find_ffmpeg(); + $v->extensions = extensions::get_upload_filters(); $v->suhosin_session_encrypt = (bool) ini_get("suhosin.session.encrypt"); list ($toolkit_max_filesize_bytes, $toolkit_max_filesize) = graphics::max_filesize(); diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index 2a5e6894..5ccbe75c 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -127,6 +127,15 @@ class Item_Model_Core extends ORM_MPTT { return $this; } + /** + * Get the path to the data file associated with this item. + * This data file field is only set until you call save(). + * After that, you can get the path using get_file_path(). + */ + public function get_data_file() { + return $this->data_file; + } + /** * Return the server-relative url to this item, eg: * /gallery3/index.php/BobsWedding?page=2 @@ -408,6 +417,16 @@ class Item_Model_Core extends ORM_MPTT { // If any significant fields have changed, load up a copy of the original item and // keep it around. $original = ORM::factory("item", $this->id); + + // Preserve the extension of the data file. + if (isset($this->data_file)) { + $extension = pathinfo($this->data_file, PATHINFO_EXTENSION); + $new_name = pathinfo($this->name, PATHINFO_FILENAME) . ".$extension"; + if (!empty($extension) && strcmp($this->name, $new_name)) { + $this->name = $new_name; + } + } + if (array_intersect($this->changed, array("parent_id", "name", "slug"))) { $original->_build_relative_caches(); $this->relative_path_cache = null; @@ -430,7 +449,10 @@ class Item_Model_Core extends ORM_MPTT { if ($original->parent_id != $this->parent_id || $original->name != $this->name) { // Move all of the items associated data files - @rename($original->file_path(), $this->file_path()); + $this->_build_relative_caches(); + if (!isset($this->data_file)) { + @rename($original->file_path(), $this->file_path()); + } if ($this->is_album()) { @rename(dirname($original->resize_path()), dirname($this->resize_path())); @rename(dirname($original->thumb_path()), dirname($this->thumb_path())); @@ -460,8 +482,6 @@ class Item_Model_Core extends ORM_MPTT { } // Replace the data file, if requested. - // @todo: we don't handle the case where you swap in a file of a different mime type - // should we prevent that in validation? or in set_data_file() if ($this->data_file && ($this->is_photo() || $this->is_movie())) { copy($this->data_file, $this->file_path()); @@ -481,6 +501,9 @@ class Item_Model_Core extends ORM_MPTT { // Null out the data file variable here, otherwise this event will trigger another // save() which will think that we're doing another file move. $this->data_file = null; + if ($original->file_path() != $this->file_path()) { + @unlink($original->file_path()); + } module::event("item_updated_data_file", $this); } } @@ -517,6 +540,8 @@ class Item_Model_Core extends ORM_MPTT { $this->name = "$base_name-$rand"; } $this->slug = "$base_slug-$rand"; + $this->relative_path_cache = null; + $this->relative_url_cache = null; } } @@ -768,16 +793,7 @@ class Item_Model_Core extends ORM_MPTT { } if ($this->is_movie() || $this->is_photo()) { - if ($this->loaded()) { - // Existing items can't change their extension - $original = ORM::factory("item", $this->id); - $new_ext = pathinfo($this->name, PATHINFO_EXTENSION); - $old_ext = pathinfo($original->name, PATHINFO_EXTENSION); - if (strcasecmp($new_ext, $old_ext)) { - $v->add_error("name", "illegal_data_file_extension"); - return; - } - } else { + if (!$this->loaded()) { // New items must have an extension $ext = pathinfo($this->name, PATHINFO_EXTENSION); if (!$ext) { @@ -785,9 +801,11 @@ class Item_Model_Core extends ORM_MPTT { return; } - if ($this->is_movie() && !preg_match("/^(flv|mp4|m4v)$/i", $ext)) { - $v->add_error("name", "illegal_data_file_extension"); - } else if ($this->is_photo() && !preg_match("/^(gif|jpg|jpeg|png)$/i", $ext)) { + if (($this->is_movie() || $this->is_photo()) && + !preg_match("/^(" . + implode("|", array_map("preg_quote", + extensions::get_upload_extensions())) . + ")\$/i", $ext)) { $v->add_error("name", "illegal_data_file_extension"); } } @@ -813,17 +831,6 @@ class Item_Model_Core extends ORM_MPTT { } else if (filesize($this->data_file) == 0) { $v->add_error("name", "empty_data_file"); } - - if ($this->loaded()) { - if ($this->is_photo()) { - list ($a, $b, $mime_type) = photo::get_file_metadata($this->data_file); - } else if ($this->is_movie()) { - list ($a, $b, $mime_type) = movie::get_file_metadata($this->data_file); - } - if ($mime_type != $this->mime_type) { - $v->add_error("name", "cant_change_mime_type"); - } - } } /** @@ -879,7 +886,7 @@ class Item_Model_Core extends ORM_MPTT { if ($this->is_movie()) { $legal_values = array("video/flv", "video/x-flv", "video/mp4"); } if ($this->is_photo()) { - $legal_values = array("image/jpeg", "image/gif", "image/png"); + $legal_values = array("image/jpeg", "image/gif", "image/png", "image/tiff"); } break; diff --git a/modules/gallery/tests/Mock_Built_In.php b/modules/gallery/tests/Mock_Built_In.php new file mode 100644 index 00000000..b02e5ecf --- /dev/null +++ b/modules/gallery/tests/Mock_Built_In.php @@ -0,0 +1,39 @@ +nonces = func_get_args(); + } + + function _tempnam($dir, $prefix) { + if (empty($this->nonces)) + return false; + $filename = "$dir/$prefix" . array_shift($this->nonces); + if (!touch($filename)) + return false; + return $filename; + } +} diff --git a/modules/gallery/tests/System_Helper_Test.php b/modules/gallery/tests/System_Helper_Test.php new file mode 100644 index 00000000..734f98ac --- /dev/null +++ b/modules/gallery/tests/System_Helper_Test.php @@ -0,0 +1,49 @@ +assert_true(file_exists($filename), "File not created"); + unlink($filename); + } + + public function tempnam_collision_test() { + require_once('Mock_Built_In.php'); + $existing = TMPPATH . "/file1.ext"; + $available = TMPPATH . "/file2.ext"; + touch($existing); + $filename = system::_tempnam(TMPPATH, "file", ".ext", + array(new Mock_Built_In("1", "2"), "_tempnam")); + unlink($existing); + $this->assert_true(file_exists($filename), "File not created"); + unlink($filename); + $this->assert_equal($available, $filename, "Incorrect filename created"); + } + + public function tempnam_abort_test() { + require_once('Mock_Built_In.php'); + $filename = system::_tempnam(TMPPATH, "file", ".ext", + array(new Mock_Built_In(), "_tempnam")); + if ($filename) { + @unlink($filename); + } + $this->assert_false($filename, "Operation not aborted"); + } +} diff --git a/modules/gallery/views/form_uploadify.html.php b/modules/gallery/views/form_uploadify.html.php index 83dfcc68..ba4a3621 100644 --- a/modules/gallery/views/form_uploadify.html.php +++ b/modules/gallery/views/form_uploadify.html.php @@ -28,7 +28,7 @@ uploader: "", script: "id}") ?>", scriptData: , - fileExt: "*.gif;*.jpg;*.jpeg;*.png;*.GIF;*.JPG;*.JPEG;*.PNG;*.flv;*.mp4;*.m4v;*.FLV;*.MP4;*.M4V", + fileExt: "", fileDesc: for_js() ?>, cancelImg: "", simUploadLimit: , -- cgit v1.2.3 From d2d37fe3f2e550dff0c62afa9faa3100f305df0e Mon Sep 17 00:00:00 2001 From: Chad Parry Date: Sat, 30 Apr 2011 21:33:20 -0600 Subject: Results from a round of feedback with Bharat. Squashed commit of the following: commit 13dbd3515bfb5324cfbcb3bbeafc179771b54f75 Merge: f0f094c 97400b7 Author: Chad Parry Date: Sat Apr 30 20:33:02 2011 -0600 Merge branch 'master' of https://github.com/gallery/gallery3 into rawphoto commit f0f094c3f79b09536f58083681c28f73271c506d Author: Chad Parry Date: Sat Apr 30 20:22:49 2011 -0600 Explain the conditional rename in item::save() with a comment. commit 1b3a6b85c156e4777d2aa8205b130984f55dc66d Author: Chad Parry Date: Sat Apr 30 18:29:34 2011 -0600 Improve the comment explaining why the data_file extension is important. commit c3e8c1e3b5e3cb1046acd4c923bb0ae9dbcd603a Author: Chad Parry Date: Sat Apr 30 18:12:56 2011 -0600 The data_file field is public, so we don't need to supply an accessor method. commit 2375a02e2cdbd1ccaf7dc4d3db9d85119972e3a9 Author: Chad Parry Date: Sat Apr 30 16:40:55 2011 -0600 Change the signature of system::tempnam to something more appropriate for Gallery. commit a8ca9dcf9edd54633c0c78b3af76aa974d38fc64 Author: Chad Parry Date: Sat Apr 30 16:10:06 2011 -0600 Change the name of the extensions helper to legal_file. commit 7e61a01a96f5eab7212dba754ac64fdfb4d9e8ab Author: Chad Parry Date: Sat Apr 30 16:08:49 2011 -0600 Change the name of the extensions helper to legal_file. commit 4c2b2ebd3f2052898fbfb175650ed4cf49c8006e Author: Chad Parry Date: Wed Apr 27 20:52:35 2011 -0600 Remove a newline at the end of the file that I accidentally introduced. commit 6d564f185e5279d6cca9a7385066514ff18a2455 Merge: 7ff485f 4060640 Author: Chad Parry Date: Wed Apr 27 20:35:58 2011 -0600 Merge branch 'master' of https://github.com/gallery/gallery3 into rawphoto commit 7ff485fa48c392bbbb0370f67cb1bd6fcc00c2a4 Author: Chad Parry Date: Wed Apr 27 20:29:06 2011 -0600 Move the extensions helpers out of the Kohana system directory and into their own Gallery Extensions class. commit 26585fed03236f0f70a75959e1d3002025f4e15e Merge: 809567f c8f90e8 Author: Chad Parry Date: Sun Apr 24 08:28:39 2011 -0600 Merge branch 'master' of https://github.com/gallery/gallery3 into rawphoto commit 809567f12850f59bdeb47a2963f6968b99b5a201 Author: Chad Parry Date: Sun Apr 24 08:10:04 2011 -0600 Expose the data file field. commit fcb06bf175bb9eeff36d9c294e97ace9374ef0f3 Author: Chad Parry Date: Sun Apr 24 00:45:12 2011 -0600 Don't assign to the item->name field if the name is unchanged, because the save method will crash. commit c6ef706d70c7e48bea1145eec1b13fb5683e023f Author: Chad Parry Date: Sat Apr 23 22:55:59 2011 -0600 Preserve old data files long enough for them to be available to event handlers. commit 0d6a3a3cfc4f38f450db9e18da47a5e2ad826af8 Author: Chad Parry Date: Sat Apr 23 21:19:47 2011 -0600 Create a tempnam substitute that safely creates files with a given extension. commit e149cf7238a1f8eaddfc68580f2d636dd8255795 Author: Chad Parry Date: Sat Apr 23 16:39:25 2011 -0600 Support data files that change their extension and MIME type. commit 6702104f571413e4d57db3515b2070c48d3e9b55 Author: Chad Parry Date: Sat Apr 23 16:35:00 2011 -0600 Resolve an infinite recursion that happens when the path caches are updated during saving. commit 944cb72eea946f4c45a04b7e4c7c33929fa8b9f3 Merge: 567522b 5af74d4 Author: Chad Parry Date: Fri Apr 22 14:10:42 2011 -0600 Merge remote branch 'origin/master' into rawphoto commit 567522bfa08c370bb5baf8454afc5b04bc9e49b4 Author: Chad Parry Date: Thu Apr 21 20:12:32 2011 -0600 Add an event for when a new graphics toolkit is chosen. commit 31ba081b793141ca36866a6dd349cd2eac5af68e Author: Chad Parry Date: Thu Apr 21 02:06:53 2011 -0600 Add an event that will collect all valid filename extensions. --- modules/gallery/controllers/quick.php | 4 +-- modules/gallery/controllers/uploader.php | 2 +- modules/gallery/helpers/legal_file.php | 39 ++++++++++++++++++++++++++++ modules/gallery/helpers/system.php | 13 ++++++---- modules/gallery/libraries/Form_Uploadify.php | 2 +- modules/gallery/models/item.php | 18 ++++++++++--- modules/gallery/tests/System_Helper_Test.php | 5 ++-- 7 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 modules/gallery/helpers/legal_file.php (limited to 'modules/gallery/libraries') diff --git a/modules/gallery/controllers/quick.php b/modules/gallery/controllers/quick.php index ce52cb8d..b6576ec0 100644 --- a/modules/gallery/controllers/quick.php +++ b/modules/gallery/controllers/quick.php @@ -36,8 +36,8 @@ class Quick_Controller extends Controller { } if ($degrees) { - $tmpfile = system::tempnam(TMPPATH, "rotate", - "." . pathinfo($item->file_path(), PATHINFO_EXTENSION)); + $tmpfile = system::temp_filename("rotate", + pathinfo($item->file_path(), PATHINFO_EXTENSION)); gallery_graphics::rotate($item->file_path(), $tmpfile, array("degrees" => $degrees), $item); $item->set_data_file($tmpfile); $item->save(); diff --git a/modules/gallery/controllers/uploader.php b/modules/gallery/controllers/uploader.php index 5f3e9ca4..9c2bf7d7 100644 --- a/modules/gallery/controllers/uploader.php +++ b/modules/gallery/controllers/uploader.php @@ -51,7 +51,7 @@ class Uploader_Controller extends Controller { $file_validation = new Validation($_FILES); $file_validation->add_rules( "Filedata", "upload::valid", "upload::required", - "upload::type[" . implode(",", extensions::get_upload_extensions()) . "]"); + "upload::type[" . implode(",", legal_file::get_extensions()) . "]"); if ($form->validate() && $file_validation->validate()) { $temp_filename = upload::save("Filedata"); diff --git a/modules/gallery/helpers/legal_file.php b/modules/gallery/helpers/legal_file.php new file mode 100644 index 00000000..2cb0fb25 --- /dev/null +++ b/modules/gallery/helpers/legal_file.php @@ -0,0 +1,39 @@ +extensions = array("gif", "jpg", "jpeg", "png"); + if (movie::find_ffmpeg()) { + array_push($extensions_wrapper->extensions, "flv", "mp4", "m4v"); + } + module::event("legal_file_extensions", $extensions_wrapper); + return $extensions_wrapper->extensions; + } + + static function get_filters() { + $filters = array(); + foreach (self::get_extensions() as $extension) { + array_push($filters, "*." . $extension, "*." . strtoupper($extension)); + } + return $filters; + } +} diff --git a/modules/gallery/helpers/system.php b/modules/gallery/helpers/system.php index 31ecafa7..9815d588 100644 --- a/modules/gallery/helpers/system.php +++ b/modules/gallery/helpers/system.php @@ -43,15 +43,18 @@ class system_Core { /** * Create a file with a unique file name. - * This helper is similar to the built-in tempnam, except that it supports an optional postfix. + * This helper is similar to the built-in tempnam. + * It allows the caller to specify a prefix and an extension. + * It always places the file in TMPPATH. */ - static function tempnam($dir = TMPPATH, $prefix = "", $postfix = "") { - return self::_tempnam($dir, $prefix, $postfix, "tempnam"); + static function temp_filename($prefix = "", $extension = "") { + return self::_tempnam(TMPPATH, $prefix, ".$extension", "tempnam"); } - // This helper provides a dependency-injected implementation of tempnam. + /** + * This helper provides a dependency-injected implementation of tempnam. + */ static function _tempnam($dir, $prefix, $postfix, $builtin) { - $success = false; do { $basename = call_user_func($builtin, $dir, $prefix); if (!$basename) { diff --git a/modules/gallery/libraries/Form_Uploadify.php b/modules/gallery/libraries/Form_Uploadify.php index 884653e2..450320b3 100644 --- a/modules/gallery/libraries/Form_Uploadify.php +++ b/modules/gallery/libraries/Form_Uploadify.php @@ -47,7 +47,7 @@ class Form_Uploadify_Core extends Form_Input { $v->script_data = $this->data["script_data"]; $v->simultaneous_upload_limit = module::get_var("gallery", "simultaneous_upload_limit"); $v->movies_allowed = (bool) movie::find_ffmpeg(); - $v->extensions = extensions::get_upload_filters(); + $v->extensions = legal_file::get_filters(); $v->suhosin_session_encrypt = (bool) ini_get("suhosin.session.encrypt"); list ($toolkit_max_filesize_bytes, $toolkit_max_filesize) = graphics::max_filesize(); diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index 5ccbe75c..6eb93cfa 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -418,7 +418,11 @@ class Item_Model_Core extends ORM_MPTT { // keep it around. $original = ORM::factory("item", $this->id); - // Preserve the extension of the data file. + // Preserve the extension of the data file. Many helpers, (e.g. ImageMagick), assume + // the MIME type from the extension. So when we adopt the new data file, it's important + // to adopt the new extension. That ensures that the item's extension is always + // appropriate for its data. We don't try to preserve the name of the data file, though, + // because the name is typically a temporary randomly-generated name. if (isset($this->data_file)) { $extension = pathinfo($this->data_file, PATHINFO_EXTENSION); $new_name = pathinfo($this->name, PATHINFO_FILENAME) . ".$extension"; @@ -448,11 +452,19 @@ class Item_Model_Core extends ORM_MPTT { } if ($original->parent_id != $this->parent_id || $original->name != $this->name) { - // Move all of the items associated data files $this->_build_relative_caches(); + // If there is a data file, then we want to preserve both the old data and the new data. + // (Third-party event handlers would like access to both). The old data file will be + // accessible via the $original item, and the new one via $this item. But in that case, + // we don't want to rename the original as below, because the old data would end up being + // clobbered by the new data file. Also, the rename isn't necessary, because the new item + // data is coming from the data file anyway. So we only perform the rename if there isn't + // a data file. Another way to solve this would be to copy the original file rather than + // conditionally rename it, but a copy would cost far more than the rename. if (!isset($this->data_file)) { @rename($original->file_path(), $this->file_path()); } + // Move all of the items associated data files if ($this->is_album()) { @rename(dirname($original->resize_path()), dirname($this->resize_path())); @rename(dirname($original->thumb_path()), dirname($this->thumb_path())); @@ -804,7 +816,7 @@ class Item_Model_Core extends ORM_MPTT { if (($this->is_movie() || $this->is_photo()) && !preg_match("/^(" . implode("|", array_map("preg_quote", - extensions::get_upload_extensions())) . + legal_file::get_extensions())) . ")\$/i", $ext)) { $v->add_error("name", "illegal_data_file_extension"); } diff --git a/modules/gallery/tests/System_Helper_Test.php b/modules/gallery/tests/System_Helper_Test.php index 734f98ac..dfe5d9ab 100644 --- a/modules/gallery/tests/System_Helper_Test.php +++ b/modules/gallery/tests/System_Helper_Test.php @@ -18,10 +18,11 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class System_Helper_Test extends Gallery_Unit_Test_Case { - public function tempnam_random_test() { - $filename = system::tempnam(TMPPATH, "file", ".ext"); + public function temp_filename_random_test() { + $filename = system::temp_filename("file", "ext"); $this->assert_true(file_exists($filename), "File not created"); unlink($filename); + $this->assert_pattern($filename, "|/file.*\\.ext$|"); } public function tempnam_collision_test() { -- cgit v1.2.3 From 7f8056b593d04a9abd070399c0d4d176c650a309 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Thu, 4 Aug 2011 19:44:19 -0700 Subject: fix for ticket #1759. correct parameter names to match usage. --- modules/gallery/libraries/IdentityProvider.php | 12 ++++++------ modules/user/libraries/drivers/IdentityProvider/Gallery.php | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'modules/gallery/libraries') diff --git a/modules/gallery/libraries/IdentityProvider.php b/modules/gallery/libraries/IdentityProvider.php index 153446e6..3f995923 100644 --- a/modules/gallery/libraries/IdentityProvider.php +++ b/modules/gallery/libraries/IdentityProvider.php @@ -110,11 +110,11 @@ class IdentityProvider_Core { Kohana_Log::add("error", "Error restoring original identity provider\n" . $e2->getMessage() . "\n" . $e2->getTraceAsString()); } - + message::error( t("Error attempting to enable \"%new_provider\" identity provider, reverted to \"%old_provider\" identity provider", array("new_provider" => $new_provider, "old_provider" => $current_provider))); - + $restore_already_running = false; } throw $e; @@ -260,14 +260,14 @@ class IdentityProvider_Core { /** * @see IdentityProvider_Driver::add_user_to_group. */ - public function add_user_to_group($user, $group_id) { - return $this->driver->add_user_to_group($user, $group_id); + public function add_user_to_group($user, $group) { + return $this->driver->add_user_to_group($user, $group); } /** * @see IdentityProvider_Driver::remove_user_to_group. */ - public function remove_user_from_group($user, $group_id) { - return $this->driver->remove_user_from_group($user, $group_id); + public function remove_user_from_group($user, $group) { + return $this->driver->remove_user_from_group($user, $group); } } // End Identity diff --git a/modules/user/libraries/drivers/IdentityProvider/Gallery.php b/modules/user/libraries/drivers/IdentityProvider/Gallery.php index 8cba3c82..79d31f7c 100644 --- a/modules/user/libraries/drivers/IdentityProvider/Gallery.php +++ b/modules/user/libraries/drivers/IdentityProvider/Gallery.php @@ -156,7 +156,7 @@ class IdentityProvider_Gallery_Driver implements IdentityProvider_Driver { /** * @see IdentityProvider_Driver::remove_user_to_group. */ - public function remove_user_from_group($user, $group_id) { + public function remove_user_from_group($user, $group) { $group->remove($user); $group->save(); } -- cgit v1.2.3 From a246b6fe450e21f735824d5a089a8e652b12885f Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sun, 7 Aug 2011 13:27:27 -0700 Subject: Initial commit of a patch for Ticket #1764. as discussed here: https://github.com/gallery/gallery3/pull/58/files#r72949. Create a Breadcrumb library which has two static methods for_item (which takes a an item and builds the entire breadcrumb for the item) or build (which takes a variable number of Breadcrumb elements and creates a breadcrumb based on the specified elements). Used tag->url() to build the tag album url. Escaped the query string for the search. Tightened up the breadcrumb code in page.html.php. When adding the show query parameter, we can't blindly concatenate using the ? separator. We have to check that we use a & if a query parameter already exists. --- modules/gallery/controllers/albums.php | 1 + modules/gallery/controllers/movies.php | 1 + modules/gallery/controllers/photos.php | 1 + modules/gallery/libraries/Breadcrumb.php | 74 +++++++++++++++++++++++++++++++ modules/gallery/tests/Breadcrumb_Test.php | 51 +++++++++++++++++++++ modules/search/controllers/search.php | 5 +++ modules/tag/controllers/tag.php | 3 ++ themes/wind/views/page.html.php | 27 +++-------- 8 files changed, 143 insertions(+), 20 deletions(-) create mode 100644 modules/gallery/libraries/Breadcrumb.php create mode 100644 modules/gallery/tests/Breadcrumb_Test.php (limited to 'modules/gallery/libraries') diff --git a/modules/gallery/controllers/albums.php b/modules/gallery/controllers/albums.php index ccf6c1cb..90071fb5 100644 --- a/modules/gallery/controllers/albums.php +++ b/modules/gallery/controllers/albums.php @@ -69,6 +69,7 @@ class Albums_Controller extends Items_Controller { "item" => $album, "children" => $album->viewable()->children($page_size, $offset), "parents" => $album->parents()->as_array(), // view calls empty() on this + "breadcrumbs" => Breadcrumb::build_from_item($album), "children_count" => $children_count)); $template->content = new View("album.html"); diff --git a/modules/gallery/controllers/movies.php b/modules/gallery/controllers/movies.php index 8e81c594..77d92e13 100644 --- a/modules/gallery/controllers/movies.php +++ b/modules/gallery/controllers/movies.php @@ -43,6 +43,7 @@ class Movies_Controller extends Items_Controller { "children" => array(), "children_count" => 0, "parents" => $movie->parents()->as_array(), + "breadcrumbs" => Breadcrumb::build_from_item($movie), "next_item" => $next_item, "previous_item" => $previous_item, "sibling_count" => $movie->parent()->viewable()->children_count($where), diff --git a/modules/gallery/controllers/photos.php b/modules/gallery/controllers/photos.php index 054300a1..5c8c7b34 100644 --- a/modules/gallery/controllers/photos.php +++ b/modules/gallery/controllers/photos.php @@ -43,6 +43,7 @@ class Photos_Controller extends Items_Controller { "children" => array(), "children_count" => 0, "parents" => $photo->parents()->as_array(), + "breadcrumbs" => Breadcrumb::build_from_item($photo), "next_item" => $next_item, "previous_item" => $previous_item, "sibling_count" => $photo->parent()->viewable()->children_count($where), diff --git a/modules/gallery/libraries/Breadcrumb.php b/modules/gallery/libraries/Breadcrumb.php new file mode 100644 index 00000000..3a2499ba --- /dev/null +++ b/modules/gallery/libraries/Breadcrumb.php @@ -0,0 +1,74 @@ +parents() as $element) { + $breadcrumbs[] = new Breadcrumb($element->title, $element->url(), $element->id); + } + + if (!empty($breadcrumbs)) { + $breadcrumbs[] = new Breadcrumb($item->title, $item->url(), $item->id); + } + + return self::generate_show_query_strings($breadcrumbs); + } + + /** + * This static function takes a list (variable arguments) of Breadcrumbs and builds a dynamic + * breadcrumb list. Used to create a breadcrumb for dynamic albums. Will really be useful + * for the display context change. + */ + static function build_from_list() { + return self::generate_show_query_strings(func_get_args()); + } + + private static function generate_show_query_strings($breadcrumbs) { + if (!empty($breadcrumbs)) { + + end($breadcrumbs)->last = true;; + while ($breadcrumb = current($breadcrumbs)) { + if (isset($last_id) && $last_id > 0) { + $query = parse_url($breadcrumb->url, PHP_URL_QUERY); + $breadcrumb->url = $breadcrumb->url . ($query ? "&" : "?") . "show={$last_id}"; + } + $last_id = $breadcrumb->id; + $breadcrumb = prev($breadcrumbs); + } + $breadcrumbs[0]->first = true; + } + + return $breadcrumbs; + } + + public function __construct($title, $url, $id=0) { + $this->title = $title; + $this->url = $url; + $this->id = $id; + $this->first = false; + $this->last = false; + } +} diff --git a/modules/gallery/tests/Breadcrumb_Test.php b/modules/gallery/tests/Breadcrumb_Test.php new file mode 100644 index 00000000..9f9aeddc --- /dev/null +++ b/modules/gallery/tests/Breadcrumb_Test.php @@ -0,0 +1,51 @@ +album = test::random_album(); + $this->item = test::random_photo($this->album); + $this->album->reload(); + } + + public function teardown() { + $this->album = null; + $this->item = null; + } + + public function build_breadcrumbs_for_item_test() { + $breadcrumbs = Breadcrumb::build_from_item($this->item); + $this->assert_equal("Gallery", $breadcrumbs[0]->title); + $this->assert_equal($this->album->title, $breadcrumbs[1]->title); + $this->assert_equal($this->item->title, $breadcrumbs[2]->title); + } + + public function build_breadcrumbs_from_items_test() { + $breadcrumbs = Breadcrumb::build_from_list( + new Breadcrumb(item::root()->title, "/", item::root()->id), + new Breadcrumb($this->album->title, $this->album->relative_path(), $this->album->id), + new Breadcrumb($this->item->title, $this->item->relative_path(), $this->item->id)); + $this->assert_equal("Gallery", $breadcrumbs[0]->title); + $this->assert_equal($this->album->title, $breadcrumbs[1]->title); + $this->assert_equal($this->item->title, $breadcrumbs[2]->title); + } +} \ No newline at end of file diff --git a/modules/search/controllers/search.php b/modules/search/controllers/search.php index 261d67ee..d30ffa67 100644 --- a/modules/search/controllers/search.php +++ b/modules/search/controllers/search.php @@ -36,9 +36,14 @@ class Search_Controller extends Controller { $max_pages = max(ceil($count / $page_size), 1); $template = new Theme_View("page.html", "collection", "search"); + $root = item::root(); + $search_url = url::abs_site("search?q=" . urlencode($q)); $template->set_global(array("page" => $page, "max_pages" => $max_pages, "page_size" => $page_size, + "breadcrumbs" => Breadcrumb::build_from_list( + new Breadcrumb(item::root()->title, "/", item::root()->id), + new Breadcrumb($q, $search_url)), "children_count" => $count)); $template->content = new View("search.html"); diff --git a/modules/tag/controllers/tag.php b/modules/tag/controllers/tag.php index 8f885dea..44a171fd 100644 --- a/modules/tag/controllers/tag.php +++ b/modules/tag/controllers/tag.php @@ -40,6 +40,9 @@ class Tag_Controller extends Controller { "page_size" => $page_size, "tag" => $tag, "children" => $tag->items($page_size, $offset), + "breadcrumbs" => Breadcrumb::build_from_list( + new Breadcrumb(item::root()->title, "/", item::root()->id), + new Breadcrumb($tag->name, $tag->url())), "children_count" => $children_count)); $template->content = new View("dynamic.html"); $template->content->title = t("Tag: %tag_name", array("tag_name" => $tag->name)); diff --git a/themes/wind/views/page.html.php b/themes/wind/views/page.html.php index 045e3c45..534b7de4 100644 --- a/themes/wind/views/page.html.php +++ b/themes/wind/views/page.html.php @@ -107,28 +107,15 @@ header_bottom() ?> - item() && !empty($parents)): ?> + -- cgit v1.2.3 From 67719c6f610a71c1bc12d5126e0adac84d7b85c6 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 8 Aug 2011 20:28:31 -0700 Subject: Simplification of 59b04b897b8a664cd7334b017fac8158a6281434 for #1764: - Breadcrumb::build_from_item becomes Breadcrumb::array_from_item_parents - Eliminate Breadcrumb::$id -- it's no longer necessary - Fold Breadcrumb::generate_show_query_strings into Breadcrumb::array_from_item_parents - Create Breadcrumb::set_first() and Breadcrumb::set_last() - Breadcrumb::build_from_list goes away, we just use arrays for this - Change Search_Controller and Tag_Controller to just create an array of Breadcrumb instances with the first/last marked appropriately - Breadcrumb_Test loses a bunch of complexity. --- modules/gallery/controllers/albums.php | 2 +- modules/gallery/controllers/movies.php | 2 +- modules/gallery/controllers/photos.php | 2 +- modules/gallery/libraries/Breadcrumb.php | 70 +++++++++++++++---------------- modules/gallery/tests/Breadcrumb_Test.php | 33 ++++----------- modules/search/controllers/search.php | 17 ++++---- modules/tag/controllers/tag.php | 20 +++++---- 7 files changed, 65 insertions(+), 81 deletions(-) (limited to 'modules/gallery/libraries') diff --git a/modules/gallery/controllers/albums.php b/modules/gallery/controllers/albums.php index 90071fb5..1c48c734 100644 --- a/modules/gallery/controllers/albums.php +++ b/modules/gallery/controllers/albums.php @@ -69,7 +69,7 @@ class Albums_Controller extends Items_Controller { "item" => $album, "children" => $album->viewable()->children($page_size, $offset), "parents" => $album->parents()->as_array(), // view calls empty() on this - "breadcrumbs" => Breadcrumb::build_from_item($album), + "breadcrumbs" => Breadcrumb::array_from_item_parents($album), "children_count" => $children_count)); $template->content = new View("album.html"); diff --git a/modules/gallery/controllers/movies.php b/modules/gallery/controllers/movies.php index 77d92e13..0f12c3fb 100644 --- a/modules/gallery/controllers/movies.php +++ b/modules/gallery/controllers/movies.php @@ -43,7 +43,7 @@ class Movies_Controller extends Items_Controller { "children" => array(), "children_count" => 0, "parents" => $movie->parents()->as_array(), - "breadcrumbs" => Breadcrumb::build_from_item($movie), + "breadcrumbs" => Breadcrumb::array_from_item_parents($movie), "next_item" => $next_item, "previous_item" => $previous_item, "sibling_count" => $movie->parent()->viewable()->children_count($where), diff --git a/modules/gallery/controllers/photos.php b/modules/gallery/controllers/photos.php index 5c8c7b34..af8aed16 100644 --- a/modules/gallery/controllers/photos.php +++ b/modules/gallery/controllers/photos.php @@ -43,7 +43,7 @@ class Photos_Controller extends Items_Controller { "children" => array(), "children_count" => 0, "parents" => $photo->parents()->as_array(), - "breadcrumbs" => Breadcrumb::build_from_item($photo), + "breadcrumbs" => Breadcrumb::array_from_item_parents($photo), "next_item" => $next_item, "previous_item" => $previous_item, "sibling_count" => $photo->parent()->viewable()->children_count($where), diff --git a/modules/gallery/libraries/Breadcrumb.php b/modules/gallery/libraries/Breadcrumb.php index 3a2499ba..e151890d 100644 --- a/modules/gallery/libraries/Breadcrumb.php +++ b/modules/gallery/libraries/Breadcrumb.php @@ -20,55 +20,51 @@ class Breadcrumb_Core { public $title; public $url; - public $id; public $first; public $last; - static function build_from_item($item) { - $breadcrumbs = array(); - foreach ($item->parents() as $element) { - $breadcrumbs[] = new Breadcrumb($element->title, $element->url(), $element->id); - } - - if (!empty($breadcrumbs)) { - $breadcrumbs[] = new Breadcrumb($item->title, $item->url(), $item->id); - } + static function instance($title, $url) { + return new Breadcrumb($title, $url); + } - return self::generate_show_query_strings($breadcrumbs); + public function __construct($title, $url) { + $this->title = $title; + $this->url = $url; + $this->first = false; + $this->last = false; } /** - * This static function takes a list (variable arguments) of Breadcrumbs and builds a dynamic - * breadcrumb list. Used to create a breadcrumb for dynamic albums. Will really be useful - * for the display context change. + * Return an array of Breadcrumb instances build from the parents of a given item. + * The first and last Breadcrumb instances will be marked first/last as appropriate. + * Each breadcrumb will have a ?show= query parameter that refers to the id of the next + * item in line. + * + * @return array Breadcrumb instances */ - static function build_from_list() { - return self::generate_show_query_strings(func_get_args()); - } - - private static function generate_show_query_strings($breadcrumbs) { - if (!empty($breadcrumbs)) { + static function array_from_item_parents($item) { + if ($item->id == item::root()->id) { + return array(); + } - end($breadcrumbs)->last = true;; - while ($breadcrumb = current($breadcrumbs)) { - if (isset($last_id) && $last_id > 0) { - $query = parse_url($breadcrumb->url, PHP_URL_QUERY); - $breadcrumb->url = $breadcrumb->url . ($query ? "&" : "?") . "show={$last_id}"; - } - $last_id = $breadcrumb->id; - $breadcrumb = prev($breadcrumbs); - } - $breadcrumbs[0]->first = true; + $bc = array_merge($item->parents()->as_array(), array($item)); + for ($i = 0; $i < count($bc) - 1; $i++) { + $bc[$i] = new Breadcrumb($bc[$i]->title, $bc[$i]->url("show={$bc[$i+1]->id}")); } + $bc[$i] = new Breadcrumb($item->title, $item->url()); - return $breadcrumbs; + $bc[0]->set_first(); + end($bc)->set_last(); + return $bc; } - public function __construct($title, $url, $id=0) { - $this->title = $title; - $this->url = $url; - $this->id = $id; - $this->first = false; - $this->last = false; + public function set_first() { + $this->first = true; + return $this; + } + + public function set_last() { + $this->last = true; + return $this; } } diff --git a/modules/gallery/tests/Breadcrumb_Test.php b/modules/gallery/tests/Breadcrumb_Test.php index 9f9aeddc..ed2e5608 100644 --- a/modules/gallery/tests/Breadcrumb_Test.php +++ b/modules/gallery/tests/Breadcrumb_Test.php @@ -21,31 +21,16 @@ class Breadcrumb_Test extends Gallery_Unit_Test_Case { private $album; private $item; - public function setup() { - $this->album = test::random_album(); - $this->item = test::random_photo($this->album); - $this->album->reload(); - } - - public function teardown() { - $this->album = null; - $this->item = null; - } - public function build_breadcrumbs_for_item_test() { - $breadcrumbs = Breadcrumb::build_from_item($this->item); - $this->assert_equal("Gallery", $breadcrumbs[0]->title); - $this->assert_equal($this->album->title, $breadcrumbs[1]->title); - $this->assert_equal($this->item->title, $breadcrumbs[2]->title); - } + $album = test::random_album(); + $item = test::random_photo($album); - public function build_breadcrumbs_from_items_test() { - $breadcrumbs = Breadcrumb::build_from_list( - new Breadcrumb(item::root()->title, "/", item::root()->id), - new Breadcrumb($this->album->title, $this->album->relative_path(), $this->album->id), - new Breadcrumb($this->item->title, $this->item->relative_path(), $this->item->id)); - $this->assert_equal("Gallery", $breadcrumbs[0]->title); - $this->assert_equal($this->album->title, $breadcrumbs[1]->title); - $this->assert_equal($this->item->title, $breadcrumbs[2]->title); + $expected = array(); + $expected[] = Breadcrumb::instance( + item::root()->title, item::root()->url("show={$album->id}"))->set_first(); + $expected[] = + Breadcrumb::instance($album->title, $album->url("show={$item->id}")); + $expected[] = Breadcrumb::instance($item->title, $item->url())->set_last(); + $this->assert_equal($expected, Breadcrumb::array_from_item_parents($item)); } } \ No newline at end of file diff --git a/modules/search/controllers/search.php b/modules/search/controllers/search.php index d30ffa67..5db63ab0 100644 --- a/modules/search/controllers/search.php +++ b/modules/search/controllers/search.php @@ -37,14 +37,15 @@ class Search_Controller extends Controller { $template = new Theme_View("page.html", "collection", "search"); $root = item::root(); - $search_url = url::abs_site("search?q=" . urlencode($q)); - $template->set_global(array("page" => $page, - "max_pages" => $max_pages, - "page_size" => $page_size, - "breadcrumbs" => Breadcrumb::build_from_list( - new Breadcrumb(item::root()->title, "/", item::root()->id), - new Breadcrumb($q, $search_url)), - "children_count" => $count)); + $template->set_global( + array("page" => $page, + "max_pages" => $max_pages, + "page_size" => $page_size, + "breadcrumbs" => array( + Breadcrumb::instance($root->title, $root->url())->set_first(), + Breadcrumb::instance($q, url::abs_site("search?q=" . urlencode($q)))->set_last(), + ), + "children_count" => $count)); $template->content = new View("search.html"); $template->content->items = $result; diff --git a/modules/tag/controllers/tag.php b/modules/tag/controllers/tag.php index 44a171fd..7786daa1 100644 --- a/modules/tag/controllers/tag.php +++ b/modules/tag/controllers/tag.php @@ -34,16 +34,18 @@ class Tag_Controller extends Controller { url::redirect(url::merge(array("page" => $max_pages))); } + $root = item::root(); $template = new Theme_View("page.html", "collection", "tag"); - $template->set_global(array("page" => $page, - "max_pages" => $max_pages, - "page_size" => $page_size, - "tag" => $tag, - "children" => $tag->items($page_size, $offset), - "breadcrumbs" => Breadcrumb::build_from_list( - new Breadcrumb(item::root()->title, "/", item::root()->id), - new Breadcrumb($tag->name, $tag->url())), - "children_count" => $children_count)); + $template->set_global( + array("page" => $page, + "max_pages" => $max_pages, + "page_size" => $page_size, + "tag" => $tag, + "children" => $tag->items($page_size, $offset), + "breadcrumbs" => array( + Breadcrumb::instance($root->title, $root->url())->set_first(), + Breadcrumb::instance($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)); -- cgit v1.2.3 From 933a34986dbca248f388e8aa3c3aea4a6d71a94b Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Thu, 11 Aug 2011 22:04:20 -0700 Subject: Patch for tickets #1428 and #1760 Create the concept of a Photo_Display_Context. If the user is browsing a dynamic album (i.e. tags) and chooses to look at an image in that album. The display of the image happens correctly, but the 'next' and 'previous' buttons are no longer consistent. When one of these is clicked, Gallery will open the adjacent image in the actuall album, not the dynamic album. --- modules/gallery/controllers/albums.php | 3 + modules/gallery/controllers/movies.php | 23 ++----- modules/gallery/controllers/photos.php | 23 ++----- modules/gallery/libraries/Display_Context.php | 71 ++++++++++++++++++++++ modules/gallery/libraries/Item_Display_Context.php | 44 ++++++++++++++ modules/search/controllers/search.php | 21 ++++++- modules/search/helpers/search.php | 45 +++++++++++--- .../search/libraries/Search_Display_Context.php | 52 ++++++++++++++++ modules/tag/controllers/tag.php | 22 ++++++- modules/tag/helpers/tag.php | 19 ++++++ modules/tag/libraries/Tag_Display_Context.php | 49 +++++++++++++++ modules/tag/models/tag.php | 38 ++++++------ 12 files changed, 344 insertions(+), 66 deletions(-) create mode 100644 modules/gallery/libraries/Display_Context.php create mode 100644 modules/gallery/libraries/Item_Display_Context.php create mode 100644 modules/search/libraries/Search_Display_Context.php create mode 100644 modules/tag/libraries/Tag_Display_Context.php (limited to 'modules/gallery/libraries') diff --git a/modules/gallery/controllers/albums.php b/modules/gallery/controllers/albums.php index 1c48c734..9bf7b9bf 100644 --- a/modules/gallery/controllers/albums.php +++ b/modules/gallery/controllers/albums.php @@ -60,6 +60,9 @@ class Albums_Controller extends Items_Controller { url::redirect($album->abs_url("page=$max_pages")); } + Display_Context::factory("item") + ->save(); + $template = new Theme_View("page.html", "collection", "album"); $template->set_global( array("page" => $page, diff --git a/modules/gallery/controllers/movies.php b/modules/gallery/controllers/movies.php index 0f12c3fb..bbf89f17 100644 --- a/modules/gallery/controllers/movies.php +++ b/modules/gallery/controllers/movies.php @@ -27,27 +27,12 @@ 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)); + array_merge(array("item" => $movie, + "children" => array(), + "children_count" => 0), + Display_Context::factory()->display_context($movie))); $template->content = new View("movie.html"); diff --git a/modules/gallery/controllers/photos.php b/modules/gallery/controllers/photos.php index af8aed16..49bb5a25 100644 --- a/modules/gallery/controllers/photos.php +++ b/modules/gallery/controllers/photos.php @@ -27,27 +27,12 @@ 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)); + array_merge(array("item" => $photo, + "children" => array(), + "children_count" => 0), + Display_Context::factory()->display_context($photo))); $template->content = new View("photo.html"); diff --git a/modules/gallery/libraries/Display_Context.php b/modules/gallery/libraries/Display_Context.php new file mode 100644 index 00000000..5054cffb --- /dev/null +++ b/modules/gallery/libraries/Display_Context.php @@ -0,0 +1,71 @@ +get("display_context_id", ""); + $context = Cache::instance()->get($display_context_id, null); + $context = empty($context) ? new Item_Display_Context() : unserialize($context); + } else { + $class_prefix = ucfirst(strtolower($display_context_name)); + $class_name = "{$class_prefix}_Display_Context"; + $context = new $class_name(); + } + + return $context; + } + + protected function __construct($display_context_name) { + // $this->reset($display_context_name); + $this->_data = array(); + $this->_display_context_name = $display_context_name; + } + + final function get($key) { + return empty($this->_data[$key]) ? null : $this->_data[$key]; + } + + final function set($key, $value=null) { + if (is_array($key)) { + if ((array)$key == $key) { + $this->_data = array_merge($this->_data, $key); + } else { + $this->_data = array_merge($this->_data, array_fill_keys($key, $value)); + } + } else { + $this->_data[$key] = $value; + } + return $this; + } + + final function save() { + $context_data = serialize($this); + $display_context_id = "display_context_" . md5($context_data); + Session::instance()->set("display_context_id", $display_context_id); + Cache::instance()->set($display_context_id, $context_data); + return $this; + } + + abstract function display_context($item); +} diff --git a/modules/gallery/libraries/Item_Display_Context.php b/modules/gallery/libraries/Item_Display_Context.php new file mode 100644 index 00000000..d0562790 --- /dev/null +++ b/modules/gallery/libraries/Item_Display_Context.php @@ -0,0 +1,44 @@ + 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)); + } +} diff --git a/modules/search/controllers/search.php b/modules/search/controllers/search.php index 5db63ab0..1d5a55bf 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,11 +42,18 @@ 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); + Display_Context::factory("search") + ->set(array("title" => $title, + "query_terms" => $q_with_more_terms, + "q" => $q)) + ->save(); + $template = new Theme_View("page.html", "collection", "search"); $root = item::root(); $template->set_global( 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/search/libraries/Search_Display_Context.php b/modules/search/libraries/Search_Display_Context.php new file mode 100644 index 00000000..aed3a125 --- /dev/null +++ b/modules/search/libraries/Search_Display_Context.php @@ -0,0 +1,52 @@ +get("query_terms")); + + if ($position > 1) { + list ($count, $result_data) = + search::search($this->get("query_terms"), 3, $position - 2); + list ($previous_item, $ignore, $next_item) = $result_data; + } else { + $previous_item = null; + list ($count, $result_data) = search::search($this->get("query_terms"), 1, $position); + list ($next_item) = $result_data; + } + + $q = $this->get("q"); + $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), + Breadcrumb::instance($q, $search_url), + Breadcrumb::instance($item->title, $item->url()))); + } +} diff --git a/modules/tag/controllers/tag.php b/modules/tag/controllers/tag.php index 7786daa1..06e2b5ed 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); @@ -35,6 +50,11 @@ class Tag_Controller extends Controller { } $root = item::root(); + $title = t("Tag: %tag_name", array("tag_name" => $tag->name)); + Display_Context::factory("tag") + ->set(array("tag" => $tag)) + ->save(); + $template = new Theme_View("page.html", "collection", "tag"); $template->set_global( array("page" => $page, 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/libraries/Tag_Display_Context.php b/modules/tag/libraries/Tag_Display_Context.php new file mode 100644 index 00000000..47c79088 --- /dev/null +++ b/modules/tag/libraries/Tag_Display_Context.php @@ -0,0 +1,49 @@ +get("tag"); + + $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($tag->name, $tag->url("show={$item->id}")), + Breadcrumb::instance($item->title, $item->url())->set_last())); + } +} diff --git a/modules/tag/models/tag.php b/modules/tag/models/tag.php index bb79e707..a7150df8 100644 --- a/modules/tag/models/tag.php +++ b/modules/tag/models/tag.php @@ -33,35 +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); - if ($type) { - $model->where("items.type", "=", $type); - } - return $model->find_all($limit, $offset); + ->where("items_tags.tag_id", "=", $this->id) + ->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(); } /** -- cgit v1.2.3 From ce43f29e2ceaac3d638061f81dc6037b5e0018d3 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sat, 27 Aug 2011 11:18:07 -0700 Subject: Refactor the display context code a bit: 1) Move the display context code into the controller themselves so that it's more logically a continuation callback from the original controller rendering code. 2) Simplify the display context set/get code and put it in the item helper, it's just a couple of lines of code now. 3) Add more descriptive breadcrumb strings --- modules/gallery/controllers/albums.php | 24 ++++++-- modules/gallery/controllers/movies.php | 10 ++- modules/gallery/controllers/photos.php | 10 ++- modules/gallery/helpers/item.php | 17 ++++++ modules/gallery/libraries/Display_Context.php | 71 ---------------------- modules/gallery/libraries/Item_Display_Context.php | 44 -------------- modules/search/controllers/search.php | 36 ++++++++--- .../search/libraries/Search_Display_Context.php | 52 ---------------- modules/tag/controllers/tag.php | 34 +++++++++-- modules/tag/libraries/Tag_Display_Context.php | 49 --------------- 10 files changed, 102 insertions(+), 245 deletions(-) delete mode 100644 modules/gallery/libraries/Display_Context.php delete mode 100644 modules/gallery/libraries/Item_Display_Context.php delete mode 100644 modules/search/libraries/Search_Display_Context.php delete mode 100644 modules/tag/libraries/Tag_Display_Context.php (limited to 'modules/gallery/libraries') diff --git a/modules/gallery/controllers/albums.php b/modules/gallery/controllers/albums.php index 9bf7b9bf..8aa3bb35 100644 --- a/modules/gallery/controllers/albums.php +++ b/modules/gallery/controllers/albums.php @@ -60,9 +60,6 @@ class Albums_Controller extends Items_Controller { url::redirect($album->abs_url("page=$max_pages")); } - Display_Context::factory("item") - ->save(); - $template = new Theme_View("page.html", "collection", "album"); $template->set_global( array("page" => $page, @@ -75,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 bbf89f17..76263dc0 100644 --- a/modules/gallery/controllers/movies.php +++ b/modules/gallery/controllers/movies.php @@ -28,12 +28,10 @@ class Movies_Controller extends Items_Controller { access::required("view", $movie); $template = new Theme_View("page.html", "item", "movie"); - $template->set_global( - array_merge(array("item" => $movie, - "children" => array(), - "children_count" => 0), - Display_Context::factory()->display_context($movie))); - + $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 49bb5a25..7e78b205 100644 --- a/modules/gallery/controllers/photos.php +++ b/modules/gallery/controllers/photos.php @@ -28,12 +28,10 @@ class Photos_Controller extends Items_Controller { access::required("view", $photo); $template = new Theme_View("page.html", "item", "photo"); - $template->set_global( - array_merge(array("item" => $photo, - "children" => array(), - "children_count" => 0), - Display_Context::factory()->display_context($photo))); - + $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..494c6db4 100644 --- a/modules/gallery/helpers/item.php +++ b/modules/gallery/helpers/item.php @@ -402,4 +402,21 @@ class item_Core { return $position; } + + /** + * Set the display context callback for any future item renders. + */ + static function set_display_context_callback() { + Cache::instance()->set("display_context_" . $sid = Session::instance()->id(), func_get_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/Display_Context.php b/modules/gallery/libraries/Display_Context.php deleted file mode 100644 index 5054cffb..00000000 --- a/modules/gallery/libraries/Display_Context.php +++ /dev/null @@ -1,71 +0,0 @@ -get("display_context_id", ""); - $context = Cache::instance()->get($display_context_id, null); - $context = empty($context) ? new Item_Display_Context() : unserialize($context); - } else { - $class_prefix = ucfirst(strtolower($display_context_name)); - $class_name = "{$class_prefix}_Display_Context"; - $context = new $class_name(); - } - - return $context; - } - - protected function __construct($display_context_name) { - // $this->reset($display_context_name); - $this->_data = array(); - $this->_display_context_name = $display_context_name; - } - - final function get($key) { - return empty($this->_data[$key]) ? null : $this->_data[$key]; - } - - final function set($key, $value=null) { - if (is_array($key)) { - if ((array)$key == $key) { - $this->_data = array_merge($this->_data, $key); - } else { - $this->_data = array_merge($this->_data, array_fill_keys($key, $value)); - } - } else { - $this->_data[$key] = $value; - } - return $this; - } - - final function save() { - $context_data = serialize($this); - $display_context_id = "display_context_" . md5($context_data); - Session::instance()->set("display_context_id", $display_context_id); - Cache::instance()->set($display_context_id, $context_data); - return $this; - } - - abstract function display_context($item); -} diff --git a/modules/gallery/libraries/Item_Display_Context.php b/modules/gallery/libraries/Item_Display_Context.php deleted file mode 100644 index d0562790..00000000 --- a/modules/gallery/libraries/Item_Display_Context.php +++ /dev/null @@ -1,44 +0,0 @@ - 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)); - } -} diff --git a/modules/search/controllers/search.php b/modules/search/controllers/search.php index 1d5a55bf..e4ac6702 100644 --- a/modules/search/controllers/search.php +++ b/modules/search/controllers/search.php @@ -29,7 +29,7 @@ class Search_Controller extends Controller { $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"))); + url::redirect(url::abs_site("search?q=" . urlencode($q) . ($page == 1 ? "" : "&page=$page"))); } } @@ -48,12 +48,6 @@ class Search_Controller extends Controller { $max_pages = max(ceil($count / $page_size), 1); - Display_Context::factory("search") - ->set(array("title" => $title, - "query_terms" => $q_with_more_terms, - "q" => $q)) - ->save(); - $template = new Theme_View("page.html", "collection", "search"); $root = item::root(); $template->set_global( @@ -71,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/libraries/Search_Display_Context.php b/modules/search/libraries/Search_Display_Context.php deleted file mode 100644 index aed3a125..00000000 --- a/modules/search/libraries/Search_Display_Context.php +++ /dev/null @@ -1,52 +0,0 @@ -get("query_terms")); - - if ($position > 1) { - list ($count, $result_data) = - search::search($this->get("query_terms"), 3, $position - 2); - list ($previous_item, $ignore, $next_item) = $result_data; - } else { - $previous_item = null; - list ($count, $result_data) = search::search($this->get("query_terms"), 1, $position); - list ($next_item) = $result_data; - } - - $q = $this->get("q"); - $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), - Breadcrumb::instance($q, $search_url), - Breadcrumb::instance($item->title, $item->url()))); - } -} diff --git a/modules/tag/controllers/tag.php b/modules/tag/controllers/tag.php index 1628f0ac..559e2a5a 100644 --- a/modules/tag/controllers/tag.php +++ b/modules/tag/controllers/tag.php @@ -50,10 +50,6 @@ class Tag_Controller extends Controller { } $root = item::root(); - Display_Context::factory("tag") - ->set(array("tag" => $tag)) - ->save(); - $template = new Theme_View("page.html", "collection", "tag"); $template->set_global( array("page" => $page, @@ -63,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/libraries/Tag_Display_Context.php b/modules/tag/libraries/Tag_Display_Context.php deleted file mode 100644 index 47c79088..00000000 --- a/modules/tag/libraries/Tag_Display_Context.php +++ /dev/null @@ -1,49 +0,0 @@ -get("tag"); - - $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($tag->name, $tag->url("show={$item->id}")), - Breadcrumb::instance($item->title, $item->url())->set_last())); - } -} -- cgit v1.2.3