diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 5dbb4f2b..af96d9c6 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -197,6 +197,35 @@ class AttachmentsController < ApplicationController end end + def add_exist_file_to_course + class_id = params[:class_id] + attachments = params[:attachment][:attach] + + obj = Course.find_by_id(class_id) + attachments.collect do |attach_id| + ori = Attachment.find_by_id(attach_id) + next if ori.blank? + attach_copied_obj = ori.copy + attach_copied_obj.tag_list.add(ori.tag_list) # tag关联 + attach_copied_obj.container = obj + attach_copied_obj.created_on = Time.now + attach_copied_obj.author_id = User.current.id + @obj = obj + @save_flag = attach_copied_obj.save + @save_message = attach_copied_obj.errors.full_messages + end + + respond_to do |format| + format.js + end + rescue NoMethodError + @save_flag = false + @save_message = [] << l(:error_attachment_empty) + respond_to do |format| + format.js + end + end + private def find_project @attachment = Attachment.find(params[:id]) diff --git a/app/views/attachments/add_exist_file_to_course.js.erb b/app/views/attachments/add_exist_file_to_course.js.erb new file mode 100644 index 00000000..58e5690e --- /dev/null +++ b/app/views/attachments/add_exist_file_to_course.js.erb @@ -0,0 +1,17 @@ +<% if @save_flag %> + window.location.reload(); +<% else %> + // alert('添加文件失败:\n<%=@save_message[0]%>'); + $('#ajax-modal').html('

添加文件失败

<%=@save_message.join(', ')%>'); + + var el = $('#ajax-modal').first(); + var title = el.find('h3.title').text(); + el.dialog({ + width: '200px', + modal: true, + resizable: false, + dialogClass: 'modal', + title: title + }); + +<% end %> diff --git a/app/views/files/_course_file.html.erb b/app/views/files/_course_file.html.erb index c6f872d6..d9913539 100644 --- a/app/views/files/_course_file.html.erb +++ b/app/views/files/_course_file.html.erb @@ -40,7 +40,7 @@ <%= text_field_tag(:attach_search) %> <%#= submit_tag("Search") %> <% end -%> - <%= form_tag attach_relation_path(:format => 'js'), + <%= form_tag course_attach_relation_path(:format => 'js'), method: :post, remote: true, id: "relation_file_form", diff --git a/config/routes.rb b/config/routes.rb index ad493bce..2e11d9b4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -510,6 +510,7 @@ RedmineApp::Application.routes.draw do get 'attachments/autocomplete' match 'attachments/autocomplete', :to => 'attachments#autocomplete', via: [:post] post 'attachments/relationfile', to: 'attachments#add_exist_file_to_project', as: 'attach_relation' + post 'attachments/courserelationfile', to: 'attachments#add_exist_file_to_course', as: 'course_attach_relation' get 'attachments/renderTag/:attchmentId', :to => 'attachments#renderTag', :attchmentId => /\d+/ resources :attachments, :only => [:show, :destroy] do collection do