From 0fc14c1bfc799615b092f29648ab3816f1a07052 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Fri, 26 Dec 2008 01:32:12 +0000 Subject: Simplify the watermark module. We can now upload, edit and delete one watermark. The UI is rough and we don't yet apply the watermark to images.. that's next. --- modules/watermark/controllers/admin_watermarks.php | 137 ++++++++++++--------- modules/watermark/helpers/watermark.php | 23 +++- modules/watermark/js/watermark.js | 125 ------------------- modules/watermark/models/watermark.php | 21 ---- modules/watermark/views/admin_watermarks.html.php | 41 ++++-- .../watermark/views/watermark_position.html.php | 16 --- 6 files changed, 126 insertions(+), 237 deletions(-) delete mode 100644 modules/watermark/js/watermark.js delete mode 100644 modules/watermark/models/watermark.php delete mode 100644 modules/watermark/views/watermark_position.html.php (limited to 'modules') diff --git a/modules/watermark/controllers/admin_watermarks.php b/modules/watermark/controllers/admin_watermarks.php index fa17ba8c..7a8eb217 100644 --- a/modules/watermark/controllers/admin_watermarks.php +++ b/modules/watermark/controllers/admin_watermarks.php @@ -19,81 +19,102 @@ */ class Admin_Watermarks_Controller extends Admin_Controller { public function index() { - $form = watermark::get_watermark_form(); - if (request::method() == "post" && $form->validate()) { - $file = $_POST["file"]; - $pathinfo = pathinfo($file); - $name = preg_replace("/uploadfile-[^-]+-(.*)/", '$1', $pathinfo["basename"]); - if (ORM::factory("watermark")->where("name", $name)->count_all() > 0) { - message::error(_("There is already a watermark with that name")); - } else if (!($image_info = getimagesize($file))) { - message::warning(_("An error occurred while saving this watermark")); - } else { - if (empty($pathinfo["extension"])) { - $name .= "." . image_type_to_extension($image_info[2]); - } - if (!rename($file, VARPATH . "modules/watermark/$name")) { - message::warning(_("An error occurred while saving this watermark")); - } else { - $watermark = ORM::factory("watermark"); - $watermark->name = $name; - $watermark->width = $image_info[0]; - $watermark->height = $image_info[1]; - $watermark->mime_type = $image_info["mime"]; - $watermark->save(); - - message::success(_("Watermark saved")); - url::redirect("admin/watermarks"); - } - } - @unlink($file); - } + $name = module::get_var("watermark", "name"); $view = new Admin_View("admin.html"); $view->content = new View("admin_watermarks.html"); - $view->content->watermarks = ORM::factory("watermark")->find_all(); - $view->content->form = watermark::get_watermark_form(); + if ($name) { + $view->content->name = $name; + $view->content->url = url::file("var/modules/watermark/$name"); + $view->content->width = $name; + $view->content->height = $name; + } print $view; } - public function edit($watermark_id) { + public function form_add() { + print watermark::get_add_form(); } - public function delete($watermark_id) { + public function form_edit() { + print watermark::get_edit_form(); } - public function get_form($user_id) { - try { - $path = module::get_var("watermark", "watermark_image_path"); - $view = new View("watermark_position.html"); + public function edit() { + $form = watermark::get_edit_form(); + if ($form->validate()) { + module::set_var("watermark", "position", $form->edit_watermark->position->value); + print json_encode( + array("result" => "success", + "location" => url::site("admin/watermarks"))); + } else { + print json_encode( + array("result" => "error", + "form" => $form->__toString())); + } + } - if (empty($path)) { - // @todo need to do something when there is no watermark - } + public function form_delete() { + print watermark::get_delete_form(); + } + + public function delete() { + $form = watermark::get_delete_form(); + if ($form->validate()) { + if ($name = module::get_var("watermark", "name")) { + @unlink(VARPATH . "modules/watermark/$name"); - $photo = ORM::factory("item") - ->where("type", "photo") - ->find_all(1, 0)->current(); + module::clear_var("watermark", "name"); + module::clear_var("watermark", "width"); + module::clear_var("watermark", "height"); + module::clear_var("watermark", "mime_type"); + module::clear_var("watermark", "position"); - // @todo determine what to do if water mark is not set - // @todo calculate the view sizes - $view->sample_image = $photo->resize_url(); - $scaleWidth = $photo->resize_width / $photo->width; - $scaleHeight = $photo->resize_height / $photo->height; - $scale = $scaleHeight < $scaleWidth ? $scaleHeight : $scaleWidth; + log::success("watermark", _("Deleted watermark")); + message::success(_("Watermark deleted")); + } + print json_encode( + array("result" => "success", + "location" => url::site("admin/watermarks"))); + } else { + print json_encode( + array("result" => "error", + "form" => $form->__toString())); + } + } - $imageinfo = getimagesize(VARPATH . $path); + public function upload() { + $form = watermark::get_add_form(); + if ($form->validate()) { + $file = $_POST["file"]; + $pathinfo = pathinfo($file); + // Forge prefixes files with "uploadfile-xxxxxxx" for uniqueness + $name = preg_replace("/uploadfile-[^-]+-(.*)/", '$1', $pathinfo["basename"]); - $view->watermark_height = $imageinfo[1] * $scale; - $view->watermark_width = $imageinfo[0] * $scale; - $view->watermark_image = url::abs_file("var/" . $path); + if (!($image_info = getimagesize($file)) || + !in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) { + message::error(_("Unable to identify this image file")); + @unlink($file); + return; + } - $current_position = module::get_var("watermark", "watermark_position"); - $view->watermark_position_form = watermark::get_watermark_postion_form($current_position); + rename($file, VARPATH . "modules/watermark/$name"); + module::set_var("watermark", "name", $name); + module::set_var("watermark", "width", $image_info[0]); + module::set_var("watermark", "height", $image_info[1]); + module::set_var("watermark", "mime_type", $image_info["mime"]); + module::set_var("watermark", "position", $form->add_watermark->position->value); + message::success(_("Watermark saved")); + url::redirect("admin/watermarks"); + @unlink($file); - print $view; - } catch (Exception $e) { - print $e; + print json_encode( + array("result" => "success", + "location" => url::site("admin/watermarks"))); + } else { + print json_encode( + array("result" => "error", + "form" => $form->__toString())); } } } \ No newline at end of file diff --git a/modules/watermark/helpers/watermark.php b/modules/watermark/helpers/watermark.php index 6d8da35a..c17bf0ab 100644 --- a/modules/watermark/helpers/watermark.php +++ b/modules/watermark/helpers/watermark.php @@ -18,22 +18,35 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class watermark_Core { - public static function get_watermark_form() { - $form = new Forge("admin/watermarks", "", "post"); + public static function get_add_form() { + $form = new Forge("admin/watermarks/upload", "", "post"); $group = $form->group("add_watermark")->label(_("Upload Watermark")); $group->upload("file")->label(_("Watermark"))->rules("allow[jpg,png,gif]|size[1MB]|required"); + $group->dropdown("position")->label(_("Watermark Position")) + ->options(array("northwest", "north", "northeast", + "west", "center", "east", + "southwest", "south", "southeast")) + ->selected("8"); $group->submit(_("Upload")); return $form; } - public static function get_watermark_position_form($position="southeast") { - $form = new Forge("admin/watermark/position", "", "post"); - $group = $form->group("watermark_position")->label(_("Update Position")); + public static function get_edit_form() { + $form = new Forge("admin/watermarks/edit", "", "post"); + $group = $form->group("edit_watermark")->label(_("Edit Watermark")); $group->dropdown("position")->label(_("Watermark Position")) ->options(array("northwest", "north", "northeast", "west", "center", "east", "southwest", "south", "southeast")) ->selected("8"); + $group->submit(_("Modify")); + return $form; + } + + public static function get_delete_form() { + $form = new Forge("admin/watermarks/delete", "", "post"); + $group = $form->group("delete_watermark")->label(_("Delete Watermark")); + $group->submit(_("Delete")); return $form; } } \ No newline at end of file diff --git a/modules/watermark/js/watermark.js b/modules/watermark/js/watermark.js deleted file mode 100644 index d90cdb80..00000000 --- a/modules/watermark/js/watermark.js +++ /dev/null @@ -1,125 +0,0 @@ -$("gUploadWatermarkForm").ready(function() { - ajaxify_watermark_add_form(); -}); - -function ajaxify_watermark_add_form() { - $("#gUploadWatermarkForm").ajaxForm({ - complete:function(xhr, statusText) { - $("#gUploadWatermarkForm").replaceWith(xhr.responseText); - ajaxify_watermark_add_form(); - } - }); -} - -var locations = { - areas: {}, - names: ["northwest", "north", "northeast", - "west", "center", "east", - "southwest", "south", "southeast"], - nameIndex: function(name) { - for (var row=0; row < 3; row++) { - for (var col=0; col < 3; col++) { - var index = row * 3 + col; - if (this.names[index] == name) { - return index; - } - } - } - }, - getArea: function(x, y) { - for (var row=0; row < 3; row++) { - for (var col=0; col < 3; col++) { - var name = this.names[row * 3 + col]; - var area = this.areas[name]; - var check = area.top <= y && y < area.bottom && area.left <= x && x < area.right; - if (check) { - return name; - } - } - } - }, - getDimension: function (area) { - return this.areas[area]; - } -}; - - - -locations.areas["northeast"] = {}; -locations.areas["north"] = {}; -locations.areas["northwest"] = {}; -locations.areas["east"] = {}; -locations.areas["center"] = {}; -locations.areas["west"] = {}; -locations.areas["southeast"] = {}; -locations.areas["south"] = {}; -locations.areas["southwest"] = {}; - -function calculateAreas(target) { - var cell_height = $(target).attr("offsetHeight") / 3; - var cell_width = $(target).attr("offsetWidth") / 3; - - var top = $(target).attr("offsetTop"); - for (var row=0; row < 3; row++) { - var left = $(target).attr("offsetLeft"); - for (var col=0; col < 3; col++) { - var name = locations.names[row * 3 + col]; - locations.areas[name] = { - top: top, - left: left, - right: left + cell_width, - bottom: top + cell_height}; - left += cell_width; - } - top += cell_height; - } -} - -function watermark_dialog_initialize() { - // Adjust the size of the dialog to accomodate the image content - var container = $("#gDialog").parent().parent(); - var container_height = $(container).attr("offsetHeight"); - var container_width = $(container).attr("offsetWidth"); - - var new_height = $("#gDialog").attr("offsetHeight") + - container.find("div.ui-dialog-titlebar").attr("offsetHeight") + - container.find("div.ui-dialog-buttonpane").attr("offsetHeight"); - var height = Math.max(new_height, container_height); - var width = Math.max($("#gDialog").attr("offsetWidth"), container_width); - container.css("height", height + "px"); - container.css("width", width + "px"); - container.css("top", ((document.height - height) / 2) + "px"); - container.css("left", ((document.width - width) / 2) + "px"); - - $("#gTargetImage").droppable({ - accept: "div", - greedy: true, - hoverClass: "droppable-hover", - drop: function(ev, ui) { - var areaname = locations.getArea(ui.position.left, ui.position.top); - positionWatermark(areaname); - $("#position").val(locations.nameIndex(areaname)); - } - }); - - $("#gWaterMark").draggable({ - helper: 'clone', - containment: "#gTargetImage", - opacity: .6 - }); - - $("#position").change(function() { - positionWatermark($("option:selected", this).text()); - }); - - calculateAreas($("#gTargetImage")); - var dropdown = $("#position"); - positionWatermark($("option:selected", dropdown).text()); -} - -function positionWatermark(area) { - var region = locations.getDimension(area); - - $("#gWaterMark").css("top", region.top + "px"); - $("#gWaterMark").css("left", region.left + "px"); -} diff --git a/modules/watermark/models/watermark.php b/modules/watermark/models/watermark.php deleted file mode 100644 index b03ce0b7..00000000 --- a/modules/watermark/models/watermark.php +++ /dev/null @@ -1,21 +0,0 @@ - - - - + + " + title="" + class="gDialogLink"> + +

+

+ +

+

+

+ +
+
+ " + title="" + class="gDialogLink"> + " + title="" + class="gDialogLink"> +
+

+ + diff --git a/modules/watermark/views/watermark_position.html.php b/modules/watermark/views/watermark_position.html.php deleted file mode 100644 index 6a8b1d71..00000000 --- a/modules/watermark/views/watermark_position.html.php +++ /dev/null @@ -1,16 +0,0 @@ - - -
-
- -
- -
-
-
- -
-
-- cgit v1.2.3