static function move($source, $target) { access::required("view", $source); access::required("view", $target); access::required("edit", $source); access::required("edit", $target); $parent = $source->parent(); if ($parent->album_cover_item_id == $source->id) { if ($parent->children_count() > 1) { foreach ($parent->children(2) as $child) { if ($child->id != $source->id) { $new_cover_item = $child; break; } } item::make_album_cover($new_cover_item); } else { item::remove_album_cover($parent); } } $source->move_to($target); // If the target has no cover item, make this it. if ($target->album_cover_item_id == null) { item::make_album_cover($source); } }
public function delete() { if ($this->id == 1) { $v = new Validation(array("id")); $v->add_error("id", "cant_delete_root_album"); ORM_Validation_Exception::handle_validation($this->table_name, $v); } $old = clone $this; module::event("item_before_delete", $this); $parent = $this->parent(); if ($parent->album_cover_item_id == $this->id) { item::remove_album_cover($parent); } $path = $this->file_path(); $resize_path = $this->resize_path(); $thumb_path = $this->thumb_path(); parent::delete(); if (is_dir($path)) { // Take some precautions against accidentally deleting way too much $delete_resize_path = dirname($resize_path); $delete_thumb_path = dirname($thumb_path); if ($delete_resize_path == VARPATH . "resizes" || $delete_thumb_path == VARPATH . "thumbs" || $path == VARPATH . "albums") { throw new Exception("@todo DELETING_TOO_MUCH ({$delete_resize_path}, {$delete_thumb_path}, {$path})"); } @dir::unlink($path); @dir::unlink($delete_resize_path); @dir::unlink($delete_thumb_path); } else { @unlink($path); @unlink($resize_path); @unlink($thumb_path); } module::event("item_deleted", $old); }
static function item_deleted($item) { access::delete_item($item); // Find any other albums that had the deleted item as the album cover and null it out. // In some cases this may leave us with a missing album cover up in this item's parent // hierarchy, but in most cases it'll work out fine. foreach (ORM::factory("item")->where("album_cover_item_id", "=", $item->id)->find_all() as $parent) { item::remove_album_cover($parent); } $parent = $item->parent(); if (!$parent->album_cover_item_id) { // Assume that we deleted the album cover if (batch::in_progress()) { // Remember that this parent is missing an album cover, for later. $batch_missing_album_cover = Session::instance()->get("batch_missing_album_cover", array()); $batch_missing_album_cover[$parent->id] = 1; Session::instance()->set("batch_missing_album_cover", $batch_missing_album_cover); } else { // Choose the first viewable child as the new cover. if ($child = $parent->viewable()->children(1)->current()) { item::make_album_cover($child); } } } }
static function move($source, $target) { access::required("view", $source); access::required("view", $target); access::required("edit", $source); access::required("edit", $target); $parent = $source->parent(); if ($parent->album_cover_item_id == $source->id) { if ($parent->children_count() > 1) { foreach ($parent->children(2) as $child) { if ($child->id != $source->id) { $new_cover_item = $child; break; } } item::make_album_cover($new_cover_item); } else { item::remove_album_cover($parent); } } $orig_name = $source->name; $source->parent_id = $target->id; $source->save(); if ($orig_name != $source->name) { switch ($source->type) { case "album": message::info(t("Album <b>%old_name</b> renamed to <b>%new_name</b> to avoid a conflict", array("old_name" => $orig_name, "new_name" => $source->name))); break; case "photo": message::info(t("Photo <b>%old_name</b> renamed to <b>%new_name</b> to avoid a conflict", array("old_name" => $orig_name, "new_name" => $source->name))); break; case "movie": message::info(t("Movie <b>%old_name</b> renamed to <b>%new_name</b> to avoid a conflict", array("old_name" => $orig_name, "new_name" => $source->name))); break; } } // If the target has no cover item, make this it. if ($target->album_cover_item_id == null) { item::make_album_cover($source); } }
public function delete() { $old = clone $this; module::event("item_before_delete", $this); $parent = $this->parent(); if ($parent->album_cover_item_id == $this->id) { item::remove_album_cover($parent); } $path = $this->file_path(); $resize_path = $this->resize_path(); $thumb_path = $this->thumb_path(); parent::delete(); if (is_dir($path)) { @dir::unlink($path); @dir::unlink(dirname($resize_path)); @dir::unlink(dirname($thumb_path)); } else { @unlink($path); @unlink($resize_path); @unlink($thumb_path); } module::event("item_deleted", $old); }
static function move($source, $target) { access::required("view", $source); access::required("view", $target); access::required("edit", $source); access::required("edit", $target); $parent = $source->parent(); if ($parent->album_cover_item_id == $source->id) { if ($parent->children_count() > 1) { foreach ($parent->children(2) as $child) { if ($child->id != $source->id) { $new_cover_item = $child; break; } } item::make_album_cover($new_cover_item); } else { item::remove_album_cover($parent); } } $source->parent_id = $target->id; // Moving may result in name or slug conflicts. If that happens, try up to 5 times to pick a // random name (or slug) to avoid the conflict. $orig_name = $source->name; $orig_name_filename = pathinfo($source->name, PATHINFO_FILENAME); $orig_name_extension = pathinfo($source->name, PATHINFO_EXTENSION); $orig_slug = $source->slug; for ($i = 0; $i < 5; $i++) { try { $source->save(); if ($orig_name != $source->name) { switch ($source->type) { case "album": message::info(t("Album <b>%old_name</b> renamed to <b>%new_name</b> to avoid a conflict", array("old_name" => $orig_name, "new_name" => $source->name))); break; case "photo": message::info(t("Photo <b>%old_name</b> renamed to <b>%new_name</b> to avoid a conflict", array("old_name" => $orig_name, "new_name" => $source->name))); break; case "movie": message::info(t("Movie <b>%old_name</b> renamed to <b>%new_name</b> to avoid a conflict", array("old_name" => $orig_name, "new_name" => $source->name))); break; } } break; } catch (ORM_Validation_Exception $e) { $rand = rand(10, 99); $errors = $e->validation->errors(); if (isset($errors["name"])) { $source->name = $orig_name_filename . "-{$rand}." . $orig_name_extension; unset($errors["name"]); } if (isset($errors["slug"])) { $source->slug = $orig_slug . "-{$rand}"; unset($errors["slug"]); } if ($errors) { // There were other validation issues-- we don't know how to handle those throw $e; } } } // If the target has no cover item, make this it. if ($target->album_cover_item_id == null) { item::make_album_cover($source); } }