diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb
index 976772b80..589398c07 100644
--- a/app/controllers/attachments_controller.rb
+++ b/app/controllers/attachments_controller.rb
@@ -16,7 +16,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
class AttachmentsController < ApplicationController
- before_filter :find_project, :except => :upload
+ before_filter :find_project, :only => [:show, :download, :thumbnail, :destroy, :delete_homework]#, :except => [:upload, :autocomplete]
before_filter :file_readable, :read_authorize, :only => [:show, :thumbnail]#Modified by young
before_filter :delete_authorize, :only => :destroy
before_filter :authorize_global, :only => :upload
@@ -131,6 +131,41 @@ class AttachmentsController < ApplicationController
end
end
+ def autocomplete
+ @project = Project.find_by_id(params[:project_id])
+ respond_to do |format|
+ format.js
+ end
+ end
+
+ def add_exist_file_to_project
+ classname = params[:class_name]
+ class_id = params[:class_id]
+ attachments = params[:attachment][:attach]
+
+ obj = Object.const_get(classname).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.container = obj
+ attach_copied_obj.created_on = Time.now
+ @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/helpers/attachments_helper.rb b/app/helpers/attachments_helper.rb
index a90807718..a3080fbfd 100644
--- a/app/helpers/attachments_helper.rb
+++ b/app/helpers/attachments_helper.rb
@@ -85,4 +85,82 @@ module AttachmentsHelper
Attachment.tagged_with(tag_name).order('created_on desc')
end
+ def render_attachments_for_new_project(project, limit=nil)
+ # 查询条件
+ params[:q] ||= ""
+ filename_condition = params[:q]
+
+ attachAll = Attachment.scoped
+ # 当前项目所有资源
+ # attachments = Attachment.find_all_by_container_type_and_container_id(project.class, project.id)
+ # attachments = Attachment.where("container_type = '#{project.class}' and container_id = #{project.id}")
+
+ # 除去当前项目的所有资源
+ nobelong_attach = Attachment.where("container_type <> '#{project.class}' and container_id <> #{project.id}") unless project.blank?
+
+ # 搜索域确定
+ domain = project.nil? ? attachAll : nobelong_attach
+
+ # 搜索到的资源
+ searched_attach = domain.where("filename like '%#{filename_condition}%' ").limit(limit).order('created_on desc')
+ searched_attach = private_filter searched_attach
+ searched_attach = paginateHelper(searched_attach, 10)
+
+ s = content_tag('div', attachments_check_box_tags('attachment[attach][]', searched_attach), :id => 'attachments')
+ links = pagination_links_full(@obj_pages, @obj_count, :per_page_links => false) {|text, parameters, options|
+ link_to text, attachments_autocomplete_path( parameters.merge(:q => params[:q], :format => 'js')), :remote => true }
+
+ return s + content_tag('div', content_tag('ul', links), :class => 'pagination')
+
+ # ================================================================================================
+
+ # attach_count = searched_attach.count
+ # attach_pages = Redmine::Pagination::Paginator.new attach_count, 10, params['page'] #by young
+ # attachs = searched_attach.offset(attach_pages.offset).limit(attach_pages.per_page).all
+
+ # s = content_tag('div', attachments_check_box_tags('attachment[attach][]', attachs), :id => 'attachments')
+ # links = pagination_links_full(attach_pages, attach_count, :per_page_links => false) {|text, parameters, options|
+ # link_to text, attachments_autocomplete_path( parameters.merge(:q => params[:q], :format => 'js')), :remote => true }
+
+ # return s + content_tag('div', content_tag('ul', links), :class => 'pagination')
+ # return searched_attach.to_json
+ end
+
+ def attachments_check_box_tags(name, attachs)
+ s = ''
+ attachs.each do |attach|
+ s << "
"
+ end
+ s.html_safe
+ end
+
+ def private_filter resultSet
+ result = resultSet.to_a.dup
+
+ resultSet.map { |res|
+ if(res.container.nil? ||
+ (res.container.class.to_s=="Project" && res.container.is_public == false) ||
+ (res.container.has_attribute?(:project) && res.container.project.is_public == false) ||
+ (res.container.class.to_s=="HomeworkAttach" && res.container.bid.reward_type == 3) ||
+ false
+ )
+ result.delete(res)
+ end
+ }
+ result
+ end
+ include Redmine::Pagination
+ def paginateHelper obj, pre_size=10
+ @obj_count = obj.count
+ @obj_pages = Paginator.new @obj_count, pre_size, params['page']
+ if obj.kind_of? ActiveRecord::Base or obj.kind_of? ActiveRecord::Relation
+ obj.limit(@obj_pages.per_page).offset(@obj_pages.offset)
+ elsif obj.kind_of? Array
+ obj[@obj_pages.offset, @obj_pages.per_page]
+ else
+ logger.error "[ApplicationController] Error : application_controller#paginateHelper ===> unknow category: #{obj.class}"
+ raise RuntimeError, 'unknow type, Please input you type into this helper.'
+ end
+ end
+
end
diff --git a/app/models/attachment.rb b/app/models/attachment.rb
index e669428f6..768be578f 100644
--- a/app/models/attachment.rb
+++ b/app/models/attachment.rb
@@ -169,7 +169,7 @@ class Attachment < ActiveRecord::Base
def deletable?(user=User.current)
if container_id
- container && container.attachments_deletable?(user)
+ container && (container.is_a?(Project) ? container.attachments_deletable?(user) : true )
else
author == user
end
diff --git a/app/views/attachments/_links.html.erb b/app/views/attachments/_links.html.erb
index 81eb416af..c135ee214 100644
--- a/app/views/attachments/_links.html.erb
+++ b/app/views/attachments/_links.html.erb
@@ -9,14 +9,14 @@
<%= h(" - #{attachment.description}") unless attachment.description.blank? %>
(<%= number_to_human_size attachment.filesize %>)
<% if options[:deletable] %>
- <% unless attachment.container_type == 'HomeworkAttach' %>
- <%= link_to image_tag('delete.png'), attachment_path(attachment),
+ <% if attachment.container_type == 'HomeworkAttach' %>
+ <%= link_to image_tag('delete.png'), {:controller => 'attachments', :action => 'delete_homework', :id => attachment.id},
:data => {:confirm => l(:text_are_you_sure)},
:method => :delete,
:class => 'delete',
:title => l(:button_delete) %>
<% else %>
- <%= link_to image_tag('delete.png'), {:controller => 'attachments', :action => 'delete_homework', :id => attachment.id},
+ <%= link_to image_tag('delete.png'), attachment_path(attachment),
:data => {:confirm => l(:text_are_you_sure)},
:method => :delete,
:class => 'delete',
diff --git a/app/views/attachments/add_exist_file_to_project.js.erb b/app/views/attachments/add_exist_file_to_project.js.erb
new file mode 100644
index 000000000..58e5690ef
--- /dev/null
+++ b/app/views/attachments/add_exist_file_to_project.js.erb
@@ -0,0 +1,17 @@
+<% if @save_flag %>
+ window.location.reload();
+<% else %>
+ // alert('添加文件失败:\n<%=@save_message[0]%>');
+ $('#ajax-modal').html('
<%= link_to_attachment file, :download => true, :title => file.filename+"\n"+file.description, :style => "width: 230px; overflow: hidden; white-space: nowrap;text-overflow: ellipsis;" %> | +<%= link_to_attachment file, :download => true, :title => file.filename+"\n"+file.description.to_s, :style => "width: 230px; overflow: hidden; white-space: nowrap;text-overflow: ellipsis;" %> | <%= format_time(file.created_on) %> | <%= number_to_human_size(file.filesize) %> | <%= file.downloads %> | diff --git a/app/views/tags/_tag.html.erb b/app/views/tags/_tag.html.erb index b2cd77054..7eb360d53 100644 --- a/app/views/tags/_tag.html.erb +++ b/app/views/tags/_tag.html.erb @@ -31,7 +31,7 @@ <% elsif object_flag == '6' %> - <%= image_tag("/images/sidebar/tags.png") %> + <%#= image_tag("/images/sidebar/tags.png") %> <%= link_to (image_tag "/images/sidebar/add.png"), 'javascript:void(0);', :class => "tags_icona", :onclick=>"$('#put-tag-form-#{obj.class}-#{obj.id}').toggle(); readmore(this);" if User.current.logged? %> <%#= toggle_link (image_tag "/images/sidebar/add.png"), "put-tag-form-#{obj.class}-#{obj.id}", {:focus => "put-tag-form-#{obj.class}-#{obj.id} #name"} if User.current.logged? %> diff --git a/config/locales/zh.yml b/config/locales/zh.yml index 0c1797553..5d7f440ff 100644 --- a/config/locales/zh.yml +++ b/config/locales/zh.yml @@ -1150,6 +1150,7 @@ zh: button_export: 导出 label_export_options: "%{export_format} 导出选项" error_attachment_too_big: 该文件无法上传。超过文件大小限制 (%{max_size}) + error_attachment_empty: 文件不能为空 notice_failed_to_save_time_entries: "无法保存下列所选取的 %{total} 个项目中的 %{count} 工时: %{ids}。" label_x_issues: zero: 0 问题 diff --git a/config/routes.rb b/config/routes.rb index 882544a55..7633a9c9f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -385,6 +385,9 @@ RedmineApp::Application.routes.draw do 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' + get 'attachments/autocomplete' + match 'attachments/autocomplete', :to => 'attachments#autocomplete', via: [:post] + post 'attachments/relationfile', to: 'attachments#add_exist_file_to_project', as: 'attach_relation' resources :attachments, :only => [:show, :destroy] resources :groups do diff --git a/public/themes/redpenny-master/stylesheets/application.css b/public/themes/redpenny-master/stylesheets/application.css index f55078750..c68e36cd7 100644 --- a/public/themes/redpenny-master/stylesheets/application.css +++ b/public/themes/redpenny-master/stylesheets/application.css @@ -1898,7 +1898,7 @@ input[type="submit"], .button_submit { font-family: '微软雅黑',Arial,Helvetica,sans-serif; font-size: 12px; color: #fff; - padding: 0px; + padding: 3px 9px; background: #15bccf; border-radius: 4px; border: 1px solid #15bccf; diff --git a/test/fixtures/attachments.yml b/test/fixtures/attachments.yml index 6384c8ee6..33bd60159 100644 --- a/test/fixtures/attachments.yml +++ b/test/fixtures/attachments.yml @@ -267,3 +267,31 @@ attachments_020: filename: root_attachment.txt filesize: 54 author_id: 2 +attachments_021: + content_type: text/plain + downloads: 0 + created_on: 2012-05-12 16:14:50 +09:00 + disk_filename: 120512161450_project_test.txt + disk_directory: + container_id: 2 + digest: b0fe2abdb2599743d554a61d7da7ff74 + id: 21 + container_type: Project + description: "" + filename: project_test.txt + filesize: 54 + author_id: 2 +attachments_022: + content_type: text/plain + downloads: 0 + created_on: 2012-05-12 16:14:50 +09:00 + disk_filename: 120512161450_course_test.txt + disk_directory: + container_id: 1 + digest: b0fe2abdb2599743d554a61d7da7ff74 + id: 22 + container_type: Project + description: "" + filename: course_test.txt + filesize: 54 + author_id: 2 \ No newline at end of file diff --git a/test/unit/attachment_test.rb b/test/unit/attachment_test.rb new file mode 100644 index 000000000..bd3f4659a --- /dev/null +++ b/test/unit/attachment_test.rb @@ -0,0 +1,20 @@ +# encoding: utf-8 +require 'test_helper' + +class AttachmentsTest < ActiveSupport::TestCase + include AttachmentsHelper + + def setup + @file_belong_to = attachments(:attachments_021) + @file_not_belong_to = attachments(:attachments_022) + @project = @file_belong_to.container + end + + test "can be find file not belong to project" do + params = {:q => @file_not_belong_to} + found_file = render_attachments_for_new_project(@project) + assert found_file.include?(@file_not_belong_to) + assert_not found_file.include?(@file_belong_to) + end + +end