static function user_deleted($old)
 {
     // Check for and delete existing Annotations linked to that user.
     $existingFaces = ORM::factory("items_user")->where("user_id", "=", $old->id)->find_all();
     if (count($existingFaces) > 0) {
         $onuserdelete = module::get_var("photoannotation", "onuserdelete", "0");
         if (module::get_var("photoannotation", "fullname", false)) {
             $new_tag_name = $old->display_name();
         } else {
             $new_tag_name = $old->name;
         }
         switch ($onuserdelete) {
             case "1":
                 //convert to tag
                 $tag = ORM::factory("tag")->where("name", "=", $new_tag_name)->find();
                 if (!$tag->loaded()) {
                     $tag->name = $new_tag_name;
                     $tag->count = 0;
                 }
                 foreach ($existingFaces as $existingFace) {
                     $item = ORM::factory("item")->where("id", "=", $existingFace->item_id)->find();
                     $tag->add($item);
                     $tag->count++;
                     $tag->save();
                     photoannotation::saveface($tag->id, $existingFace->item_id, $existingFace->x1, $existingFace->y1, $existingFace->x2, $existingFace->y2, $existingFace->description);
                 }
                 break;
             case "2":
                 //convert to note
                 foreach ($existingFaces as $existingFace) {
                     photoannotation::savenote($new_tag_name, $existingFace->item_id, $existingFace->x1, $existingFace->y1, $existingFace->x2, $existingFace->y2, $existingFace->description);
                 }
         }
         db::build()->delete("items_users")->where("user_id", "=", $old->id)->execute();
     }
     // Delete notification settings
     $notification_settings = ORM::factory("photoannotation_notification")->where("user_id", "=", $old->id)->find();
     if ($notification_settings->loaded()) {
         $notification_settings->delete();
     }
 }
Ejemplo n.º 2
0
 public function save($item_id)
 {
     // Prevent Cross Site Request Forgery
     access::verify_csrf();
     //Get form data
     $item = ORM::factory("item", $item_id);
     $annotate_id = $_POST["noteid"];
     $notetype = $_POST["notetype"];
     $str_y1 = $_POST["top"];
     $str_x1 = $_POST["left"];
     $str_y2 = $_POST["height"] + $str_y1;
     //Annotation uses area size, tagfaces uses positions
     $str_x2 = $_POST["width"] + $str_x1;
     //Annotation uses area size, tagfaces uses positions
     $item_title = $_POST["text"];
     $tag_data = $_POST["tagsList"];
     $user_id = "";
     $user_id = $_POST["userlist"];
     $description = $_POST["desc"];
     $error_noselection = t("Please select a person or tag or specify a title.");
     $redir_uri = url::abs_site("{$item->type}s/{$item->id}");
     //If this is a user then get the id
     if ($user_id != "") {
         $getuser = photoannotation::getuser($user_id);
         if (!$getuser->found) {
             json::reply(array("result" => "error", "message" => (string) t("Could not find anyone with the name %user.", array("user" => $user_id))));
             return;
         }
         if ($getuser->isguest) {
             json::reply(array("result" => "error", "message" => (string) t("You cannot create an annotation for the guest user.")));
             return;
         }
         $user_id = $getuser->user->id;
     }
     //Add tag to item, create tag if not exists
     if ($tag_data != "") {
         $tag = ORM::factory("tag")->where("name", "=", $tag_data)->find();
         if (!$tag->loaded()) {
             $tag->name = $tag_data;
             $tag->count = 0;
         }
         $tag->add($item);
         $tag->count++;
         $tag->save();
         $tag_data = $tag->id;
     } else {
         $tag_data = "";
     }
     //Save annotation
     if ($annotate_id == "new") {
         //This is a new annotation
         $annotate_id = -1;
         if ($user_id != "") {
             //Save user
             $new_id = photoannotation::saveuser($user_id, $item_id, $str_x1, $str_y1, $str_x2, $str_y2, $description);
             $dest_type = "user";
         } elseif ($tag_data != "") {
             //Save face
             $new_id = photoannotation::saveface($tag_data, $item_id, $str_x1, $str_y1, $str_x2, $str_y2, $description);
             $dest_type = "face";
         } elseif ($item_title != "") {
             //Save note
             $new_id = photoannotation::savenote($item_title, $item_id, $str_x1, $str_y1, $str_x2, $str_y2, $description);
             $dest_type = "note";
         } else {
             //Something's wrong
             json::reply(array("result" => "error", "message" => (string) $error_noselection));
             return;
         }
     } else {
         //This is an update to an existing annotation
         switch ($notetype) {
             case "user":
                 //the original annotation is a user
                 $updateduser = ORM::factory("items_user")->where("id", "=", $annotate_id)->find();
                 if ($user_id != "") {
                     //Conversion user -> user
                     $new_id = photoannotation::saveuser($user_id, $item_id, $str_x1, $str_y1, $str_x2, $str_y2, $description);
                     $dest_type = "user";
                 } elseif ($tag_data != "") {
                     //Conversion user -> face
                     $new_id = photoannotation::saveface($tag_data, $item_id, $str_x1, $str_y1, $str_x2, $str_y2, $description);
                     $dest_type = "face";
                     $updateduser->delete();
                     //delete old user
                 } elseif ($item_title != "") {
                     //Conversion user -> note
                     $new_id = photoannotation::savenote($item_title, $item_id, $str_x1, $str_y1, $str_x2, $str_y2, $description);
                     $dest_type = "note";
                     $updateduser->delete();
                     //delete old user
                 } else {
                     //Somethings wrong
                     json::reply(array("result" => "error", "message" => (string) $error_noselection));
                     return;
                 }
                 break;
             case "face":
                 //the original annotation is a face
                 $updatedface = ORM::factory("items_face")->where("id", "=", $annotate_id)->find();
                 if ($user_id != "") {
                     //Conversion face -> user
                     $new_id = photoannotation::saveuser($user_id, $item_id, $str_x1, $str_y1, $str_x2, $str_y2, $description);
                     $dest_type = "user";
                     $updatedface->delete();
                     //delete old face
                 } elseif ($tag_data != "") {
                     //Conversion face -> face
                     $new_id = photoannotation::saveface($tag_data, $item_id, $str_x1, $str_y1, $str_x2, $str_y2, $description, $annotate_id);
                     $dest_type = "face";
                 } elseif ($item_title != "") {
                     //Conversion face -> note
                     $new_id = photoannotation::savenote($item_title, $item_id, $str_x1, $str_y1, $str_x2, $str_y2, $description);
                     $dest_type = "note";
                     $updatedface->delete();
                     //delete old face
                 } else {
                     //Somethings wrong
                     json::reply(array("result" => "error", "message" => (string) $error_noselection));
                     return;
                 }
                 break;
             case "note":
                 //the original annotation is a note
                 $updatednote = ORM::factory("items_note")->where("id", "=", $annotate_id)->find();
                 if ($user_id != "") {
                     //Conversion note -> user
                     $new_id = photoannotation::saveuser($user_id, $item_id, $str_x1, $str_y1, $str_x2, $str_y2, $description);
                     $dest_type = "user";
                     $updatednote->delete();
                     //delete old note
                 } elseif ($tag_data != "") {
                     //Conversion note -> face
                     $new_id = photoannotation::saveface($tag_data, $item_id, $str_x1, $str_y1, $str_x2, $str_y2, $description);
                     $dest_type = "face";
                     $updatednote->delete();
                     //delete old note
                 } elseif ($item_title != "") {
                     //Conversion note -> note
                     $new_id = photoannotation::savenote($item_title, $item_id, $str_x1, $str_y1, $str_x2, $str_y2, $description, $annotate_id);
                     $dest_type = "note";
                 } else {
                     //Somethings wrong
                     json::reply(array("result" => "error", "message" => (string) $error_noselection));
                     return;
                 }
                 break;
             default:
                 json::reply(array("result" => "error", "message" => (string) $error_noselection));
                 return;
         }
     }
     $int_text = "";
     $editable = true;
     switch ($dest_type) {
         case "user":
             $fullname = module::get_var("photoannotation", "fullname", false);
             $int_text = $getuser->user->display_name() . " (" . $getuser->user->name . ")";
             if ($fullname) {
                 $note_text = $getuser->user->display_name();
             } else {
                 $note_text = $getuser->user->name;
             }
             $note_url = user_profile::url($getuser->user->id);
             break;
         case "face":
             $note_text = $tag->name;
             $note_url = $tag->url();
             break;
         case "note":
             $note_text = $item_title;
             $note_url = "";
             $editable = false;
     }
     if ($annotate_id == -1) {
         $annotation_id = "";
     } else {
         $annotation_id = "photoannotation-area-" . $notetype . "-" . $annotate_id;
     }
     $reply = array("result" => "success", "notetype" => (string) $dest_type, "description" => (string) $description, "height" => (int) $_POST["height"], "internaltext" => (string) $int_text, "left" => (int) $str_x1, "noteid" => (int) $new_id, "text" => (string) $note_text, "top" => (int) $str_y1, "url" => (string) $note_url, "width" => (int) $_POST["width"], "editable" => (bool) $editable, "annotationid" => (string) $annotation_id, "oldid" => (string) $annotate_id, "oldtype" => (string) $notetype);
     json::reply($reply);
 }