summaryrefslogtreecommitdiff
path: root/modules/keeporiginal
diff options
context:
space:
mode:
authorTristan Zur <tzur@webserver.ccwn.org>2015-06-20 23:56:17 +0200
committerTristan Zur <tzur@webserver.ccwn.org>2015-06-20 23:56:17 +0200
commit18a1d682ff18ee69d5c252b013a6a6935cd9b5fe (patch)
treeb1da62c8b7d03341a7b74b3fbb533e6391950be0 /modules/keeporiginal
parentb69c03794f80aa811f0613cf4b802619e7ecdbdd (diff)
New modules added
Diffstat (limited to 'modules/keeporiginal')
-rw-r--r--modules/keeporiginal/controllers/keeporiginal.php72
-rw-r--r--modules/keeporiginal/helpers/keeporiginal_event.php139
-rw-r--r--modules/keeporiginal/helpers/keeporiginal_installer.php25
-rw-r--r--modules/keeporiginal/module.info7
4 files changed, 243 insertions, 0 deletions
diff --git a/modules/keeporiginal/controllers/keeporiginal.php b/modules/keeporiginal/controllers/keeporiginal.php
new file mode 100644
index 0000000..da401bd
--- /dev/null
+++ b/modules/keeporiginal/controllers/keeporiginal.php
@@ -0,0 +1,72 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2013 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class keeporiginal_Controller extends Controller {
+
+ public function restore($id) {
+ // Allow the user to restore the original photo.
+
+ // Make sure the current user has suficient access to view and edit the item.
+ $item = ORM::factory("item", $id);
+ access::required("view", $item);
+ access::required("edit", $item);
+
+ // Figure out where the original was stashed at.
+ $original_image = VARPATH . "original/" . str_replace(VARPATH . "albums/", "", $item->file_path());
+
+ // 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());
+
+ // 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);
+
+ // 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());
+ }
+ } else {
+ // Display an error message if there is not an original photo.
+ message::error(t("Image restore failed!"));
+ url::redirect($item->url());
+ }
+ }
+}
diff --git a/modules/keeporiginal/helpers/keeporiginal_event.php b/modules/keeporiginal/helpers/keeporiginal_event.php
new file mode 100644
index 0000000..3b325f3
--- /dev/null
+++ b/modules/keeporiginal/helpers/keeporiginal_event.php
@@ -0,0 +1,139 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2013 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class keeporiginal_event_Core {
+ static function graphics_rotate($input_file, $output_file, $options) {
+ // Make a copy of the original fullsized image before rotating it.
+ keeporiginal_event_Core::_preserve($input_file);
+ }
+
+ static function _preserve($input_file) {
+ // If $input_file is located in VARPATH/albums/ then assume its a fullsize photo.
+ if (strncmp($input_file, VARPATH . "albums/", strlen(VARPATH . "albums/")) == 0) {
+ // 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 = preg_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;
+ 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);
+ }
+ }
+ if (!@copy($input_file, $original_image)) {
+ // If the copy failed, display an error message.
+ message::error(t("Your original image was not backed up!"));
+ }
+ }
+ }
+ }
+
+ static function item_before_delete($item) {
+ // If deleting a photo, make sure the original is deleted as well, if it exists.
+ if ($item->is_photo()) {
+ $original_file = VARPATH . "original/" . str_replace(VARPATH . "albums/", "", $item->file_path());
+ if (file_exists($original_file)) {
+ @unlink($original_file);
+ }
+ }
+
+ // When deleting an album, make sure its corresponding location in
+ // VARPATH/original/ is deleted as well, if it exists.
+ if ($item->is_album()) {
+ $original_file = VARPATH . "original/" . str_replace(VARPATH . "albums/", "", $item->file_path());
+ if (file_exists($original_file)) {
+ @dir::unlink($original_file);
+ }
+ }
+ }
+
+ static function item_updated($old, $new) {
+ // When updating an item, check and see if the file name is being changed.
+ // If so, check for and modify any corresponding file/folder in
+ // VARPATH/original/ as well.
+
+ if ($old->is_photo() || $old->is_album()) {
+ $data_file = $new->data_file;
+ if (isset($data_file)) {
+ keeporiginal_event_Core::_preserve($old->file_path());
+ }
+ if ($old->file_path() != $new->file_path()) {
+ $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);
+ }
+ }
+ }
+ }
+
+ 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)) {
+
+ // Make sure the new folder exists, create it if it doesn't.
+ $individual_dirs = split("[/\]", "original/" . str_replace(VARPATH . "albums/", "", $item->file_path()));
+ $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);
+ }
+ }
+
+ // Move the file to its new location.
+ // TODO: If the files have different extensions, then the old extension should be preserved.
+ @rename($old_original, $new_original);
+ }
+ }
+ }
+ }
+
+ static function site_menu($menu, $theme) {
+ // Create a menu option to restore the original photo.
+ if ($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)) {
+ $menu->get("options_menu")
+ ->append(Menu::factory("link")
+ ->id("restore")
+ ->label(t("Restore original"))
+ ->css_id("g-keep-originals-link")
+ ->url(url::site("keeporiginal/restore/" . $item->id)));
+ }
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/modules/keeporiginal/helpers/keeporiginal_installer.php b/modules/keeporiginal/helpers/keeporiginal_installer.php
new file mode 100644
index 0000000..d2f0d26
--- /dev/null
+++ b/modules/keeporiginal/helpers/keeporiginal_installer.php
@@ -0,0 +1,25 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2013 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class keeporiginal_installer {
+ static function install() {
+ @mkdir(VARPATH . "original");
+ module::set_version("keeporiginal", 1);
+ }
+}
diff --git a/modules/keeporiginal/module.info b/modules/keeporiginal/module.info
new file mode 100644
index 0000000..5c7b180
--- /dev/null
+++ b/modules/keeporiginal/module.info
@@ -0,0 +1,7 @@
+name = "Keep Original"
+description = "Make a copy of the original photo before rotating it."
+version = 1
+author_name = ""
+author_url = ""
+info_url = "http://codex.gallery2.org/Gallery3:Modules:keeporiginal"
+discuss_url = "http://gallery.menalto.com/forum_module_keeporiginal"