Advertisement
Guest User

Untitled

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