Exemple #1
0
 function rearrange()
 {
     access::verify_csrf();
     $input = Input::instance();
     $target = ORM::factory("item", $input->post("target_id"));
     if (!$target->loaded()) {
         json::reply(null);
         return;
     }
     $album = $target->parent();
     access::required("edit", $album);
     if ($album->sort_column != "weight") {
         // Force all the weights into the current order before changing the order to manual
         // @todo: consider making this a trigger in the Item_Model.
         item::resequence_child_weights($album);
         $album->sort_column = "weight";
         $album->sort_order = "ASC";
         $album->save();
     }
     $source_ids = explode(",", $input->post("source_ids"));
     $base_weight = $target->weight;
     if ($input->post("relative") == "after") {
         $base_weight++;
     }
     if ($source_ids) {
         // Make a hole the right size
         db::build()->update("items")->set("weight", db::expr("`weight` + " . count($source_ids)))->where("parent_id", "=", $album->id)->where("weight", ">=", $base_weight)->execute();
         // Move all the source items to the right spots.
         for ($i = 0; $i < count($source_ids); $i++) {
             $source = ORM::factory("item", $source_ids[$i]);
             if ($source->parent_id == $album->id) {
                 $source->weight = $base_weight + $i;
                 $source->save();
             }
         }
     }
     json::reply(null);
 }
 public function resequence_child_weights_test()
 {
     $album = test::random_album_unsaved();
     $album->sort_column = "id";
     $album->save();
     $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);
 }