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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2012 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]
  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]
  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. helper :gantt
  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. 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 self, @issue_count, @limit, params['page']
  68. @offset ||= @issue_pages.current.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_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(issues_to_csv(@issues, @project, @query, params), :type => 'text/csv; header=present', :filename => 'export.csv') }
  81. format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.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.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
  95. @journals.each_with_index {|j,i| j.indice = i+1}
  96. @journals.reverse! if User.current.wants_comments_in_reverse_order?
  97. @changesets = @issue.changesets.visible.all
  98. @changesets.reverse! if User.current.wants_comments_in_reverse_order?
  99. @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
  100. @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
  101. @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
  102. @priorities = IssuePriority.active
  103. @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
  104. respond_to do |format|
  105. format.html {
  106. retrieve_previous_and_next_issue_ids
  107. render :template => 'issues/show'
  108. }
  109. format.api
  110. format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
  111. format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
  112. end
  113. end
  114. # Add a new issue
  115. # The new issue will be created from an existing one if copy_from parameter is given
  116. def new
  117. respond_to do |format|
  118. format.html { render :action => 'new', :layout => !request.xhr? }
  119. format.js {
  120. render(:update) { |page|
  121. if params[:project_change]
  122. page.replace_html 'all_attributes', :partial => 'form'
  123. else
  124. page.replace_html 'attributes', :partial => 'attributes'
  125. end
  126. m = User.current.allowed_to?(:log_time, @issue.project) ? 'show' : 'hide'
  127. page << "if ($('log_time')) {Element.#{m}('log_time');}"
  128. }
  129. }
  130. end
  131. end
  132. def create
  133. call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
  134. @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
  135. if @issue.save
  136. call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
  137. respond_to do |format|
  138. format.html {
  139. render_attachment_warning_if_needed(@issue)
  140. flash[:notice] = l(:notice_issue_successful_create, :id => "<a href='#{issue_path(@issue)}'>##{@issue.id}</a>")
  141. redirect_to(params[:continue] ? { :action => 'new', :project_id => @issue.project, :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } :
  142. { :action => 'show', :id => @issue })
  143. }
  144. format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
  145. end
  146. return
  147. else
  148. respond_to do |format|
  149. format.html { render :action => 'new' }
  150. format.api { render_validation_errors(@issue) }
  151. end
  152. end
  153. end
  154. def edit
  155. return unless update_issue_from_params
  156. respond_to do |format|
  157. format.html { }
  158. format.xml { }
  159. end
  160. end
  161. def update
  162. return unless update_issue_from_params
  163. @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
  164. saved = false
  165. begin
  166. saved = @issue.save_issue_with_child_records(params, @time_entry)
  167. rescue ActiveRecord::StaleObjectError
  168. @conflict = true
  169. if params[:last_journal_id]
  170. if params[:last_journal_id].present?
  171. last_journal_id = params[:last_journal_id].to_i
  172. @conflict_journals = @issue.journals.all(:conditions => ["#{Journal.table_name}.id > ?", last_journal_id])
  173. else
  174. @conflict_journals = @issue.journals.all
  175. end
  176. end
  177. end
  178. if saved
  179. render_attachment_warning_if_needed(@issue)
  180. flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
  181. respond_to do |format|
  182. format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
  183. format.api { head :ok }
  184. end
  185. else
  186. respond_to do |format|
  187. format.html { render :action => 'edit' }
  188. format.api { render_validation_errors(@issue) }
  189. end
  190. end
  191. end
  192. # Bulk edit/copy a set of issues
  193. def bulk_edit
  194. @issues.sort!
  195. @copy = params[:copy].present?
  196. @notes = params[:notes]
  197. if User.current.allowed_to?(:move_issues, @projects)
  198. @allowed_projects = Issue.allowed_target_projects_on_move
  199. if params[:issue]
  200. @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
  201. if @target_project
  202. target_projects = [@target_project]
  203. end
  204. end
  205. end
  206. target_projects ||= @projects
  207. if @copy
  208. @available_statuses = [IssueStatus.default]
  209. else
  210. @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
  211. end
  212. @custom_fields = target_projects.map{|p|p.all_issue_custom_fields}.reduce(:&)
  213. @assignables = target_projects.map(&:assignable_users).reduce(:&)
  214. @trackers = target_projects.map(&:trackers).reduce(:&)
  215. @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
  216. @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
  217. if @copy
  218. @attachments_present = @issues.detect {|i| i.attachments.any?}.present?
  219. end
  220. @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&)
  221. render :layout => false if request.xhr?
  222. end
  223. def bulk_update
  224. @issues.sort!
  225. @copy = params[:copy].present?
  226. attributes = parse_params_for_bulk_issue_attributes(params)
  227. unsaved_issue_ids = []
  228. moved_issues = []
  229. @issues.each do |issue|
  230. issue.reload
  231. if @copy
  232. issue = issue.copy({}, :attachments => params[:copy_attachments].present?)
  233. end
  234. journal = issue.init_journal(User.current, params[:notes])
  235. issue.safe_attributes = attributes
  236. call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
  237. if issue.save
  238. moved_issues << issue
  239. else
  240. # Keep unsaved issue ids to display them in flash error
  241. unsaved_issue_ids << issue.id
  242. end
  243. end
  244. set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
  245. if params[:follow]
  246. if @issues.size == 1 && moved_issues.size == 1
  247. redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first
  248. elsif moved_issues.map(&:project).uniq.size == 1
  249. redirect_to :controller => 'issues', :action => 'index', :project_id => moved_issues.map(&:project).first
  250. end
  251. else
  252. redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
  253. end
  254. end
  255. def destroy
  256. @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
  257. if @hours > 0
  258. case params[:todo]
  259. when 'destroy'
  260. # nothing to do
  261. when 'nullify'
  262. TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
  263. when 'reassign'
  264. reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
  265. if reassign_to.nil?
  266. flash.now[:error] = l(:error_issue_not_found_in_project)
  267. return
  268. else
  269. TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
  270. end
  271. else
  272. # display the destroy form if it's a user request
  273. return unless api_request?
  274. end
  275. end
  276. @issues.each do |issue|
  277. begin
  278. issue.reload.destroy
  279. rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
  280. # nothing to do, issue was already deleted (eg. by a parent)
  281. end
  282. end
  283. respond_to do |format|
  284. format.html { redirect_back_or_default(:action => 'index', :project_id => @project) }
  285. format.api { head :ok }
  286. end
  287. end
  288. private
  289. def find_issue
  290. # Issue.visible.find(...) can not be used to redirect user to the login form
  291. # if the issue actually exists but requires authentication
  292. @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
  293. unless @issue.visible?
  294. deny_access
  295. return
  296. end
  297. @project = @issue.project
  298. rescue ActiveRecord::RecordNotFound
  299. render_404
  300. end
  301. def find_project
  302. project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id])
  303. @project = Project.find(project_id)
  304. rescue ActiveRecord::RecordNotFound
  305. render_404
  306. end
  307. def retrieve_previous_and_next_issue_ids
  308. retrieve_query_from_session
  309. if @query
  310. sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
  311. sort_update(@query.sortable_columns, 'issues_index_sort')
  312. limit = 500
  313. issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version])
  314. if (idx = issue_ids.index(@issue.id)) && idx < limit
  315. if issue_ids.size < 500
  316. @issue_position = idx + 1
  317. @issue_count = issue_ids.size
  318. end
  319. @prev_issue_id = issue_ids[idx - 1] if idx > 0
  320. @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
  321. end
  322. end
  323. end
  324. # Used by #edit and #update to set some common instance variables
  325. # from the params
  326. # TODO: Refactor, not everything in here is needed by #edit
  327. def update_issue_from_params
  328. @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
  329. @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
  330. @time_entry.attributes = params[:time_entry]
  331. @notes = params[:notes] || (params[:issue].present? ? params[:issue][:notes] : nil)
  332. @issue.init_journal(User.current, @notes)
  333. issue_attributes = params[:issue]
  334. if issue_attributes && params[:conflict_resolution]
  335. case params[:conflict_resolution]
  336. when 'overwrite'
  337. issue_attributes = issue_attributes.dup
  338. issue_attributes.delete(:lock_version)
  339. when 'add_notes'
  340. issue_attributes = {}
  341. when 'cancel'
  342. redirect_to issue_path(@issue)
  343. return false
  344. end
  345. end
  346. @issue.safe_attributes = issue_attributes
  347. @priorities = IssuePriority.active
  348. @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
  349. true
  350. end
  351. # TODO: Refactor, lots of extra code in here
  352. # TODO: Changing tracker on an existing issue should not trigger this
  353. def build_new_issue_from_params
  354. if params[:id].blank?
  355. @issue = Issue.new
  356. if params[:copy_from]
  357. begin
  358. @copy_from = Issue.visible.find(params[:copy_from])
  359. @copy_attachments = params[:copy_attachments].present? || request.get?
  360. @issue.copy_from(@copy_from, :attachments => @copy_attachments)
  361. rescue ActiveRecord::RecordNotFound
  362. render_404
  363. return
  364. end
  365. end
  366. @issue.project = @project
  367. else
  368. @issue = @project.issues.visible.find(params[:id])
  369. end
  370. @issue.project = @project
  371. @issue.author = User.current
  372. # Tracker must be set before custom field values
  373. @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
  374. if @issue.tracker.nil?
  375. render_error l(:error_no_tracker_in_project)
  376. return false
  377. end
  378. @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date?
  379. @issue.safe_attributes = params[:issue]
  380. @priorities = IssuePriority.active
  381. @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
  382. @available_watchers = (@issue.project.users.sort + @issue.watcher_users).uniq
  383. end
  384. def check_for_default_issue_status
  385. if IssueStatus.default.nil?
  386. render_error l(:error_no_default_issue_status)
  387. return false
  388. end
  389. end
  390. def parse_params_for_bulk_issue_attributes(params)
  391. attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
  392. attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
  393. if custom = attributes[:custom_field_values]
  394. custom.reject! {|k,v| v.blank?}
  395. custom.keys.each do |k|
  396. if custom[k].is_a?(Array)
  397. custom[k] << '' if custom[k].delete('__none__')
  398. else
  399. custom[k] = '' if custom[k] == '__none__'
  400. end
  401. end
  402. end
  403. attributes
  404. end
  405. end