| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  | # redMine - project management software | 
					
						
							|  |  |  | # Copyright (C) 2006-2007  Jean-Philippe Lang | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  | # modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  | # as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  | # of the License, or (at your option) any later version. | 
					
						
							|  |  |  | #  | 
					
						
							|  |  |  | # This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | # GNU General Public License for more details. | 
					
						
							|  |  |  | #  | 
					
						
							|  |  |  | # You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | # along with this program; if not, write to the Free Software | 
					
						
							|  |  |  | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class IssuesController < ApplicationController | 
					
						
							| 
									
										
										
										
											2007-11-05 18:38:42 +00:00
										 |  |  |   layout 'base' | 
					
						
							|  |  |  |   before_filter :find_project, :authorize, :except => [:index, :changes, :preview] | 
					
						
							|  |  |  |   before_filter :find_optional_project, :only => [:index, :changes] | 
					
						
							|  |  |  |   accept_key_auth :index, :changes | 
					
						
							| 
									
										
										
										
											2007-05-06 16:40:33 +00:00
										 |  |  |    | 
					
						
							|  |  |  |   cache_sweeper :issue_sweeper, :only => [ :edit, :change_status, :destroy ] | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-06-12 23:07:00 +00:00
										 |  |  |   helper :projects | 
					
						
							|  |  |  |   include ProjectsHelper    | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |   helper :custom_fields | 
					
						
							|  |  |  |   include CustomFieldsHelper | 
					
						
							|  |  |  |   helper :ifpdf | 
					
						
							|  |  |  |   include IfpdfHelper | 
					
						
							| 
									
										
										
										
											2007-05-05 13:22:27 +00:00
										 |  |  |   helper :issue_relations | 
					
						
							|  |  |  |   include IssueRelationsHelper | 
					
						
							| 
									
										
										
										
											2007-05-13 19:43:35 +00:00
										 |  |  |   helper :watchers | 
					
						
							|  |  |  |   include WatchersHelper | 
					
						
							| 
									
										
										
										
											2007-05-26 15:42:37 +00:00
										 |  |  |   helper :attachments | 
					
						
							| 
									
										
										
										
											2007-08-31 17:02:44 +00:00
										 |  |  |   include AttachmentsHelper | 
					
						
							|  |  |  |   helper :queries | 
					
						
							|  |  |  |   helper :sort | 
					
						
							|  |  |  |   include SortHelper | 
					
						
							| 
									
										
										
										
											2007-11-05 18:38:42 +00:00
										 |  |  |   include IssuesHelper | 
					
						
							| 
									
										
										
										
											2006-09-09 16:07:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-31 17:02:44 +00:00
										 |  |  |   def index | 
					
						
							|  |  |  |     sort_init "#{Issue.table_name}.id", "desc" | 
					
						
							|  |  |  |     sort_update | 
					
						
							|  |  |  |     retrieve_query | 
					
						
							|  |  |  |     if @query.valid? | 
					
						
							| 
									
										
										
										
											2007-11-05 18:38:42 +00:00
										 |  |  |       limit = %w(pdf csv).include?(params[:format]) ? Setting.issues_export_limit.to_i : 25
 | 
					
						
							|  |  |  |       @issue_count = Issue.count(:include => [:status, :project], :conditions => @query.statement) | 
					
						
							|  |  |  |       @issue_pages = Paginator.new self, @issue_count, limit, params['page'] | 
					
						
							| 
									
										
										
										
											2007-08-31 17:02:44 +00:00
										 |  |  |       @issues = Issue.find :all, :order => sort_clause, | 
					
						
							| 
									
										
										
										
											2007-11-18 15:53:58 +00:00
										 |  |  |                            :include => [ :assigned_to, :status, :tracker, :project, :priority, :category, :fixed_version ], | 
					
						
							| 
									
										
										
										
											2007-08-31 17:02:44 +00:00
										 |  |  |                            :conditions => @query.statement, | 
					
						
							| 
									
										
										
										
											2007-11-05 18:38:42 +00:00
										 |  |  |                            :limit  =>  limit, | 
					
						
							|  |  |  |                            :offset =>  @issue_pages.current.offset | 
					
						
							|  |  |  |       respond_to do |format| | 
					
						
							|  |  |  |         format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? } | 
					
						
							|  |  |  |         format.atom { render_feed(@issues, :title => l(:label_issue_plural)) } | 
					
						
							|  |  |  |         format.csv  { send_data(issues_to_csv(@issues, @project).read, :type => 'text/csv; header=present', :filename => 'export.csv') } | 
					
						
							|  |  |  |         format.pdf  { send_data(render(:template => 'issues/index.rfpdf', :layout => false), :type => 'application/pdf', :filename => 'export.pdf') } | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       # Send html if the query is not valid | 
					
						
							|  |  |  |       render(:template => 'issues/index.rhtml', :layout => !request.xhr?) | 
					
						
							| 
									
										
										
										
											2007-08-31 17:02:44 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2007-11-05 18:38:42 +00:00
										 |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   def changes | 
					
						
							|  |  |  |     sort_init "#{Issue.table_name}.id", "desc" | 
					
						
							|  |  |  |     sort_update | 
					
						
							|  |  |  |     retrieve_query | 
					
						
							|  |  |  |     if @query.valid? | 
					
						
							|  |  |  |       @changes = Journal.find :all, :include => [ :details, :user, {:issue => [:project, :author, :tracker, :status]} ], | 
					
						
							|  |  |  |                                      :conditions => @query.statement, | 
					
						
							|  |  |  |                                      :limit => 25, | 
					
						
							|  |  |  |                                      :order => "#{Journal.table_name}.created_on DESC" | 
					
						
							| 
									
										
										
										
											2007-08-31 20:22:36 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2007-11-05 18:38:42 +00:00
										 |  |  |     @title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name) | 
					
						
							|  |  |  |     render :layout => false, :content_type => 'application/atom+xml' | 
					
						
							| 
									
										
										
										
											2007-08-31 17:02:44 +00:00
										 |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2006-09-09 16:07:02 +00:00
										 |  |  |   def show | 
					
						
							| 
									
										
										
										
											2007-11-12 17:23:14 +00:00
										 |  |  |     @custom_values = @issue.custom_values.find(:all, :include => :custom_field, :order => "#{CustomField.table_name}.position") | 
					
						
							| 
									
										
										
										
											2007-08-14 09:02:40 +00:00
										 |  |  |     @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC") | 
					
						
							| 
									
										
										
										
											2007-11-20 15:40:16 +00:00
										 |  |  |     @status_options = @issue.status.find_new_statuses_allowed_to(User.current.role_for_project(@project), @issue.tracker) | 
					
						
							| 
									
										
										
										
											2007-11-05 18:38:42 +00:00
										 |  |  |     respond_to do |format| | 
					
						
							|  |  |  |       format.html { render :template => 'issues/show.rhtml' } | 
					
						
							|  |  |  |       format.pdf  { send_data(render(:template => 'issues/show.rfpdf', :layout => false), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") } | 
					
						
							| 
									
										
										
										
											2007-10-05 23:17:49 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2006-09-09 16:07:02 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2006-06-28 18:11:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |   def edit | 
					
						
							|  |  |  |     @priorities = Enumeration::get_values('IPRI') | 
					
						
							|  |  |  |     if request.get? | 
					
						
							|  |  |  |       @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) } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       begin | 
					
						
							| 
									
										
										
										
											2007-11-20 15:40:16 +00:00
										 |  |  |         @issue.init_journal(User.current) | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |         # Retrieve custom fields and values | 
					
						
							| 
									
										
										
										
											2007-10-28 10:55:59 +00:00
										 |  |  |         if params["custom_fields"] | 
					
						
							|  |  |  |           @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]) } | 
					
						
							|  |  |  |           @issue.custom_values = @custom_values | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |         @issue.attributes = params[:issue] | 
					
						
							| 
									
										
										
										
											2006-09-09 16:07:02 +00:00
										 |  |  |         if @issue.save | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |           flash[:notice] = l(:notice_successful_update) | 
					
						
							| 
									
										
										
										
											2007-10-28 10:55:59 +00:00
										 |  |  |           redirect_to(params[:back_to] || {:action => 'show', :id => @issue}) | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |       rescue ActiveRecord::StaleObjectError | 
					
						
							|  |  |  |         # Optimistic locking exception | 
					
						
							| 
									
										
										
										
											2007-08-02 17:42:20 +00:00
										 |  |  |         flash[:error] = l(:notice_locking_conflict) | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2006-09-09 16:07:02 +00:00
										 |  |  |     end		 | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   def add_note | 
					
						
							| 
									
										
										
										
											2007-10-19 19:09:47 +00:00
										 |  |  |     journal = @issue.init_journal(User.current, params[:notes]) | 
					
						
							|  |  |  |     params[:attachments].each { |file| | 
					
						
							|  |  |  |       next unless file.size > 0
 | 
					
						
							| 
									
										
										
										
											2007-11-20 15:40:16 +00:00
										 |  |  |       a = Attachment.create(:container => @issue, :file => file, :author => User.current) | 
					
						
							| 
									
										
										
										
											2007-10-19 19:09:47 +00:00
										 |  |  |       journal.details << JournalDetail.new(:property => 'attachment', | 
					
						
							|  |  |  |                                            :prop_key => a.id, | 
					
						
							|  |  |  |                                            :value => a.filename) unless a.new_record? | 
					
						
							|  |  |  |     } if params[:attachments] and params[:attachments].is_a? Array | 
					
						
							|  |  |  |     if journal.save | 
					
						
							|  |  |  |       flash[:notice] = l(:notice_successful_update) | 
					
						
							|  |  |  |       Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated') | 
					
						
							|  |  |  |       redirect_to :action => 'show', :id => @issue | 
					
						
							|  |  |  |       return | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |     show | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def change_status | 
					
						
							| 
									
										
										
										
											2007-11-20 15:40:16 +00:00
										 |  |  |     @status_options = @issue.status.find_new_statuses_allowed_to(User.current.role_for_project(@project), @issue.tracker) | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |     @new_status = IssueStatus.find(params[:new_status_id]) | 
					
						
							|  |  |  |     if params[:confirm] | 
					
						
							|  |  |  |       begin | 
					
						
							| 
									
										
										
										
											2007-11-20 15:40:16 +00:00
										 |  |  |         journal = @issue.init_journal(User.current, params[:notes]) | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |         @issue.status = @new_status | 
					
						
							|  |  |  |         if @issue.update_attributes(params[:issue]) | 
					
						
							| 
									
										
										
										
											2007-04-05 17:20:04 +00:00
										 |  |  |           # Save attachments | 
					
						
							|  |  |  |           params[:attachments].each { |file| | 
					
						
							|  |  |  |             next unless file.size > 0
 | 
					
						
							| 
									
										
										
										
											2007-11-20 15:40:16 +00:00
										 |  |  |             a = Attachment.create(:container => @issue, :file => file, :author => User.current)             | 
					
						
							| 
									
										
										
										
											2007-04-05 17:20:04 +00:00
										 |  |  |             journal.details << JournalDetail.new(:property => 'attachment', | 
					
						
							|  |  |  |                                                  :prop_key => a.id, | 
					
						
							|  |  |  |                                                  :value => a.filename) unless a.new_record? | 
					
						
							|  |  |  |           } if params[:attachments] and params[:attachments].is_a? Array | 
					
						
							|  |  |  |          | 
					
						
							| 
									
										
										
										
											2007-07-14 14:37:22 +00:00
										 |  |  |           # Log time | 
					
						
							| 
									
										
										
										
											2007-08-29 16:52:35 +00:00
										 |  |  |           if current_role.allowed_to?(:log_time) | 
					
						
							| 
									
										
										
										
											2007-11-20 15:40:16 +00:00
										 |  |  |             @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today) | 
					
						
							| 
									
										
										
										
											2007-07-14 14:37:22 +00:00
										 |  |  |             @time_entry.attributes = params[:time_entry] | 
					
						
							|  |  |  |             @time_entry.save | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |            | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |           flash[:notice] = l(:notice_successful_update) | 
					
						
							| 
									
										
										
										
											2007-09-14 15:30:46 +00:00
										 |  |  |           Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated') | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |           redirect_to :action => 'show', :id => @issue | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       rescue ActiveRecord::StaleObjectError | 
					
						
							|  |  |  |         # Optimistic locking exception | 
					
						
							| 
									
										
										
										
											2007-08-02 17:42:20 +00:00
										 |  |  |         flash[:error] = l(:notice_locking_conflict) | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |     end     | 
					
						
							|  |  |  |     @assignable_to = @project.members.find(:all, :include => :user).collect{ |m| m.user } | 
					
						
							| 
									
										
										
										
											2007-07-14 14:37:22 +00:00
										 |  |  |     @activities = Enumeration::get_values('ACTI') | 
					
						
							| 
									
										
										
										
											2006-09-09 16:07:02 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def destroy | 
					
						
							|  |  |  |     @issue.destroy | 
					
						
							| 
									
										
										
										
											2007-11-05 18:38:42 +00:00
										 |  |  |     redirect_to :action => 'index', :project_id => @project | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def destroy_attachment | 
					
						
							| 
									
										
										
										
											2007-04-05 17:20:04 +00:00
										 |  |  |     a = @issue.attachments.find(params[:attachment_id]) | 
					
						
							|  |  |  |     a.destroy | 
					
						
							| 
									
										
										
										
											2007-11-20 15:40:16 +00:00
										 |  |  |     journal = @issue.init_journal(User.current) | 
					
						
							| 
									
										
										
										
											2007-04-05 17:20:04 +00:00
										 |  |  |     journal.details << JournalDetail.new(:property => 'attachment', | 
					
						
							|  |  |  |                                          :prop_key => a.id, | 
					
						
							|  |  |  |                                          :old_value => a.filename) | 
					
						
							|  |  |  |     journal.save | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |     redirect_to :action => 'show', :id => @issue | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2007-10-28 10:55:59 +00:00
										 |  |  |    | 
					
						
							|  |  |  |   def context_menu | 
					
						
							|  |  |  |     @priorities = Enumeration.get_values('IPRI').reverse | 
					
						
							|  |  |  |     @statuses = IssueStatus.find(:all, :order => 'position') | 
					
						
							|  |  |  |     @allowed_statuses = @issue.status.find_new_statuses_allowed_to(User.current.role_for_project(@project), @issue.tracker) | 
					
						
							|  |  |  |     @assignables = @issue.assignable_users | 
					
						
							|  |  |  |     @assignables << @issue.assigned_to if @issue.assigned_to && !@assignables.include?(@issue.assigned_to) | 
					
						
							|  |  |  |     @can = {:edit => User.current.allowed_to?(:edit_issues, @project), | 
					
						
							|  |  |  |             :change_status => User.current.allowed_to?(:change_issue_status, @project), | 
					
						
							| 
									
										
										
										
											2007-10-28 14:31:59 +00:00
										 |  |  |             :add => User.current.allowed_to?(:add_issues, @project), | 
					
						
							| 
									
										
										
										
											2007-10-28 10:55:59 +00:00
										 |  |  |             :move => User.current.allowed_to?(:move_issues, @project), | 
					
						
							|  |  |  |             :delete => User.current.allowed_to?(:delete_issues, @project)} | 
					
						
							|  |  |  |     render :layout => false | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-06 08:54:05 +00:00
										 |  |  |   def preview | 
					
						
							|  |  |  |     issue = Issue.find_by_id(params[:id]) | 
					
						
							|  |  |  |     @attachements = issue.attachments if issue | 
					
						
							|  |  |  |     @text = params[:issue][:description] | 
					
						
							|  |  |  |     render :partial => 'common/preview' | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  | private | 
					
						
							|  |  |  |   def find_project | 
					
						
							|  |  |  |     @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category]) | 
					
						
							|  |  |  |     @project = @issue.project | 
					
						
							|  |  |  |   rescue ActiveRecord::RecordNotFound | 
					
						
							|  |  |  |     render_404 | 
					
						
							| 
									
										
										
										
											2007-08-31 17:02:44 +00:00
										 |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2007-11-05 18:38:42 +00:00
										 |  |  |   def find_optional_project | 
					
						
							|  |  |  |     return true unless params[:project_id] | 
					
						
							|  |  |  |     @project = Project.find(params[:project_id]) | 
					
						
							|  |  |  |     authorize | 
					
						
							|  |  |  |   rescue ActiveRecord::RecordNotFound | 
					
						
							|  |  |  |     render_404 | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2007-08-31 17:02:44 +00:00
										 |  |  |   # Retrieve query from session or build a new query | 
					
						
							|  |  |  |   def retrieve_query | 
					
						
							| 
									
										
										
										
											2007-11-05 18:38:42 +00:00
										 |  |  |     if params[:query_id] | 
					
						
							|  |  |  |       @query = Query.find(params[:query_id], :conditions => {:project_id => (@project ? @project.id : nil)}) | 
					
						
							|  |  |  |       session[:query] = @query | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       if params[:set_filter] or !session[:query] or session[:query].project != @project | 
					
						
							|  |  |  |         # Give it a name, required to be valid | 
					
						
							| 
									
										
										
										
											2007-11-20 15:40:16 +00:00
										 |  |  |         @query = Query.new(:name => "_") | 
					
						
							| 
									
										
										
										
											2007-11-05 18:38:42 +00:00
										 |  |  |         @query.project = @project | 
					
						
							|  |  |  |         if params[:fields] and params[:fields].is_a? Array | 
					
						
							|  |  |  |           params[:fields].each do |field| | 
					
						
							|  |  |  |             @query.add_filter(field, params[:operators][field], params[:values][field]) | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           @query.available_filters.keys.each do |field| | 
					
						
							|  |  |  |             @query.add_short_filter(field, params[field]) if params[field] | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2007-08-31 17:02:44 +00:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2007-11-05 18:38:42 +00:00
										 |  |  |         session[:query] = @query | 
					
						
							| 
									
										
										
										
											2007-08-31 17:02:44 +00:00
										 |  |  |       else | 
					
						
							| 
									
										
										
										
											2007-11-05 18:38:42 +00:00
										 |  |  |         @query = session[:query] | 
					
						
							| 
									
										
										
										
											2007-08-31 17:02:44 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2006-06-28 18:11:03 +00:00
										 |  |  | end |