Better error handling and support for moving items to different parent albums.
Signed-off-by: Bharat Mediratta <bharat@menalto.com>
This commit is contained in:
parent
6471ac4dd6
commit
b73666e77d
@ -32,31 +32,41 @@ class keeporiginal_Controller extends Controller {
|
||||
|
||||
// Make sure the current item is a photo and that an original exists.
|
||||
if ($item->is_photo() && file_exists($original_image)) {
|
||||
// Delete the modified version of the photo.
|
||||
@unlink($item->file_path());
|
||||
|
||||
// Delete the modified version and move the original over in place of it.
|
||||
unlink($item->file_path());
|
||||
rename($original_image, $item->file_path());
|
||||
// Copy the original image back over, display an error message if the copy fails.
|
||||
if (@rename($original_image, $item->file_path())) {
|
||||
// Re-generate the items resize and thumbnail.
|
||||
$item_data = model_cache::get("item", $id);
|
||||
$item_data->resize_dirty= 1;
|
||||
$item_data->thumb_dirty= 1;
|
||||
$item_data->save();
|
||||
graphics::generate($item_data);
|
||||
|
||||
// Re-generate the items resize and thumbnail.
|
||||
$item_data = model_cache::get("item", $id);
|
||||
$item_data->resize_dirty= 1;
|
||||
$item_data->thumb_dirty= 1;
|
||||
$item_data->save();
|
||||
graphics::generate($item_data);
|
||||
|
||||
// If the item is the thumbnail for the parent album,
|
||||
// fix the parent's thumbnail as well.
|
||||
$parent = $item_data->parent();
|
||||
if ($parent->album_cover_item_id == $item_data->id) {
|
||||
copy($item_data->thumb_path(), $parent->thumb_path());
|
||||
$parent->thumb_width = $item_data->thumb_width;
|
||||
$parent->thumb_height = $item_data->thumb_height;
|
||||
$parent->save();
|
||||
// If the item is the thumbnail for the parent album,
|
||||
// fix the parent's thumbnail as well.
|
||||
$parent = $item_data->parent();
|
||||
if ($parent->album_cover_item_id == $item_data->id) {
|
||||
copy($item_data->thumb_path(), $parent->thumb_path());
|
||||
$parent->thumb_width = $item_data->thumb_width;
|
||||
$parent->thumb_height = $item_data->thumb_height;
|
||||
$parent->save();
|
||||
}
|
||||
|
||||
// Display a success message and redirect to the items page.
|
||||
message::success(t("Your original image has been restored."));
|
||||
url::redirect($item->url());
|
||||
|
||||
} else {
|
||||
// Display an error message if the copy failed.
|
||||
message::error(t("Image restore failed!"));
|
||||
url::redirect($item->url());
|
||||
}
|
||||
|
||||
// Display a success message and redirect to the items page.
|
||||
message::success(t("Your Original Image Has Been Restored."));
|
||||
url::redirect($item->url());
|
||||
} else {
|
||||
// Display an error message if there is not an original photo.
|
||||
message::error(t("Image restore failed!"));
|
||||
url::redirect($item->url());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,19 +26,22 @@ class keeporiginal_event_Core {
|
||||
// Figure out where the original copy should be stashed at.
|
||||
$temp_path = str_replace(VARPATH . "albums/", "", $input_file);
|
||||
$original_image = VARPATH . "original/" . $temp_path;
|
||||
$individual_dirs = split("[/\]", $temp_path);
|
||||
$individual_dirs = split("[/\]", "original/" . $temp_path);
|
||||
// If any original file does not already exist, then create a folder structure
|
||||
// similar to that found in VARPATH/albums/ and copy the photo over before
|
||||
// rotating it.
|
||||
if (!file_exists($original_image)) {
|
||||
$new_img_path = VARPATH . "original/";
|
||||
$new_img_path = VARPATH;
|
||||
for($i = 0; $i < count($individual_dirs)-1; $i++) {
|
||||
$new_img_path = $new_img_path . "/" . $individual_dirs[$i];
|
||||
if(!file_exists($new_img_path)) {
|
||||
@mkdir($new_img_path);
|
||||
}
|
||||
}
|
||||
copy($input_file, $original_image);
|
||||
if (!@copy($input_file, $original_image)) {
|
||||
// If the copy failed, display an error message.
|
||||
message::error(t("Your original image was not backed up!"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -48,7 +51,7 @@ class keeporiginal_event_Core {
|
||||
if ($item->is_photo()) {
|
||||
$original_file = VARPATH . "original/" . str_replace(VARPATH . "albums/", "", $item->file_path());
|
||||
if (file_exists($original_file)) {
|
||||
unlink($original_file);
|
||||
@unlink($original_file);
|
||||
}
|
||||
}
|
||||
|
||||
@ -71,24 +74,40 @@ class keeporiginal_event_Core {
|
||||
$old_original = VARPATH . "original/" . str_replace(VARPATH . "albums/", "", $old->file_path());
|
||||
$new_original = VARPATH . "original/" . str_replace(VARPATH . "albums/", "", $new->file_path());
|
||||
if (file_exists($old_original)) {
|
||||
rename($old_original, $new_original);
|
||||
@rename($old_original, $new_original);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static function site_menu($menu, $theme) {
|
||||
static function item_moved($item, $old_parent) {
|
||||
// When moving an item, check and see if a corresponding file exists
|
||||
// in VARPATH/original/. If so, move that item to a similar directory
|
||||
// in original as well.
|
||||
if ($item->is_photo() || $item->is_album()) {
|
||||
$old_item_path = $old_parent->file_path() . "/" . $item->name;
|
||||
if ($item->file_path() != $old_item_path) {
|
||||
$old_original = VARPATH . "original/" . str_replace(VARPATH . "albums/", "", $old_item_path);
|
||||
$new_original = VARPATH . "original/" . str_replace(VARPATH . "albums/", "", $item->file_path());
|
||||
if (file_exists($old_original)) {
|
||||
@rename($old_original, $new_original);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static function site_menu($menu, $theme) {
|
||||
// Create a menu option to restore the original photo.
|
||||
$item = $theme->item();
|
||||
|
||||
if ((access::can("view", $item)) && (access::can("edit", $item))) {
|
||||
$original_image = VARPATH . "original/" . str_replace(VARPATH . "albums/", "", $item->file_path());
|
||||
|
||||
if ($item->is_photo() && file_exists($original_image)) {
|
||||
if ($item->is_photo() && file_exists($original_image)) {
|
||||
$menu->get("options_menu")
|
||||
->append(Menu::factory("link")
|
||||
->id("restore")
|
||||
->label("Restore Original")
|
||||
->label("Restore original")
|
||||
->css_id("gKeepOriginalLink")
|
||||
->url(url::site("keeporiginal/restore/" . $item->id)));
|
||||
}
|
||||
|
Reference in New Issue
Block a user