From b7eda83def37363757861f542019b73c8c2afdbd Mon Sep 17 00:00:00 2001
From: lizanle <491823689@qq.com>
Date: Wed, 30 Dec 2015 17:27:44 +0800
Subject: [PATCH] =?UTF-8?q?=E5=8E=86=E5=8F=B2=E8=B5=84=E6=BA=90=E5=8A=9F?=
=?UTF-8?q?=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
app/controllers/attachments_controller.rb | 37 ++++++++++++-
app/models/attachment.rb | 4 --
.../_show_attachment_history.html.erb | 54 +++++++++++++++++++
.../_upload_attachment_new_version.html.erb | 23 ++++++++
.../attachments/attachment_versions.js.erb | 7 +++
app/views/attachments/upload.js.erb | 1 +
.../upload_attachment_version.js.erb | 7 +++
app/views/files/_course_list.html.erb | 2 +-
app/views/files/index.html.erb | 21 ++++++++
config/routes.rb | 2 +
10 files changed, 152 insertions(+), 6 deletions(-)
create mode 100644 app/views/attachments/_show_attachment_history.html.erb
create mode 100644 app/views/attachments/_upload_attachment_new_version.html.erb
create mode 100644 app/views/attachments/attachment_versions.js.erb
create mode 100644 app/views/attachments/upload_attachment_version.js.erb
diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb
index 664dc4cf5..0a4b432ce 100644
--- a/app/controllers/attachments_controller.rb
+++ b/app/controllers/attachments_controller.rb
@@ -185,7 +185,6 @@ class AttachmentsController < ApplicationController
end
@attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16)
saved = @attachment.save
-
respond_to do |format|
format.js
format.api {
@@ -198,6 +197,33 @@ class AttachmentsController < ApplicationController
end
end
+ def upload_attachment_version
+ @flag = false
+ Attachment.transaction do
+ @old_attachment = Attachment.find params[:old_attachment_id]
+ #取出当前上传的文件
+ @attachment = Attachment.find(params[:attachments]['1'][:attachment_id])
+ #将需要修改的记录保存到历史记录
+ @history = AttachmentHistory.new
+ @history.attributes = @old_attachment.attributes.dup.except("id")
+ @history.attachment_id = params[:old_attachment_id]
+ #需要更新版本号,需要拿到原来该文件最大的历史版本号
+ @old_history = @old_attachment.attachment_histories.reorder('version desc').first
+ @history.version = @old_history.nil? ? 1 : @old_history.version + 1
+ @history.save #历史记录保存完毕
+ #将最新保存的记录 数据替换到 需要修改的文件记录
+ @old_attachment.attributes = @attachment.attributes.dup.except("id","container_id","container_type")
+ @old_attachment.save
+ #删除当前记录
+ @attachment.delete
+ @flag = true
+ end
+
+ respond_to do |format|
+ format.js
+ end
+ end
+
def destroy
if @attachment.container.respond_to?(:init_journal)
@attachment.container.init_journal(User.current)
@@ -492,6 +518,15 @@ class AttachmentsController < ApplicationController
end
end
+ #找到文件的所有的历史版本
+ def attachment_versions
+ @attachment = Attachment.find(params[:id])
+ @attachment_histories = @attachment.attachment_histories
+ respond_to do |format|
+ format.js
+ end
+ end
+
private
def find_project
@attachment = Attachment.find(params[:id])
diff --git a/app/models/attachment.rb b/app/models/attachment.rb
index f08bcdddf..ae23428f1 100644
--- a/app/models/attachment.rb
+++ b/app/models/attachment.rb
@@ -519,10 +519,6 @@ class Attachment < ActiveRecord::Base
end
end
- #更新文件的版本
- def update_attchment_version
- end
-
private
# Physically deletes the file from the file system
diff --git a/app/views/attachments/_show_attachment_history.html.erb b/app/views/attachments/_show_attachment_history.html.erb
new file mode 100644
index 000000000..c1a6464e8
--- /dev/null
+++ b/app/views/attachments/_show_attachment_history.html.erb
@@ -0,0 +1,54 @@
+
+更新资源版本
+
+
+
+
当前版本
+
+
+
+
+ <% unless @attachment_histories.empty? %>
+
+
历史版本
+
+ <% @attachment_histories.each do |history| %>
+
+
+ 版本号:<%= history.version %>
+
+
+ <% end %>
+
+ <% end %>
+
+
+ <%= form_tag(upload_attachment_version_path, :multipart => true,:remote => !ie8?,:name=>"upload_form",:id=>'upload_form') do %>
+ <%= hidden_field_tag :old_attachment_id,@attachment.id %>
+
+
+
+
+
+
+ <%= render :partial => 'attachments/upload_attachment_new_version' %>
+
+
+
+
+
+
(未选择文件)
+
您可以上传小于50MB的文件
+
+
+
+
+
+
+ <%= submit_tag '确定',:onclick=>'upload_attachment_version(event);',:onfocus=>'this.blur()',:id=>'upload_files_submit_btn',:class=>'sendSourceText' %>
+
+
+
+ <% end %>
+
+
\ No newline at end of file
diff --git a/app/views/attachments/_upload_attachment_new_version.html.erb b/app/views/attachments/_upload_attachment_new_version.html.erb
new file mode 100644
index 000000000..cd35535c0
--- /dev/null
+++ b/app/views/attachments/_upload_attachment_new_version.html.erb
@@ -0,0 +1,23 @@
+
+
+
+
+ 选择文件
+<%= file_field_tag 'attachments[dummy][file]',
+ :id => '_file',
+ :class => ie8? ? '':'file_selector',
+ :multiple => true,
+ :onchange => 'addInputFiles(this,"'+'upload_files_submit_btn'+'");',
+ :style => ie8? ? '': 'display:none',
+ :data => {
+ :max_file_size => Setting.attachment_max_size.to_i.kilobytes,
+ :max_file_size_message => l(:error_attachment_too_big, :max_size => number_to_human_size(Setting.attachment_max_size.to_i.kilobytes)),
+ :max_concurrent_uploads => Redmine::Configuration['max_concurrent_ajax_uploads'].to_i,
+ :upload_path => uploads_path(:format => 'js',:old_attachment_id=>@attachment.id),
+ :description_placeholder => l(:label_optional_description),
+ :field_is_public => l(:field_is_public),
+ :are_you_sure => l(:text_are_you_sure),
+ :file_count => l(:label_file_count),
+ :lebel_file_uploding => l(:lebel_file_uploding),
+ :delete_all_files => l(:text_are_you_sure_all)
+ } %>
diff --git a/app/views/attachments/attachment_versions.js.erb b/app/views/attachments/attachment_versions.js.erb
new file mode 100644
index 000000000..4f3bf41c3
--- /dev/null
+++ b/app/views/attachments/attachment_versions.js.erb
@@ -0,0 +1,7 @@
+$("#ajax-modal").html('<%= escape_javascript( render :partial => 'attachments/show_attachment_history' )%>');
+showModal('ajax-modal', '452px');
+$('#ajax-modal').siblings().remove();
+$('#ajax-modal').before("");
+$('#ajax-modal').parent().css("top","40%").css("left","46%");
+$('#ajax-modal').parent().addClass("resourceUploadPopup");
+$('#ajax-modal').css("padding-left","16px").css("padding-bottom","16px");
\ No newline at end of file
diff --git a/app/views/attachments/upload.js.erb b/app/views/attachments/upload.js.erb
index 970c5b22d..62db5ebfd 100644
--- a/app/views/attachments/upload.js.erb
+++ b/app/views/attachments/upload.js.erb
@@ -11,4 +11,5 @@ fileSpan.find('a.remove-upload')
})
.off('click');
$('', { type: 'hidden', name: 'attachments[<%= j params[:attachment_id] %>][token]' } ).val('<%= j @attachment.token %>').appendTo(fileSpan);
+$('',{type:'hidden',name:'attachments[<%= j params[:attachment_id] %>][attachment_id]'}).val('<%= @attachment.id %>').appendTo(fileSpan);
<% end %>
diff --git a/app/views/attachments/upload_attachment_version.js.erb b/app/views/attachments/upload_attachment_version.js.erb
new file mode 100644
index 000000000..eb5559563
--- /dev/null
+++ b/app/views/attachments/upload_attachment_version.js.erb
@@ -0,0 +1,7 @@
+<% if @flag %>
+ hideModal();
+ alert('更新成功')
+ $(".re_search").submit();
+<%else%>
+ $("#upload_file_count").html('(更新失败)')
+<%end %>
\ No newline at end of file
diff --git a/app/views/files/_course_list.html.erb b/app/views/files/_course_list.html.erb
index 0212ce8bd..653877260 100644
--- a/app/views/files/_course_list.html.erb
+++ b/app/views/files/_course_list.html.erb
@@ -42,7 +42,7 @@
<%= link_to("发 送".html_safe, 'javascript:void(0)',:class => "postOptionLink",:onclick=>"show_send('#{file.id}')") %>
<% if (is_course_teacher(User.current,@course) || file.author_id == User.current.id) && course_contains_attachment?(@course,file) %>
<% if (delete_allowed || User.current.id == file.author_id) && file.container_id == @course.id && file.container_type == "Course" %>
- <%= link_to '更新版本','',:class => "postOptionLink",:remote=>true %>
+ <%= link_to '更新版本',attachments_versions_path(file),:class => "postOptionLink",:remote=>true %>
<% if @course.is_public? %>
diff --git a/app/views/files/index.html.erb b/app/views/files/index.html.erb
index 0010f0b4f..383ea155e 100644
--- a/app/views/files/index.html.erb
+++ b/app/views/files/index.html.erb
@@ -487,6 +487,27 @@
}
<%end %>
+ function show_attachments_history(){
+
+ }
+
+ //更新文件版本 表单提交确认,原则是只能有一个更新文件
+ function upload_attachment_version(event){
+ if($("#upload_form").find('.upload_filename').length > 1){
+ $("#upload_file_count").html('(只能上传一个更新文件)')
+ event.preventDefault();
+
+ return false;
+ }else if($("#upload_form").find('.upload_filename').length == 0){
+ $("#upload_file_count").html('(请上传一个更新文件)')
+ event.preventDefault();
+
+ return false;
+ }else{
+ $("#upload_form").submit();
+ }
+ }
+
diff --git a/config/routes.rb b/config/routes.rb
index b9622c047..5090eb03c 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -805,6 +805,8 @@ RedmineApp::Application.routes.draw do
# additional routes for having the file name at the end of url
get 'attachments/:id/:filename', :to => 'attachments#show', :id => /\d+/, :filename => /.*/, :as => 'named_attachment'
+ get 'attachments/attachment_versions/:id',:to=>'attachments#attachment_versions',:as=>'attachments_versions'
+ post 'attachments/upload_attachment_version',:to=>'attachments#upload_attachment_version',:as=>'upload_attachment_version'
get 'attachments/download/:id/:filename', :to => 'attachments#download', :id => /\d+/, :filename => /.*/, :as => 'download_named_attachment'
get 'attachments/download/:id', :to => 'attachments#download', :id => /\d+/
get 'attachments/thumbnail/:id(/:size)', :to => 'attachments#thumbnail', :id => /\d+/, :size => /\d+/, :as => 'thumbnail'