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 34 kB

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

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