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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 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. 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. @issue.save_attachments(params[:attachments] ||
  177. (params[:issue] && params[:issue][:uploads]))
  178. saved = false
  179. begin
  180. saved = save_issue_with_child_records
  181. rescue ActiveRecord::StaleObjectError
  182. @issue.detach_saved_attachments
  183. @conflict = true
  184. if params[:last_journal_id]
  185. @conflict_journals = @issue.journals_after(params[:last_journal_id]).to_a
  186. unless User.current.allowed_to?(:view_private_notes, @issue.project)
  187. @conflict_journals.reject!(&:private_notes?)
  188. end
  189. end
  190. end
  191. if saved
  192. render_attachment_warning_if_needed(@issue)
  193. unless @issue.current_journal.new_record? || params[:no_flash]
  194. flash[:notice] = l(:notice_successful_update)
  195. end
  196. respond_to do |format|
  197. format.html do
  198. redirect_back_or_default(
  199. issue_path(@issue, previous_and_next_issue_ids_params)
  200. )
  201. end
  202. format.api {render_api_ok}
  203. end
  204. else
  205. respond_to do |format|
  206. format.html {render :action => 'edit'}
  207. format.api {render_validation_errors(@issue)}
  208. end
  209. end
  210. end
  211. def issue_tab
  212. return render_error :status => 422 unless request.xhr?
  213. tab = params[:name]
  214. case tab
  215. when 'time_entries'
  216. @time_entries = @issue.time_entries.visible.preload(:activity, :user).to_a
  217. render :partial => 'issues/tabs/time_entries', :locals => {:time_entries => @time_entries}
  218. when 'changesets'
  219. @changesets = @issue.changesets.visible.preload(:repository, :user).to_a
  220. @changesets.reverse! if User.current.wants_comments_in_reverse_order?
  221. render :partial => 'issues/tabs/changesets', :locals => {:changesets => @changesets}
  222. end
  223. end
  224. # Bulk edit/copy a set of issues
  225. def bulk_edit
  226. @issues.sort!
  227. @copy = params[:copy].present?
  228. @notes = params[:notes]
  229. if @copy
  230. unless User.current.allowed_to?(:copy_issues, @projects)
  231. raise ::Unauthorized
  232. end
  233. else
  234. unless @issues.all?(&:attributes_editable?)
  235. raise ::Unauthorized
  236. end
  237. end
  238. edited_issues = Issue.where(:id => @issues.map(&:id)).to_a
  239. @values_by_custom_field = {}
  240. edited_issues.each do |issue|
  241. issue.custom_field_values.each do |c|
  242. if c.value_present?
  243. @values_by_custom_field[c.custom_field] ||= []
  244. @values_by_custom_field[c.custom_field] << issue.id
  245. end
  246. end
  247. end
  248. @allowed_projects = Issue.allowed_target_projects
  249. if params[:issue]
  250. @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
  251. if @target_project
  252. target_projects = [@target_project]
  253. edited_issues.each {|issue| issue.project = @target_project}
  254. end
  255. end
  256. target_projects ||= @projects
  257. @trackers = target_projects.map {|p| Issue.allowed_target_trackers(p)}.reduce(:&)
  258. if params[:issue]
  259. @target_tracker = @trackers.detect {|t| t.id.to_s == params[:issue][:tracker_id].to_s}
  260. if @target_tracker
  261. edited_issues.each {|issue| issue.tracker = @target_tracker}
  262. end
  263. end
  264. if @copy
  265. # Copied issues will get their default statuses
  266. @available_statuses = []
  267. else
  268. @available_statuses = edited_issues.map(&:new_statuses_allowed_to).reduce(:&)
  269. end
  270. if params[:issue]
  271. @target_status = @available_statuses.detect {|t| t.id.to_s == params[:issue][:status_id].to_s}
  272. if @target_status
  273. edited_issues.each {|issue| issue.status = @target_status}
  274. end
  275. end
  276. edited_issues.each do |issue|
  277. issue.custom_field_values.each do |c|
  278. if c.value_present? && @values_by_custom_field[c.custom_field]
  279. @values_by_custom_field[c.custom_field].delete(issue.id)
  280. end
  281. end
  282. end
  283. @values_by_custom_field.delete_if {|k, v| v.blank?}
  284. @custom_fields =
  285. edited_issues.map{|i| i.editable_custom_fields}.
  286. reduce(:&).select {|field| field.format.bulk_edit_supported}
  287. @assignables = target_projects.map(&:assignable_users).reduce(:&)
  288. @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
  289. @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
  290. if @copy
  291. @attachments_present = @issues.detect {|i| i.attachments.any?}.present?
  292. @subtasks_present = @issues.detect {|i| !i.leaf?}.present?
  293. @watchers_present = User.current.allowed_to?(:add_issue_watchers, @projects) &&
  294. Watcher.where(:watchable_type => 'Issue',
  295. :watchable_id => @issues.map(&:id)).exists?
  296. end
  297. @safe_attributes = edited_issues.map(&:safe_attribute_names).reduce(:&)
  298. @issue_params = params[:issue] || {}
  299. @issue_params[:custom_field_values] ||= {}
  300. end
  301. def bulk_update
  302. @issues.sort!
  303. @copy = params[:copy].present?
  304. attributes = parse_params_for_bulk_update(params[:issue])
  305. copy_subtasks = (params[:copy_subtasks] == '1')
  306. copy_attachments = (params[:copy_attachments] == '1')
  307. copy_watchers = (params[:copy_watchers] == '1')
  308. if @copy
  309. unless User.current.allowed_to?(:copy_issues, @projects)
  310. raise ::Unauthorized
  311. end
  312. target_projects = @projects
  313. if attributes['project_id'].present?
  314. target_projects = Project.where(:id => attributes['project_id']).to_a
  315. end
  316. unless User.current.allowed_to?(:add_issues, target_projects)
  317. raise ::Unauthorized
  318. end
  319. unless User.current.allowed_to?(:add_issue_watchers, @projects)
  320. copy_watchers = false
  321. end
  322. else
  323. unless @issues.all?(&:attributes_editable?)
  324. raise ::Unauthorized
  325. end
  326. end
  327. unsaved_issues = []
  328. saved_issues = []
  329. if @copy && copy_subtasks
  330. # Descendant issues will be copied with the parent task
  331. # Don't copy them twice
  332. @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}}
  333. end
  334. @issues.each do |orig_issue|
  335. orig_issue.reload
  336. if @copy
  337. issue = orig_issue.copy(
  338. {},
  339. :attachments => copy_attachments,
  340. :subtasks => copy_subtasks,
  341. :watchers => copy_watchers,
  342. :link => link_copy?(params[:link_copy])
  343. )
  344. else
  345. issue = orig_issue
  346. end
  347. journal = issue.init_journal(User.current, params[:notes])
  348. issue.safe_attributes = attributes
  349. call_hook(:controller_issues_bulk_edit_before_save, {:params => params, :issue => issue})
  350. if issue.save
  351. saved_issues << issue
  352. else
  353. unsaved_issues << orig_issue
  354. end
  355. end
  356. if unsaved_issues.empty?
  357. flash[:notice] = l(:notice_successful_update) unless saved_issues.empty?
  358. if params[:follow]
  359. if @issues.size == 1 && saved_issues.size == 1
  360. redirect_to issue_path(saved_issues.first)
  361. elsif saved_issues.map(&:project).uniq.size == 1
  362. redirect_to project_issues_path(saved_issues.map(&:project).first)
  363. end
  364. else
  365. redirect_back_or_default _project_issues_path(@project)
  366. end
  367. else
  368. @saved_issues = @issues
  369. @unsaved_issues = unsaved_issues
  370. @issues = Issue.visible.where(:id => @unsaved_issues.map(&:id)).to_a
  371. bulk_edit
  372. render :action => 'bulk_edit'
  373. end
  374. end
  375. def destroy
  376. raise Unauthorized unless @issues.all?(&:deletable?)
  377. # all issues and their descendants are about to be deleted
  378. issues_and_descendants_ids = Issue.self_and_descendants(@issues).pluck(:id)
  379. time_entries = TimeEntry.where(:issue_id => issues_and_descendants_ids)
  380. @hours = time_entries.sum(:hours).to_f
  381. if @hours > 0
  382. case params[:todo]
  383. when 'destroy'
  384. # nothing to do
  385. when 'nullify'
  386. if Setting.timelog_required_fields.include?('issue_id')
  387. flash.now[:error] = l(:field_issue) + " " + ::I18n.t('activerecord.errors.messages.blank')
  388. return
  389. else
  390. time_entries.update_all(:issue_id => nil)
  391. end
  392. when 'reassign'
  393. reassign_to = @project && @project.issues.find_by_id(params[:reassign_to_id])
  394. if reassign_to.nil?
  395. flash.now[:error] = l(:error_issue_not_found_in_project)
  396. return
  397. elsif issues_and_descendants_ids.include?(reassign_to.id)
  398. flash.now[:error] = l(:error_cannot_reassign_time_entries_to_an_issue_about_to_be_deleted)
  399. return
  400. else
  401. time_entries.update_all(:issue_id => reassign_to.id, :project_id => reassign_to.project_id)
  402. end
  403. else
  404. # display the destroy form if it's a user request
  405. return unless api_request?
  406. end
  407. end
  408. @issues.each do |issue|
  409. begin
  410. issue.reload.destroy
  411. rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
  412. # nothing to do, issue was already deleted (eg. by a parent)
  413. end
  414. end
  415. respond_to do |format|
  416. format.html do
  417. flash[:notice] = l(:notice_successful_delete)
  418. redirect_back_or_default _project_issues_path(@project)
  419. end
  420. format.api {render_api_ok}
  421. end
  422. end
  423. # Overrides Redmine::MenuManager::MenuController::ClassMethods for
  424. # when the "New issue" tab is enabled
  425. def current_menu_item
  426. if Setting.new_item_menu_tab == '1' && [:new, :create].include?(action_name.to_sym)
  427. :new_issue
  428. else
  429. super
  430. end
  431. end
  432. private
  433. def query_error(exception)
  434. session.delete(:issue_query)
  435. super
  436. end
  437. def retrieve_default_query(use_session)
  438. return if params[:query_id].present?
  439. return if api_request?
  440. return if params[:set_filter] && (params.key?(:op) || params.key?(:f))
  441. if params[:without_default].present?
  442. params[:set_filter] = 1
  443. return
  444. end
  445. if !params[:set_filter] && use_session && session[:issue_query]
  446. query_id, project_id = session[:issue_query].values_at(:id, :project_id)
  447. return if IssueQuery.where(id: query_id).exists? && project_id == @project&.id
  448. end
  449. if default_query = IssueQuery.default(project: @project)
  450. params[:query_id] = default_query.id
  451. end
  452. end
  453. def retrieve_previous_and_next_issue_ids
  454. if params[:prev_issue_id].present? || params[:next_issue_id].present?
  455. @prev_issue_id = params[:prev_issue_id].presence.try(:to_i)
  456. @next_issue_id = params[:next_issue_id].presence.try(:to_i)
  457. @issue_position = params[:issue_position].presence.try(:to_i)
  458. @issue_count = params[:issue_count].presence.try(:to_i)
  459. else
  460. retrieve_query_from_session
  461. if @query
  462. @per_page = per_page_option
  463. limit = 500
  464. issue_ids = @query.issue_ids(:limit => (limit + 1))
  465. if (idx = issue_ids.index(@issue.id)) && idx < limit
  466. if issue_ids.size < 500
  467. @issue_position = idx + 1
  468. @issue_count = issue_ids.size
  469. end
  470. @prev_issue_id = issue_ids[idx - 1] if idx > 0
  471. @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
  472. end
  473. query_params = @query.as_params
  474. if @issue_position
  475. query_params = query_params.merge(:page => (@issue_position / per_page_option) + 1, :per_page => per_page_option)
  476. end
  477. @query_path = _project_issues_path(@query.project, query_params)
  478. end
  479. end
  480. end
  481. def previous_and_next_issue_ids_params
  482. {
  483. :prev_issue_id => params[:prev_issue_id],
  484. :next_issue_id => params[:next_issue_id],
  485. :issue_position => params[:issue_position],
  486. :issue_count => params[:issue_count]
  487. }.reject {|k, v| k.blank?}
  488. end
  489. # Used by #edit and #update to set some common instance variables
  490. # from the params
  491. def update_issue_from_params
  492. @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
  493. if params[:time_entry]
  494. @time_entry.safe_attributes = params[:time_entry]
  495. end
  496. @issue.init_journal(User.current)
  497. issue_attributes = params[:issue]
  498. if issue_attributes && issue_attributes[:assigned_to_id] == 'me'
  499. issue_attributes[:assigned_to_id] = User.current.id
  500. end
  501. if issue_attributes && params[:conflict_resolution]
  502. case params[:conflict_resolution]
  503. when 'overwrite'
  504. issue_attributes = issue_attributes.dup
  505. issue_attributes.delete(:lock_version)
  506. when 'add_notes'
  507. issue_attributes = issue_attributes.slice(:notes, :private_notes)
  508. when 'cancel'
  509. redirect_to issue_path(@issue)
  510. return false
  511. end
  512. end
  513. issue_attributes = replace_none_values_with_blank(issue_attributes)
  514. @issue.safe_attributes = issue_attributes
  515. @priorities = IssuePriority.active
  516. @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
  517. true
  518. end
  519. # Used by #new and #create to build a new issue from the params
  520. # The new issue will be copied from an existing one if copy_from parameter is given
  521. def build_new_issue_from_params
  522. @issue = Issue.new
  523. if params[:copy_from]
  524. begin
  525. @issue.init_journal(User.current)
  526. @copy_from = Issue.visible.find(params[:copy_from])
  527. unless User.current.allowed_to?(:copy_issues, @copy_from.project)
  528. raise ::Unauthorized
  529. end
  530. @link_copy = link_copy?(params[:link_copy]) || request.get?
  531. @copy_attachments = params[:copy_attachments].present? || request.get?
  532. @copy_subtasks = params[:copy_subtasks].present? || request.get?
  533. @copy_watchers = User.current.allowed_to?(:add_issue_watchers, @project)
  534. @issue.copy_from(@copy_from, :attachments => @copy_attachments,
  535. :subtasks => @copy_subtasks, :watchers => @copy_watchers,
  536. :link => @link_copy)
  537. @issue.parent_issue_id = @copy_from.parent_id
  538. rescue ActiveRecord::RecordNotFound
  539. render_404
  540. return
  541. end
  542. end
  543. @issue.project = @project
  544. if request.get?
  545. @issue.project ||= @issue.allowed_target_projects.first
  546. end
  547. @issue.author ||= User.current
  548. @issue.start_date ||= User.current.today if Setting.default_issue_start_date_to_creation_date?
  549. attrs = (params[:issue] || {}).deep_dup
  550. if action_name == 'new' && params[:was_default_status] == attrs[:status_id]
  551. attrs.delete(:status_id)
  552. end
  553. if action_name == 'new' && params[:form_update_triggered_by] == 'issue_project_id'
  554. # Discard submitted version when changing the project on the issue form
  555. # so we can use the default version for the new project
  556. attrs.delete(:fixed_version_id)
  557. end
  558. attrs[:assigned_to_id] = User.current.id if attrs[:assigned_to_id] == 'me'
  559. @issue.safe_attributes = attrs
  560. if @issue.project
  561. @issue.tracker ||= @issue.allowed_target_trackers.first
  562. if @issue.tracker.nil?
  563. if @issue.project.trackers.any?
  564. # None of the project trackers is allowed to the user
  565. render_error :message => l(:error_no_tracker_allowed_for_new_issue_in_project), :status => 403
  566. else
  567. # Project has no trackers
  568. render_error l(:error_no_tracker_in_project)
  569. end
  570. return false
  571. end
  572. if @issue.status.nil?
  573. render_error l(:error_no_default_issue_status)
  574. return false
  575. end
  576. elsif request.get?
  577. render_error :message => l(:error_no_projects_with_tracker_allowed_for_new_issue), :status => 403
  578. return false
  579. end
  580. @priorities = IssuePriority.active
  581. @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
  582. end
  583. # Saves @issue and a time_entry from the parameters
  584. def save_issue_with_child_records
  585. Issue.transaction do
  586. if params[:time_entry] &&
  587. (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) &&
  588. User.current.allowed_to?(:log_time, @issue.project)
  589. time_entry = @time_entry || TimeEntry.new
  590. time_entry.project = @issue.project
  591. time_entry.issue = @issue
  592. time_entry.author = User.current
  593. time_entry.user = User.current
  594. time_entry.spent_on = User.current.today
  595. time_entry.safe_attributes = params[:time_entry]
  596. @issue.time_entries << time_entry
  597. end
  598. call_hook(
  599. :controller_issues_edit_before_save,
  600. {:params => params, :issue => @issue,
  601. :time_entry => time_entry,
  602. :journal => @issue.current_journal}
  603. )
  604. if @issue.save
  605. call_hook(
  606. :controller_issues_edit_after_save,
  607. {:params => params, :issue => @issue,
  608. :time_entry => time_entry,
  609. :journal => @issue.current_journal}
  610. )
  611. else
  612. raise ActiveRecord::Rollback
  613. end
  614. end
  615. end
  616. # Returns true if the issue copy should be linked
  617. # to the original issue
  618. def link_copy?(param)
  619. case Setting.link_copied_issue
  620. when 'yes'
  621. true
  622. when 'no'
  623. false
  624. when 'ask'
  625. param == '1'
  626. end
  627. end
  628. # Redirects user after a successful issue creation
  629. def redirect_after_create
  630. if params[:continue]
  631. url_params = {}
  632. url_params[:issue] =
  633. {
  634. :tracker_id => @issue.tracker,
  635. :parent_issue_id => @issue.parent_issue_id
  636. }.reject {|k, v| v.nil?}
  637. url_params[:back_url] = params[:back_url].presence
  638. if params[:project_id]
  639. redirect_to new_project_issue_path(@issue.project, url_params)
  640. else
  641. url_params[:issue][:project_id] = @issue.project_id
  642. redirect_to new_issue_path(url_params)
  643. end
  644. elsif params[:follow]
  645. redirect_to issue_path(@issue)
  646. else
  647. redirect_back_or_default issue_path(@issue)
  648. end
  649. end
  650. end