You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

files_controller.rb 38 kB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
​
12 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
​
​
​
​
​
​
​
​
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. #encoding: utf-8
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2013 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class FilesController < ApplicationController
  19. layout 'base_projects' #by young
  20. menu_item :files
  21. before_filter :auth_login1, :only => [:index]
  22. before_filter :logged_user_by_apptoken,:only => [:index]
  23. before_filter :find_project_by_project_id#, :except => [:getattachtype]
  24. before_filter :authorize, :except => [:create,:getattachtype,:quote_resource_show,:search,:searchone4reload,:search_project,:quote_resource_show_project,
  25. :search_tag_attachment,:subfield_upload_file,:search_org_subfield_tag_attachment,
  26. :search_tag_attachment,:quote_resource_show_org_subfield,:find_org_subfield_attache,
  27. :search_files_in_subfield,:upload_files_menu,:file_hidden,:republish_file,:update_file_description, :edit_file_description]
  28. helper :sort
  29. include SortHelper
  30. include FilesHelper
  31. helper :project_score
  32. include CoursesHelper
  33. include ApplicationHelper
  34. def show_attachments obj
  35. @attachments = []
  36. obj.each do |container|
  37. @attachments += container.attachments
  38. end
  39. @all_attachments = User.current.admin? ? @attachments : visable_attachemnts(@attachments)
  40. @limit = 10
  41. @feedback_count = @all_attachments.count
  42. @feedback_pages = Paginator.new @feedback_count, @limit, params['page']
  43. @offset ||= @feedback_pages.offset
  44. #@curse_attachments_all = @all_attachments[@offset, @limit]
  45. @obj_attachments = paginateHelper @all_attachments,10
  46. end
  47. def searchone4reload
  48. attachment = Attachment.find_by_id(params[:fileid]);
  49. respond_to do |format|
  50. format.html{render :layout => nil,:locals=>{:file=>attachment,:course=>@course}}
  51. end
  52. end
  53. def search
  54. sort = ""
  55. @sort = ""
  56. @order = ""
  57. @is_remote = true
  58. @q = params[:name].strip
  59. if params[:sort]
  60. order_by = params[:sort].split(":")
  61. @sort = order_by[0]
  62. if order_by.count > 1
  63. @order = order_by[1]
  64. end
  65. sort = "#{@sort} #{@order}"
  66. end
  67. # show_attachments [@course]
  68. begin
  69. q = "%#{params[:name].strip}%"
  70. #(redirect_to stores_url, :notice => l(:label_sumbit_empty);return) if params[:name].blank?
  71. if params[:insite]
  72. if q == "%%"
  73. @result = []
  74. # @searched_attach = paginateHelper @result,10
  75. else
  76. @result = find_public_attache q,sort
  77. @result = visable_attachemnts_insite @result,@course
  78. # @searched_attach = paginateHelper @result,10
  79. end
  80. else
  81. @result = find_course_attache q,@course,sort
  82. @result = visable_attachemnts @result
  83. # @searched_attach = paginateHelper @result,10
  84. @tag_list = get_course_tag_list @course
  85. end
  86. @all_attachments = @result
  87. get_attachment_for_tip(@all_attachments)
  88. @limit = 10
  89. @feedback_count = @all_attachments.count
  90. @feedback_pages = Paginator.new @feedback_count, @limit, params['page']
  91. @offset ||= @feedback_pages.offset
  92. #@curse_attachments_all = @all_attachments[@offset, @limit]
  93. @obj_attachments = paginateHelper @all_attachments,10
  94. #rescue Exception => e
  95. # #render 'stores'
  96. # redirect_to search_course_files_url
  97. end
  98. end
  99. def file_hidden
  100. @file = Attachment.find params[:id]
  101. @course = Course.find params[:course_id]
  102. respond_to do |format|
  103. format.js
  104. end
  105. end
  106. def republish_file
  107. @file = Attachment.find params[:id]
  108. @course = Course.find params[:course_id]
  109. if params[:publish_time]
  110. unless params[:publish_time] == ""
  111. @file.publish_time = params[:publish_time]
  112. end
  113. end
  114. if @file.publish_time > Date.today
  115. @file.is_publish = 0
  116. else
  117. @file.is_publish = 1
  118. end
  119. @file.save
  120. respond_to do |format|
  121. format.js
  122. end
  123. end
  124. def search_project
  125. sort = ""
  126. @sort = ""
  127. @order = ""
  128. @is_remote = true
  129. @q = params[:name].strip
  130. if params[:sort]
  131. order_by = params[:sort].split(":")
  132. @sort = order_by[0]
  133. if order_by.count > 1
  134. @order = order_by[1]
  135. end
  136. sort = "#{@sort} #{@order}"
  137. end
  138. # show_attachments [@course]
  139. begin
  140. q = "%#{params[:name].strip}%"
  141. #(redirect_to stores_url, :notice => l(:label_sumbit_empty);return) if params[:name].blank?
  142. if params[:insite]
  143. if q == "%%"
  144. @project_attachment_result = []
  145. # @searched_attach = paginateHelper @project_attachment_result, 10
  146. else
  147. @project_attachment_result = find_public_attache q,sort
  148. @project_attachment_result = visable_attachemnts_insite @project_attachment_result, @project
  149. # @searched_attach = paginateHelper @project_attachment_result, 10
  150. end
  151. else
  152. @project_attachment_result = find_project_attache q, @project,sort
  153. @project_attachment_result = visable_attachemnts @project_attachment_result
  154. # @searched_attach = paginateHelper @project_attachment_result,10
  155. @tag_list = get_project_tag_list @project
  156. end
  157. @all_attachments = @project_attachment_result
  158. get_attachment_for_tip(@all_attachments)
  159. @limit = 10
  160. @feedback_count = @all_attachments.count
  161. @feedback_pages = Paginator.new @feedback_count, @limit, params['page']
  162. @offset ||= @feedback_pages.offset
  163. #@curse_attachments_all = @all_attachments[@offset, @limit]
  164. @obj_attachments = paginateHelper @all_attachments,10
  165. #rescue Exception => e
  166. # #render 'stores'
  167. # redirect_to search_course_files_url
  168. end
  169. end
  170. def search_files_in_subfield
  171. sort = ""
  172. @sort = ""
  173. @order = ""
  174. @is_remote = true
  175. @q = params[:name].strip
  176. if params[:sort]
  177. order_by = params[:sort].split(":")
  178. @sort = order_by[0]
  179. if order_by.count > 1
  180. @order = order_by[1]
  181. end
  182. sort = "#{@sort} #{@order}"
  183. end
  184. # show_attachments [@course]
  185. begin
  186. q = "%#{params[:name].strip}%"
  187. #(redirect_to stores_url, :notice => l(:label_sumbit_empty);return) if params[:name].blank?
  188. if params[:insite]
  189. if q == "%%"
  190. @result = []
  191. @searched_attach = paginateHelper @result,10
  192. else
  193. @result = find_public_attache q,sort
  194. @result = visable_attachemnts_insite @result,@org_subfield
  195. @searched_attach = paginateHelper @result,10
  196. end
  197. else
  198. @result = find_org_subfield_attache q,@org_subfield,sort
  199. @result = visable_attachemnts @result
  200. @searched_attach = paginateHelper @result,10
  201. @tag_list = attachment_tag_list @result
  202. end
  203. #rescue Exception => e
  204. # #render 'stores'
  205. # redirect_to search_course_files_url
  206. end
  207. @page = params[:page] || 1
  208. end
  209. def find_course_attache keywords,course,sort = ""
  210. if sort == ""
  211. sort = "created_on DESC"
  212. end
  213. if keywords != "%%"
  214. resultSet = Attachment.where("attachments.container_type = 'Course' And attachments.container_id = '#{course.id}' AND filename LIKE :like ", like: "%#{keywords}%").reorder(sort)
  215. else
  216. resultSet = Attachment.where("attachments.container_type = 'Course' And attachments.container_id = '#{course.id}' "). reorder(sort)
  217. end
  218. #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")
  219. end
  220. def find_org_subfield_attache keywords,org_subfield,sort = ""
  221. if sort == ""
  222. sort = "created_on DESC"
  223. end
  224. if keywords != "%%"
  225. resultSet = Attachment.where("attachments.container_type = 'OrgSubfield' And attachments.container_id = '#{org_subfield.id}' AND filename LIKE :like ", like: "%#{keywords}%").
  226. reorder(sort)
  227. else
  228. resultSet = Attachment.where("attachments.container_type = 'OrgSubfield' And attachments.container_id = '#{org_subfield.id}' "). reorder(sort)
  229. end
  230. #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")
  231. end
  232. def find_project_attache keywords,project,sort = ""
  233. if sort == ""
  234. sort = "created_on DESC"
  235. end
  236. if keywords != "%%"
  237. resultSet = Attachment.where("attachments.container_type = 'Project' And attachments.container_id = '#{project.id}' AND filename LIKE :like ", like: "%#{keywords}%").reorder(sort)
  238. else
  239. resultSet = Attachment.where("attachments.container_type = 'Project' And attachments.container_id = '#{project.id}' "). reorder(sort)
  240. end
  241. end
  242. def find_public_attache keywords,sort = ""
  243. # StoresController#search 将每条文件都查出来,再次进行判断过滤。---> resultSet.to_a.map
  244. # 此时内容不多速度还可,但文件增长,每条判断多则进行3-4次表连接。
  245. # 现在还木有思路 药丸
  246. if sort == ""
  247. sort = "created_on DESC"
  248. end
  249. resultSet = Attachment.where("attachments.container_type IS NOT NULL AND attachments.copy_from IS NULL AND filename LIKE :like ", like: "%#{keywords}%").
  250. reorder(sort)
  251. end
  252. def index
  253. # 顶部导航
  254. @project_menu_type = 4
  255. @flag = params[:flag] || false
  256. sort = ""
  257. @sort = ""
  258. @order = ""
  259. @is_remote = false
  260. if params[:project_id]
  261. # 更新资源申请消息为已读
  262. # ar_ids = ApplyResource.where("user_id =? and container_id =? and container_type =?", User.current.id, params[:project_id].to_i, "Project").map{|ar| ar.id}
  263. # cms = CourseMessage.where("course_message_type =? and user_id =? and course_message_id in (#{ar_ids.empty? ? '0': ar_ids.join(',')})", "ApplyResource", User.current.id)
  264. # cms.each do |cm|
  265. # cm.update_column(:viewed, true)
  266. # end
  267. # over
  268. @page = params[:page] ? params[:page].to_i + 1 : 2
  269. @container_type = 0
  270. if params[:sort]
  271. params[:sort].split(",").each do |sort_type|
  272. order_by = sort_type.split(":")
  273. case order_by[0]
  274. when "filename"
  275. attribute = "filename"
  276. when "size"
  277. attribute = "filesize"
  278. when "attach_type"
  279. attribute = "attachtype"
  280. when "content_type"
  281. attribute = "created_on"
  282. when "field_file_dense"
  283. attribute = "is_public"
  284. when "downloads"
  285. attribute = "downloads"
  286. when "created_on"
  287. attribute = "created_on"
  288. when "quotes"
  289. attribute = "quotes"
  290. else
  291. attribute = "created_on"
  292. end
  293. @sort = order_by[0]
  294. @order = order_by[1]
  295. if order_by.count == 1 && attribute
  296. sort += "#{Attachment.table_name}.#{attribute} asc "
  297. if sort_type != params[:sort].split(",").last
  298. sort += ","
  299. end
  300. elsif order_by.count == 2 && order_by[1]
  301. sort += "#{Attachment.table_name}.#{attribute} #{order_by[1]} "
  302. if sort_type != params[:sort].split(",").last
  303. sort += ","
  304. end
  305. end
  306. end
  307. else
  308. sort = "#{Attachment.table_name}.created_on desc"
  309. end
  310. # @containers = [ Project.includes(:attachments).find(@project.id)]
  311. # @containers += @project.versions.includes(:attachments).all
  312. @containers = [ Project.includes(:attachments).reorder(sort).find(@project.id)]
  313. show_attachments @containers
  314. get_attachment_for_tip(@all_attachments)
  315. @tag_list = attachment_tag_list @all_attachments
  316. render :layout => 'base_projects'
  317. elsif params[:course_id]
  318. @container_type = 1
  319. @page = params[:page] ? params[:page].to_i + 1 : 2
  320. if params[:sort]
  321. params[:sort].split(",").each do |sort_type|
  322. order_by = sort_type.split(":")
  323. case order_by[0]
  324. when "filename"
  325. attribute = "filename"
  326. when "size"
  327. attribute = "filesize"
  328. when "attach_type"
  329. attribute = "attachtype"
  330. when "content_type"
  331. attribute = "created_on"
  332. when "field_file_dense"
  333. attribute = "is_public"
  334. when "downloads"
  335. attribute = "downloads"
  336. when "created_on"
  337. attribute = "created_on"
  338. when "quotes"
  339. attribute = "quotes"
  340. else
  341. attribute = "created_on"
  342. end
  343. @sort = order_by[0]
  344. @order = order_by[1]
  345. if order_by.count == 1 && attribute
  346. sort += "#{Attachment.table_name}.#{attribute} asc "
  347. if sort_type != params[:sort].split(",").last
  348. sort += ","
  349. end
  350. elsif order_by.count == 2 && order_by[1]
  351. sort += "#{Attachment.table_name}.#{attribute} #{order_by[1]} "
  352. if sort_type != params[:sort].split(",").last
  353. sort += ","
  354. end
  355. end
  356. end
  357. else
  358. sort = "#{Attachment.table_name}.created_on desc"
  359. end
  360. @containers = [ Course.includes(:attachments).reorder(sort).find(@course.id)]
  361. show_attachments @containers
  362. get_attachment_for_tip(@all_attachments)
  363. @tag_list = attachment_tag_list @all_attachments
  364. @left_nav_type = 5
  365. render :layout => 'base_courses'
  366. elsif params[:org_subfield_id]
  367. if params[:sort]
  368. params[:sort].split(",").each do |sort_type|
  369. order_by = sort_type.split(":")
  370. case order_by[0]
  371. when "filename"
  372. attribute = "filename"
  373. when "size"
  374. attribute = "filesize"
  375. when "attach_type"
  376. attribute = "attachtype"
  377. when "content_type"
  378. attribute = "created_on"
  379. when "field_file_dense"
  380. attribute = "is_public"
  381. when "downloads"
  382. attribute = "downloads"
  383. when "created_on"
  384. attribute = "created_on"
  385. when "quotes"
  386. attribute = "quotes"
  387. else
  388. attribute = "created_on"
  389. end
  390. @sort = order_by[0]
  391. @order = order_by[1]
  392. if order_by.count == 1 && attribute
  393. sort += "#{Attachment.table_name}.#{attribute} asc "
  394. if sort_type != params[:sort].split(",").last
  395. sort += ","
  396. end
  397. elsif order_by.count == 2 && order_by[1]
  398. sort += "#{Attachment.table_name}.#{attribute} #{order_by[1]} "
  399. if sort_type != params[:sort].split(",").last
  400. sort += ","
  401. end
  402. end
  403. end
  404. else
  405. sort = "#{Attachment.table_name}.created_on desc"
  406. end
  407. @container_type = 2
  408. @containers = [ OrgSubfield.includes(:attachments).reorder(sort).find(@org_subfield.id)]
  409. @organization = Organization.find(@containers.first.organization_id)
  410. if @organization.is_public? or User.current.admin? or User.current.member_of_org?(@organization)
  411. show_attachments @containers
  412. @tag_list = attachment_tag_list @all_attachments
  413. @page = params[:page] || 1
  414. render :layout => 'base_org'
  415. else
  416. render_403
  417. end
  418. # @subfield = params[:org_subfield_id]
  419. end
  420. end
  421. # 获取提示中私有、公开总数信息
  422. def get_attachment_for_tip all_attachment
  423. @tip_all_public_attachments = all_attachment.select{|attach| attach.is_public == 1}.count
  424. if params[:project_id]
  425. if User.current.member_of?(@project) || User.current.admin?
  426. @tip_all_attachments = all_attachment.count
  427. @tip_all_private_attachments = all_attachment.select{|attach| attach.is_public == 0}.count
  428. else
  429. if params[:tag_name]
  430. @tip_all_attachments = all_attachment.count
  431. @tip_all_private_attachments = 0
  432. else
  433. @tip_all_attachments = Attachment.where(:container_id => params[:project_id], :container_type => "Project").count
  434. @tip_all_private_attachments = Attachment.where(:container_id => params[:project_id], :container_type => "Project", :is_public => 0).count
  435. end
  436. end
  437. elsif params[:course_id]
  438. if User.current.member_of_course?(@course) || User.current.admin?
  439. @tip_all_attachments = all_attachment.count
  440. @tip_all_private_attachments = all_attachment.select{|attach| attach.is_public == 0}.count
  441. else
  442. if params[:tag_name]
  443. @tip_all_attachments = all_attachment.count
  444. @tip_all_private_attachments = 0
  445. else
  446. @tip_all_attachments = Attachment.where(:container_id => params[:course_id], :container_type => "Course").count
  447. @tip_all_private_attachments = Attachment.where(:container_id => params[:course_id], :container_type => "Course", :is_public => 0).count
  448. end
  449. end
  450. end
  451. end
  452. def quote_resource_show
  453. @file = Attachment.find(params[:id])
  454. @can_quote = attachment_candown @file
  455. end
  456. def quote_resource_show_project
  457. @file = Attachment.find(params[:id])
  458. @can_quote = attachment_candown @file
  459. end
  460. def quote_resource_show_org_subfield
  461. @file = Attachment.find(params[:id])
  462. @org_subfield = OrgSubfield.find(params[:org_subfield_id])
  463. @can_quote = attachment_candown @file
  464. end
  465. def new
  466. @versions = @project.versions.sort
  467. @course_tag = @project.project_type
  468. if @project.project_type == 1
  469. @left_nav_type = 5
  470. render :layout => 'base_courses'
  471. end
  472. end
  473. def create
  474. if params[:add_tag]
  475. @addTag=true
  476. #render :back
  477. tag_saveEx
  478. #render :text =>"success"
  479. respond_to do |format|
  480. format.js
  481. end
  482. else
  483. #modify by nwb
  484. if @project
  485. @addTag=false
  486. if params[:in_project_toolbar]
  487. @in_project_toolbar = params[:in_project_toolbar]
  488. end
  489. attachments = Attachment.attach_filesex(@project, params[:attachments], params[:attachment_type])
  490. if !attachments.empty? && !attachments[:files].blank? && Setting.notified_events.include?('file_added')
  491. # 发送邮件
  492. Mailer.run.attachments_added(attachments[:files])
  493. # 生成动态
  494. attachments[:files].each do |file|
  495. ForgeActivity.create(:user_id => User.current.id, :project_id => @project.id, :forge_act_id => file.id, :forge_act_type => "Attachment")
  496. end
  497. # 更新资源总数, 根据上传的附件数累加
  498. @project.project_score.update_attribute(:attach_num, @project.project_score.attach_num + attachments[:files].count) unless @project.project_score.nil?
  499. end
  500. # end
  501. if params[:project_attachment_type] && params[:project_attachment_type].is_a?(Array)
  502. params[:project_attachment_type].each do |type|
  503. tag_name = get_project_tag_name_by_type_nmuber type
  504. if !attachments.empty? && attachments[:files] && tag_name != ""
  505. attachments[:files].each do |attachment|
  506. attachment.tag_list.add(tag_name)
  507. attachment.description = params[:description]
  508. attachment.save
  509. end
  510. end
  511. end
  512. else
  513. if params[:project_attachment_type] && params[:project_attachment_type] != "6"
  514. tag_name = get_project_tag_name_by_type_nmuber params[:project_attachment_type]
  515. if !attachments.empty? && attachments[:files] && tag_name != ""
  516. attachments[:files].each do |attachment|
  517. attachment.tag_list.add(tag_name)
  518. attachment.description = params[:description]
  519. attachment.save
  520. end
  521. end
  522. end
  523. end
  524. # TODO: 临时用 nyan
  525. sort_init 'created_on', 'desc'
  526. sort_update 'created_on' => "#{Attachment.table_name}.created_on",
  527. 'filename' => "#{Attachment.table_name}.filename",
  528. 'size' => "#{Attachment.table_name}.filesize",
  529. 'downloads' => "#{Attachment.table_name}.downloads"
  530. @containers = [Project.includes(:attachments).reorder("#{Attachment.table_name}.created_on DESC").find(@project.id)]
  531. show_attachments @containers
  532. @tag_list = attachment_tag_list @all_attachments
  533. @attachtype = 0
  534. @contenttype = 0
  535. respond_to do |format|
  536. format.js
  537. format.html {
  538. redirect_to project_files_url(@project)
  539. }
  540. end
  541. elsif @course
  542. @addTag=false
  543. if params[:in_course_toolbar]
  544. @in_course_toolbar = params[:in_course_toolbar]
  545. end
  546. attachments = Attachment.attach_filesex(@course, params[:attachments], params[:attachment_type])
  547. if !attachments.empty? && !attachments[:files].blank? && Setting.notified_events.include?('file_added')
  548. Mailer.run.attachments_added(attachments[:files])
  549. end
  550. if !attachments.empty? && attachments[:files]
  551. attachments[:files].each do |attachment|
  552. if params[:publish_time]
  553. if params[:publish_time] == ""
  554. attachment.publish_time = Date.today
  555. else
  556. attachment.publish_time = params[:publish_time]
  557. end
  558. else
  559. attachment.publish_time = Date.today
  560. end
  561. if attachment.publish_time > Date.today
  562. attachment.is_publish = 0
  563. end
  564. attachment.description = params[:description]
  565. attachment.save
  566. AttachmentsCache.update_course_attachments_cache(@course.id,attachment.id,0,{})
  567. end
  568. end
  569. if params[:course_attachment_type] && params[:course_attachment_type].is_a?(Array)
  570. params[:course_attachment_type].each do |type|
  571. tag_name = get_tag_name_by_type_number type
  572. if !attachments.empty? && attachments[:files] && tag_name != ""
  573. attachments[:files].each do |attachment|
  574. attachment.tag_list.add(tag_name)
  575. attachment.save
  576. end
  577. end
  578. end
  579. else
  580. if params[:course_attachment_type] && params[:course_attachment_type] != "5"
  581. tag_name = get_tag_name_by_type_number params[:course_attachment_type]
  582. if !attachments.empty? && attachments[:files] && tag_name != ""
  583. attachments[:files].each do |attachment|
  584. attachment.tag_list.add(tag_name)
  585. attachment.save
  586. end
  587. end
  588. end
  589. end
  590. # 更新课程英雄榜得分
  591. course_member_score(@course.id, attachments[:files].first.author_id, "Attachment")
  592. # end
  593. # end
  594. # TODO: 临时用 nyan
  595. sort_init 'created_on', 'desc'
  596. sort_update 'created_on' => "#{Attachment.table_name}.created_on",
  597. 'filename' => "#{Attachment.table_name}.filename",
  598. 'size' => "#{Attachment.table_name}.filesize",
  599. 'downloads' => "#{Attachment.table_name}.downloads"
  600. @containers = [Course.includes(:attachments).reorder("#{Attachment.table_name}.created_on DESC").find(@course.id)]
  601. show_attachments @containers
  602. @tag_list = attachment_tag_list @all_attachments
  603. @attachtype = 0
  604. @contenttype = 0
  605. respond_to do |format|
  606. format.js
  607. format.html {
  608. redirect_to course_files_url(@course)
  609. }
  610. end
  611. elsif @org_subfield
  612. @addTag=false
  613. attachments = Attachment.attach_filesex(@org_subfield, params[:attachments], params[:org_subfield_attachment_type])
  614. if params[:org_subfield_attachment_type] && params[:org_subfield_attachment_type].is_a?(Array)
  615. params[:org_subfield_attachment_type].each do |type|
  616. tag_name = get_tag_name_by_type_number type
  617. if !attachments.empty? && attachments[:files] && tag_name != ""
  618. attachments[:files].each do |attachment|
  619. attachment.tag_list.add(tag_name)
  620. attachment.description = params[:description]
  621. attachment.save
  622. end
  623. end
  624. end
  625. else
  626. if params[:org_subfield_attachment_type] && params[:org_subfield_attachment_type] != "5"
  627. tag_name = get_tag_name_by_type_number params[:org_subfield_attachment_type]
  628. if !attachments.empty? && attachments[:files] && tag_name != ""
  629. attachments[:files].each do |attachment|
  630. attachment.tag_list.add(tag_name)
  631. attachment.description = params[:description]
  632. attachment.save
  633. end
  634. end
  635. end
  636. end
  637. # TODO: 临时用 nyan
  638. sort_init 'created_on', 'desc'
  639. sort_update 'created_on' => "#{Attachment.table_name}.created_on",
  640. 'filename' => "#{Attachment.table_name}.filename",
  641. 'size' => "#{Attachment.table_name}.filesize",
  642. 'downloads' => "#{Attachment.table_name}.downloads"
  643. @containers = [OrgSubfield.includes(:attachments).reorder("#{Attachment.table_name}.created_on DESC").find(@org_subfield.id)]
  644. show_attachments @containers
  645. @tag_list = attachment_tag_list @all_attachments
  646. @attachtype = 0
  647. @contenttype = 0
  648. respond_to do |format|
  649. format.js
  650. # format.html {
  651. # redirect_to org_subfield_files_url(@org_subfield)
  652. # }
  653. end
  654. # 组织添加附件,为了修改图片
  655. elsif params[:organization_id]
  656. @organization = Organization.find(params[:organization_id])
  657. @addTag=false
  658. # atttchment_type = 0为logo 1为banner
  659. if params[:logo]
  660. attachments = Attachment.attach_filesex(@organization, params[:attachments], false)
  661. else
  662. attachments = Attachment.attach_filesex(@organization, params[:attachments], true)
  663. end
  664. # TODO: 临时用 nyan
  665. sort_init 'created_on', 'desc'
  666. sort_update 'created_on' => "#{Attachment.table_name}.created_on",
  667. 'filename' => "#{Attachment.table_name}.filename",
  668. 'size' => "#{Attachment.table_name}.filesize",
  669. 'downloads' => "#{Attachment.table_name}.downloads"
  670. @containers = [Organization.includes(:attachments).reorder("#{Attachment.table_name}.created_on DESC").find(@organization.id)]
  671. show_attachments @containers
  672. @tag_list = attachment_tag_list @all_attachments
  673. @attachtype = 0
  674. @contenttype = 0
  675. respond_to do |format|
  676. format.js
  677. # format.html {
  678. # redirect_to org_subfield_files_url(@org_subfield)
  679. # }
  680. end
  681. end
  682. end
  683. end
  684. def get_project_tag_name_by_type_nmuber type
  685. case type
  686. when "1"
  687. tag_name = "软件版本"
  688. when "2"
  689. tag_name = "文档"
  690. when "3"
  691. tag_name = "代码"
  692. when "4"
  693. tag_name = "媒体"
  694. when "5"
  695. tag_name = "论文"
  696. when "6"
  697. tag_name = "其它"
  698. else
  699. tag_name = ""
  700. end
  701. end
  702. def get_tag_name_by_type_number type
  703. case type
  704. when "1"
  705. tag_name = l(:label_courseware)
  706. when "2"
  707. tag_name = l(:label_software)
  708. when "3"
  709. tag_name = l(:label_media)
  710. when "4"
  711. tag_name = l(:label_code)
  712. when "6"
  713. tag_name = "论文"
  714. else
  715. tag_name = ""
  716. end
  717. end
  718. def tag_saveEx
  719. @tags = params[:tag_name][:name]
  720. @obj_id = params[:object_id]
  721. @obj_flag = params[:object_flag]
  722. case @obj_flag
  723. when '1' then
  724. @obj = User.find_by_id(@obj_id)
  725. when '2' then
  726. @obj = Project.find_by_id(@obj_id)
  727. when '3' then
  728. @obj = Issue.find_by_id(@obj_id)
  729. when '4' then
  730. @obj = Bid.find_by_id(@obj_id)
  731. when '5' then
  732. @obj = Forum.find_by_id(@obj_id)
  733. when '6'
  734. @obj = Attachment.find_by_id(@obj_id)
  735. when '7' then
  736. @obj = Contest.find_by_id(@obj_id)
  737. when '8'
  738. @obj = OpenSourceProject.find_by_id(@obj_id)
  739. when '9'
  740. @obj = Course.find_by_id(@obj_id)
  741. else
  742. @obj = nil
  743. end
  744. unless @obj.nil?
  745. @obj.tag_list.add(@tags.split(","))
  746. else
  747. return
  748. end
  749. if @obj.save
  750. ## 执行成功的操作。
  751. else
  752. #捕获异常
  753. end
  754. end
  755. # 返回指定资源类型的资源列表
  756. # added by nwb
  757. def getattachtype
  758. sort_init 'created_on', 'desc'
  759. sort_update 'created_on' => "#{Attachment.table_name}.created_on",
  760. 'filename' => "#{Attachment.table_name}.filename",
  761. 'size' => "#{Attachment.table_name}.filesize",
  762. 'downloads' => "#{Attachment.table_name}.downloads"
  763. sort=''
  764. if params[:sort]
  765. params[:sort].split(",").each do |sort_type|
  766. order_by = sort_type.split(":")
  767. case order_by[0]
  768. when "filename"
  769. attribute = "filename"
  770. when "size"
  771. attribute = "filesize"
  772. when "attach_type"
  773. attribute = "attachtype"
  774. when "content_type"
  775. attribute = "created_on"
  776. when "field_file_dense"
  777. attribute = "is_public"
  778. when "downloads"
  779. attribute = "downloads"
  780. when "created_on"
  781. attribute = "created_on"
  782. end
  783. if order_by.count == 1
  784. sort += "#{Attachment.table_name}.#{attribute} asc "
  785. elsif order_by.count == 2
  786. sort += "#{Attachment.table_name}.#{attribute} #{order_by[1]} "
  787. end
  788. if sort_type != params[:sort].split(",").last
  789. sort += ","
  790. end
  791. end
  792. end
  793. if @project
  794. @isproject = true
  795. @containers = [ Project.includes(:attachments).reorder("#{Attachment.table_name}.created_on DESC").find(@project.id)]
  796. @containers += @project.versions.includes(:attachments).reorder(sort).all
  797. show_attachments @containers
  798. @attachtype = params[:type].to_i
  799. @contenttype = params[:contentType].to_s
  800. respond_to do |format|
  801. format.js
  802. format.html {
  803. render :layout => 'base_projects'
  804. }
  805. end
  806. elsif @course
  807. @isproject = false
  808. @containers = [ Course.includes(:attachments).reorder(sort).find(@course.id)]
  809. show_attachments @containers
  810. @attachtype = params[:type].to_i
  811. @contenttype = params[:contentType].to_s
  812. # render layout: 'base_courses'
  813. @left_nav_type = 5
  814. respond_to do |format|
  815. format.js
  816. format.html {
  817. render :layout => 'base_courses'
  818. }
  819. end
  820. else
  821. show_attachments @containers
  822. @attachtype = params[:type].to_i
  823. @contenttype = params[:contentType].to_s
  824. respond_to do |format|
  825. format.js
  826. format.html
  827. end
  828. end
  829. end
  830. #查找指定TAG的按条件过滤的资源列表,只有课程内搜索有此功能
  831. def search_tag_attachment
  832. @q,@tag_name,@order = params[:q],params[:tag_name]
  833. @is_remote = true
  834. if params[:sort]
  835. order_by = params[:sort].split(":")
  836. @sort = order_by[0]
  837. if order_by.count > 1
  838. @order = order_by[1]
  839. end
  840. sort = "#{@sort} #{@order}"
  841. end
  842. q = "%#{@q.strip}%"
  843. if @project
  844. @result = find_project_attache q, @project,sort
  845. else
  846. @result = find_course_attache q,@course,sort
  847. end
  848. @result = visable_attachemnts @result
  849. if params[:other]
  850. if @project
  851. @result = @result.select{|attachment| !attachment.tag_list.include?('软件版本') && !attachment.tag_list.include?('文档') && !attachment.tag_list.include?('代码') && !attachment.tag_list.include?('媒体') && !attachment.tag_list.include?('论文') }
  852. else
  853. @result = @result.select{|attachment|
  854. !attachment.tag_list.include?('课件') &&
  855. !attachment.tag_list.include?('软件') &&
  856. !attachment.tag_list.include?('媒体') &&
  857. !attachment.tag_list.include?('代码') &&
  858. !attachment.tag_list.include?('论文') }
  859. end
  860. else
  861. @result = @result.select{|attachment| attachment.tag_list.include?(@tag_name)} unless @tag_name.blank?
  862. end
  863. @all_attachments = @result
  864. get_attachment_for_tip(@all_attachments)
  865. @limit = 10
  866. @feedback_count = @all_attachments.count
  867. @feedback_pages = Paginator.new @feedback_count, @limit, params['page']
  868. @offset ||= @feedback_pages.offset
  869. #@curse_attachments_all = @all_attachments[@offset, @limit]
  870. @obj_attachments = paginateHelper @all_attachments,10
  871. # @searched_attach = paginateHelper @result,10
  872. if @project
  873. @tag_list = get_project_tag_list @project
  874. @result_search_project = @result
  875. else
  876. @tag_list = get_course_tag_list @course
  877. end
  878. respond_to do |format|
  879. format.js
  880. # format.html
  881. end
  882. end
  883. #搜索资源栏目的指定TAG的资源列表
  884. def search_org_subfield_tag_attachment
  885. @q,@tag_name,@order = params[:q],params[:tag_name]
  886. @is_remote = true
  887. if params[:sort]
  888. order_by = params[:sort].split(":")
  889. @sort = order_by[0]
  890. if order_by.count > 1
  891. @order = order_by[1]
  892. end
  893. sort = "#{@sort} #{@order}"
  894. end
  895. q = "%#{@q.strip}%"
  896. @result = find_org_subfield_attache q,@org_subfield,sort
  897. @result = visable_attachemnts @result
  898. if params[:other]
  899. @result = @result.select{|attachment|
  900. attachment.tag_list.index{|tag|tag != '软件' and tag != '媒体' and tag != '代码'}.present?
  901. }
  902. else
  903. @result = @result.select{|attachment| attachment.tag_list.include?(@tag_name)} unless @tag_name.blank?
  904. end
  905. @searched_attach = paginateHelper @result,10
  906. @tag_list = get_org_subfield_tag_list @org_subfield
  907. respond_to do |format|
  908. format.js
  909. # format.html
  910. end
  911. end
  912. def subfield_upload_file
  913. @org_subfield = OrgSubfield.find(params[:org_subfield_id])
  914. end
  915. def upload_files_menu
  916. end
  917. def edit_file_description
  918. @attachment = Attachment.find(params[:id])
  919. @attachment.description = params[:file_description_edit]
  920. @attachment.save
  921. end
  922. def update_file_description
  923. @attachment = Attachment.find(params[:id])
  924. @attachment.description = params[:description]
  925. @attachment.save
  926. end
  927. end

软件技术创新是当今国家整体创新能力的重要基础和发展引擎。近年来,大众参与的软件创新与创业活动已经成为网络时代软件开发和应用的新形态,正在快速改变着全球软件创新模式和软件产业格局。 ...