2014-10-23 11:30:34 +08:00
# Redmine - project management software
# Copyright (C) 2006-2013 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
class FilesController < ApplicationController
2014-10-29 16:13:49 +08:00
2014-10-28 23:48:58 +08:00
layout 'base_projects' #by young
2014-10-29 16:13:49 +08:00
2014-10-23 11:30:34 +08:00
menu_item :files
2015-02-11 20:23:50 +08:00
before_filter :auth_login1 , :only = > [ :index ]
2015-03-11 11:22:58 +08:00
before_filter :logged_user_by_apptoken , :only = > [ :index ]
2014-10-23 11:30:34 +08:00
before_filter :find_project_by_project_id #, :except => [:getattachtype]
2015-05-20 11:56:34 +08:00
before_filter :authorize , :except = > [ :getattachtype , :quote_resource_show , :search , :searchone4reload , :search_project , :quote_resource_show_project , :search_tag_attachment ]
2014-10-23 11:30:34 +08:00
helper :sort
include SortHelper
2014-11-22 13:45:17 +08:00
include FilesHelper
2014-10-23 11:30:34 +08:00
helper :project_score
2014-11-26 14:54:31 +08:00
include CoursesHelper
2015-03-31 10:03:52 +08:00
include ApplicationHelper
2014-10-23 11:30:34 +08:00
def show_attachments obj
2014-11-22 13:45:17 +08:00
@attachments = [ ]
2014-10-23 11:30:34 +08:00
obj . each do | container |
2014-11-22 13:45:17 +08:00
@attachments += container . attachments
2014-10-23 11:30:34 +08:00
end
2015-07-01 10:30:26 +08:00
@all_attachments = User . current . admin? ? @attachments : visable_attachemnts ( @attachments )
2014-10-23 11:30:34 +08:00
@limit = 10
2014-11-18 18:02:01 +08:00
@feedback_count = @all_attachments . count
2014-10-23 11:30:34 +08:00
@feedback_pages = Paginator . new @feedback_count , @limit , params [ 'page' ]
@offset || = @feedback_pages . offset
2015-03-30 17:14:52 +08:00
#@curse_attachments_all = @all_attachments[@offset, @limit]
@obj_attachments = paginateHelper @all_attachments , 10
2014-10-23 11:30:34 +08:00
end
2015-05-20 11:56:34 +08:00
def searchone4reload
attachment = Attachment . find_by_id ( params [ :fileid ] ) ;
respond_to do | format |
format . html { render :layout = > nil , :locals = > { :file = > attachment , :course = > @course } }
end
end
2014-11-21 11:38:53 +08:00
def search
2014-11-28 11:33:51 +08:00
sort = " "
@sort = " "
@order = " "
@is_remote = true
2015-04-15 19:57:16 +08:00
@q = params [ :name ] . strip
2014-11-28 11:33:51 +08:00
if params [ :sort ]
order_by = params [ :sort ] . split ( " : " )
@sort = order_by [ 0 ]
if order_by . count > 1
@order = order_by [ 1 ]
end
sort = " #{ @sort } #{ @order } "
end
2015-04-15 17:31:15 +08:00
# show_attachments [@course]
2014-11-21 11:38:53 +08:00
begin
q = " % #{ params [ :name ] . strip } % "
#(redirect_to stores_url, :notice => l(:label_sumbit_empty);return) if params[:name].blank?
if params [ :insite ]
2015-04-15 17:43:08 +08:00
if q == " %% "
@result = [ ]
@searched_attach = paginateHelper @result , 10
else
@result = find_public_attache q , sort
@result = visable_attachemnts_insite @result , @course
@searched_attach = paginateHelper @result , 10
end
2014-11-21 11:38:53 +08:00
else
2014-11-28 11:33:51 +08:00
@result = find_course_attache q , @course , sort
2014-11-22 13:45:17 +08:00
@result = visable_attachemnts @result
2014-11-21 11:38:53 +08:00
@searched_attach = paginateHelper @result , 10
2015-04-16 14:21:05 +08:00
@tag_list = get_course_tag_list @course
2014-11-21 11:38:53 +08:00
end
2014-12-12 16:53:49 +08:00
#rescue Exception => e
# #render 'stores'
# redirect_to search_course_files_url
2014-11-21 11:38:53 +08:00
end
end
2015-03-31 15:57:15 +08:00
def search_project
sort = " "
@sort = " "
@order = " "
@is_remote = true
if params [ :sort ]
order_by = params [ :sort ] . split ( " : " )
@sort = order_by [ 0 ]
if order_by . count > 1
@order = order_by [ 1 ]
end
sort = " #{ @sort } #{ @order } "
end
begin
q = " % #{ params [ :name ] . strip } % "
#(redirect_to stores_url, :notice => l(:label_sumbit_empty);return) if params[:name].blank?
if params [ :insite ]
2015-05-23 10:40:15 +08:00
if q == " %% "
@result = [ ]
@searched_attach = paginateHelper @result , 10
else
@result = find_public_attache q , sort
@result = visable_attachemnts_insite @result , @project
@searched_attach = paginateHelper @result , 10
end
2015-03-31 15:57:15 +08:00
else
@result = find_project_attache q , @project , sort
@result = visable_attachemnts @result
@searched_attach = paginateHelper @result , 10
end
#rescue Exception => e
# #render 'stores'
# redirect_to search_course_files_url
end
end
2014-11-28 11:33:51 +08:00
def find_course_attache keywords , course , sort = " "
if sort == " "
sort = " created_on DESC "
end
2015-10-09 09:06:01 +08:00
if keywords != " %% "
2014-11-28 11:33:51 +08:00
resultSet = Attachment . where ( " attachments.container_type = 'Course' And attachments.container_id = ' #{ course . id } ' AND filename LIKE :like " , like : " % #{ keywords } % " ) .
reorder ( sort )
2015-10-09 09:06:01 +08:00
else
resultSet = Attachment . where ( " attachments.container_type = 'Course' And attachments.container_id = ' #{ course . id } ' " ) . reorder ( sort )
end
2014-11-27 17:17:33 +08:00
#resultSet = Attachment.find_by_sql("SELECT `attachments`.* FROM `attachments` LEFT OUTER JOIN `homework_attaches` ON `attachments`.container_type = 'HomeworkAttach' AND `attachments`.container_id = `homework_attaches`.id LEFT OUTER JOIN `homework_for_courses` ON `homework_attaches`.bid_id = `homework_for_courses`.bid_id LEFT OUTER JOIN `homework_for_courses` AS H_C ON `attachments`.container_type = 'Bid' AND `attachments`.container_id = H_C.bid_id WHERE (`homework_for_courses`.course_id = 117 OR H_C.course_id = 117 OR (`attachments`.container_type = 'Course' AND `attachments`.container_id = 117)) AND `attachments`.filename LIKE '%#{keywords}%'").reorder("created_on DESC")
2014-11-21 11:38:53 +08:00
end
2015-03-31 15:57:15 +08:00
def find_project_attache keywords , project , sort = " "
if sort == " "
sort = " created_on DESC "
end
ids = " "
len = 0
count = project . versions . count
project . versions . each do | version |
len = len + 1
if len != count
ids += version . id . to_s + ','
else
ids += version . id . to_s
end
end
2015-04-21 09:49:10 +08:00
if ids . blank?
resultSet = Attachment . where ( " attachments.container_type = 'Project' And attachments.container_id = ' #{ project . id } ' AND filename LIKE :like " , like : " % #{ keywords } % " ) .
reorder ( sort )
else
resultSet = Attachment . where ( " ((attachments.container_type = 'Project' And attachments.container_id = ' #{ project . id } ') OR (container_type = 'Version' AND container_id IN ( #{ ids } ))) AND filename LIKE :like " , like : " % #{ keywords } % " ) .
reorder ( sort )
end
end
2015-03-31 15:57:15 +08:00
2014-11-28 11:33:51 +08:00
def find_public_attache keywords , sort = " "
2014-11-21 11:38:53 +08:00
# StoresController#search 将每条文件都查出来,再次进行判断过滤。---> resultSet.to_a.map
# 此时内容不多速度还可, 但文件增长, 每条判断多则进行3-4次表连接。
# 现在还木有思路 药丸
2014-11-28 11:33:51 +08:00
if sort == " "
sort = " created_on DESC "
end
2014-12-02 15:41:10 +08:00
resultSet = Attachment . where ( " attachments.container_type IS NOT NULL AND attachments.copy_from IS NULL AND filename LIKE :like " , like : " % #{ keywords } % " ) .
2014-11-28 11:33:51 +08:00
reorder ( sort )
2014-11-21 11:38:53 +08:00
end
2014-10-23 11:30:34 +08:00
def index
2015-03-11 15:32:50 +08:00
@flag = params [ :flag ] || false
2014-10-23 11:30:34 +08:00
sort = " "
2014-11-19 17:40:24 +08:00
@sort = " "
@order = " "
2014-11-21 11:38:53 +08:00
@is_remote = false
2014-10-23 11:30:34 +08:00
if params [ :project_id ]
@isproject = true
if params [ :sort ]
params [ :sort ] . split ( " , " ) . each do | sort_type |
order_by = sort_type . split ( " : " )
case order_by [ 0 ]
when " filename "
attribute = " filename "
when " size "
attribute = " filesize "
when " attach_type "
attribute = " attachtype "
when " content_type "
attribute = " created_on "
when " field_file_dense "
attribute = " is_public "
when " downloads "
attribute = " downloads "
when " created_on "
attribute = " created_on "
2015-03-30 17:14:52 +08:00
when " quotes "
attribute = " quotes "
2015-03-31 09:34:27 +08:00
else
attribute = " created_on "
2014-10-23 11:30:34 +08:00
end
2015-03-30 17:14:52 +08:00
@sort = order_by [ 0 ]
@order = order_by [ 1 ]
if order_by . count == 1 && attribute
sort += " #{ Attachment . table_name } . #{ attribute } asc "
if sort_type != params [ :sort ] . split ( " , " ) . last
sort += " , "
end
elsif order_by . count == 2 && order_by [ 1 ]
sort += " #{ Attachment . table_name } . #{ attribute } #{ order_by [ 1 ] } "
if sort_type != params [ :sort ] . split ( " , " ) . last
sort += " , "
end
2014-10-23 11:30:34 +08:00
end
end
2015-03-31 09:34:27 +08:00
else
sort = " #{ Attachment . table_name } .created_on desc "
2014-10-23 11:30:34 +08:00
end
2015-03-10 21:50:47 +08:00
@containers = [ Project . includes ( :attachments ) . find ( @project . id ) ]
@containers += @project . versions . includes ( :attachments ) . all
2014-10-28 23:38:14 +08:00
2015-03-10 21:50:47 +08:00
ids = [ ]
@containers . each do | c |
ids += c . attachments . pluck ( :id )
end
@containers = [ Struct . new ( :attachments ) . new ( Attachment . where ( 'id in (?)' , ids ) . reorder ( sort ) ) ]
2014-10-23 11:30:34 +08:00
2015-03-10 21:50:47 +08:00
show_attachments @containers
2015-03-06 14:55:21 +08:00
respond_to do | format |
format . html
format . js
end
2014-10-23 11:30:34 +08:00
elsif params [ :course_id ]
@isproject = false
if params [ :sort ]
params [ :sort ] . split ( " , " ) . each do | sort_type |
order_by = sort_type . split ( " : " )
case order_by [ 0 ]
when " filename "
attribute = " filename "
when " size "
attribute = " filesize "
when " attach_type "
attribute = " attachtype "
when " content_type "
attribute = " created_on "
when " field_file_dense "
attribute = " is_public "
when " downloads "
attribute = " downloads "
when " created_on "
attribute = " created_on "
2014-12-02 15:30:32 +08:00
when " quotes "
attribute = " quotes "
2015-03-31 09:34:27 +08:00
else
attribute = " created_on "
2014-10-23 11:30:34 +08:00
end
2014-11-20 17:44:30 +08:00
@sort = order_by [ 0 ]
@order = order_by [ 1 ]
2014-12-04 14:53:53 +08:00
if order_by . count == 1 && attribute
2014-10-23 11:30:34 +08:00
sort += " #{ Attachment . table_name } . #{ attribute } asc "
2014-12-04 14:53:53 +08:00
if sort_type != params [ :sort ] . split ( " , " ) . last
sort += " , "
end
elsif order_by . count == 2 && order_by [ 1 ]
2014-10-23 11:30:34 +08:00
sort += " #{ Attachment . table_name } . #{ attribute } #{ order_by [ 1 ] } "
2014-12-04 14:53:53 +08:00
if sort_type != params [ :sort ] . split ( " , " ) . last
sort += " , "
end
2014-10-23 11:30:34 +08:00
end
end
2015-03-31 09:34:27 +08:00
else
sort = " #{ Attachment . table_name } .created_on desc "
2014-10-23 11:30:34 +08:00
end
2014-10-28 23:38:14 +08:00
2014-10-23 11:30:34 +08:00
@containers = [ Course . includes ( :attachments ) . reorder ( sort ) . find ( @course . id ) ]
show_attachments @containers
2015-04-15 17:16:28 +08:00
@tag_list = attachment_tag_list @all_attachments
2014-10-23 11:30:34 +08:00
render :layout = > 'base_courses'
end
end
2014-11-20 17:44:30 +08:00
def quote_resource_show
@file = Attachment . find ( params [ :id ] )
2014-12-22 16:16:33 +08:00
@can_quote = attachment_candown @file
2014-11-19 17:40:24 +08:00
end
2015-03-31 15:57:15 +08:00
def quote_resource_show_project
@file = Attachment . find ( params [ :id ] )
@can_quote = attachment_candown @file
end
2014-10-23 11:30:34 +08:00
def new
@versions = @project . versions . sort
@course_tag = @project . project_type
if @project . project_type == 1
render :layout = > 'base_courses'
end
end
def create
if params [ :add_tag ]
@addTag = true
#render :back
tag_saveEx
#render :text =>"success"
respond_to do | format |
format . js
end
else
#modify by nwb
if @project
@addTag = false
container = ( params [ :version_id ] . blank? ? @project : @project . versions . find_by_id ( params [ :version_id ] ) )
attachments = Attachment . attach_filesex ( container , params [ :attachments ] , params [ :attachment_type ] )
render_attachment_warning_if_needed ( container )
if ! attachments . empty? && ! attachments [ :files ] . blank? && Setting . notified_events . include? ( 'file_added' )
2015-03-31 11:42:36 +08:00
Mailer . run . attachments_added ( attachments [ :files ] )
2014-10-23 11:30:34 +08:00
end
# TODO: 临时用 nyan
sort_init 'created_on' , 'desc'
sort_update 'created_on' = > " #{ Attachment . table_name } .created_on " ,
'filename' = > " #{ Attachment . table_name } .filename " ,
'size' = > " #{ Attachment . table_name } .filesize " ,
'downloads' = > " #{ Attachment . table_name } .downloads "
@containers = [ Project . includes ( :attachments ) . reorder ( " #{ Attachment . table_name } .created_on DESC " ) . find ( @project . id ) ] #modify by Long Jun
@containers += @project . versions . includes ( :attachments ) . reorder ( " #{ Attachment . table_name } .created_on DESC " ) . all . sort
show_attachments @containers
@attachtype = 0
@contenttype = 0
respond_to do | format |
format . js
format . html {
redirect_to project_files_url ( @project )
}
end
elsif @course
@addTag = false
attachments = Attachment . attach_filesex ( @course , params [ :attachments ] , params [ :attachment_type ] )
if ! attachments . empty? && ! attachments [ :files ] . blank? && Setting . notified_events . include? ( 'file_added' )
2015-03-31 11:42:36 +08:00
Mailer . run . attachments_added ( attachments [ :files ] )
2014-10-23 11:30:34 +08:00
end
2015-04-15 16:00:45 +08:00
if params [ :course_attachment_type ] && params [ :course_attachment_type ] != " 5 "
case params [ :course_attachment_type ]
when " 1 "
tag_name = l ( :label_courseware )
when " 2 "
tag_name = l ( :label_software )
when " 3 "
tag_name = l ( :label_media )
when " 4 "
tag_name = l ( :label_code )
else
tag_name = " "
end
if ! attachments . empty? && attachments [ :files ] && tag_name != " "
attachments [ :files ] . each do | attachment |
attachment . tag_list . add ( tag_name )
attachment . save
end
end
end
2014-10-23 11:30:34 +08:00
# TODO: 临时用 nyan
sort_init 'created_on' , 'desc'
sort_update 'created_on' = > " #{ Attachment . table_name } .created_on " ,
'filename' = > " #{ Attachment . table_name } .filename " ,
'size' = > " #{ Attachment . table_name } .filesize " ,
'downloads' = > " #{ Attachment . table_name } .downloads "
@containers = [ Course . includes ( :attachments ) . reorder ( " #{ Attachment . table_name } .created_on DESC " ) . find ( @course . id ) ]
show_attachments @containers
2015-04-16 11:14:06 +08:00
@tag_list = attachment_tag_list @all_attachments
2014-10-23 11:30:34 +08:00
@attachtype = 0
@contenttype = 0
respond_to do | format |
format . js
format . html {
redirect_to course_files_url ( @course )
}
end
end
end
end
def tag_saveEx
@tags = params [ :tag_name ] [ :name ]
@obj_id = params [ :object_id ]
@obj_flag = params [ :object_flag ]
case @obj_flag
when '1' then
@obj = User . find_by_id ( @obj_id )
when '2' then
@obj = Project . find_by_id ( @obj_id )
when '3' then
@obj = Issue . find_by_id ( @obj_id )
when '4' then
@obj = Bid . find_by_id ( @obj_id )
when '5' then
@obj = Forum . find_by_id ( @obj_id )
when '6'
@obj = Attachment . find_by_id ( @obj_id )
when '7' then
@obj = Contest . find_by_id ( @obj_id )
when '8'
@obj = OpenSourceProject . find_by_id ( @obj_id )
when '9'
@obj = Course . find_by_id ( @obj_id )
else
@obj = nil
end
unless @obj . nil?
@obj . tag_list . add ( @tags . split ( " , " ) )
else
return
end
if @obj . save
## 执行成功的操作。
else
#捕获异常
end
end
# 返回指定资源类型的资源列表
# added by nwb
def getattachtype
sort_init 'created_on' , 'desc'
sort_update 'created_on' = > " #{ Attachment . table_name } .created_on " ,
'filename' = > " #{ Attachment . table_name } .filename " ,
'size' = > " #{ Attachment . table_name } .filesize " ,
'downloads' = > " #{ Attachment . table_name } .downloads "
2014-10-28 23:38:14 +08:00
sort = ''
2014-10-31 16:38:04 +08:00
2014-10-28 23:38:14 +08:00
if params [ :sort ]
params [ :sort ] . split ( " , " ) . each do | sort_type |
order_by = sort_type . split ( " : " )
case order_by [ 0 ]
when " filename "
attribute = " filename "
when " size "
attribute = " filesize "
when " attach_type "
attribute = " attachtype "
when " content_type "
attribute = " created_on "
when " field_file_dense "
attribute = " is_public "
when " downloads "
attribute = " downloads "
when " created_on "
attribute = " created_on "
end
if order_by . count == 1
sort += " #{ Attachment . table_name } . #{ attribute } asc "
elsif order_by . count == 2
sort += " #{ Attachment . table_name } . #{ attribute } #{ order_by [ 1 ] } "
end
if sort_type != params [ :sort ] . split ( " , " ) . last
sort += " , "
end
end
end
2014-10-23 11:30:34 +08:00
if @project
2014-10-28 23:38:14 +08:00
@isproject = true
2014-10-23 11:30:34 +08:00
@containers = [ Project . includes ( :attachments ) . reorder ( " #{ Attachment . table_name } .created_on DESC " ) . find ( @project . id ) ]
2014-10-28 23:38:14 +08:00
@containers += @project . versions . includes ( :attachments ) . reorder ( sort ) . all
2014-10-29 16:13:49 +08:00
show_attachments @containers
@attachtype = params [ :type ] . to_i
@contenttype = params [ :contentType ] . to_s
respond_to do | format |
format . js
format . html {
render :layout = > 'base_projects'
}
end
2014-10-23 11:30:34 +08:00
2014-10-28 23:38:14 +08:00
elsif @course
@isproject = false
@containers = [ Course . includes ( :attachments ) . reorder ( sort ) . find ( @course . id ) ]
2014-10-29 16:13:49 +08:00
show_attachments @containers
@attachtype = params [ :type ] . to_i
@contenttype = params [ :contentType ] . to_s
# render layout: 'base_courses'
respond_to do | format |
format . js
format . html {
render :layout = > 'base_courses'
}
end
2014-12-26 15:35:38 +08:00
else
show_attachments @containers
@attachtype = params [ :type ] . to_i
@contenttype = params [ :contentType ] . to_s
respond_to do | format |
format . js
format . html
end
2014-10-29 16:13:49 +08:00
end
2015-04-15 19:57:16 +08:00
end
2014-10-28 23:38:14 +08:00
2015-04-15 20:26:54 +08:00
#查找指定TAG的按条件过滤的资源列表, 只有课程内搜索有此功能
2015-04-15 19:57:16 +08:00
def search_tag_attachment
@q , @tag_name , @order = params [ :q ] , params [ :tag_name ]
@is_remote = true
if params [ :sort ]
order_by = params [ :sort ] . split ( " : " )
@sort = order_by [ 0 ]
if order_by . count > 1
@order = order_by [ 1 ]
end
sort = " #{ @sort } #{ @order } "
end
2014-12-02 15:30:32 +08:00
2015-04-15 19:57:16 +08:00
q = " % #{ @q . strip } % "
@result = find_course_attache q , @course , sort
@result = visable_attachemnts @result
2015-10-09 09:06:01 +08:00
@result = @result . select { | attachment | attachment . tag_list . include? ( @tag_name ) } unless @tag_name . blank?
2015-04-15 19:57:16 +08:00
@searched_attach = paginateHelper @result , 10
2015-04-16 14:21:05 +08:00
@tag_list = get_course_tag_list @course
2014-10-28 23:38:14 +08:00
2015-04-15 19:57:16 +08:00
respond_to do | format |
format . js
# format.html
end
2014-10-23 11:30:34 +08:00
end
end