summaryrefslogtreecommitdiff
path: root/modules/watermark/controllers
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2008-12-26 01:32:12 +0000
committerBharat Mediratta <bharat@menalto.com>2008-12-26 01:32:12 +0000
commit0fc14c1bfc799615b092f29648ab3816f1a07052 (patch)
treeb21968c4a00845cdb8b4835d4c011f4c4b03d4f1 /modules/watermark/controllers
parenteb35afc987a4e8b2772d10ac2bd63b1a3a091239 (diff)
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.
Diffstat (limited to 'modules/watermark/controllers')
-rw-r--r--modules/watermark/controllers/admin_watermarks.php137
1 files changed, 79 insertions, 58 deletions
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