From c39d8dce4afa3f7ff9c8a5fd18bbba919de54d71 Mon Sep 17 00:00:00 2001 From: Karel Picman Date: Thu, 4 Jan 2018 14:45:17 +0100 Subject: [PATCH] Moving Issue to other project does not move attached documents in DMS #812 --- .../controllers/issues_controller_hooks.rb | 33 +++++++++++++++++-- lib/redmine_dmsf/patches/issue_patch.rb | 11 ++++--- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/lib/redmine_dmsf/hooks/controllers/issues_controller_hooks.rb b/lib/redmine_dmsf/hooks/controllers/issues_controller_hooks.rb index 8000f5bd..b6323cfc 100644 --- a/lib/redmine_dmsf/hooks/controllers/issues_controller_hooks.rb +++ b/lib/redmine_dmsf/hooks/controllers/issues_controller_hooks.rb @@ -49,7 +49,7 @@ module RedmineDmsf end def controller_issues_edit_after_save(context={}) - controller_issues_after_save(context) + controller_issues_after_save(context, true) end private @@ -58,7 +58,9 @@ module RedmineDmsf if context.is_a?(Hash) issue = context[:issue] params = context[:params] + # Save upload preferences DMS/Attachments User.current.pref.update_attribute :dmsf_attachments_upload_choice, params[:dmsf_attachments_upload_choice] + # Save attachments issue.save_dmsf_attachments(params[:dmsf_attachments]) issue.save_dmsf_links(params[:dmsf_links]) issue.save_dmsf_attachments_wfs(params[:dmsf_attachments_wfs], params[:dmsf_attachments]) @@ -66,10 +68,37 @@ module RedmineDmsf end end - def controller_issues_after_save(context) + def controller_issues_after_save(context, edit = false) if context.is_a?(Hash) issue = context[:issue] params = context[:params] + # Move existing attached documents if needed + if edit + project_id = params[:issue][:project_id].to_i + old_project_id = context[:project].id + system_folder = issue.system_folder(false, old_project_id) + if system_folder + # Change the title if the issue's subject changed + system_folder.title = "#{issue.id} - #{issue.subject}" + # Move system folders if needed + if system_folder.dmsf_folder + system_folder.dmsf_folder.project_id = project_id + system_folder.dmsf_folder.save! + end + system_folder.project_id = project_id + system_folder.save! + # Move documents + issue.dmsf_files.each do |dmsf_file| + dmsf_file.project_id = project_id + dmsf_file.save! + end + # Move links + issue.dmsf_links.each do | dmsf_link| + dmsf_link.project_id = project_id + dmsf_link.save! + end + end + end # Attach DMS documents uploaded_files = params[:dmsf_attachments] if uploaded_files && uploaded_files.is_a?(Hash) diff --git a/lib/redmine_dmsf/patches/issue_patch.rb b/lib/redmine_dmsf/patches/issue_patch.rb index 757de55f..836c1697 100644 --- a/lib/redmine_dmsf/patches/issue_patch.rb +++ b/lib/redmine_dmsf/patches/issue_patch.rb @@ -94,11 +94,12 @@ module RedmineDmsf @saved_dmsf_links_wfs || {} end - def system_folder(create = false) - parent = DmsfFolder.system.where(:project_id => self.project_id, :title => '.Issues').first + def system_folder(create = false, prj_id = nil) + prj_id ||= self.project_id + parent = DmsfFolder.system.where(:project_id => prj_id, :title => '.Issues').first if create && !parent parent = DmsfFolder.new - parent.project_id = self.project_id + parent.project_id = prj_id parent.title = '.Issues' parent.description = 'Documents assigned to issues' parent.user_id = User.anonymous.id @@ -107,11 +108,11 @@ module RedmineDmsf end if parent folder = DmsfFolder.system.where(["project_id = ? AND dmsf_folder_id = ? AND title LIKE '? - %'", - self.project_id, parent.id, self.id]).first + prj_id, parent.id, self.id]).first if create && !folder folder = DmsfFolder.new folder.dmsf_folder_id = parent.id - folder.project_id = self.project_id + folder.project_id = prj_id folder.title = "#{self.id} - #{self.subject}" folder.user_id = User.anonymous.id folder.system = true