diff --git a/app/assets/javascripts/posts.js b/app/assets/javascripts/posts.js index eca56795b..39b86b027 100644 --- a/app/assets/javascripts/posts.js +++ b/app/assets/javascripts/posts.js @@ -133,6 +133,29 @@ $("#post_tag_string").trigger("focus"); e.preventDefault(); }); + + $("#copy-notes").click(function(e) { + var current_post_id = $("meta[name=post-id]").attr("content"); + var other_post_id = prompt("Enter the ID of the post to copy all notes to:"); + + if (other_post_id !== null) { + $.ajax("/posts/" + current_post_id + "/copy_notes", { + type: "PUT", + data: { + other_post_id: other_post_id + }, + complete: function(data) { + if (data.status === 200) { + Danbooru.notice("Successfully copied notes to post #" + other_post_id + ""); + } else { + Danbooru.error("There was an error copying notes to post #" + other_post_id + ""); + } + }, + }); + } + + e.preventDefault(); + }); } Danbooru.Post.initialize_post_relationship_previews = function() { diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index e63f79466..5ac7ba684 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -1,5 +1,6 @@ class PostsController < ApplicationController before_filter :member_only, :except => [:show, :show_seq, :index] + before_filter :builder_only, :only => [:copy_notes] after_filter :save_recent_tags, :only => [:update] respond_to :html, :xml, :json rescue_from PostSets::SearchError, :with => :rescue_exception @@ -76,6 +77,13 @@ class PostsController < ApplicationController end end + def copy_notes + @post = Post.find(params[:id]) + @other_post = Post.find(params[:other_post_id].to_i) + @post.copy_notes_to(@other_post) + render :nothing => true + end + private def tag_query params[:tags] || (params[:post] && params[:post][:tags]) diff --git a/app/models/note.rb b/app/models/note.rb index 80c0a1ec4..ccbec9de2 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -174,6 +174,20 @@ class Note < ActiveRecord::Base save! end + def copy_to(new_post) + new_note = dup + new_note.post_id = new_post.id + + width_ratio = new_post.image_width.to_f / post.image_width + height_ratio = new_post.image_height.to_f / post.image_height + new_note.x = x * width_ratio + new_note.y = y * height_ratio + new_note.width = width * width_ratio + new_note.height = height * height_ratio + + new_note.save + end + def self.undo_changes_by_user(user_id) transaction do notes = Note.joins(:versions).where(["note_versions.updater_id = ?", user_id]).select("DISTINCT notes.*").all diff --git a/app/models/post.rb b/app/models/post.rb index 5640f634c..6990597b2 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -900,6 +900,12 @@ class Post < ActiveRecord::Base def last_noted_at_as_integer last_noted_at.to_i end + + def copy_notes_to(other_post) + notes.each do |note| + note.copy_to(other_post) + end + end end module ApiMethods diff --git a/app/views/posts/partials/show/_options.html.erb b/app/views/posts/partials/show/_options.html.erb index df37d39b6..d26e25178 100644 --- a/app/views/posts/partials/show/_options.html.erb +++ b/app/views/posts/partials/show/_options.html.erb @@ -9,6 +9,9 @@ <% else %>