socialforge/app/helpers/files_helper.rb

186 lines
6.8 KiB
Ruby

# encoding: utf-8
module FilesHelper
include AttachmentsHelper
def downloadAll containers
paths = []
files = []
tmpfile = "tmp.zip"
containers.each do |container|
next if container.attachments.empty?
if container.is_a?(Version);end
container.attachments.each do |attachment|
paths << attachment.diskfile
file = attachment.diskfile
# logger.error "[FilesHelper] downloadAll: #{e}"
begin
File.new(file, "r")
rescue Exception => e
logger.error e
next
end
files << file
# zip.add(file.path.dup.sub(directory, ''), file.path)
end
end
zipfile_name = "archive.zip"
if File.exists? File.open(zipfile_name, "w+")
ff = File.open(zipfile_name, "w+")
ff.close
File.delete ff
end
Zip::ZipFile.open(zipfile_name, Zip::ZipFile::CREATE) do |zipfile|
files.each do |filename|
directory = File.dirname filename
# Two arguments:
# - The name of the file as it will appear in the archive
# - The original file, including the path to find it
dir = filename.sub(directory+"/", '')
zipfile.add(dir, filename)
end
end
File.new(zipfile_name,'w+')
end
#带勾选框的课程列表
def courses_check_box_tags(name,courses,current_course,attachment)
s = ''
courses.each do |course|
if !course_contains_attachment?(course,attachment) && is_course_teacher(User.current,course) && course_in_current_or_next_term(course)
s << "<label>#{ check_box_tag name, course.id, false, :id => nil } #{h course.name}</label>&nbsp;[#{get_course_term course}]<br/>"
end
end
s.html_safe
end
#带勾选框的项目列表
def projects_check_box_tags(name,projects,current_project,attachment)
s = ''
projects.each do |project|
if !project_contains_attachment?(project,attachment) && User.current.allowed_to?(:manage_files, project)
s << "<label>#{ check_box_tag name, project.id, false, :id => nil } #{h project.name}</label><br/>"
end
end
s.html_safe
end
#带勾选框的组织资源栏目列表
def org_subfields_check_box_tags(name,org_subfields,attachment)
s = ''
org_subfields.each do |org_subfield|
if !org_subfield.attachments.include?attachment
s << "<label>#{ check_box_tag name, org_subfield.id, false, :id => nil } #{h org_subfield.name}</label><br/>"
end
end
s.html_safe
end
#判断用户是否拥有不包含当前资源的课程,需用户在该课程中角色为教师且该课程属于当前学期或下一学期
def has_course? user,file
result = false
user.courses.each do |course|
if !course_contains_attachment?(course,file) && is_course_teacher(User.current,course) && course_in_current_or_next_term(course)
return true
end
end
result
end
#判断用户是否拥有不包含当前资源的项目,需用户在该项目中有资源管理相关资源
def has_project? user,file
result = false
user.projects.each do |project|
if !project_contains_attachment?(project,file) && User.current.allowed_to?(:manage_files, project)
return true
end
end
result
end
# 判断指定的资源时候符合类型
def isTypeOk(attachment, type, contentType)
result = false
if type != 0
if attachment.attachtype == type
result = true
end
else
result = true
end
if result
if contentType != '0' && contentType != attachment.suffix_type
result = false
end
end
result
end
# def visable_attachemnts attachments
# result = []
# attachments.each do |attachment|
# if (attachment.is_public? && attachment.container_type != "Course") ||
# (attachment.is_public? && attachment.container_type == "Course" && attachment.is_publish == 1)||
# (attachment.container_type == "Project" && User.current.member_of?(attachment.project)) ||
# (attachment.container_type == "Course" && User.current.allowed_to?(:as_teacher,Course.find(attachment.container_id)))||
# (attachment.container_type == "Course" && User.current.member_of_course?(Course.find(attachment.container_id)) && attachment.is_publish == 1)||
# attachment.author_id == User.current.id ||
# (attachment.container_type == "OrgSubfield" and User.current.member_of_org?(attachment.container.organization))
# result << attachment
# end
# end
# result
# end
def visable_attachemnts attachments
result = []
attachments.each do |attachment|
if (attachment.is_public? && attachment.container_type != "Course") ||
(attachment.is_public? && attachment.container_type == "Course" && attachment.is_publish == 1)||
(attachment.container_type == "Project" && User.current.member_of?(attachment.project)) ||
(attachment.container_type == "Course" && User.current.allowed_to?(:as_teacher,Course.find(attachment.container_id)))||
(attachment.container_type == "Course" && User.current.member_of_course?(Course.find(attachment.container_id)) && attachment.is_publish == 1)||
attachment.author_id == User.current.id ||
(attachment.container_type == "OrgSubfield" and User.current.member_of_org?(attachment.container.organization))
result << attachment
end
end
result
end
def get_attachments_by_tag attachments,tag
attachments.each do |attachment|
attachment.tag_list.include?(tag)
end
end
def visable_attachemnts_insite attachments,obj
result = []
if obj.is_a?(Course)
attachments.each do |attachment|
if attachment.is_public? || (attachment.container_type == "Course" && attachment.container_id == obj.id && User.current.member_of_course?(Course.find(attachment.container_id)))|| attachment.author_id == User.current.id
result << attachment
end
end
else if obj.is_a?(Project)
attachments.each do |attachment|
if attachment.is_public? || (attachment.container_type == "Project" && attachment.container_id == obj.id && User.current.member_of_course?(Project.find(attachment.container_id)))|| attachment.author_id == User.current.id
result << attachment
end
end
elsif obj.is_a?(OrgSubfield)
attachments.each do |attachment|
if attachment.is_public? || (attachment.container_type == "OrgSubfield" && attachment.container_id == obj.id )|| attachment.author_id == User.current.id
result << attachment
end
end
end
end
result
end
end