2009-08-14 03:53:00 +00:00
|
|
|
<?php defined("SYSPATH") or die("No direct script access.");
|
|
|
|
/**
|
|
|
|
* Gallery - a web based photo album viewer and editor
|
|
|
|
* Copyright (C) 2000-2009 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 tagfaces_Controller extends Controller {
|
|
|
|
public function drawfaces($id) {
|
|
|
|
// Generate the page that allows the user to draw boxes over a photo.
|
2009-08-16 04:18:49 +00:00
|
|
|
|
2009-08-14 03:53:00 +00:00
|
|
|
// Make sure user has access to view and edit the photo.
|
|
|
|
$item = ORM::factory("item", $id);
|
|
|
|
access::required("view", $item);
|
|
|
|
access::required("edit", $item);
|
2009-08-16 04:18:49 +00:00
|
|
|
|
2009-08-14 03:53:00 +00:00
|
|
|
// Create the page.
|
|
|
|
$template = new Theme_View("page.html", "drawfaces");
|
|
|
|
$template->set_global("item_id", $id);
|
|
|
|
$template->set_global("page_title", t("Draw Faces"));
|
|
|
|
$template->set_global("page_type", "photoface");
|
|
|
|
$template->content = new View("drawfaces.html");
|
|
|
|
$template->content->title = t("Tag Faces");
|
|
|
|
$template->content->form = $this->_get_faces_form($id);
|
|
|
|
$template->content->delete_form = $this->_get_delfaces_form($id);
|
|
|
|
|
|
|
|
// Display the page.
|
|
|
|
print $template;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delface() {
|
|
|
|
// Delete the specified face data from the photo.
|
|
|
|
|
|
|
|
// Prevent Cross Site Request Forgery
|
|
|
|
access::verify_csrf();
|
|
|
|
|
|
|
|
// Convert submitted data to local variables.
|
|
|
|
$tag_data = Input::instance()->post("facesList");
|
|
|
|
$item_data = Input::instance()->post("item_id");
|
|
|
|
|
|
|
|
// If the user didn't select a tag, display and error and abort.
|
|
|
|
if (count($tag_data) == 0) {
|
|
|
|
message::error(t("Please select a tag."));
|
|
|
|
url::redirect("tagfaces/drawfaces/$item_data");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the face(s) from the database.
|
|
|
|
foreach ($tag_data as $one_tag) {
|
|
|
|
ORM::factory("items_face")
|
|
|
|
->where("id", $one_tag)
|
|
|
|
->delete_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display a success message.
|
|
|
|
if (count($tag_data) == 1) {
|
|
|
|
message::success(t("One face deleted."));
|
|
|
|
} else {
|
|
|
|
message::success(count($tag_data) . t(" faces deleted."));
|
|
|
|
}
|
|
|
|
url::redirect("tagfaces/drawfaces/$item_data");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function saveface() {
|
|
|
|
// Save the face coordinates to the specified tag.
|
|
|
|
|
|
|
|
// Prevent Cross Site Request Forgery
|
|
|
|
access::verify_csrf();
|
|
|
|
|
|
|
|
// Convert submitted data to local variables.
|
|
|
|
$tag_data = Input::instance()->post("tagsList");
|
2009-08-28 01:29:08 +00:00
|
|
|
$str_face_title = str_replace("'", "\'", Input::instance()->post("face_title"));
|
|
|
|
$str_face_description = str_replace("'", "\'", Input::instance()->post("face_description"));
|
2009-08-14 03:53:00 +00:00
|
|
|
$item_data = Input::instance()->post("item_id");
|
|
|
|
$str_x1 = Input::instance()->post("x");
|
|
|
|
$str_y1 = Input::instance()->post("y");
|
|
|
|
$str_x2 = Input::instance()->post("x2");
|
|
|
|
$str_y2 = Input::instance()->post("y2");
|
2009-08-28 01:29:08 +00:00
|
|
|
|
2009-08-14 03:53:00 +00:00
|
|
|
// If the user didn't select a face, display an error and abort.
|
|
|
|
if (($str_x1 == "") || ($str_x2 == "") || ($str_y1 == "") || ($str_y2 == "")) {
|
|
|
|
message::error(t("Please select a face."));
|
|
|
|
url::redirect("tagfaces/drawfaces/$item_data");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-28 01:29:08 +00:00
|
|
|
// Decide if we are saving a face or a note.
|
|
|
|
if ($tag_data == -1) {
|
|
|
|
// Make sure there's a title.
|
|
|
|
if ($str_face_title == "") {
|
|
|
|
message::error(t("Please select a Tag or specify a Title."));
|
|
|
|
url::redirect("tagfaces/drawfaces/$item_data");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save a new Note to the database.
|
|
|
|
$newnote = ORM::factory("items_note");
|
|
|
|
$newnote->item_id = $item_data;
|
|
|
|
$newnote->x1 = $str_x1;
|
|
|
|
$newnote->y1 = $str_y1;
|
|
|
|
$newnote->x2 = $str_x2;
|
|
|
|
$newnote->y2 = $str_y2;
|
|
|
|
$newnote->title = $str_face_title;
|
|
|
|
$newnote->description = $str_face_description;
|
|
|
|
$newnote->save();
|
|
|
|
|
2009-08-14 03:53:00 +00:00
|
|
|
} else {
|
2009-08-28 01:29:08 +00:00
|
|
|
// Check to see if the tag already has a face associated with it.
|
|
|
|
$existingFace = ORM::factory("items_face")
|
|
|
|
->where("tag_id", $tag_data)
|
|
|
|
->where("item_id", $item_data)
|
|
|
|
->find_all();
|
|
|
|
|
|
|
|
if (count($existingFace) == 0) {
|
|
|
|
// Save the new face to the database.
|
|
|
|
$newface = ORM::factory("items_face");
|
|
|
|
$newface->tag_id = $tag_data;
|
|
|
|
$newface->item_id = $item_data;
|
|
|
|
$newface->x1 = $str_x1;
|
|
|
|
$newface->y1 = $str_y1;
|
|
|
|
$newface->x2 = $str_x2;
|
|
|
|
$newface->y2 = $str_y2;
|
|
|
|
$newface->description = $str_face_description;
|
|
|
|
$newface->save();
|
|
|
|
} else {
|
|
|
|
// Update the coordinates of an existing face.
|
|
|
|
$updatedFace = ORM::factory("items_face", $existingFace[0]->id);
|
|
|
|
$updatedFace->x1 = $str_x1;
|
|
|
|
$updatedFace->y1 = $str_y1;
|
|
|
|
$updatedFace->x2 = $str_x2;
|
|
|
|
$updatedFace->y2 = $str_y2;
|
|
|
|
$updatedFace->description = $str_face_description;
|
|
|
|
$updatedFace->save();
|
|
|
|
}
|
2009-08-14 03:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect back to the main screen and display a "success" message.
|
|
|
|
message::success(t("Face saved."));
|
|
|
|
url::redirect("tagfaces/drawfaces/$item_data");
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _get_faces_form($id) {
|
|
|
|
// Generate the form that allows the user to select a tag to
|
|
|
|
// save the face too. Also displays the coordinates of the face
|
|
|
|
// and the "Save face" button.
|
|
|
|
|
|
|
|
// Make a new Form.
|
|
|
|
$form = new Forge("tagfaces/saveface", "", "post",
|
2009-10-04 20:40:59 +00:00
|
|
|
array("id" => "g-tag-faces-form"));
|
2009-08-14 03:53:00 +00:00
|
|
|
|
|
|
|
// Create an array of all the tags for the current item.
|
|
|
|
$all_tags = ORM::factory("tag")
|
|
|
|
->join("items_tags", "tags.id", "items_tags.tag_id")
|
|
|
|
->where("items_tags.item_id", $id)
|
|
|
|
->find_all();
|
|
|
|
|
|
|
|
// Generate an array of tags to use as checkboxes.
|
|
|
|
$array_tags = "";
|
2009-08-28 01:29:08 +00:00
|
|
|
$array_tags[-1] = t("No Tag");
|
2009-08-14 03:53:00 +00:00
|
|
|
foreach ($all_tags as $oneTag) {
|
2009-08-28 01:29:08 +00:00
|
|
|
$array_tags[$oneTag->id] = $oneTag->name;
|
2009-08-14 03:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make a checklist of tags on the form.
|
2009-08-28 01:29:08 +00:00
|
|
|
$tags_group = $form->group("FaceTag")
|
|
|
|
->label(t("Select a tag or enter in a title:"));
|
|
|
|
|
|
|
|
$tags_group->dropdown('tagsList')
|
|
|
|
->label(t("Select a tag:"))
|
|
|
|
->options($array_tags);
|
|
|
|
|
|
|
|
$tags_group->input("face_title")
|
|
|
|
->label(t("Title"));
|
|
|
|
|
|
|
|
$tags_description = $form->group("TagsDescription")
|
|
|
|
->label(t("Description (optional):"));
|
|
|
|
$tags_description->input("face_description");
|
|
|
|
|
2009-08-14 03:53:00 +00:00
|
|
|
// Generate input boxes to hold the coordinates of the face.
|
|
|
|
$coordinates_group = $form->group("FaceCoordinates")
|
|
|
|
->label(t("Coordinates:"));
|
|
|
|
$coordinates_group->input("x")
|
|
|
|
->label(t("X1"));
|
|
|
|
$coordinates_group->input("y")
|
|
|
|
->label(t("Y1"));
|
|
|
|
$coordinates_group->input("x2")
|
|
|
|
->label(t("X2"));
|
|
|
|
$coordinates_group->input("y2")
|
|
|
|
->label(t("Y2"));
|
|
|
|
|
|
|
|
// Add the id# of the photo and a save button to the form.
|
|
|
|
$form->hidden("item_id")->value($id);
|
|
|
|
$form->submit("SaveFace")->value(t("Save face"));
|
|
|
|
|
|
|
|
// Return the newly generated form.
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _get_delfaces_form($id) {
|
|
|
|
// Generate a form to allow the user to remove face data
|
|
|
|
// from a photo.
|
|
|
|
|
|
|
|
// Make a new Form.
|
|
|
|
$form = new Forge("tagfaces/delface", "", "post",
|
2009-10-04 20:40:59 +00:00
|
|
|
array("id" => "g-tag-del-faces-form"));
|
2009-08-14 03:53:00 +00:00
|
|
|
|
|
|
|
// Create an array of all the tags that already have faces.
|
|
|
|
$existing_faces = ORM::factory("items_face")
|
|
|
|
->where("item_id", $id)
|
|
|
|
->find_all();
|
|
|
|
|
|
|
|
// turn the $existing_faces array into an array that can be used
|
|
|
|
// for a checklist.
|
|
|
|
$array_faces = "";
|
|
|
|
foreach ($existing_faces as $oneFace) {
|
|
|
|
$array_faces[$oneFace->id] = array(ORM::factory("tag",
|
|
|
|
$oneFace->tag_id)->name, false);
|
|
|
|
}
|
|
|
|
|
2009-09-01 05:33:54 +00:00
|
|
|
if ($array_faces) {
|
|
|
|
// Add a checklist to the form.
|
|
|
|
$tags_group = $form->group("ExistingFaces")
|
|
|
|
->label(t("Tags with faces:"));
|
|
|
|
$tags_group->checklist("facesList")
|
|
|
|
->options($array_faces)
|
|
|
|
->label(t("Select the tag(s) that correspond(s) to the face(s) you wish to delete:"));
|
|
|
|
}
|
2009-08-14 03:53:00 +00:00
|
|
|
|
|
|
|
// Add the id# of the photo and a delete button to the form.
|
|
|
|
$form->hidden("item_id")->value($id);
|
|
|
|
$form->submit("DeleteFace")->value(t("Delete face(s)"));
|
|
|
|
|
|
|
|
// Return the newly generated form.
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
}
|