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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. # redMine - project management software
  2. # Copyright (C) 2006-2007 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class IssuesController < ApplicationController
  18. layout 'base'
  19. menu_item :new_issue, :only => :new
  20. before_filter :find_issue, :only => [:show, :edit, :destroy_attachment]
  21. before_filter :find_issues, :only => [:bulk_edit, :move, :destroy]
  22. before_filter :find_project, :only => [:new, :update_form, :preview]
  23. before_filter :authorize, :except => [:index, :changes, :preview, :update_form, :context_menu]
  24. before_filter :find_optional_project, :only => [:index, :changes]
  25. accept_key_auth :index, :changes
  26. helper :journals
  27. helper :projects
  28. include ProjectsHelper
  29. helper :custom_fields
  30. include CustomFieldsHelper
  31. helper :ifpdf
  32. include IfpdfHelper
  33. helper :issue_relations
  34. include IssueRelationsHelper
  35. helper :watchers
  36. include WatchersHelper
  37. helper :attachments
  38. include AttachmentsHelper
  39. helper :queries
  40. helper :sort
  41. include SortHelper
  42. include IssuesHelper
  43. def index
  44. retrieve_query
  45. sort_init 'id', 'desc'
  46. sort_update({'id' => "#{Issue.table_name}.id"}.merge(@query.columns.inject({}) {|h, c| h[c.name.to_s] = c.sortable; h}))
  47. if @query.valid?
  48. limit = per_page_option
  49. respond_to do |format|
  50. format.html { }
  51. format.atom { }
  52. format.csv { limit = Setting.issues_export_limit.to_i }
  53. format.pdf { limit = Setting.issues_export_limit.to_i }
  54. end
  55. @issue_count = Issue.count(:include => [:status, :project], :conditions => @query.statement)
  56. @issue_pages = Paginator.new self, @issue_count, limit, params['page']
  57. @issues = Issue.find :all, :order => sort_clause,
  58. :include => [ :assigned_to, :status, :tracker, :project, :priority, :category, :fixed_version ],
  59. :conditions => @query.statement,
  60. :limit => limit,
  61. :offset => @issue_pages.current.offset
  62. respond_to do |format|
  63. format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
  64. format.atom { render_feed(@issues, :title => l(:label_issue_plural)) }
  65. format.csv { send_data(issues_to_csv(@issues, @project).read, :type => 'text/csv; header=present', :filename => 'export.csv') }
  66. format.pdf { send_data(render(:template => 'issues/index.rfpdf', :layout => false), :type => 'application/pdf', :filename => 'export.pdf') }
  67. end
  68. else
  69. # Send html if the query is not valid
  70. render(:template => 'issues/index.rhtml', :layout => !request.xhr?)
  71. end
  72. rescue ActiveRecord::RecordNotFound
  73. render_404
  74. end
  75. def changes
  76. retrieve_query
  77. sort_init 'id', 'desc'
  78. sort_update({'id' => "#{Issue.table_name}.id"}.merge(@query.columns.inject({}) {|h, c| h[c.name.to_s] = c.sortable; h}))
  79. if @query.valid?
  80. @journals = Journal.find :all, :include => [ :details, :user, {:issue => [:project, :author, :tracker, :status]} ],
  81. :conditions => @query.statement,
  82. :limit => 25,
  83. :order => "#{Journal.table_name}.created_on DESC"
  84. end
  85. @title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name)
  86. render :layout => false, :content_type => 'application/atom+xml'
  87. rescue ActiveRecord::RecordNotFound
  88. render_404
  89. end
  90. def show
  91. @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| @issue.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x, :customized => @issue) }
  92. @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
  93. @journals.each_with_index {|j,i| j.indice = i+1}
  94. @journals.reverse! if User.current.wants_comments_in_reverse_order?
  95. @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
  96. @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
  97. @activities = Enumeration::get_values('ACTI')
  98. @priorities = Enumeration::get_values('IPRI')
  99. @time_entry = TimeEntry.new
  100. respond_to do |format|
  101. format.html { render :template => 'issues/show.rhtml' }
  102. format.atom { render :action => 'changes', :layout => false, :content_type => 'application/atom+xml' }
  103. format.pdf { send_data(render(:template => 'issues/show.rfpdf', :layout => false), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
  104. end
  105. end
  106. # Add a new issue
  107. # The new issue will be created from an existing one if copy_from parameter is given
  108. def new
  109. @issue = params[:copy_from] ? Issue.new.copy_from(params[:copy_from]) : Issue.new(params[:issue])
  110. @issue.project = @project
  111. @issue.author = User.current
  112. @issue.tracker ||= @project.trackers.find(params[:tracker_id] ? params[:tracker_id] : :first)
  113. if @issue.tracker.nil?
  114. flash.now[:error] = 'No tracker is associated to this project. Please check the Project settings.'
  115. render :nothing => true, :layout => true
  116. return
  117. end
  118. default_status = IssueStatus.default
  119. unless default_status
  120. flash.now[:error] = 'No default issue status is defined. Please check your configuration (Go to "Administration -> Issue statuses").'
  121. render :nothing => true, :layout => true
  122. return
  123. end
  124. @issue.status = default_status
  125. @allowed_statuses = ([default_status] + default_status.find_new_statuses_allowed_to(User.current.role_for_project(@project), @issue.tracker)).uniq
  126. if request.get? || request.xhr?
  127. @issue.start_date ||= Date.today
  128. @custom_values = @issue.custom_values.empty? ?
  129. @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue) } :
  130. @issue.custom_values
  131. else
  132. requested_status = (params[:issue] && params[:issue][:status_id] ? IssueStatus.find_by_id(params[:issue][:status_id]) : default_status)
  133. # Check that the user is allowed to apply the requested status
  134. @issue.status = (@allowed_statuses.include? requested_status) ? requested_status : default_status
  135. @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x,
  136. :customized => @issue,
  137. :value => (params[:custom_fields] ? params[:custom_fields][x.id.to_s] : nil)) }
  138. @issue.custom_values = @custom_values
  139. if @issue.save
  140. attach_files(@issue, params[:attachments])
  141. flash[:notice] = l(:notice_successful_create)
  142. Mailer.deliver_issue_add(@issue) if Setting.notified_events.include?('issue_added')
  143. redirect_to :controller => 'issues', :action => 'show', :id => @issue
  144. return
  145. end
  146. end
  147. @priorities = Enumeration::get_values('IPRI')
  148. render :layout => !request.xhr?
  149. end
  150. # Attributes that can be updated on workflow transition (without :edit permission)
  151. # TODO: make it configurable (at least per role)
  152. UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION)
  153. def edit
  154. @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
  155. @activities = Enumeration::get_values('ACTI')
  156. @priorities = Enumeration::get_values('IPRI')
  157. @custom_values = []
  158. @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
  159. @notes = params[:notes]
  160. journal = @issue.init_journal(User.current, @notes)
  161. # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed
  162. if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue]
  163. attrs = params[:issue].dup
  164. attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed
  165. attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s}
  166. @issue.attributes = attrs
  167. end
  168. if request.get?
  169. @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| @issue.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x, :customized => @issue) }
  170. else
  171. # Update custom fields if user has :edit permission
  172. if @edit_allowed && params[:custom_fields]
  173. @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue, :value => params["custom_fields"][x.id.to_s]) }
  174. @issue.custom_values = @custom_values
  175. end
  176. @time_entry = TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
  177. @time_entry.attributes = params[:time_entry]
  178. attachments = attach_files(@issue, params[:attachments])
  179. attachments.each {|a| journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
  180. if @issue.save
  181. # Log spend time
  182. if current_role.allowed_to?(:log_time)
  183. @time_entry.save
  184. end
  185. if !journal.new_record?
  186. # Only send notification if something was actually changed
  187. flash[:notice] = l(:notice_successful_update)
  188. Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated')
  189. end
  190. redirect_to(params[:back_to] || {:action => 'show', :id => @issue})
  191. end
  192. end
  193. rescue ActiveRecord::StaleObjectError
  194. # Optimistic locking exception
  195. flash.now[:error] = l(:notice_locking_conflict)
  196. end
  197. # Bulk edit a set of issues
  198. def bulk_edit
  199. if request.post?
  200. status = params[:status_id].blank? ? nil : IssueStatus.find_by_id(params[:status_id])
  201. priority = params[:priority_id].blank? ? nil : Enumeration.find_by_id(params[:priority_id])
  202. assigned_to = (params[:assigned_to_id].blank? || params[:assigned_to_id] == 'none') ? nil : User.find_by_id(params[:assigned_to_id])
  203. category = (params[:category_id].blank? || params[:category_id] == 'none') ? nil : @project.issue_categories.find_by_id(params[:category_id])
  204. fixed_version = (params[:fixed_version_id].blank? || params[:fixed_version_id] == 'none') ? nil : @project.versions.find_by_id(params[:fixed_version_id])
  205. unsaved_issue_ids = []
  206. @issues.each do |issue|
  207. journal = issue.init_journal(User.current, params[:notes])
  208. issue.priority = priority if priority
  209. issue.assigned_to = assigned_to if assigned_to || params[:assigned_to_id] == 'none'
  210. issue.category = category if category || params[:category_id] == 'none'
  211. issue.fixed_version = fixed_version if fixed_version || params[:fixed_version_id] == 'none'
  212. issue.start_date = params[:start_date] unless params[:start_date].blank?
  213. issue.due_date = params[:due_date] unless params[:due_date].blank?
  214. issue.done_ratio = params[:done_ratio] unless params[:done_ratio].blank?
  215. # Don't save any change to the issue if the user is not authorized to apply the requested status
  216. if (status.nil? || (issue.status.new_status_allowed_to?(status, current_role, issue.tracker) && issue.status = status)) && issue.save
  217. # Send notification for each issue (if changed)
  218. Mailer.deliver_issue_edit(journal) if journal.details.any? && Setting.notified_events.include?('issue_updated')
  219. else
  220. # Keep unsaved issue ids to display them in flash error
  221. unsaved_issue_ids << issue.id
  222. end
  223. end
  224. if unsaved_issue_ids.empty?
  225. flash[:notice] = l(:notice_successful_update) unless @issues.empty?
  226. else
  227. flash[:error] = l(:notice_failed_to_save_issues, unsaved_issue_ids.size, @issues.size, '#' + unsaved_issue_ids.join(', #'))
  228. end
  229. redirect_to :controller => 'issues', :action => 'index', :project_id => @project
  230. return
  231. end
  232. # Find potential statuses the user could be allowed to switch issues to
  233. @available_statuses = Workflow.find(:all, :include => :new_status,
  234. :conditions => {:role_id => current_role.id}).collect(&:new_status).compact.uniq
  235. end
  236. def move
  237. @allowed_projects = []
  238. # find projects to which the user is allowed to move the issue
  239. if User.current.admin?
  240. # admin is allowed to move issues to any active (visible) project
  241. @allowed_projects = Project.find(:all, :conditions => Project.visible_by(User.current), :order => 'name')
  242. else
  243. User.current.memberships.each {|m| @allowed_projects << m.project if m.role.allowed_to?(:move_issues)}
  244. end
  245. @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:new_project_id]} if params[:new_project_id]
  246. @target_project ||= @project
  247. @trackers = @target_project.trackers
  248. if request.post?
  249. new_tracker = params[:new_tracker_id].blank? ? nil : @target_project.trackers.find_by_id(params[:new_tracker_id])
  250. unsaved_issue_ids = []
  251. @issues.each do |issue|
  252. unsaved_issue_ids << issue.id unless issue.move_to(@target_project, new_tracker)
  253. end
  254. if unsaved_issue_ids.empty?
  255. flash[:notice] = l(:notice_successful_update) unless @issues.empty?
  256. else
  257. flash[:error] = l(:notice_failed_to_save_issues, unsaved_issue_ids.size, @issues.size, '#' + unsaved_issue_ids.join(', #'))
  258. end
  259. redirect_to :controller => 'issues', :action => 'index', :project_id => @project
  260. return
  261. end
  262. render :layout => false if request.xhr?
  263. end
  264. def destroy
  265. @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
  266. if @hours > 0
  267. case params[:todo]
  268. when 'destroy'
  269. # nothing to do
  270. when 'nullify'
  271. TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
  272. when 'reassign'
  273. reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
  274. if reassign_to.nil?
  275. flash.now[:error] = l(:error_issue_not_found_in_project)
  276. return
  277. else
  278. TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
  279. end
  280. else
  281. # display the destroy form
  282. return
  283. end
  284. end
  285. @issues.each(&:destroy)
  286. redirect_to :action => 'index', :project_id => @project
  287. end
  288. def destroy_attachment
  289. a = @issue.attachments.find(params[:attachment_id])
  290. a.destroy
  291. journal = @issue.init_journal(User.current)
  292. journal.details << JournalDetail.new(:property => 'attachment',
  293. :prop_key => a.id,
  294. :old_value => a.filename)
  295. journal.save
  296. redirect_to :action => 'show', :id => @issue
  297. end
  298. def context_menu
  299. @issues = Issue.find_all_by_id(params[:ids], :include => :project)
  300. if (@issues.size == 1)
  301. @issue = @issues.first
  302. @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
  303. @assignables = @issue.assignable_users
  304. @assignables << @issue.assigned_to if @issue.assigned_to && !@assignables.include?(@issue.assigned_to)
  305. end
  306. projects = @issues.collect(&:project).compact.uniq
  307. @project = projects.first if projects.size == 1
  308. @can = {:edit => (@project && User.current.allowed_to?(:edit_issues, @project)),
  309. :update => (@issue && (User.current.allowed_to?(:edit_issues, @project) || (User.current.allowed_to?(:change_status, @project) && !@allowed_statuses.empty?))),
  310. :move => (@project && User.current.allowed_to?(:move_issues, @project)),
  311. :copy => (@issue && @project.trackers.include?(@issue.tracker) && User.current.allowed_to?(:add_issues, @project)),
  312. :delete => (@project && User.current.allowed_to?(:delete_issues, @project))
  313. }
  314. @priorities = Enumeration.get_values('IPRI').reverse
  315. @statuses = IssueStatus.find(:all, :order => 'position')
  316. @back = request.env['HTTP_REFERER']
  317. render :layout => false
  318. end
  319. def update_form
  320. @issue = Issue.new(params[:issue])
  321. render :action => :new, :layout => false
  322. end
  323. def preview
  324. @issue = @project.issues.find_by_id(params[:id]) unless params[:id].blank?
  325. @attachements = @issue.attachments if @issue
  326. @text = params[:notes] || (params[:issue] ? params[:issue][:description] : nil)
  327. render :partial => 'common/preview'
  328. end
  329. private
  330. def find_issue
  331. @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
  332. @project = @issue.project
  333. rescue ActiveRecord::RecordNotFound
  334. render_404
  335. end
  336. # Filter for bulk operations
  337. def find_issues
  338. @issues = Issue.find_all_by_id(params[:id] || params[:ids])
  339. raise ActiveRecord::RecordNotFound if @issues.empty?
  340. projects = @issues.collect(&:project).compact.uniq
  341. if projects.size == 1
  342. @project = projects.first
  343. else
  344. # TODO: let users bulk edit/move/destroy issues from different projects
  345. render_error 'Can not bulk edit/move/destroy issues from different projects' and return false
  346. end
  347. rescue ActiveRecord::RecordNotFound
  348. render_404
  349. end
  350. def find_project
  351. @project = Project.find(params[:project_id])
  352. rescue ActiveRecord::RecordNotFound
  353. render_404
  354. end
  355. def find_optional_project
  356. return true unless params[:project_id]
  357. @project = Project.find(params[:project_id])
  358. authorize
  359. rescue ActiveRecord::RecordNotFound
  360. render_404
  361. end
  362. # Retrieve query from session or build a new query
  363. def retrieve_query
  364. if !params[:query_id].blank?
  365. cond = "project_id IS NULL"
  366. cond << " OR project_id = #{@project.id}" if @project
  367. @query = Query.find(params[:query_id], :conditions => cond)
  368. @query.project = @project
  369. session[:query] = {:id => @query.id, :project_id => @query.project_id}
  370. else
  371. if params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil)
  372. # Give it a name, required to be valid
  373. @query = Query.new(:name => "_")
  374. @query.project = @project
  375. if params[:fields] and params[:fields].is_a? Array
  376. params[:fields].each do |field|
  377. @query.add_filter(field, params[:operators][field], params[:values][field])
  378. end
  379. else
  380. @query.available_filters.keys.each do |field|
  381. @query.add_short_filter(field, params[field]) if params[field]
  382. end
  383. end
  384. session[:query] = {:project_id => @query.project_id, :filters => @query.filters}
  385. else
  386. @query = Query.find_by_id(session[:query][:id]) if session[:query][:id]
  387. @query ||= Query.new(:name => "_", :project => @project, :filters => session[:query][:filters])
  388. @query.project = @project
  389. end
  390. end
  391. end
  392. end