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

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