public static function saveuser($user_id, $item_id, $str_x1, $str_y1, $str_x2, $str_y2, $description)
 {
     //Since we are associating a user we will remove any old annotation of this user on this photo
     $item_old_users = ORM::factory("items_user")->where("user_id", "=", $user_id)->where("item_id", "=", $item_id)->find_all();
     if (count($item_old_users) > 1) {
         foreach ($item_old_users as $item_old_user) {
             $item_old_user->delete();
         }
         $item_user = ORM::factory("items_user");
     } elseif (count($item_old_users) == 1) {
         $item_user = ORM::factory("items_user", $item_old_users[0]->id);
     } else {
         $item_user = ORM::factory("items_user");
         photoannotation::send_notifications($user_id, $item_id, "newtag");
     }
     $item_user->user_id = $user_id;
     $item_user->item_id = $item_id;
     $item_user->x1 = $str_x1;
     $item_user->y1 = $str_y1;
     $item_user->x2 = $str_x2;
     $item_user->y2 = $str_y2;
     $item_user->description = $description;
     $item_user->save();
     return $item_user->id;
 }
 static function get($block_id, $theme)
 {
     $block = "";
     if (!identity::active_user()->guest || module::get_var("photoannotation", "allowguestsearch", false)) {
         switch ($block_id) {
             case "photoannotation":
                 $block = new Block();
                 $block->css_id = "g-photoannotation";
                 $block->title = t("People");
                 $block->content = new View("photoannotation_block.html");
                 $block->content->cloud = photoannotation::cloud(30);
                 $block->content->form = photoannotation::get_user_search_form("g-user-cloud-form");
         }
     }
     return $block;
 }
 public function converthandler()
 {
     access::verify_csrf();
     $form = $this->_get_converter_form();
     if ($form->validate()) {
         //Load the source tag
         $sourcetag = ORM::factory("tag", $form->sourcetag->value);
         if (!$sourcetag->loaded()) {
             message::error(t("The specified tag could not be found"));
             url::redirect("admin/photoannotation/converter");
         }
         //Load the target user
         $targetuser = ORM::factory("user", $form->targetuser->value);
         if (!$targetuser->loaded()) {
             message::error(t("The specified person could not be found"));
             url::redirect("admin/photoannotation/converter");
         }
         //Load all existing tag annotations
         $tag_annotations = ORM::factory("items_face")->where("tag_id", "=", $sourcetag->id)->find_all();
         //Disable user notifications so that users don't get flooded with mails
         $old_notification_setting = module::get_var("photoannotation", "nonotifications", false);
         module::set_var("photoannotation", "nonotifications", true, true);
         foreach ($tag_annotations as $tag_annotation) {
             photoannotation::saveuser($targetuser->id, $tag_annotation->item_id, $tag_annotation->x1, $tag_annotation->y1, $tag_annotation->x2, $tag_annotation->y2, $tag_annotation->description);
             //Delete the old annotation
             $tag_annotation->delete();
         }
         //Remove and delete old tag
         if ($form->deletetag->value) {
             $this->_remove_tag($sourcetag, true);
         } elseif ($form->removetag->value) {
             $this->_remove_tag($sourcetag, false);
         }
         module::set_var("photoannotation", "nonotifications", $old_notification_setting, true);
         message::success(t("%count tag annotations (%tagname) have been converted to user annotations (%username)", array("count" => count($tag_annotations), "tagname" => $sourcetag->name, "username" => $targetuser->display_name())));
         url::redirect("admin/photoannotation/converter");
     }
     print $this->_get_converter_view($form);
 }
</th>
            <td colspan="2"><?php 
        echo photoannotation::annotation_count($user->id);
        ?>
</td>
          </tr>
          <?php 
        if (module::is_active("comment")) {
            ?>
          <tr>
            <th style="width: 20%"><?php 
            echo t("Comments");
            ?>
</th>
            <td colspan="2"><?php 
            echo photoannotation::comment_count($user->id);
            ?>
</td>
          </tr>
          <?php 
        }
        ?>
        </tbody></table>
    </div>
  </div>
  <?php 
    }
    ?>
  <?php 
    echo $paginator;
    ?>
 static function comment_updated($comment)
 {
     //Check if there are any user annotations on the photo and send notification if applicable
     $item_users = ORM::factory("items_user")->where("item_id", "=", $comment->item_id)->find_all();
     if (count($item_users) > 0) {
         foreach ($item_users as $item_user) {
             //Don't send if the commenter is the user to be notified
             if ($comment->author_id != $item_user->user_id && module::is_active("notification")) {
                 photoannotation::send_notifications($item_user->user_id, $comment->item_id, "updatedcomment");
             }
         }
     }
 }
 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);
 }