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.
This commit is contained in:
Bharat Mediratta
2013-01-23 21:33:19 -05:00
parent de78d3bfca
commit aa8fffcd8f
3 changed files with 28 additions and 7 deletions
+12
View File
@@ -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();
}
}
}
@@ -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);
}
}
+1 -7
View File
@@ -128,13 +128,7 @@ class Organize_Controller extends Controller {
access::required("edit", $album);
if ($album->sort_column != "weight") {
// Force all the weights into the current order before changing the order to manual
$weight = 0;
foreach ($album->children() as $child) {
$child->weight = ++$weight;
$child->save();
}
item::resequence_child_weights($album);
$album->sort_column = "weight";
$album->sort_order = "ASC";
$album->save();