From aa8fffcd8fc7d16f4bd84a9f8f7ead206d9ce45a Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Wed, 23 Jan 2013 21:33:19 -0500 Subject: Extract reweighting logic out of Organize_Controller into item::reweight_all_children as an API and write a test for it. Work in progress on #1914. --- modules/gallery/helpers/item.php | 12 ++++++++++++ modules/gallery/tests/Item_Helper_Test.php | 15 +++++++++++++++ 2 files changed, 27 insertions(+) (limited to 'modules/gallery') diff --git a/modules/gallery/helpers/item.php b/modules/gallery/helpers/item.php index 20a865d1..b2c4cadf 100644 --- a/modules/gallery/helpers/item.php +++ b/modules/gallery/helpers/item.php @@ -437,4 +437,16 @@ class item_Core { } return call_user_func_array($callback, $args); } + + /** + * Reset all child weights of a given album to a monotonically increasing sequence based on the + * current sort order of the album. + */ + static function resequence_child_weights($album) { + $weight = 0; + foreach ($album->children() as $child) { + $child->weight = ++$weight; + $child->save(); + } + } } \ No newline at end of file diff --git a/modules/gallery/tests/Item_Helper_Test.php b/modules/gallery/tests/Item_Helper_Test.php index 0c08d1af..63081884 100644 --- a/modules/gallery/tests/Item_Helper_Test.php +++ b/modules/gallery/tests/Item_Helper_Test.php @@ -235,4 +235,19 @@ class Item_Helper_Test extends Gallery_Unit_Test_Case { $level3b->id, item::find_by_relative_url("{$level1->slug}/{$level2b->slug}/{$level3b->slug}")->id); } + + public function resequence_child_weights_test() { + $album = test::random_album(); + $photo1 = test::random_photo($album); + $photo2 = test::random_photo($album); + $this->assert_true($photo2->weight > $photo1->weight); + + $album->reload(); + $album->sort_order = "DESC"; + $album->save(); + item::resequence_child_weights($album); + + $this->assert_equal(2, $photo1->reload()->weight); + $this->assert_equal(1, $photo2->reload()->weight); + } } -- cgit v1.2.3