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.

issues_controller.rb 17 kB

13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
13 years ago
​
​
​
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
13 years ago
​
​
13 years ago
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2013 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class IssuesController < ApplicationController
  18. layout 'base_projects'#Added by young
  19. menu_item :new_issue, :only => [:new, :create]
  20. default_search_scope :issues
  21. before_filter :find_issue, :only => [:show, :edit, :update]
  22. before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy]
  23. before_filter :find_project, :only => [:new, :create, :update_form]
  24. before_filter :authorize, :except => [:index]
  25. before_filter :find_optional_project, :only => [:index]
  26. before_filter :check_for_default_issue_status, :only => [:new, :create]
  27. before_filter :build_new_issue_from_params, :only => [:new, :create, :update_form]
  28. accept_rss_auth :index, :show
  29. accept_api_auth :index, :show, :create, :update, :destroy
  30. rescue_from Query::StatementInvalid, :with => :query_statement_invalid
  31. helper :journals
  32. helper :projects
  33. include ProjectsHelper
  34. helper :custom_fields
  35. include CustomFieldsHelper
  36. helper :issue_relations
  37. include IssueRelationsHelper
  38. helper :watchers
  39. include WatchersHelper
  40. helper :attachments
  41. include AttachmentsHelper
  42. helper :queries
  43. include QueriesHelper
  44. helper :repositories
  45. include RepositoriesHelper
  46. helper :sort
  47. include SortHelper
  48. include IssuesHelper
  49. helper :timelog
  50. include Redmine::Export::PDF
  51. def index
  52. retrieve_query
  53. sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
  54. sort_update(@query.sortable_columns)
  55. @query.sort_criteria = sort_criteria.to_a
  56. @project_base_tag = (params[:project_id] || @issue.project) ? 'base_projects':'base'#by young
  57. if @query.valid?
  58. case params[:format]
  59. when 'csv', 'pdf'
  60. @limit = 10#Setting.issues_export_limit.to_i
  61. when 'atom'
  62. @limit = 10#Setting.feeds_limit.to_i
  63. when 'xml', 'json'
  64. @offset, @limit = api_offset_and_limit({:limit => 10})
  65. else
  66. @limit = 10#per_page_option
  67. end
  68. @issue_count = @query.issue_count
  69. @issue_pages = Paginator.new @issue_count, @limit, params['page']
  70. @offset ||= @issue_pages.offset
  71. @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
  72. :order => sort_clause,
  73. :offset => @offset,
  74. :limit => @limit)
  75. @issue_count_by_group = @query.issue_count_by_group
  76. respond_to do |format|
  77. format.html { render :template => 'issues/index', :layout => @project_base_tag }#by young
  78. format.api {
  79. Issue.load_visible_relations(@issues) if include_in_api_response?('relations')
  80. }
  81. format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
  82. format.csv { send_data(query_to_csv(@issues, @query, params), :type => 'text/csv; header=present', :filename => 'issues.csv') }
  83. format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'issues.pdf') }
  84. end
  85. else
  86. respond_to do |format|
  87. format.html { render(:template => 'issues/index', :layout => @project_base_tag) }#by young
  88. format.any(:atom, :csv, :pdf) { render(:nothing => true) }
  89. format.api { render_validation_errors(@query) }
  90. end
  91. end
  92. rescue ActiveRecord::RecordNotFound
  93. render_404
  94. end
  95. def show
  96. @journals = @issue.journals.includes(:user, :details).reorder("#{Journal.table_name}.id ASC").all
  97. @journals.each_with_index {|j,i| j.indice = i+1}
  98. @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
  99. @journals.reverse! if User.current.wants_comments_in_reverse_order?
  100. @changesets = @issue.changesets.visible.all
  101. @changesets.reverse! if User.current.wants_comments_in_reverse_order?
  102. @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
  103. @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
  104. @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
  105. @priorities = IssuePriority.active
  106. @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
  107. @project_base_tag = (params[:project_id] || @issue.project) ? 'base_projects':'base'#by young
  108. respond_to do |format|
  109. format.html {
  110. retrieve_previous_and_next_issue_ids
  111. render :template => 'issues/show', :layout => @project_base_tag#by young
  112. }
  113. format.api
  114. format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
  115. format.pdf {
  116. pdf = issue_to_pdf(@issue, :journals => @journals)
  117. send_data(pdf, :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf")
  118. }
  119. end
  120. end
  121. # Add a new issue
  122. # The new issue will be created from an existing one if copy_from parameter is given
  123. def new
  124. respond_to do |format|
  125. format.html { render :action => 'new', :layout => 'base_projects' }
  126. end
  127. end
  128. def create
  129. call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
  130. @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
  131. if @issue.save
  132. call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
  133. respond_to do |format|
  134. format.html {
  135. render_attachment_warning_if_needed(@issue)
  136. flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("#{@issue.source_from}", issue_path(@issue), :title => @issue.subject))
  137. #flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject))
  138. if params[:continue]
  139. attrs = {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?}
  140. redirect_to new_project_issue_path(@issue.project, :issue => attrs)
  141. else
  142. redirect_to issue_path(@issue)
  143. end
  144. }
  145. format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
  146. end
  147. return
  148. else
  149. respond_to do |format|
  150. format.html { render :action => 'new' }
  151. format.api { render_validation_errors(@issue) }
  152. end
  153. end
  154. end
  155. def edit
  156. return unless update_issue_from_params
  157. respond_to do |format|
  158. format.html {render :layout => 'base_projects' }#added by young
  159. format.xml { }
  160. end
  161. end
  162. def update
  163. return unless update_issue_from_params
  164. @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
  165. saved = false
  166. begin
  167. saved = @issue.save_issue_with_child_records(params, @time_entry)
  168. rescue ActiveRecord::StaleObjectError
  169. @conflict = true
  170. if params[:last_journal_id]
  171. @conflict_journals = @issue.journals_after(params[:last_journal_id]).all
  172. @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
  173. end
  174. end
  175. if saved
  176. render_attachment_warning_if_needed(@issue)
  177. reply_id = params[:reference_user_id].to_i
  178. if reply_id > 0
  179. JournalReply.add_reply(@issue.current_journal.id, reply_id, User.current.id)
  180. end
  181. flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
  182. respond_to do |format|
  183. format.html { redirect_back_or_default issue_path(@issue) }
  184. format.api { render_api_ok }
  185. end
  186. else
  187. respond_to do |format|
  188. format.html { render :action => 'edit' }
  189. format.api { render_validation_errors(@issue) }
  190. end
  191. end
  192. end
  193. # Updates the issue form when changing the project, status or tracker
  194. # on issue creation/update
  195. def update_form
  196. end
  197. # Bulk edit/copy a set of issues
  198. def bulk_edit
  199. @issues.sort!
  200. @copy = params[:copy].present?
  201. @notes = params[:notes]
  202. if User.current.allowed_to?(:move_issues, @projects)
  203. @allowed_projects = Issue.allowed_target_projects_on_move
  204. if params[:issue]
  205. @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
  206. if @target_project
  207. target_projects = [@target_project]
  208. end
  209. end
  210. end
  211. target_projects ||= @projects
  212. if @copy
  213. @available_statuses = [IssueStatus.default]
  214. else
  215. @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
  216. end
  217. @custom_fields = target_projects.map{|p|p.all_issue_custom_fields}.reduce(:&)
  218. @assignables = target_projects.map(&:assignable_users).reduce(:&)
  219. @trackers = target_projects.map(&:trackers).reduce(:&)
  220. @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
  221. @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
  222. if @copy
  223. @attachments_present = @issues.detect {|i| i.attachments.any?}.present?
  224. @subtasks_present = @issues.detect {|i| !i.leaf?}.present?
  225. end
  226. @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&)
  227. render :layout => false if request.xhr?
  228. end
  229. def bulk_update
  230. @issues.sort!
  231. @copy = params[:copy].present?
  232. attributes = parse_params_for_bulk_issue_attributes(params)
  233. unsaved_issue_ids = []
  234. moved_issues = []
  235. if @copy && params[:copy_subtasks].present?
  236. # Descendant issues will be copied with the parent task
  237. # Don't copy them twice
  238. @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}}
  239. end
  240. @issues.each do |issue|
  241. issue.reload
  242. if @copy
  243. issue = issue.copy({},
  244. :attachments => params[:copy_attachments].present?,
  245. :subtasks => params[:copy_subtasks].present?
  246. )
  247. end
  248. journal = issue.init_journal(User.current, params[:notes])
  249. issue.safe_attributes = attributes
  250. call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
  251. if issue.save
  252. moved_issues << issue
  253. else
  254. # Keep unsaved issue ids to display them in flash error
  255. unsaved_issue_ids << issue.id
  256. end
  257. end
  258. set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
  259. if params[:follow]
  260. if @issues.size == 1 && moved_issues.size == 1
  261. redirect_to issue_path(moved_issues.first)
  262. elsif moved_issues.map(&:project).uniq.size == 1
  263. redirect_to project_issues_path(moved_issues.map(&:project).first)
  264. end
  265. else
  266. redirect_back_or_default _project_issues_path(@project)
  267. end
  268. end
  269. def destroy
  270. @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
  271. if @hours > 0
  272. case params[:todo]
  273. when 'destroy'
  274. # nothing to do
  275. when 'nullify'
  276. TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
  277. when 'reassign'
  278. reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
  279. if reassign_to.nil?
  280. flash.now[:error] = l(:error_issue_not_found_in_project)
  281. return
  282. else
  283. TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
  284. end
  285. else
  286. # display the destroy form if it's a user request
  287. return unless api_request?
  288. end
  289. end
  290. @issues.each do |issue|
  291. begin
  292. issue.reload.destroy
  293. rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
  294. # nothing to do, issue was already deleted (eg. by a parent)
  295. end
  296. end
  297. respond_to do |format|
  298. format.html { redirect_back_or_default _project_issues_path(@project) }
  299. format.api { render_api_ok }
  300. end
  301. end
  302. private
  303. def find_project
  304. project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id])
  305. @project = Project.find(project_id)
  306. rescue ActiveRecord::RecordNotFound
  307. render_404
  308. end
  309. def retrieve_previous_and_next_issue_ids
  310. retrieve_query_from_session
  311. if @query
  312. sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
  313. sort_update(@query.sortable_columns, 'issues_index_sort')
  314. limit = 500
  315. issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version])
  316. if (idx = issue_ids.index(@issue.id)) && idx < limit
  317. if issue_ids.size < 500
  318. @issue_position = idx + 1
  319. @issue_count = issue_ids.size
  320. end
  321. @prev_issue_id = issue_ids[idx - 1] if idx > 0
  322. @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
  323. end
  324. end
  325. end
  326. # Used by #edit and #update to set some common instance variables
  327. # from the params
  328. # TODO: Refactor, not everything in here is needed by #edit
  329. def update_issue_from_params
  330. @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
  331. @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
  332. @time_entry.attributes = params[:time_entry]
  333. @issue.init_journal(User.current)
  334. issue_attributes = params[:issue]
  335. if issue_attributes && params[:conflict_resolution]
  336. case params[:conflict_resolution]
  337. when 'overwrite'
  338. issue_attributes = issue_attributes.dup
  339. issue_attributes.delete(:lock_version)
  340. when 'add_notes'
  341. issue_attributes = issue_attributes.slice(:notes)
  342. when 'cancel'
  343. redirect_to issue_path(@issue)
  344. return false
  345. end
  346. end
  347. @issue.safe_attributes = issue_attributes
  348. @priorities = IssuePriority.active
  349. @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
  350. true
  351. end
  352. # TODO: Refactor, lots of extra code in here
  353. # TODO: Changing tracker on an existing issue should not trigger this
  354. def build_new_issue_from_params
  355. if params[:id].blank?
  356. @issue = Issue.new
  357. if params[:copy_from]
  358. begin
  359. @copy_from = Issue.visible.find(params[:copy_from])
  360. @copy_attachments = params[:copy_attachments].present? || request.get?
  361. @copy_subtasks = params[:copy_subtasks].present? || request.get?
  362. @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks)
  363. rescue ActiveRecord::RecordNotFound
  364. render_404
  365. return
  366. end
  367. end
  368. @issue.project = @project
  369. else
  370. @issue = @project.issues.visible.find(params[:id])
  371. end
  372. @issue.project = @project
  373. @issue.author ||= User.current
  374. # Tracker must be set before custom field values
  375. @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
  376. if @issue.tracker.nil?
  377. render_error l(:error_no_tracker_in_project)
  378. return false
  379. end
  380. @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date?
  381. @issue.safe_attributes = params[:issue]
  382. @priorities = IssuePriority.active
  383. @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
  384. @available_watchers = (@issue.project.users.sort + @issue.watcher_users).uniq
  385. end
  386. def check_for_default_issue_status
  387. if IssueStatus.default.nil?
  388. render_error l(:error_no_default_issue_status)
  389. return false
  390. end
  391. end
  392. def parse_params_for_bulk_issue_attributes(params)
  393. attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
  394. attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
  395. if custom = attributes[:custom_field_values]
  396. custom.reject! {|k,v| v.blank?}
  397. custom.keys.each do |k|
  398. if custom[k].is_a?(Array)
  399. custom[k] << '' if custom[k].delete('__none__')
  400. else
  401. custom[k] = '' if custom[k] == '__none__'
  402. end
  403. end
  404. end
  405. attributes
  406. end
  407. end

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