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 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 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 IssuesController < ApplicationController
  19. default_search_scope :issues
  20. before_action :find_issue, :only => [:show, :edit, :update, :issue_tab]
  21. before_action :find_issues, :only => [:bulk_edit, :bulk_update, :destroy]
  22. before_action :authorize, :except => [:index, :new, :create]
  23. before_action :find_optional_project, :only => [:index, :new, :create]
  24. before_action :build_new_issue_from_params, :only => [:new, :create]
  25. accept_atom_auth :index, :show
  26. accept_api_auth :index, :show, :create, :update, :destroy
  27. rescue_from Query::StatementInvalid, :with => :query_statement_invalid
  28. rescue_from Query::QueryError, :with => :query_error
  29. helper :journals
  30. helper :projects
  31. helper :custom_fields
  32. helper :issue_relations
  33. helper :watchers
  34. helper :attachments
  35. helper :queries
  36. include QueriesHelper
  37. helper :repositories
  38. helper :timelog
  39. def index
  40. use_session = !request.format.csv?
  41. retrieve_default_query(use_session)
  42. retrieve_query(IssueQuery, use_session)
  43. if @query.valid?
  44. respond_to do |format|
  45. format.html do
  46. @issue_count = @query.issue_count
  47. @issue_pages = Paginator.new @issue_count, per_page_option, params['page']
  48. @issues = @query.issues(:offset => @issue_pages.offset, :limit => @issue_pages.per_page)
  49. render :layout => !request.xhr?
  50. end
  51. format.api do
  52. @offset, @limit = api_offset_and_limit
  53. @query.column_names = %w(author)
  54. @issue_count = @query.issue_count
  55. @issues = @query.issues(:offset => @offset, :limit => @limit)
  56. Issue.load_visible_relations(@issues) if include_in_api_response?('relations')
  57. if User.current.allowed_to?(:view_time_entries, nil, :global => true)
  58. Issue.load_visible_spent_hours(@issues)
  59. Issue.load_visible_total_spent_hours(@issues)
  60. end
  61. end
  62. format.atom do
  63. @issues = @query.issues(:limit => Setting.feeds_limit.to_i)
  64. render_feed(@issues,
  65. :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}")
  66. end
  67. format.csv do
  68. @issues = @query.issues(:limit => Setting.issues_export_limit.to_i)
  69. send_data(query_to_csv(@issues, @query, params[:csv]),
  70. :type => 'text/csv; header=present', :filename => 'issues.csv')
  71. end
  72. format.pdf do
  73. @issues = @query.issues(:limit => Setting.issues_export_limit.to_i)
  74. send_file_headers! :type => 'application/pdf', :filename => 'issues.pdf'
  75. end
  76. end
  77. else
  78. respond_to do |format|
  79. format.html {render :layout => !request.xhr?}
  80. format.any(:atom, :csv, :pdf) {head 422}
  81. format.api {render_validation_errors(@query)}
  82. end
  83. end
  84. rescue ActiveRecord::RecordNotFound
  85. render_404
  86. end
  87. def show
  88. @journals = @issue.visible_journals_with_index
  89. @has_changesets = @issue.changesets.visible.preload(:repository, :user).exists?
  90. @relations =
  91. @issue.relations.
  92. select do |r|
  93. r.other_issue(@issue) && r.other_issue(@issue).visible?
  94. end
  95. @journals.reverse! if User.current.wants_comments_in_reverse_order?
  96. if User.current.allowed_to?(:view_time_entries, @project)
  97. Issue.load_visible_spent_hours([@issue])
  98. Issue.load_visible_total_spent_hours([@issue])
  99. end
  100. respond_to do |format|
  101. format.html do
  102. @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
  103. @priorities = IssuePriority.active
  104. @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
  105. @time_entries = @issue.time_entries.visible.preload(:activity, :user)
  106. @relation = IssueRelation.new
  107. retrieve_previous_and_next_issue_ids
  108. render :template => 'issues/show'
  109. end
  110. format.api do
  111. @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
  112. @changesets = @issue.changesets.visible.preload(:repository, :user).to_a
  113. @changesets.reverse! if User.current.wants_comments_in_reverse_order?
  114. end
  115. format.atom do
  116. render :template => 'journals/index', :layout => false,
  117. :content_type => 'application/atom+xml'
  118. end
  119. format.pdf do
  120. send_file_headers!(:type => 'application/pdf',
  121. :filename => "#{@project.identifier}-#{@issue.id}.pdf")
  122. end
  123. end
  124. end
  125. def new
  126. respond_to do |format|
  127. format.html {render :action => 'new', :layout => !request.xhr?}
  128. format.js
  129. end
  130. end
  131. def create
  132. unless User.current.allowed_to?(:add_issues, @issue.project, :global => true)
  133. raise ::Unauthorized
  134. end
  135. call_hook(:controller_issues_new_before_save, {:params => params, :issue => @issue})
  136. @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
  137. if @issue.save
  138. call_hook(:controller_issues_new_after_save, {:params => params, :issue => @issue})
  139. respond_to do |format|
  140. format.html do
  141. render_attachment_warning_if_needed(@issue)
  142. flash[:notice] =
  143. l(:notice_issue_successful_create,
  144. :id => view_context.link_to("##{@issue.id}", issue_path(@issue),
  145. :title => @issue.subject))
  146. redirect_after_create
  147. end
  148. format.api do
  149. render :action => 'show', :status => :created,
  150. :location => issue_url(@issue)
  151. end
  152. end
  153. return
  154. else
  155. respond_to do |format|
  156. format.html do
  157. if @issue.project.nil?
  158. render_error :status => 422
  159. else
  160. render :action => 'new'
  161. end
  162. end
  163. format.api {render_validation_errors(@issue)}
  164. end
  165. end
  166. end
  167. def edit
  168. return unless update_issue_from_params
  169. respond_to do |format|
  170. format.html {}
  171. format.js
  172. end
  173. end
  174. def update
  175. return unless update_issue_from_params
  176. attachments = params[:attachments] || params.dig(:issue, :uploads)
  177. if @issue.attachments_addable?
  178. @issue.save_attachments(attachments)
  179. else
  180. attachments = attachments.to_unsafe_hash if attachments.respond_to?(:to_unsafe_hash)
  181. if [Hash, Array].any? { |klass| attachments.is_a?(klass) } && attachments.any?
  182. flash[:warning] = l(:warning_attachments_not_saved, attachments.size)
  183. end
  184. end
  185. saved = false
  186. begin
  187. saved = save_issue_with_child_records
  188. rescue ActiveRecord::StaleObjectError
  189. @issue.detach_saved_attachments
  190. @conflict = true
  191. if params[:last_journal_id]
  192. @conflict_journals = @issue.journals_after(params[:last_journal_id]).to_a
  193. unless User.current.allowed_to?(:view_private_notes, @issue.project)
  194. @conflict_journals.reject!(&:private_notes?)
  195. end
  196. end
  197. end
  198. if saved
  199. render_attachment_warning_if_needed(@issue)
  200. unless @issue.current_journal.new_record? || params[:no_flash]
  201. flash[:notice] = l(:notice_successful_update)
  202. end
  203. respond_to do |format|
  204. format.html do
  205. redirect_back_or_default(
  206. issue_path(@issue, previous_and_next_issue_ids_params)
  207. )
  208. end
  209. format.api {render_api_ok}
  210. end
  211. else
  212. respond_to do |format|
  213. format.html {render :action => 'edit'}
  214. format.api {render_validation_errors(@issue)}
  215. end
  216. end
  217. end
  218. def issue_tab
  219. return render_error :status => 422 unless request.xhr?
  220. tab = params[:name]
  221. case tab
  222. when 'time_entries'
  223. @time_entries = @issue.time_entries.visible.preload(:activity, :user).to_a
  224. render :partial => 'issues/tabs/time_entries', :locals => {:time_entries => @time_entries}
  225. when 'changesets'
  226. @changesets = @issue.changesets.visible.preload(:repository, :user).to_a
  227. @changesets.reverse! if User.current.wants_comments_in_reverse_order?
  228. render :partial => 'issues/tabs/changesets', :locals => {:changesets => @changesets, :project => @project}
  229. end
  230. end
  231. # Bulk edit/copy a set of issues
  232. def bulk_edit
  233. @issues.sort!
  234. @copy = params[:copy].present?
  235. @notes = params[:notes]
  236. if @copy
  237. unless User.current.allowed_to?(:copy_issues, @projects)
  238. raise ::Unauthorized
  239. end
  240. else
  241. unless @issues.all?(&:attributes_editable?)
  242. raise ::Unauthorized
  243. end
  244. end
  245. edited_issues = Issue.where(:id => @issues.map(&:id)).to_a
  246. @values_by_custom_field = {}
  247. edited_issues.each do |issue|
  248. issue.custom_field_values.each do |c|
  249. if c.value_present?
  250. @values_by_custom_field[c.custom_field] ||= []
  251. @values_by_custom_field[c.custom_field] << issue.id
  252. end
  253. end
  254. end
  255. @allowed_projects = Issue.allowed_target_projects
  256. if params[:issue]
  257. @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
  258. if @target_project
  259. target_projects = [@target_project]
  260. edited_issues.each {|issue| issue.project = @target_project}
  261. end
  262. end
  263. target_projects ||= @projects
  264. @trackers = target_projects.map {|p| Issue.allowed_target_trackers(p)}.reduce(:&)
  265. if params[:issue]
  266. @target_tracker = @trackers.detect {|t| t.id.to_s == params[:issue][:tracker_id].to_s}
  267. if @target_tracker
  268. edited_issues.each {|issue| issue.tracker = @target_tracker}
  269. end
  270. end
  271. if @copy
  272. # Copied issues will get their default statuses
  273. @available_statuses = []
  274. else
  275. @available_statuses = edited_issues.map(&:new_statuses_allowed_to).reduce(:&)
  276. end
  277. if params[:issue]
  278. @target_status = @available_statuses.detect {|t| t.id.to_s == params[:issue][:status_id].to_s}
  279. if @target_status
  280. edited_issues.each {|issue| issue.status = @target_status}
  281. end
  282. end
  283. edited_issues.each do |issue|
  284. issue.custom_field_values.each do |c|
  285. if c.value_present? && @values_by_custom_field[c.custom_field]
  286. @values_by_custom_field[c.custom_field].delete(issue.id)
  287. end
  288. end
  289. end
  290. @values_by_custom_field.delete_if {|k, v| v.blank?}
  291. @custom_fields =
  292. edited_issues.map{|i| i.editable_custom_fields}.
  293. reduce(:&).select {|field| field.format.bulk_edit_supported}
  294. @assignables = target_projects.map(&:assignable_users).reduce(:&)
  295. @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
  296. @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
  297. if @copy
  298. @attachments_present = @issues.detect {|i| i.attachments.any?}.present?
  299. @subtasks_present = @issues.detect {|i| !i.leaf?}.present?
  300. @watchers_present = User.current.allowed_to?(:add_issue_watchers, @projects) &&
  301. Watcher.where(:watchable_type => 'Issue',
  302. :watchable_id => @issues.map(&:id)).exists?
  303. end
  304. @safe_attributes = edited_issues.map(&:safe_attribute_names).reduce(:&)
  305. @issue_params = params[:issue] || {}
  306. @issue_params[:custom_field_values] ||= {}
  307. end
  308. def bulk_update
  309. @issues.sort!
  310. @copy = params[:copy].present?
  311. attributes = parse_params_for_bulk_update(params[:issue])
  312. copy_subtasks = (params[:copy_subtasks] == '1')
  313. copy_attachments = (params[:copy_attachments] == '1')
  314. copy_watchers = (params[:copy_watchers] == '1')
  315. if @copy
  316. unless User.current.allowed_to?(:copy_issues, @projects)
  317. raise ::Unauthorized
  318. end
  319. target_projects = @projects
  320. if attributes['project_id'].present?
  321. target_projects = Project.where(:id => attributes['project_id']).to_a
  322. end
  323. unless User.current.allowed_to?(:add_issues, target_projects)
  324. raise ::Unauthorized
  325. end
  326. unless User.current.allowed_to?(:add_issue_watchers, @projects)
  327. copy_watchers = false
  328. end
  329. else
  330. unless @issues.all?(&:attributes_editable?)
  331. raise ::Unauthorized
  332. end
  333. end
  334. unsaved_issues = []
  335. saved_issues = []
  336. if @copy && copy_subtasks
  337. # Descendant issues will be copied with the parent task
  338. # Don't copy them twice
  339. @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}}
  340. end
  341. @issues.each do |orig_issue|
  342. orig_issue.reload
  343. if @copy
  344. issue = orig_issue.copy(
  345. {},
  346. :attachments => copy_attachments,
  347. :subtasks => copy_subtasks,
  348. :watchers => copy_watchers,
  349. :link => link_copy?(params[:link_copy])
  350. )
  351. else
  352. issue = orig_issue
  353. end
  354. journal = issue.init_journal(User.current, params[:notes])
  355. issue.safe_attributes = attributes
  356. call_hook(:controller_issues_bulk_edit_before_save, {:params => params, :issue => issue})
  357. if issue.save
  358. saved_issues << issue
  359. else
  360. unsaved_issues << orig_issue
  361. end
  362. end
  363. if unsaved_issues.empty?
  364. flash[:notice] = l(:notice_successful_update) unless saved_issues.empty?
  365. if params[:follow]
  366. if @issues.size == 1 && saved_issues.size == 1
  367. redirect_to issue_path(saved_issues.first)
  368. elsif saved_issues.map(&:project).uniq.size == 1
  369. redirect_to project_issues_path(saved_issues.map(&:project).first)
  370. end
  371. else
  372. redirect_back_or_default _project_issues_path(@project)
  373. end
  374. else
  375. @saved_issues = @issues
  376. @unsaved_issues = unsaved_issues
  377. @issues = Issue.visible.where(:id => @unsaved_issues.map(&:id)).to_a
  378. bulk_edit
  379. render :action => 'bulk_edit'
  380. end
  381. end
  382. def destroy
  383. raise Unauthorized unless @issues.all?(&:deletable?)
  384. # all issues and their descendants are about to be deleted
  385. issues_and_descendants_ids = Issue.self_and_descendants(@issues).pluck(:id)
  386. time_entries = TimeEntry.where(:issue_id => issues_and_descendants_ids)
  387. @hours = time_entries.sum(:hours).to_f
  388. if @hours > 0
  389. case params[:todo]
  390. when 'destroy'
  391. # nothing to do
  392. when 'nullify'
  393. if Setting.timelog_required_fields.include?('issue_id')
  394. flash.now[:error] = l(:field_issue) + " " + ::I18n.t('activerecord.errors.messages.blank')
  395. return
  396. else
  397. time_entries.update_all(:issue_id => nil)
  398. end
  399. when 'reassign'
  400. reassign_to = @project && @project.issues.find_by_id(params[:reassign_to_id])
  401. if reassign_to.nil?
  402. flash.now[:error] = l(:error_issue_not_found_in_project)
  403. return
  404. elsif issues_and_descendants_ids.include?(reassign_to.id)
  405. flash.now[:error] = l(:error_cannot_reassign_time_entries_to_an_issue_about_to_be_deleted)
  406. return
  407. else
  408. time_entries.update_all(:issue_id => reassign_to.id, :project_id => reassign_to.project_id)
  409. end
  410. else
  411. # display the destroy form if it's a user request
  412. return unless api_request?
  413. end
  414. end
  415. @issues.each do |issue|
  416. begin
  417. issue.reload.destroy
  418. rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
  419. # nothing to do, issue was already deleted (eg. by a parent)
  420. end
  421. end
  422. respond_to do |format|
  423. format.html do
  424. flash[:notice] = l(:notice_successful_delete)
  425. redirect_back_or_default _project_issues_path(@project)
  426. end
  427. format.api {render_api_ok}
  428. end
  429. end
  430. # Overrides Redmine::MenuManager::MenuController::ClassMethods for
  431. # when the "New issue" tab is enabled
  432. def current_menu_item
  433. if Setting.new_item_menu_tab == '1' && [:new, :create].include?(action_name.to_sym)
  434. :new_issue
  435. else
  436. super
  437. end
  438. end
  439. private
  440. def query_error(exception)
  441. session.delete(:issue_query)
  442. super
  443. end
  444. def retrieve_default_query(use_session)
  445. return if params[:query_id].present?
  446. return if api_request?
  447. return if params[:set_filter]
  448. if params[:without_default].present?
  449. params[:set_filter] = 1
  450. return
  451. end
  452. if !params[:set_filter] && use_session && session[:issue_query]
  453. query_id, project_id = session[:issue_query].values_at(:id, :project_id)
  454. return if IssueQuery.where(id: query_id).exists? && project_id == @project&.id
  455. end
  456. if default_query = IssueQuery.default(project: @project)
  457. params[:query_id] = default_query.id
  458. end
  459. end
  460. def retrieve_previous_and_next_issue_ids
  461. if params[:prev_issue_id].present? || params[:next_issue_id].present?
  462. @prev_issue_id = params[:prev_issue_id].presence.try(:to_i)
  463. @next_issue_id = params[:next_issue_id].presence.try(:to_i)
  464. @issue_position = params[:issue_position].presence.try(:to_i)
  465. @issue_count = params[:issue_count].presence.try(:to_i)
  466. else
  467. retrieve_query_from_session
  468. if @query
  469. @per_page = per_page_option
  470. limit = 500
  471. issue_ids = @query.issue_ids(:limit => (limit + 1))
  472. if (idx = issue_ids.index(@issue.id)) && idx < limit
  473. if issue_ids.size < 500
  474. @issue_position = idx + 1
  475. @issue_count = issue_ids.size
  476. end
  477. @prev_issue_id = issue_ids[idx - 1] if idx > 0
  478. @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
  479. end
  480. query_params = @query.as_params
  481. if @issue_position
  482. query_params = query_params.merge(:page => (@issue_position / per_page_option) + 1, :per_page => per_page_option)
  483. end
  484. @query_path = _project_issues_path(@query.project, query_params)
  485. end
  486. end
  487. end
  488. def previous_and_next_issue_ids_params
  489. {
  490. :prev_issue_id => params[:prev_issue_id],
  491. :next_issue_id => params[:next_issue_id],
  492. :issue_position => params[:issue_position],
  493. :issue_count => params[:issue_count]
  494. }.reject {|k, v| k.blank?}
  495. end
  496. # Used by #edit and #update to set some common instance variables
  497. # from the params
  498. def update_issue_from_params
  499. @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
  500. if params[:time_entry]
  501. @time_entry.safe_attributes = params[:time_entry]
  502. end
  503. @issue.init_journal(User.current)
  504. issue_attributes = params[:issue]
  505. if issue_attributes && issue_attributes[:assigned_to_id] == 'me'
  506. issue_attributes[:assigned_to_id] = User.current.id
  507. end
  508. if issue_attributes && params[:conflict_resolution]
  509. case params[:conflict_resolution]
  510. when 'overwrite'
  511. issue_attributes = issue_attributes.dup
  512. issue_attributes.delete(:lock_version)
  513. when 'add_notes'
  514. issue_attributes = issue_attributes.slice(:notes, :private_notes)
  515. when 'cancel'
  516. redirect_to issue_path(@issue)
  517. return false
  518. end
  519. end
  520. issue_attributes = replace_none_values_with_blank(issue_attributes)
  521. @issue.safe_attributes = issue_attributes
  522. @priorities = IssuePriority.active
  523. @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
  524. true
  525. end
  526. # Used by #new and #create to build a new issue from the params
  527. # The new issue will be copied from an existing one if copy_from parameter is given
  528. def build_new_issue_from_params
  529. @issue = Issue.new
  530. if params[:copy_from]
  531. begin
  532. @issue.init_journal(User.current)
  533. @copy_from = Issue.visible.find(params[:copy_from])
  534. unless User.current.allowed_to?(:copy_issues, @copy_from.project)
  535. raise ::Unauthorized
  536. end
  537. @link_copy = link_copy?(params[:link_copy]) || request.get?
  538. @copy_attachments = params[:copy_attachments].present? || request.get?
  539. @copy_subtasks = params[:copy_subtasks].present? || request.get?
  540. @copy_watchers = User.current.allowed_to?(:add_issue_watchers, @project)
  541. @issue.copy_from(@copy_from, :attachments => @copy_attachments,
  542. :subtasks => @copy_subtasks, :watchers => @copy_watchers,
  543. :link => @link_copy)
  544. @issue.parent_issue_id = @copy_from.parent_id
  545. rescue ActiveRecord::RecordNotFound
  546. render_404
  547. return
  548. end
  549. end
  550. @issue.project = @project
  551. if request.get?
  552. @issue.project ||= @issue.allowed_target_projects.first
  553. end
  554. @issue.author ||= User.current
  555. @issue.start_date ||= User.current.today if Setting.default_issue_start_date_to_creation_date?
  556. attrs = (params[:issue] || {}).deep_dup
  557. if action_name == 'new' && params[:was_default_status] == attrs[:status_id]
  558. attrs.delete(:status_id)
  559. end
  560. if action_name == 'new' && params[:form_update_triggered_by] == 'issue_project_id'
  561. # Discard submitted version when changing the project on the issue form
  562. # so we can use the default version for the new project
  563. attrs.delete(:fixed_version_id)
  564. end
  565. attrs[:assigned_to_id] = User.current.id if attrs[:assigned_to_id] == 'me'
  566. @issue.safe_attributes = attrs
  567. if @issue.project
  568. @issue.tracker ||= @issue.allowed_target_trackers.first
  569. if @issue.tracker.nil?
  570. if @issue.project.trackers.any?
  571. # None of the project trackers is allowed to the user
  572. render_error :message => l(:error_no_tracker_allowed_for_new_issue_in_project), :status => 403
  573. else
  574. # Project has no trackers
  575. render_error l(:error_no_tracker_in_project)
  576. end
  577. return false
  578. end
  579. if @issue.status.nil?
  580. render_error l(:error_no_default_issue_status)
  581. return false
  582. end
  583. elsif request.get?
  584. render_error :message => l(:error_no_projects_with_tracker_allowed_for_new_issue), :status => 403
  585. return false
  586. end
  587. @priorities = IssuePriority.active
  588. @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
  589. end
  590. # Saves @issue and a time_entry from the parameters
  591. def save_issue_with_child_records
  592. Issue.transaction do
  593. if params[:time_entry] &&
  594. (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) &&
  595. User.current.allowed_to?(:log_time, @issue.project)
  596. time_entry = @time_entry || TimeEntry.new
  597. time_entry.project = @issue.project
  598. time_entry.issue = @issue
  599. time_entry.author = User.current
  600. time_entry.user = User.current
  601. time_entry.spent_on = User.current.today
  602. time_entry.safe_attributes = params[:time_entry]
  603. @issue.time_entries << time_entry
  604. end
  605. call_hook(
  606. :controller_issues_edit_before_save,
  607. {:params => params, :issue => @issue,
  608. :time_entry => time_entry,
  609. :journal => @issue.current_journal}
  610. )
  611. if @issue.save
  612. call_hook(
  613. :controller_issues_edit_after_save,
  614. {:params => params, :issue => @issue,
  615. :time_entry => time_entry,
  616. :journal => @issue.current_journal}
  617. )
  618. else
  619. raise ActiveRecord::Rollback
  620. end
  621. end
  622. end
  623. # Returns true if the issue copy should be linked
  624. # to the original issue
  625. def link_copy?(param)
  626. case Setting.link_copied_issue
  627. when 'yes'
  628. true
  629. when 'no'
  630. false
  631. when 'ask'
  632. param == '1'
  633. end
  634. end
  635. # Redirects user after a successful issue creation
  636. def redirect_after_create
  637. if params[:continue]
  638. url_params = {}
  639. url_params[:issue] =
  640. {
  641. :tracker_id => @issue.tracker,
  642. :parent_issue_id => @issue.parent_issue_id
  643. }.reject {|k, v| v.nil?}
  644. url_params[:back_url] = params[:back_url].presence
  645. if params[:project_id]
  646. redirect_to new_project_issue_path(@issue.project, url_params)
  647. else
  648. url_params[:issue][:project_id] = @issue.project_id
  649. redirect_to new_issue_path(url_params)
  650. end
  651. elsif params[:follow]
  652. redirect_to issue_path(@issue)
  653. else
  654. redirect_back_or_default issue_path(@issue)
  655. end
  656. end
  657. end