毛的资源库关联文件

This commit is contained in:
yanxd 2014-03-06 16:47:28 +08:00
parent e9373991d2
commit 0b385cdbbb
9 changed files with 190 additions and 25 deletions

View File

@ -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, :autocomplete]
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
@ -132,11 +132,40 @@ class AttachmentsController < ApplicationController
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])

View File

@ -85,17 +85,82 @@ module AttachmentsHelper
Attachment.tagged_with(tag_name).order('created_on desc')
end
def render_attachments_for_new_project(project)
def render_attachments_for_new_project(project, limit=nil)
# 查询条件
filename_condition = "trustie" #params[:q]
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}")
# 搜索到的资源
searched_attach = nobelong_attach.where("filename like '%#{filename_condition}%' ")
# attachments = Attachment.where("container_type = '#{project.class}' and container_id = #{project.id}")
return searched_attach.to_yaml
# 除去当前项目的所有资源
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 << "<label>#{ check_box_tag name, attach.id, false, :id => nil } #{h attach.filename}</label><br/>"
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

View File

@ -0,0 +1,17 @@
<% if @save_flag %>
window.location.reload();
<% else %>
// alert('添加文件失败:\n<%=@save_message[0]%>');
$('#ajax-modal').html('<h3 class="title">添加文件失败</h3><%=@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 %>

View File

@ -1,2 +1,2 @@
<% a= render_attachments_for_new_project(Project.find(2)) %>
<%=a%>
$('#relation_file_form').show();
$('#relation_file').html('<%=render_attachments_for_new_project(@project, nil)%>');

View File

@ -81,25 +81,77 @@ div.tags_area {
white-space: nowrap;
text-overflow: ellipsis;
}
div.pagination{
margin: 10px 0px;
height: 1.5em;
text-align: left;
font-size: 13px;
}
.m5p5{
display: inline-block;
height: auto;
color: white !important;
margin: 8px;
padding: 3px 7px;
}
.m5p5:hover {
text-decoration: none;
/*padding-bottom: 3px;*/
/*border-bottom: 1px solid #666666;*/
border-radius: 4px;
border: 1px solid #15bccf;
box-shadow: 3px 3px 3px #666666;
}
#relation_file_div{
margin: 0px 25px;
}
#relation_file_div fieldset{
margin: 0px 25px;
border-radius: 5px;
}
</style>
<span class="borad-title"><%=(@project.project_type == 1) ? t(:label_user_course) : t(:label_project) %>文件共享专区</span>
<div class="content-title-top">
<%= link_to(l(:label_attachment_new), new_project_file_path(@project), :class => 'icon icon-add') if User.current.allowed_to?(:manage_files, @project) %>
<%= link_to(l(:label_attachment_new), 'javascript:void(0);', :onclick=>"$('#file_buttons').slideToggle();", :class => 'icon icon-add') if User.current.allowed_to?(:manage_files, @project) %>
<div class="clearfix"></div>
<div id="file_buttons" class="hidden">
<%= link_to('上传文件', new_project_file_path(@project), :class => 'icon m5p5 button_submit') if User.current.allowed_to?(:manage_files, @project) %>
<%= link_to('关联已有文件', 'javascript:void(0);', :onclick => "$('#relation_file_div').slideToggle();", :class => 'icon m5p5 button_submit') if User.current.allowed_to?(:manage_files, @project) %>
<% if Rails.env.development? %>
<a href="#" class = 'icon' onclick="modalWin();">~测试测试~</a>
<script type="text/javascript">
function modalWin () {
$('#ajax-modal').html('<h3 class="title">~测试测试~</h3><p>导入勾选文件xxx test</p>');
showModal('ajax-modal', '400px');
<div id="relation_file_div" class="hidden" >
<fieldset>
<legend>搜索</legend>
<%= form_tag(
attachments_autocomplete_path(:format => 'js'),
:remote => true,
:method => :post) do %>
<%= label_tag(:attach_search, "按文件名搜索:") %>
<%= text_field_tag(:attach_search) %>
<%#= submit_tag("Search") %>
<% end -%>
<%= form_tag attach_relation_path(:format => 'js'),
method: :post,
remote: true,
id:"relation_file_form",
:class => 'hidden' do %>
<%= hidden_field_tag(:class_name, 'Project') %>
<%= hidden_field_tag(:class_id, params[:project_id]) %>
<div id="relation_file" >
</div>
<div class="kclearfix" style='margin-top: 10px;' >
<%= submit_tag(l(:button_add)) -%>
</div>
<% end -%>
</fieldset>
</div>
</div>
}
</script>
<% end %>
</div>
<%= javascript_tag "observeSearchfield('attach_search', null, '#{ escape_javascript attachments_autocomplete_path(:project_id => @project.id, :format => 'js') }')" %>
<% delete_allowed = User.current.allowed_to?(:manage_files, @project) %>
<table class="list files" id="ver-zebra" >
@ -130,7 +182,7 @@ div.tags_area {
<% end -%>
<% container.attachments.each do |file| %>
<tr class="file <%= cycle("odd", "odd") %>">
<td class="filename" style="font-size: 13px; width: 240px; "><%= link_to_attachment file, :download => true, :title => file.filename+"\n"+file.description, :style => "width: 230px; overflow: hidden; white-space: nowrap;text-overflow: ellipsis;" %></td>
<td class="filename" style="font-size: 13px; width: 240px; "><%= 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;" %></td>
<td class="created_on"><%= format_time(file.created_on) %></td>
<td class="filesize"><%= number_to_human_size(file.filesize) %></td>
<td class="downloads"><%= file.downloads %></td>

View File

@ -31,7 +31,7 @@
</div>
<% elsif object_flag == '6' %>
<span><%= image_tag("/images/sidebar/tags.png") %></span>
<span><%#= image_tag("/images/sidebar/tags.png") %></span>
<span>
<%= 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? %>

View File

@ -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 问题

View File

@ -386,7 +386,8 @@ RedmineApp::Application.routes.draw do
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: [:get, :post]
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

View File

@ -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;