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

issues_controller.rb 16KB

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