From 598989f535e86a178005eceb6c5ed474da833ad2 Mon Sep 17 00:00:00 2001
From: Karel Picman
Date: Wed, 29 Nov 2017 14:21:36 +0100
Subject: [PATCH] Issue DMS attachments
---
lib/redmine_dmsf.rb | 5 +-
.../easy_crm_cases_controller_hooks.rb | 119 ------------------
.../hooks/views/issue_view_hooks.rb | 12 +-
.../easy_crm_cases_controller_patch.rb | 103 +++++++++++++++
.../patches/user_preference_patch.rb | 5 +-
5 files changed, 113 insertions(+), 131 deletions(-)
delete mode 100644 lib/redmine_dmsf/hooks/controllers/easy_crm_cases_controller_hooks.rb
create mode 100644 lib/redmine_dmsf/patches/easy_crm_cases_controller_patch.rb
diff --git a/lib/redmine_dmsf.rb b/lib/redmine_dmsf.rb
index dc705511..8612ffc4 100644
--- a/lib/redmine_dmsf.rb
+++ b/lib/redmine_dmsf.rb
@@ -38,6 +38,7 @@ require 'redmine_dmsf/patches/role_patch'
if defined?(EasyExtensions)
require 'redmine_dmsf/patches/easy_crm_case_patch'
require 'redmine_dmsf/patches/attachable_patch'
+ require 'redmine_dmsf/patches/easy_crm_cases_controller_patch.rb'
end
# Load up classes that make up our WebDAV solution ontop of DAV4Rack
@@ -70,9 +71,5 @@ require 'redmine_dmsf/hooks/views/search_view_hooks'
require 'redmine_dmsf/hooks/helpers/issues_helper_hooks'
require 'redmine_dmsf/hooks/helpers/search_helper_hooks'
-if defined?(EasyExtensions)
- require 'redmine_dmsf/hooks/controllers/easy_crm_cases_controller_hooks'
-end
-
# Macros
require 'redmine_dmsf/macros'
diff --git a/lib/redmine_dmsf/hooks/controllers/easy_crm_cases_controller_hooks.rb b/lib/redmine_dmsf/hooks/controllers/easy_crm_cases_controller_hooks.rb
deleted file mode 100644
index 048b0be6..00000000
--- a/lib/redmine_dmsf/hooks/controllers/easy_crm_cases_controller_hooks.rb
+++ /dev/null
@@ -1,119 +0,0 @@
-# encoding: utf-8
-#
-# Redmine plugin for Document Management System "Features"
-#
-# Copyright (C) 2011-17 Karel Pičman
-#
-# 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.
-
-module RedmineDmsf
- module Hooks
- include Redmine::Hook
-
- class ControllerEasyCrmCasesHook < RedmineDmsf::Hooks::Listener
-
- def controller_easy_crm_cases_before_save(context={})
- if context.is_a?(Hash)
- easy_crm_case = context[:easy_crm_case]
- params = context[:params]
- easy_crm_case.save_dmsf_attachments(params[:dmsf_attachments])
- easy_crm_case.save_dmsf_links(params[:dmsf_links])
- easy_crm_case.save_dmsf_attachments_wfs(params[:dmsf_attachments_wfs], params[:dmsf_attachments])
- easy_crm_case.save_dmsf_links_wfs(params[:dmsf_links_wfs])
- end
- end
-
- def controller_easy_crm_cases_after_save(context={})
- easy_crm_cases_after_save(context)
- # Copy documents from the source issue
- if context.is_a?(Hash)
- easy_crm_case = context[:easy_crm_case]
- params = context[:params]
- copied_from = EasyCrmCase.find_by_id(params[:copy_from]) if params[:copy_from].present?
- # Save documents
- if copied_from
- copied_from.dmsf_files.each do |dmsf_file|
- dmsf_file.copy_to(easy_crm_case.project, easy_crm_cases.system_folder(true))
- end
- end
- end
- end
-
- private
-
- def easy_crm_cases_after_save(context)
- if context.is_a?(Hash)
- easy_crm_case = context[:easy_crm_case]
- params = context[:params]
- # Attach DMS documents
- uploaded_files = params[:dmsf_attachments]
- if uploaded_files && uploaded_files.is_a?(Hash)
- system_folder = easy_crm_case.system_folder(true)
- uploaded_files.each do |key, uploaded_file|
- upload = DmsfUpload.create_from_uploaded_attachment(easy_crm_case.project, system_folder, uploaded_file)
- if upload
- uploaded_file[:disk_filename] = upload.disk_filename
- uploaded_file[:name] = upload.name
- uploaded_file[:title] = upload.title
- uploaded_file[:version] = 1
- uploaded_file[:size] = upload.size
- uploaded_file[:mime_type] = upload.mime_type
- uploaded_file[:tempfile_path] = upload.tempfile_path
- if params[:dmsf_attachments_wfs].present? && params[:dmsf_attachments_wfs][key].present?
- uploaded_file[:workflow_id] = params[:dmsf_attachments_wfs][key].to_i
- end
- end
- end
- DmsfUploadHelper.commit_files_internal uploaded_files, easy_crm_case.project, system_folder,
- context[:controller]
- end
- # Attach DMS links
- easy_crm_case.saved_dmsf_links.each do |l|
- file = l.target_file
- revision = file.last_revision
- system_folder = easy_crm_case.system_folder(true)
- if system_folder
- l.project_id = system_folder.project_id
- l.dmsf_folder_id = system_folder.id
- if l.save
- easy_crm_case.dmsf_file_added file
- end
- wf = easy_crm_case.saved_dmsf_links_wfs[l.id]
- if wf
- # Assign the workflow
- revision.set_workflow(wf.id, 'assign')
- revision.assign_workflow(wf.id)
- # Start the workflow
- revision.set_workflow(wf.id, 'start')
- if revision.save
- wf.notify_users(easy_crm_case.project, revision, context[:controller])
- begin
- file.lock!
- rescue DmsfLockError => e
- Rails.logger.warn e.message
- end
- else
- Rails.logger.error l(:error_workflow_assign)
- end
- end
- end
- end
- end
- end
-
- end
-
- end
-end
\ No newline at end of file
diff --git a/lib/redmine_dmsf/hooks/views/issue_view_hooks.rb b/lib/redmine_dmsf/hooks/views/issue_view_hooks.rb
index a9a358f4..b28cf1ad 100644
--- a/lib/redmine_dmsf/hooks/views/issue_view_hooks.rb
+++ b/lib/redmine_dmsf/hooks/views/issue_view_hooks.rb
@@ -30,7 +30,7 @@ module RedmineDmsf
attach_documents_form(context)
end
- def view_issues_edit_notes_bottom(context={})
+ def view_attachments_form_top(context={})
html = ''
# Radio buttons
if allowed_to_attach_documents(context[:container])
@@ -48,6 +48,7 @@ module RedmineDmsf
html << l(:label_dmsf_attachments)
html << ''
html << '
'
+ html << "" if User.current.pref.dmsf_attachments_upload_choice == 'DMSF'
end
# Upload form
html.html_safe + attach_documents_form(context, false,
@@ -59,7 +60,7 @@ module RedmineDmsf
show_attached_documents(context[:issue], context[:controller])
end
- def view_issues_show_attachments_bottom(context={})
+ def view_issues_show_attachments_table_bottom(context={})
unless context[:options][:only_mails].present?
show_attached_documents(context[:container], context[:controller], context[:attachments])
end
@@ -138,7 +139,8 @@ module RedmineDmsf
# Add Dmsf upload form
container = context[:container]
if allowed_to_attach_documents(container)
- html = "
"
+ html = (description ? '
"
if label
html << ""
html << ""
@@ -149,7 +151,7 @@ module RedmineDmsf
{ :partial => 'dmsf_upload/form',
:locals => { :container => container, :multiple => true, :description => description, :awf => true }})
html << ''
- html << '
'
+ html << (description ? '' : '')
html.html_safe
end
end
@@ -256,7 +258,7 @@ module RedmineDmsf
:locals => {:file => dmsf_file,
:file_approval_allowed => User.current.allowed_to?(:file_approval, dmsf_file.project),
:workflows_available => DmsfWorkflow.where(['project_id = ? OR project_id IS NULL', dmsf_file.project.id]).exists?,
- :project => dmsf_file.project, :wf => wf, :dmsf_link_id => nil }})
+ :project => dmsf_file.project, :wf => wf, :dmsf_link_id => nil }})
html << ''
html << ''
html
diff --git a/lib/redmine_dmsf/patches/easy_crm_cases_controller_patch.rb b/lib/redmine_dmsf/patches/easy_crm_cases_controller_patch.rb
new file mode 100644
index 00000000..90d95c49
--- /dev/null
+++ b/lib/redmine_dmsf/patches/easy_crm_cases_controller_patch.rb
@@ -0,0 +1,103 @@
+module RedmineDmsf
+ module EasyCrmCasesControllerPatch
+
+ def self.included(base)
+ base.send(:include, InstanceMethods)
+
+ base.class_eval do
+
+ before_action :controller_easy_crm_cases_before_save, only: [:create, :update, :bulk_update]
+ after_action :controller_easy_crm_cases_after_save, only: [:create, :update, :bulk_update]
+
+
+ private
+
+ def controller_easy_crm_cases_before_save
+ easy_crm_cases = @easy_crm_cases
+ easy_crm_cases ||= [@easy_crm_case]
+ easy_crm_cases.each do |easy_crm_case|
+ easy_crm_case.save_dmsf_attachments(params[:dmsf_attachments])
+ easy_crm_case.save_dmsf_links(params[:dmsf_links])
+ easy_crm_case.save_dmsf_attachments_wfs(params[:dmsf_attachments_wfs], params[:dmsf_attachments])
+ easy_crm_case.save_dmsf_links_wfs(params[:dmsf_links_wfs])
+ end
+ end
+
+ def controller_easy_crm_cases_after_save
+ easy_crm_cases = @easy_crm_cases
+ easy_crm_cases ||= [@easy_crm_case]
+ easy_crm_cases.each do |easy_crm_case|
+ # Attach DMS documents
+ uploaded_files = params[:dmsf_attachments]
+ if uploaded_files && uploaded_files.is_a?(Hash)
+ system_folder = easy_crm_case.system_folder(true)
+ uploaded_files.each do |key, uploaded_file|
+ upload = DmsfUpload.create_from_uploaded_attachment(easy_crm_case.project, system_folder, uploaded_file)
+ if upload
+ uploaded_file[:disk_filename] = upload.disk_filename
+ uploaded_file[:name] = upload.name
+ uploaded_file[:title] = upload.title
+ uploaded_file[:version] = 1
+ uploaded_file[:size] = upload.size
+ uploaded_file[:mime_type] = upload.mime_type
+ uploaded_file[:tempfile_path] = upload.tempfile_path
+ if params[:dmsf_attachments_wfs].present? && params[:dmsf_attachments_wfs][key].present?
+ uploaded_file[:workflow_id] = params[:dmsf_attachments_wfs][key].to_i
+ end
+ end
+ end
+ DmsfUploadHelper.commit_files_internal uploaded_files, easy_crm_case.project, system_folder, self
+ end
+ # Attach DMS links
+ easy_crm_case.saved_dmsf_links.each do |l|
+ file = l.target_file
+ revision = file.last_revision
+ system_folder = easy_crm_case.system_folder(true)
+ if system_folder
+ l.project_id = system_folder.project_id
+ l.dmsf_folder_id = system_folder.id
+ if l.save
+ easy_crm_case.dmsf_file_added file
+ end
+ wf = easy_crm_case.saved_dmsf_links_wfs[l.id]
+ if wf
+ # Assign the workflow
+ revision.set_workflow(wf.id, 'assign')
+ revision.assign_workflow(wf.id)
+ # Start the workflow
+ revision.set_workflow(wf.id, 'start')
+ if revision.save
+ wf.notify_users(easy_crm_case.project, revision, self)
+ begin
+ file.lock!
+ rescue DmsfLockError => e
+ Rails.logger.warn e.message
+ end
+ else
+ Rails.logger.error l(:error_workflow_assign)
+ end
+ end
+ end
+ end
+ copied_from = EasyCrmCase.find_by_id(params[:copy_from]) if params[:copy_from].present?
+ # Save documents
+ if copied_from
+ copied_from.dmsf_files.each do |dmsf_file|
+ dmsf_file.copy_to(easy_crm_case.project, easy_crm_cases.system_folder(true))
+ end
+ end
+ end
+ end
+
+ end
+ end
+
+ module InstanceMethods
+ end
+ end
+end
+Rails.configuration.to_prepare do
+ unless EasyCrmCasesController.included_modules.include?(RedmineDmsf::EasyCrmCasesControllerPatch)
+ EasyCrmCasesController.send(:include, RedmineDmsf::EasyCrmCasesControllerPatch)
+ end
+end
diff --git a/lib/redmine_dmsf/patches/user_preference_patch.rb b/lib/redmine_dmsf/patches/user_preference_patch.rb
index b65072ea..e915a37c 100644
--- a/lib/redmine_dmsf/patches/user_preference_patch.rb
+++ b/lib/redmine_dmsf/patches/user_preference_patch.rb
@@ -25,9 +25,8 @@ module RedmineDmsf
def self.included(base)
base.send(:include, InstanceMethods)
base.class_eval do
- safe_attributes 'dmsf_tree_view',
- 'dmsf_attachments_upload_choice'
- end
+ safe_attributes 'dmsf_tree_view', 'dmsf_attachments_upload_choice' if self.included_modules.include?(Redmine::SafeAttributes)
+ end
end
module InstanceMethods