From a00619b812393cf73d1c188af7961af820d36185 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sun, 20 Sep 2009 20:59:18 -0700 Subject: * Move the captured field into the exif module where it is created and populated. * Create a new API event get_sort_fields that allows modules to contribute new sort fields that they manage. the only condition is that the module needs to add it to the item table. * Changed g2_import to call this api to get the allowable sort fields * Changed album::get_edit_form to call this new api to get the available sort fields Partial fix for ticket #627, as the Capture date is not available until the exif module is installed. --- modules/exif/helpers/exif_event.php | 4 ++++ modules/exif/helpers/exif_installer.php | 10 ++++++++- modules/g2_import/helpers/g2_import.php | 32 ++++++++++++++++++--------- modules/gallery/helpers/album.php | 12 +++++----- modules/gallery/helpers/gallery_event.php | 10 +++++++++ modules/gallery/helpers/gallery_installer.php | 17 ++++++++++++-- modules/gallery/module.info | 2 +- modules/info/views/info_block.html.php | 2 +- 8 files changed, 66 insertions(+), 23 deletions(-) diff --git a/modules/exif/helpers/exif_event.php b/modules/exif/helpers/exif_event.php index 826ec959..c5c48bdc 100644 --- a/modules/exif/helpers/exif_event.php +++ b/modules/exif/helpers/exif_event.php @@ -27,4 +27,8 @@ class exif_event_Core { static function item_deleted($item) { Database::instance()->delete("exif_records", array("item_id" => $item->id)); } + + static function get_sort_fields($sort_order) { + $sort_order->fields["captured"] = t("Date captured"); + } } diff --git a/modules/exif/helpers/exif_installer.php b/modules/exif/helpers/exif_installer.php index 66226061..b826224a 100644 --- a/modules/exif/helpers/exif_installer.php +++ b/modules/exif/helpers/exif_installer.php @@ -29,6 +29,12 @@ class exif_installer { PRIMARY KEY (`id`), KEY(`item_id`)) DEFAULT CHARSET=utf8;"); + + + $item_fields = $db->list_fields("items"); + if (empty($item_fields["captured"])) { + $db->query("ALTER TABLE {items} ADD COLUMN `captured` int(9) default NULL"); + } module::set_version("exif", 1); } @@ -41,6 +47,8 @@ class exif_installer { } static function uninstall() { - Database::instance()->query("DROP TABLE IF EXISTS {exif_records};"); + $db = Database::instance(); + $db->query("DROP TABLE IF EXISTS {exif_records};"); + $db->query("ALTER TABLE {items} DROP COLUMN `captured`"); } } diff --git a/modules/g2_import/helpers/g2_import.php b/modules/g2_import/helpers/g2_import.php index 7e5c6f75..4686b23d 100644 --- a/modules/g2_import/helpers/g2_import.php +++ b/modules/g2_import/helpers/g2_import.php @@ -23,6 +23,7 @@ class g2_import_Core { public static $map = array(); private static $current_g2_item = null; + private static $order_map = false; static function is_configured() { return module::get_var("g2_import", "embed_path"); @@ -313,7 +314,6 @@ class g2_import_Core { return $message; } - /** * Import a single album. */ @@ -357,16 +357,26 @@ class g2_import_Core { $album->view_count = g2(GalleryCoreApi::fetchItemViewCount($g2_album_id)); $album->created = $g2_album->getCreationTimestamp(); - $order_map = array( - "originationTimestamp" => "captured", - "creationTimestamp" => "created", - "description" => "description", - "modificationTimestamp" => "updated", - "orderWeight" => "weight", - "pathComponent" => "name", - "summary" => "description", - "title" => "title", - "viewCount" => "view_count"); + if (self::$order_map === false) { + self::$order_map = array( + "originationTimestamp" => "captured", + "creationTimestamp" => "created", + "description" => "description", + "modificationTimestamp" => "updated", + "orderWeight" => "weight", + "pathComponent" => "name", + "summary" => "description", + "title" => "title", + "viewCount" => "view_count"); + $sort_order = (object)array("fields" => array()); + module::event("get_sort_fields", $sort_order); + foreach (self::$order_map as $g2_sort_key => $g3_sort_key) { + if (empty($sort_order->fields[$g3_sort_key])) { + unset(self::$order_map[$g2_sort_key]); + } + } + } + $direction_map = array( ORDER_ASCENDING => "asc", ORDER_DESCENDING => "desc"); diff --git a/modules/gallery/helpers/album.php b/modules/gallery/helpers/album.php index 9cd746d7..eeeb7e52 100644 --- a/modules/gallery/helpers/album.php +++ b/modules/gallery/helpers/album.php @@ -166,12 +166,10 @@ class album_Core { * Return a structured set of all the possible sort orders. */ static function get_sort_order_options() { - return array("weight" => t("Manual"), - "captured" => t("Date captured"), - "created" => t("Date uploaded"), - "title" => t("Title"), - "updated" => t("Date modified"), - "view_count" => t("Number of views"), - "rand_key" => t("Random")); + $sort_order = (object)array("fields" => array()); + module::event("get_sort_fields", $sort_order); + asort($sort_order->fields); + + return $sort_order->fields; } } diff --git a/modules/gallery/helpers/gallery_event.php b/modules/gallery/helpers/gallery_event.php index 64f2a9ff..446a8a91 100644 --- a/modules/gallery/helpers/gallery_event.php +++ b/modules/gallery/helpers/gallery_event.php @@ -53,4 +53,14 @@ class gallery_event_Core { $data[] = $item->name; $data[] = $item->title; } + + static function get_sort_fields($sort_order) { + $sort_order->fields = array_merge($sort_order->fields, + array("weight" => t("Manual"), + "created" => t("Date uploaded"), + "title" => t("Title"), + "updated" => t("Date modified"), + "view_count" => t("Number of views"), + "rand_key" => t("Random"))); + } } diff --git a/modules/gallery/helpers/gallery_installer.php b/modules/gallery/helpers/gallery_installer.php index 6500482b..aebb7a32 100644 --- a/modules/gallery/helpers/gallery_installer.php +++ b/modules/gallery/helpers/gallery_installer.php @@ -68,7 +68,6 @@ class gallery_installer { $db->query("CREATE TABLE {items} ( `id` int(9) NOT NULL auto_increment, `album_cover_item_id` int(9) default NULL, - `captured` int(9) default NULL, `created` int(9) default NULL, `description` varchar(2048) default NULL, `height` int(9) default NULL, @@ -268,7 +267,7 @@ class gallery_installer { module::set_var("gallery", "show_credits", 1); // @todo this string needs to be picked up by l10n_scanner module::set_var("gallery", "credits", "Powered by Gallery %version"); - module::set_version("gallery", 12); + module::set_version("gallery", 13); } static function upgrade($version) { @@ -364,6 +363,20 @@ class gallery_installer { $db->query("UPDATE {items} SET `relative_url_cache` = NULL, `relative_path_cache` = NULL"); module::set_version("gallery", $version = 12); } + + if ($version == 12) { + // remove the capture field if exif is not installed and it has no data + if (!module::is_active("exif")) { + $total_records = $db->query("SELECT COUNT(id) as total_records FROM {items}") + ->current()->total_records; + $count_null = $db->query("SELECT COUNT(id) as count_null FROM {items} where `captured`is NULL") + ->current()->count_null; + if ($total_records == $count_null) { + $db->query("ALTER TABLE {items} DROP COLUMN `captured`"); + } + } + module::set_version("gallery", $version = 13); + } } static function uninstall() { diff --git a/modules/gallery/module.info b/modules/gallery/module.info index 70bd91e2..65a0691c 100644 --- a/modules/gallery/module.info +++ b/modules/gallery/module.info @@ -1,3 +1,3 @@ name = "Gallery 3" description = "Gallery core application" -version = 12 +version = 13 diff --git a/modules/info/views/info_block.html.php b/modules/info/views/info_block.html.php index d8f36984..1142bbd4 100644 --- a/modules/info/views/info_block.html.php +++ b/modules/info/views/info_block.html.php @@ -16,7 +16,7 @@ name) ?> - captured): ?> + captured): ?>
  • captured)?> -- cgit v1.2.3 From c01e467466eff51926108d030f21385179fa24d2 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Mon, 21 Sep 2009 09:11:31 -0700 Subject: If captured had been used for any sort orders, reset back to weight --- modules/gallery/helpers/gallery_installer.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/gallery/helpers/gallery_installer.php b/modules/gallery/helpers/gallery_installer.php index aebb7a32..645d6865 100644 --- a/modules/gallery/helpers/gallery_installer.php +++ b/modules/gallery/helpers/gallery_installer.php @@ -367,6 +367,8 @@ class gallery_installer { if ($version == 12) { // remove the capture field if exif is not installed and it has no data if (!module::is_active("exif")) { + $db->query("update g3_items set sort_order = 'weight' + where type = 'album' and sort_order = 'captured';"); $total_records = $db->query("SELECT COUNT(id) as total_records FROM {items}") ->current()->total_records; $count_null = $db->query("SELECT COUNT(id) as count_null FROM {items} where `captured`is NULL") -- cgit v1.2.3 From a6581ede0b7a50c6159eb5d36cf6be340a072609 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 21 Sep 2009 11:35:27 -0700 Subject: Fix Item_Model::get_position() so that our sort is stable when the comparison row has a null value in the sort field. Fix for #627 Note: this changes get_position() to take an Item_Model instead of an id! --- modules/gallery/controllers/albums.php | 15 +++++---- modules/gallery/controllers/photos.php | 2 +- modules/gallery/models/item.php | 61 +++++++++++++++++++++------------- 3 files changed, 47 insertions(+), 31 deletions(-) diff --git a/modules/gallery/controllers/albums.php b/modules/gallery/controllers/albums.php index 183c26d0..3ea08538 100644 --- a/modules/gallery/controllers/albums.php +++ b/modules/gallery/controllers/albums.php @@ -39,12 +39,15 @@ class Albums_Controller extends Items_Controller { $show = $this->input->get("show"); if ($show) { - $index = $album->get_position($show); - $page = ceil($index / $page_size); - if ($page == 1) { - url::redirect($album->abs_url()); - } else { - url::redirect($album->abs_url("page=$page")); + $child = ORM::factory("item", $show); + $index = $album->get_position($child); + if ($index) { + $page = ceil($index / $page_size); + if ($page == 1) { + url::redirect($album->abs_url()); + } else { + url::redirect($album->abs_url("page=$page")); + } } } diff --git a/modules/gallery/controllers/photos.php b/modules/gallery/controllers/photos.php index 79ad674a..e6154535 100644 --- a/modules/gallery/controllers/photos.php +++ b/modules/gallery/controllers/photos.php @@ -25,7 +25,7 @@ class Photos_Controller extends Items_Controller { public function _show($photo) { access::required("view", $photo); - $position = $photo->parent()->get_position($photo->id); + $position = $photo->parent()->get_position($photo); if ($position > 1) { list ($previous_item, $ignore, $next_item) = $photo->parent()->children(3, $position - 2); diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index da1f6959..3cc9dd53 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -412,38 +412,51 @@ class Item_Model extends ORM_MPTT { * Find the position of the given child id in this album. The resulting value is 1-indexed, so * the first child in the album is at position 1. */ - public function get_position($child_id) { + public function get_position($child) { if ($this->sort_order == "DESC") { $comp = ">"; } else { $comp = "<"; } - $db = Database::instance(); - $position = $db->query(" - SELECT COUNT(*) AS position FROM {items} - WHERE `parent_id` = {$this->id} - AND `{$this->sort_column}` $comp (SELECT `{$this->sort_column}` - FROM {items} WHERE `id` = $child_id)") - ->current()->position; - - // We stopped short of our target value in the sort (notice that we're using a < comparator - // above) because it's possible that we have duplicate values in the sort column. An - // equality check would just arbitrarily pick one of those multiple possible equivalent - // columns, which would mean that if you choose a sort order that has duplicates, it'd pick - // any one of them as the child's "position". - // - // Fix this by doing a 2nd query where we iterate over the equivalent columns and add them to - // our base value. - $result = $db->query(" - SELECT id FROM {items} - WHERE `parent_id` = {$this->id} - AND `{$this->sort_column}` = (SELECT `{$this->sort_column}` - FROM {items} WHERE `id` = $child_id) - ORDER BY `id` ASC"); + + // We can't use isset() in this comparison because ORM::__get() confuses it. + if ($child->{$this->sort_column} !== null) { + $position = $db->query(" + SELECT COUNT(*) AS position FROM {items} + WHERE `parent_id` = {$this->id} + AND `{$this->sort_column}` $comp (SELECT `{$this->sort_column}` + FROM {items} WHERE `id` = $child->id)") + ->current()->position; + + // We stopped short of our target value in the sort (notice that we're using a < comparator + // above) because it's possible that we have duplicate values in the sort column. An + // equality check would just arbitrarily pick one of those multiple possible equivalent + // columns, which would mean that if you choose a sort order that has duplicates, it'd pick + // any one of them as the child's "position". + // + // Fix this by doing a 2nd query where we iterate over the equivalent columns and add them to + // our base value. + $result = $db->query(" + SELECT id FROM {items} + WHERE `parent_id` = {$this->id} + AND `{$this->sort_column}` = (SELECT `{$this->sort_column}` + FROM {items} WHERE `id` = $child->id) + ORDER BY `id` ASC"); + } else { + // If the sort value is null, then we can't take the approach of doing a comparison to get + // most of the way there. We have to iterate the entire data set. + $position = 0; + $result = $db->query(" + SELECT id FROM {items} + WHERE `parent_id` = {$this->id} + AND `{$this->sort_column}` IS NULL + ORDER BY `id` ASC"); + } + foreach ($result as $row) { $position++; - if ($row->id == $child_id) { + if ($row->id == $child->id) { break; } } -- cgit v1.2.3 From ed6dfbc0e8eabf7662d128fd4241a92fa3a140b8 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Mon, 21 Sep 2009 13:56:10 -0700 Subject: Revert "If captured had been used for any sort orders, reset back to weight" This reverts commit c01e467466eff51926108d030f21385179fa24d2. --- modules/gallery/helpers/gallery_installer.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/gallery/helpers/gallery_installer.php b/modules/gallery/helpers/gallery_installer.php index 645d6865..aebb7a32 100644 --- a/modules/gallery/helpers/gallery_installer.php +++ b/modules/gallery/helpers/gallery_installer.php @@ -367,8 +367,6 @@ class gallery_installer { if ($version == 12) { // remove the capture field if exif is not installed and it has no data if (!module::is_active("exif")) { - $db->query("update g3_items set sort_order = 'weight' - where type = 'album' and sort_order = 'captured';"); $total_records = $db->query("SELECT COUNT(id) as total_records FROM {items}") ->current()->total_records; $count_null = $db->query("SELECT COUNT(id) as count_null FROM {items} where `captured`is NULL") -- cgit v1.2.3 From dd31b1d39f9a342afc82cc7e446e36454a882cf9 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Mon, 21 Sep 2009 13:56:52 -0700 Subject: Revert "* Move the captured field into the exif module where it is created and populated." This reverts commit a00619b812393cf73d1c188af7961af820d36185. --- modules/exif/helpers/exif_event.php | 4 ---- modules/exif/helpers/exif_installer.php | 10 +-------- modules/g2_import/helpers/g2_import.php | 32 +++++++++------------------ modules/gallery/helpers/album.php | 12 +++++----- modules/gallery/helpers/gallery_event.php | 10 --------- modules/gallery/helpers/gallery_installer.php | 17 ++------------ modules/gallery/module.info | 2 +- modules/info/views/info_block.html.php | 2 +- 8 files changed, 23 insertions(+), 66 deletions(-) diff --git a/modules/exif/helpers/exif_event.php b/modules/exif/helpers/exif_event.php index c5c48bdc..826ec959 100644 --- a/modules/exif/helpers/exif_event.php +++ b/modules/exif/helpers/exif_event.php @@ -27,8 +27,4 @@ class exif_event_Core { static function item_deleted($item) { Database::instance()->delete("exif_records", array("item_id" => $item->id)); } - - static function get_sort_fields($sort_order) { - $sort_order->fields["captured"] = t("Date captured"); - } } diff --git a/modules/exif/helpers/exif_installer.php b/modules/exif/helpers/exif_installer.php index b826224a..66226061 100644 --- a/modules/exif/helpers/exif_installer.php +++ b/modules/exif/helpers/exif_installer.php @@ -29,12 +29,6 @@ class exif_installer { PRIMARY KEY (`id`), KEY(`item_id`)) DEFAULT CHARSET=utf8;"); - - - $item_fields = $db->list_fields("items"); - if (empty($item_fields["captured"])) { - $db->query("ALTER TABLE {items} ADD COLUMN `captured` int(9) default NULL"); - } module::set_version("exif", 1); } @@ -47,8 +41,6 @@ class exif_installer { } static function uninstall() { - $db = Database::instance(); - $db->query("DROP TABLE IF EXISTS {exif_records};"); - $db->query("ALTER TABLE {items} DROP COLUMN `captured`"); + Database::instance()->query("DROP TABLE IF EXISTS {exif_records};"); } } diff --git a/modules/g2_import/helpers/g2_import.php b/modules/g2_import/helpers/g2_import.php index 4686b23d..7e5c6f75 100644 --- a/modules/g2_import/helpers/g2_import.php +++ b/modules/g2_import/helpers/g2_import.php @@ -23,7 +23,6 @@ class g2_import_Core { public static $map = array(); private static $current_g2_item = null; - private static $order_map = false; static function is_configured() { return module::get_var("g2_import", "embed_path"); @@ -314,6 +313,7 @@ class g2_import_Core { return $message; } + /** * Import a single album. */ @@ -357,26 +357,16 @@ class g2_import_Core { $album->view_count = g2(GalleryCoreApi::fetchItemViewCount($g2_album_id)); $album->created = $g2_album->getCreationTimestamp(); - if (self::$order_map === false) { - self::$order_map = array( - "originationTimestamp" => "captured", - "creationTimestamp" => "created", - "description" => "description", - "modificationTimestamp" => "updated", - "orderWeight" => "weight", - "pathComponent" => "name", - "summary" => "description", - "title" => "title", - "viewCount" => "view_count"); - $sort_order = (object)array("fields" => array()); - module::event("get_sort_fields", $sort_order); - foreach (self::$order_map as $g2_sort_key => $g3_sort_key) { - if (empty($sort_order->fields[$g3_sort_key])) { - unset(self::$order_map[$g2_sort_key]); - } - } - } - + $order_map = array( + "originationTimestamp" => "captured", + "creationTimestamp" => "created", + "description" => "description", + "modificationTimestamp" => "updated", + "orderWeight" => "weight", + "pathComponent" => "name", + "summary" => "description", + "title" => "title", + "viewCount" => "view_count"); $direction_map = array( ORDER_ASCENDING => "asc", ORDER_DESCENDING => "desc"); diff --git a/modules/gallery/helpers/album.php b/modules/gallery/helpers/album.php index eeeb7e52..9cd746d7 100644 --- a/modules/gallery/helpers/album.php +++ b/modules/gallery/helpers/album.php @@ -166,10 +166,12 @@ class album_Core { * Return a structured set of all the possible sort orders. */ static function get_sort_order_options() { - $sort_order = (object)array("fields" => array()); - module::event("get_sort_fields", $sort_order); - asort($sort_order->fields); - - return $sort_order->fields; + return array("weight" => t("Manual"), + "captured" => t("Date captured"), + "created" => t("Date uploaded"), + "title" => t("Title"), + "updated" => t("Date modified"), + "view_count" => t("Number of views"), + "rand_key" => t("Random")); } } diff --git a/modules/gallery/helpers/gallery_event.php b/modules/gallery/helpers/gallery_event.php index 446a8a91..64f2a9ff 100644 --- a/modules/gallery/helpers/gallery_event.php +++ b/modules/gallery/helpers/gallery_event.php @@ -53,14 +53,4 @@ class gallery_event_Core { $data[] = $item->name; $data[] = $item->title; } - - static function get_sort_fields($sort_order) { - $sort_order->fields = array_merge($sort_order->fields, - array("weight" => t("Manual"), - "created" => t("Date uploaded"), - "title" => t("Title"), - "updated" => t("Date modified"), - "view_count" => t("Number of views"), - "rand_key" => t("Random"))); - } } diff --git a/modules/gallery/helpers/gallery_installer.php b/modules/gallery/helpers/gallery_installer.php index aebb7a32..6500482b 100644 --- a/modules/gallery/helpers/gallery_installer.php +++ b/modules/gallery/helpers/gallery_installer.php @@ -68,6 +68,7 @@ class gallery_installer { $db->query("CREATE TABLE {items} ( `id` int(9) NOT NULL auto_increment, `album_cover_item_id` int(9) default NULL, + `captured` int(9) default NULL, `created` int(9) default NULL, `description` varchar(2048) default NULL, `height` int(9) default NULL, @@ -267,7 +268,7 @@ class gallery_installer { module::set_var("gallery", "show_credits", 1); // @todo this string needs to be picked up by l10n_scanner module::set_var("gallery", "credits", "Powered by Gallery %version"); - module::set_version("gallery", 13); + module::set_version("gallery", 12); } static function upgrade($version) { @@ -363,20 +364,6 @@ class gallery_installer { $db->query("UPDATE {items} SET `relative_url_cache` = NULL, `relative_path_cache` = NULL"); module::set_version("gallery", $version = 12); } - - if ($version == 12) { - // remove the capture field if exif is not installed and it has no data - if (!module::is_active("exif")) { - $total_records = $db->query("SELECT COUNT(id) as total_records FROM {items}") - ->current()->total_records; - $count_null = $db->query("SELECT COUNT(id) as count_null FROM {items} where `captured`is NULL") - ->current()->count_null; - if ($total_records == $count_null) { - $db->query("ALTER TABLE {items} DROP COLUMN `captured`"); - } - } - module::set_version("gallery", $version = 13); - } } static function uninstall() { diff --git a/modules/gallery/module.info b/modules/gallery/module.info index 65a0691c..70bd91e2 100644 --- a/modules/gallery/module.info +++ b/modules/gallery/module.info @@ -1,3 +1,3 @@ name = "Gallery 3" description = "Gallery core application" -version = 13 +version = 12 diff --git a/modules/info/views/info_block.html.php b/modules/info/views/info_block.html.php index 1142bbd4..d8f36984 100644 --- a/modules/info/views/info_block.html.php +++ b/modules/info/views/info_block.html.php @@ -16,7 +16,7 @@ name) ?>
  • - captured): ?> + captured): ?>
  • captured)?> -- cgit v1.2.3 From 9e6be40e31b06e5dffe7552928cb8b2d9ee7ad59 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 21 Sep 2009 20:47:55 -0700 Subject: Add viewable() protection to children() and children_count() calls. This is not currently necessary (nor is it a security hole) because we don't constrain permissions at the child level in the core, but it makes our security audits easier and will enable the scenario where somebody writes a module to add per-photo permissions. --- modules/gallery/controllers/photos.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/gallery/controllers/photos.php b/modules/gallery/controllers/photos.php index e6154535..3de9b3ee 100644 --- a/modules/gallery/controllers/photos.php +++ b/modules/gallery/controllers/photos.php @@ -31,7 +31,7 @@ class Photos_Controller extends Items_Controller { $photo->parent()->children(3, $position - 2); } else { $previous_item = null; - list ($next_item) = $photo->parent()->children(1, $position); + list ($next_item) = $photo->parent()->viewable()->children(1, $position); } $template = new Theme_View("page.html", "photo"); @@ -41,7 +41,7 @@ class Photos_Controller extends Items_Controller { $template->set_global("parents", $photo->parents()); $template->set_global("next_item", $next_item); $template->set_global("previous_item", $previous_item); - $template->set_global("sibling_count", $photo->parent()->children_count()); + $template->set_global("sibling_count", $photo->parent()->viewable()->children_count()); $template->set_global("position", $position); $template->content = new View("photo.html"); -- cgit v1.2.3 From 68f3dab7f202fbd9b690f4db27cc4f4038e58d2f Mon Sep 17 00:00:00 2001 From: Chad Kieffer Date: Mon, 21 Sep 2009 21:56:27 -0600 Subject: Don't try to initialize contextual menu unless it has items --- lib/gallery.common.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/gallery.common.js b/lib/gallery.common.js index c6a619c1..81904548 100644 --- a/lib/gallery.common.js +++ b/lib/gallery.common.js @@ -112,21 +112,23 @@ }; $.fn.gallery_context_menu = function() { - var in_progress = 0; - $(".gContextMenu *").removeAttr('title'); - $(".gContextMenu ul").hide(); - $(".gContextMenu").hover( - function() { - if (in_progress == 0) { - $(this).find("ul").slideDown("fast", function() { in_progress = 1; }); - $(this).find(".gDialogLink").gallery_dialog(); - $(this).find(".gAjaxLink").gallery_ajax(); + if ($(".gContextMenu li").length) { + var in_progress = 0; + $(".gContextMenu *").removeAttr('title'); + $(".gContextMenu ul").hide(); + $(".gContextMenu").hover( + function() { + if (in_progress == 0) { + $(this).find("ul").slideDown("fast", function() { in_progress = 1; }); + $(this).find(".gDialogLink").gallery_dialog(); + $(this).find(".gAjaxLink").gallery_ajax(); + } + }, + function() { + $(this).find("ul").slideUp("slow", function() { in_progress = 0; }); } - }, - function() { - $(this).find("ul").slideUp("slow", function() { in_progress = 0; }); - } - ); + ); + } }; $.gallery_auto_fit_window = function(imageWidth, imageHeight) { -- cgit v1.2.3 From 88350c5b88abb8bfc56b26177b64e049f6b7440f Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 21 Sep 2009 21:21:52 -0700 Subject: Update the next/previous item calculations to match what we do in photos.php Force the children_count to be zero, movies have no children. Rename $photo to $movie everywhere. --- modules/gallery/controllers/movies.php | 86 +++++++++++++++------------------- 1 file changed, 38 insertions(+), 48 deletions(-) diff --git a/modules/gallery/controllers/movies.php b/modules/gallery/controllers/movies.php index 04e15315..fa07668e 100644 --- a/modules/gallery/controllers/movies.php +++ b/modules/gallery/controllers/movies.php @@ -22,42 +22,32 @@ class Movies_Controller extends Items_Controller { /** * @see REST_Controller::_show($resource) */ - public function _show($photo) { - access::required("view", $photo); + public function _show($movie) { + access::required("view", $movie); - // We sort by id ascending so for now, find sibling info by doing id based queries. - $next_item = ORM::factory("item") - ->viewable() - ->where("parent_id", $photo->parent_id) - ->where("id >", $photo->id) - ->orderby("id", "ASC") - ->find(); - $previous_item = ORM::factory("item") - ->viewable() - ->where("parent_id", $photo->parent_id) - ->where("id <", $photo->id) - ->orderby("id", "DESC") - ->find(); - $position = ORM::factory("item") - ->viewable() - ->where("parent_id", $photo->parent_id) - ->where("id <=", $photo->id) - ->count_all(); + $position = $movie->parent()->get_position($movie); + if ($position > 1) { + list ($previous_item, $ignore, $next_item) = + $movie->parent()->children(3, $position - 2); + } else { + $previous_item = null; + list ($next_item) = $movie->parent()->viewable()->children(1, $position); + } $template = new Theme_View("page.html", "movie"); - $template->set_global("item", $photo); + $template->set_global("item", $movie); $template->set_global("children", array()); - $template->set_global("children_count", $photo->children_count()); - $template->set_global("parents", $photo->parents()); - $template->set_global("next_item", $next_item->loaded ? $next_item : null); - $template->set_global("previous_item", $previous_item->loaded ? $previous_item : null); - $template->set_global("sibling_count", $photo->parent()->children_count()); + $template->set_global("children_count", 0); + $template->set_global("parents", $movie->parents()); + $template->set_global("next_item", $next_item); + $template->set_global("previous_item", $previous_item); + $template->set_global("sibling_count", $movie->parent()->viewable()->children_count()); $template->set_global("position", $position); $template->content = new View("movie.html"); - $photo->view_count++; - $photo->save(); + $movie->view_count++; + $movie->save(); print $template; } @@ -65,21 +55,21 @@ class Movies_Controller extends Items_Controller { /** * @see REST_Controller::_update($resource) */ - public function _update($photo) { + public function _update($movie) { access::verify_csrf(); - access::required("view", $photo); - access::required("edit", $photo); + access::required("view", $movie); + access::required("edit", $movie); - $form = photo::get_edit_form($photo); + $form = photo::get_edit_form($movie); if ($valid = $form->validate()) { - if ($form->edit_item->filename->value != $photo->name || - $form->edit_item->slug->value != $photo->slug) { + if ($form->edit_item->filename->value != $movie->name || + $form->edit_item->slug->value != $movie->slug) { // Make sure that there's not a name or slug conflict if ($row = Database::instance() ->select(array("name", "slug")) ->from("items") - ->where("parent_id", $photo->parent_id) - ->where("id <>", $photo->id) + ->where("parent_id", $movie->parent_id) + ->where("id <>", $movie->id) ->open_paren() ->where("name", $form->edit_item->filename->value) ->orwhere("slug", $form->edit_item->slug->value) @@ -98,16 +88,16 @@ class Movies_Controller extends Items_Controller { } if ($valid) { - $photo->title = $form->edit_item->title->value; - $photo->description = $form->edit_item->description->value; - $photo->slug = $form->edit_item->slug->value; - $photo->rename($form->edit_item->filename->value); - $photo->save(); - module::event("item_edit_form_completed", $photo, $form); + $movie->title = $form->edit_item->title->value; + $movie->description = $form->edit_item->description->value; + $movie->slug = $form->edit_item->slug->value; + $movie->rename($form->edit_item->filename->value); + $movie->save(); + module::event("item_edit_form_completed", $movie, $form); - log::success("content", "Updated movie", "url()}\">view"); + log::success("content", "Updated movie", "url()}\">view"); message::success( - t("Saved movie %movie_title", array("movie_title" => $photo->title))); + t("Saved movie %movie_title", array("movie_title" => $movie->title))); print json_encode( array("result" => "success")); @@ -121,9 +111,9 @@ class Movies_Controller extends Items_Controller { /** * @see REST_Controller::_form_edit($resource) */ - public function _form_edit($photo) { - access::required("view", $photo); - access::required("edit", $photo); - print photo::get_edit_form($photo); + public function _form_edit($movie) { + access::required("view", $movie); + access::required("edit", $movie); + print photo::get_edit_form($movie); } } -- cgit v1.2.3 From 123afc954281c1f924f851a33ae5016774e6d9f3 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 21 Sep 2009 21:22:07 -0700 Subject: Set children_count to 0, photos have no children. --- modules/gallery/controllers/photos.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/gallery/controllers/photos.php b/modules/gallery/controllers/photos.php index 3de9b3ee..81e7519e 100644 --- a/modules/gallery/controllers/photos.php +++ b/modules/gallery/controllers/photos.php @@ -37,7 +37,7 @@ class Photos_Controller extends Items_Controller { $template = new Theme_View("page.html", "photo"); $template->set_global("item", $photo); $template->set_global("children", array()); - $template->set_global("children_count", $photo->children_count()); + $template->set_global("children_count", 0); $template->set_global("parents", $photo->parents()); $template->set_global("next_item", $next_item); $template->set_global("previous_item", $previous_item); -- cgit v1.2.3 From 529ded3388673036314eefd5bfb1cfc0b76f7f9e Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 21 Sep 2009 21:28:00 -0700 Subject: 2nd attempt at gracefully dealing with sort columns that contain nulls. This time around, do a query to determine whether or not the sort column has nulls in it. If it doesn't, then use our comparators as before. There are NULLs in the sort column, so we can't use MySQL comparators. Fall back to iterating over every child row to get to the current one. This can be wildly inefficient for really large albums, but it should be a rare case that the user is sorting an album with null values in the sort column. Fixes #627 --- modules/gallery/models/item.php | 60 ++++++++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 15 deletions(-) diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index 3cc9dd53..d1c6feb9 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -420,8 +420,15 @@ class Item_Model extends ORM_MPTT { } $db = Database::instance(); - // We can't use isset() in this comparison because ORM::__get() confuses it. - if ($child->{$this->sort_column} !== null) { + // If the comparison column has NULLs in it, we can't use comparators on it and will have to + // deal with it the hard way. + $count = $db->from("items") + ->where("parent_id", $this->id) + ->where($this->sort_column, NULL) + ->count_records(); + + if (empty($count)) { + // There are no NULLs in the sort column, so we can just use it directly. $position = $db->query(" SELECT COUNT(*) AS position FROM {items} WHERE `parent_id` = {$this->id} @@ -443,21 +450,36 @@ class Item_Model extends ORM_MPTT { AND `{$this->sort_column}` = (SELECT `{$this->sort_column}` FROM {items} WHERE `id` = $child->id) ORDER BY `id` ASC"); + foreach ($result as $row) { + $position++; + if ($row->id == $child->id) { + break; + } + } } else { - // If the sort value is null, then we can't take the approach of doing a comparison to get - // most of the way there. We have to iterate the entire data set. - $position = 0; - $result = $db->query(" - SELECT id FROM {items} - WHERE `parent_id` = {$this->id} - AND `{$this->sort_column}` IS NULL - ORDER BY `id` ASC"); - } + // There are NULLs in the sort column, so we can't use MySQL comparators. Fall back to + // iterating over every child row to get to the current one. This can be wildly inefficient + // for really large albums, but it should be a rare case that the user is sorting an album + // with null values in the sort column. + // + // Reproduce the children() functionality here using Database directly to avoid loading the + // whole ORM for each row. + $orderby = array($this->sort_column => $this->sort_order); + // Use id as a tie breaker + if ($this->sort_column != "id") { + $orderby["id"] = "ASC"; + } - foreach ($result as $row) { - $position++; - if ($row->id == $child->id) { - break; + $position = 0; + foreach ($db->select("id") + ->from("items") + ->where("parent_id", $this->id) + ->orderby($orderby) + ->get() as $row) { + $position++; + if ($row->id == $child->id) { + break; + } } } @@ -564,6 +586,10 @@ class Item_Model extends ORM_MPTT { function children($limit=null, $offset=0, $where=array(), $orderby=null) { if (empty($orderby)) { $orderby = array($this->sort_column => $this->sort_order); + // Use id as a tie breaker + if ($this->sort_column != "id") { + $orderby["id"] = "ASC"; + } } return parent::children($limit, $offset, $where, $orderby); } @@ -582,6 +608,10 @@ class Item_Model extends ORM_MPTT { function descendants($limit=null, $offset=0, $where=array(), $orderby=null) { if (empty($orderby)) { $orderby = array($this->sort_column => $this->sort_order); + // Use id as a tie breaker + if ($this->sort_column != "id") { + $orderby["id"] = "ASC"; + } } return parent::descendants($limit, $offset, $where, $orderby); } -- cgit v1.2.3 From 22296907b77a00aa28e174b6e0231faed7c10fcd Mon Sep 17 00:00:00 2001 From: Chad Kieffer Date: Mon, 21 Sep 2009 23:17:12 -0600 Subject: Don't add in contextual menu height if the menu's empty --- themes/default/js/ui.init.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/themes/default/js/ui.init.js b/themes/default/js/ui.init.js index e645eb71..4eee1bb2 100644 --- a/themes/default/js/ui.init.js +++ b/themes/default/js/ui.init.js @@ -62,9 +62,9 @@ $(document).ready(function() { // Vertical align thumbnails/metadata in album grid $(".gItem").gallery_valign(); - // Initialize context menus + // Initialize thumbnail hover effect $(".gItem").hover( - function(){ + function() { // Insert invisible placeholder to hold the item's position in the grid var placeHolder = $(this).clone(); $(placeHolder).attr("id", "gPlaceHolder"); @@ -82,12 +82,17 @@ $(document).ready(function() { // Set height based on height of descendents var title = $(this).find("h2"); var meta = $(this).find(".gMetadata"); - var context_label = $(this).find(".gContextMenu li:first"); var item_ht = $(this).height(); var title_ht = $(title).gallery_height(); var meta_ht = $(meta).gallery_height(); - var context_label_ht = $(context_label).gallery_height(); - $(this).height(item_ht + title_ht + meta_ht + context_label_ht); + var ht = item_ht + title_ht + meta_ht; + var context_label = $(this).find(".gContextMenu li:first"); + var css_id = $(this).attr("id"); + if ($("#" + css_id + " .gContextMenu li").length) { + var context_label_ht = $(context_label).gallery_height(); + ht = ht + context_label_ht; + } + $(this).height(ht); }, function() { // Reset item height, position, and z-index @@ -101,8 +106,8 @@ $(document).ready(function() { } $(this).css("height", sib_height); $(this).css("position", "relative"); - $(this).css("top", null); - $(this).css("left", null); + $(this).css("top", 0); + $(this).css("left", 0); $(this).css("z-index", 1); // Remove the placeholder and hover class from the item $("#gPlaceHolder").remove(); -- cgit v1.2.3 From bf93473de529fc20ecb774e4f1ef5ca521f6a3b0 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Tue, 22 Sep 2009 06:56:28 -0700 Subject: Revert "Change the exif module to respond to the gallery_ready event and check to see if gettext is installed. The 3rd party library used in the exif module expects gettext to be available. Rather than doing all the time use the gallery ready event to only do it when it might be needed. Fixes ticket #672" This reverts commit 33690a32bcf132e5ab470ff77ba23c073ac26271. --- modules/exif/helpers/exif_event.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/modules/exif/helpers/exif_event.php b/modules/exif/helpers/exif_event.php index 81ac5f44..826ec959 100644 --- a/modules/exif/helpers/exif_event.php +++ b/modules/exif/helpers/exif_event.php @@ -18,14 +18,6 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class exif_event_Core { - static function gallery_ready() { - if (!function_exists("gettext")) { - function gettext($message) { - return (string) t($message); - } - } - } - static function item_created($item) { if (!$item->is_album()) { exif::extract($item); -- cgit v1.2.3 From 78cce555c377b4dd3078a007c6c407cf4eb9f6dd Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Tue, 22 Sep 2009 07:31:00 -0700 Subject: Change the gallery.show_full_size widget to only scale the image if one of the dimensions is greater than the browser window size. Fixes #695 --- lib/gallery.show_full_size.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/gallery.show_full_size.js b/lib/gallery.show_full_size.js index 7e826c32..360ecdc2 100644 --- a/lib/gallery.show_full_size.js +++ b/lib/gallery.show_full_size.js @@ -15,7 +15,16 @@ '-moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; ' + '-moz-background-inline-policy: -moz-initial; z-index: 1001;"> '); - var image_size = $.gallery_auto_fit_window(image_width, image_height); + var image_size; + if (image_width >= size.width() - 6 || image_height >= size.height() - 6) { + image_size = $.gallery_auto_fit_window(image_width, image_height); + } else { + image_size = { + top: Number((height - image_height) / 2), left: Number((width - image_width) / 2), + width: Number(image_width), height: Number(image_height) + }; + } + $("body").append('
    count()): ?>

    comment!", - array("attrs" => html::mark_clean("href=\"#add_comment_form\" class=\"showCommentForm\""))) ?> + array("attrs" => html::mark_clean("id= \"gNoComments\" href=\"" . url::site("form/add/comments/{$item->id}") . "\" class=\"showCommentForm\""))) ?>