Chris@0: # Redmine - project management software Chris@0: # Copyright (C) 2006-2008 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@0: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@0: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: class IssuesController < ApplicationController Chris@0: menu_item :new_issue, :only => [:new, :create] Chris@0: default_search_scope :issues Chris@0: Chris@0: before_filter :find_issue, :only => [:show, :edit, :update, :reply] Chris@0: before_filter :find_issues, :only => [:bulk_edit, :move, :destroy] Chris@0: before_filter :find_project, :only => [:new, :create, :update_form, :preview, :auto_complete] Chris@0: before_filter :authorize, :except => [:index, :changes, :preview, :context_menu] Chris@0: before_filter :find_optional_project, :only => [:index, :changes] Chris@0: before_filter :check_for_default_issue_status, :only => [:new, :create] Chris@0: before_filter :build_new_issue_from_params, :only => [:new, :create] Chris@0: accept_key_auth :index, :show, :changes Chris@0: Chris@0: rescue_from Query::StatementInvalid, :with => :query_statement_invalid Chris@0: Chris@0: helper :journals Chris@0: helper :projects Chris@0: include ProjectsHelper Chris@0: helper :custom_fields Chris@0: include CustomFieldsHelper Chris@0: helper :issue_relations Chris@0: include IssueRelationsHelper Chris@0: helper :watchers Chris@0: include WatchersHelper Chris@0: helper :attachments Chris@0: include AttachmentsHelper Chris@0: helper :queries Chris@0: include QueriesHelper Chris@0: helper :sort Chris@0: include SortHelper Chris@0: include IssuesHelper Chris@0: helper :timelog Chris@0: include Redmine::Export::PDF Chris@0: Chris@0: verify :method => [:post, :delete], Chris@0: :only => :destroy, Chris@0: :render => { :nothing => true, :status => :method_not_allowed } Chris@0: Chris@0: verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed } Chris@0: verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } Chris@0: Chris@0: def index Chris@0: retrieve_query Chris@0: sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) Chris@0: sort_update(@query.sortable_columns) Chris@0: Chris@0: if @query.valid? Chris@0: limit = case params[:format] Chris@0: when 'csv', 'pdf' Chris@0: Setting.issues_export_limit.to_i Chris@0: when 'atom' Chris@0: Setting.feeds_limit.to_i Chris@0: else Chris@0: per_page_option Chris@0: end Chris@0: Chris@0: @issue_count = @query.issue_count Chris@0: @issue_pages = Paginator.new self, @issue_count, limit, params['page'] Chris@0: @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version], Chris@0: :order => sort_clause, Chris@0: :offset => @issue_pages.current.offset, Chris@0: :limit => limit) Chris@0: @issue_count_by_group = @query.issue_count_by_group Chris@0: Chris@0: respond_to do |format| Chris@0: format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? } Chris@0: format.xml { render :layout => false } Chris@0: format.json { render :text => @issues.to_json, :layout => false } Chris@0: format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") } Chris@0: format.csv { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') } Chris@0: format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') } Chris@0: end Chris@0: else Chris@0: # Send html if the query is not valid Chris@0: render(:template => 'issues/index.rhtml', :layout => !request.xhr?) Chris@0: end Chris@0: rescue ActiveRecord::RecordNotFound Chris@0: render_404 Chris@0: end Chris@0: Chris@0: def changes Chris@0: retrieve_query Chris@0: sort_init 'id', 'desc' Chris@0: sort_update(@query.sortable_columns) Chris@0: Chris@0: if @query.valid? Chris@0: @journals = @query.journals(:order => "#{Journal.table_name}.created_on DESC", Chris@0: :limit => 25) Chris@0: end Chris@0: @title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name) Chris@0: render :layout => false, :content_type => 'application/atom+xml' Chris@0: rescue ActiveRecord::RecordNotFound Chris@0: render_404 Chris@0: end Chris@0: Chris@0: def show Chris@0: @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC") Chris@0: @journals.each_with_index {|j,i| j.indice = i+1} Chris@0: @journals.reverse! if User.current.wants_comments_in_reverse_order? Chris@0: @changesets = @issue.changesets.visible.all Chris@0: @changesets.reverse! if User.current.wants_comments_in_reverse_order? Chris@0: @allowed_statuses = @issue.new_statuses_allowed_to(User.current) Chris@0: @edit_allowed = User.current.allowed_to?(:edit_issues, @project) Chris@0: @priorities = IssuePriority.all Chris@0: @time_entry = TimeEntry.new Chris@0: respond_to do |format| Chris@0: format.html { render :template => 'issues/show.rhtml' } Chris@0: format.xml { render :layout => false } Chris@0: format.json { render :text => @issue.to_json, :layout => false } Chris@0: format.atom { render :action => 'changes', :layout => false, :content_type => 'application/atom+xml' } Chris@0: format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") } Chris@0: end Chris@0: end Chris@0: Chris@0: # Add a new issue Chris@0: # The new issue will be created from an existing one if copy_from parameter is given Chris@0: def new Chris@0: render :action => 'new', :layout => !request.xhr? Chris@0: end Chris@0: Chris@0: def create Chris@0: call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue }) Chris@0: if @issue.save Chris@0: attachments = Attachment.attach_files(@issue, params[:attachments]) Chris@0: render_attachment_warning_if_needed(@issue) Chris@0: flash[:notice] = l(:notice_successful_create) Chris@0: call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue}) Chris@0: respond_to do |format| Chris@0: format.html { Chris@0: redirect_to(params[:continue] ? { :action => 'new', :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } : Chris@0: { :action => 'show', :id => @issue }) Chris@0: } Chris@0: format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'issues', :action => 'show', :id => @issue) } Chris@0: format.json { render :text => @issue.to_json, :status => :created, :location => url_for(:controller => 'issues', :action => 'show'), :layout => false } Chris@0: end Chris@0: return Chris@0: else Chris@0: respond_to do |format| Chris@0: format.html { render :action => 'new' } Chris@0: format.xml { render(:xml => @issue.errors, :status => :unprocessable_entity); return } Chris@0: format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false } Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: # Attributes that can be updated on workflow transition (without :edit permission) Chris@0: # TODO: make it configurable (at least per role) Chris@0: UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION) Chris@0: Chris@0: def edit Chris@0: update_issue_from_params Chris@0: Chris@0: @journal = @issue.current_journal Chris@0: Chris@0: respond_to do |format| Chris@0: format.html { } Chris@0: format.xml { } Chris@0: end Chris@0: end Chris@0: Chris@0: def update Chris@0: update_issue_from_params Chris@0: Chris@0: if @issue.save_issue_with_child_records(params, @time_entry) Chris@0: render_attachment_warning_if_needed(@issue) Chris@0: flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? Chris@0: Chris@0: respond_to do |format| Chris@0: format.html { redirect_back_or_default({:action => 'show', :id => @issue}) } Chris@0: format.xml { head :ok } Chris@0: format.json { head :ok } Chris@0: end Chris@0: else Chris@0: render_attachment_warning_if_needed(@issue) Chris@0: flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? Chris@0: @journal = @issue.current_journal Chris@0: Chris@0: respond_to do |format| Chris@0: format.html { render :action => 'edit' } Chris@0: format.xml { render :xml => @issue.errors, :status => :unprocessable_entity } Chris@0: format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false } Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: def reply Chris@0: journal = Journal.find(params[:journal_id]) if params[:journal_id] Chris@0: if journal Chris@0: user = journal.user Chris@0: text = journal.notes Chris@0: else Chris@0: user = @issue.author Chris@0: text = @issue.description Chris@0: end Chris@0: # Replaces pre blocks with [...] Chris@0: text = text.to_s.strip.gsub(%r{
((.|\s)*?)
}m, '[...]') Chris@0: content = "#{ll(Setting.default_language, :text_user_wrote, user)}\n> " Chris@0: content << text.gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n" Chris@0: Chris@0: render(:update) { |page| Chris@0: page.<< "$('notes').value = \"#{escape_javascript content}\";" Chris@0: page.show 'update' Chris@0: page << "Form.Element.focus('notes');" Chris@0: page << "Element.scrollTo('update');" Chris@0: page << "$('notes').scrollTop = $('notes').scrollHeight - $('notes').clientHeight;" Chris@0: } Chris@0: end Chris@0: Chris@0: # Bulk edit a set of issues Chris@0: def bulk_edit Chris@0: @issues.sort! Chris@0: if request.post? Chris@0: attributes = (params[:issue] || {}).reject {|k,v| v.blank?} Chris@0: attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} Chris@0: attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values] Chris@0: Chris@0: unsaved_issue_ids = [] Chris@0: @issues.each do |issue| Chris@0: issue.reload Chris@0: journal = issue.init_journal(User.current, params[:notes]) Chris@0: issue.safe_attributes = attributes Chris@0: call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue }) Chris@0: unless issue.save Chris@0: # Keep unsaved issue ids to display them in flash error Chris@0: unsaved_issue_ids << issue.id Chris@0: end Chris@0: end Chris@0: set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids) Chris@0: redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project}) Chris@0: return Chris@0: end Chris@0: @available_statuses = Workflow.available_statuses(@project) Chris@0: @custom_fields = @project.all_issue_custom_fields Chris@0: end Chris@0: Chris@0: def move Chris@0: @issues.sort! Chris@0: @copy = params[:copy_options] && params[:copy_options][:copy] Chris@0: @allowed_projects = Issue.allowed_target_projects_on_move Chris@0: @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:new_project_id]} if params[:new_project_id] Chris@0: @target_project ||= @project Chris@0: @trackers = @target_project.trackers Chris@0: @available_statuses = Workflow.available_statuses(@project) Chris@0: if request.post? Chris@0: new_tracker = params[:new_tracker_id].blank? ? nil : @target_project.trackers.find_by_id(params[:new_tracker_id]) Chris@0: unsaved_issue_ids = [] Chris@0: moved_issues = [] Chris@0: @issues.each do |issue| Chris@0: issue.reload Chris@0: changed_attributes = {} Chris@0: [:assigned_to_id, :status_id, :start_date, :due_date].each do |valid_attribute| Chris@0: unless params[valid_attribute].blank? Chris@0: changed_attributes[valid_attribute] = (params[valid_attribute] == 'none' ? nil : params[valid_attribute]) Chris@0: end Chris@0: end Chris@0: issue.init_journal(User.current) Chris@0: call_hook(:controller_issues_move_before_save, { :params => params, :issue => issue, :target_project => @target_project, :copy => !!@copy }) Chris@0: if r = issue.move_to_project(@target_project, new_tracker, {:copy => @copy, :attributes => changed_attributes}) Chris@0: moved_issues << r Chris@0: else Chris@0: unsaved_issue_ids << issue.id Chris@0: end Chris@0: end Chris@0: set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids) Chris@0: Chris@0: if params[:follow] Chris@0: if @issues.size == 1 && moved_issues.size == 1 Chris@0: redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first Chris@0: else Chris@0: redirect_to :controller => 'issues', :action => 'index', :project_id => (@target_project || @project) Chris@0: end Chris@0: else Chris@0: redirect_to :controller => 'issues', :action => 'index', :project_id => @project Chris@0: end Chris@0: return Chris@0: end Chris@0: render :layout => false if request.xhr? Chris@0: end Chris@0: Chris@0: def destroy Chris@0: @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f Chris@0: if @hours > 0 Chris@0: case params[:todo] Chris@0: when 'destroy' Chris@0: # nothing to do Chris@0: when 'nullify' Chris@0: TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues]) Chris@0: when 'reassign' Chris@0: reassign_to = @project.issues.find_by_id(params[:reassign_to_id]) Chris@0: if reassign_to.nil? Chris@0: flash.now[:error] = l(:error_issue_not_found_in_project) Chris@0: return Chris@0: else Chris@0: TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues]) Chris@0: end Chris@0: else Chris@0: unless params[:format] == 'xml' || params[:format] == 'json' Chris@0: # display the destroy form if it's a user request Chris@0: return Chris@0: end Chris@0: end Chris@0: end Chris@0: @issues.each(&:destroy) Chris@0: respond_to do |format| Chris@0: format.html { redirect_to :action => 'index', :project_id => @project } Chris@0: format.xml { head :ok } Chris@0: format.json { head :ok } Chris@0: end Chris@0: end Chris@0: Chris@0: def context_menu Chris@0: @issues = Issue.find_all_by_id(params[:ids], :include => :project) Chris@0: if (@issues.size == 1) Chris@0: @issue = @issues.first Chris@0: @allowed_statuses = @issue.new_statuses_allowed_to(User.current) Chris@0: end Chris@0: projects = @issues.collect(&:project).compact.uniq Chris@0: @project = projects.first if projects.size == 1 Chris@0: Chris@0: @can = {:edit => (@project && User.current.allowed_to?(:edit_issues, @project)), Chris@0: :log_time => (@project && User.current.allowed_to?(:log_time, @project)), Chris@0: :update => (@project && (User.current.allowed_to?(:edit_issues, @project) || (User.current.allowed_to?(:change_status, @project) && @allowed_statuses && !@allowed_statuses.empty?))), Chris@0: :move => (@project && User.current.allowed_to?(:move_issues, @project)), Chris@0: :copy => (@issue && @project.trackers.include?(@issue.tracker) && User.current.allowed_to?(:add_issues, @project)), Chris@0: :delete => (@project && User.current.allowed_to?(:delete_issues, @project)) Chris@0: } Chris@0: if @project Chris@0: @assignables = @project.assignable_users Chris@0: @assignables << @issue.assigned_to if @issue && @issue.assigned_to && !@assignables.include?(@issue.assigned_to) Chris@0: @trackers = @project.trackers Chris@0: end Chris@0: Chris@0: @priorities = IssuePriority.all.reverse Chris@0: @statuses = IssueStatus.find(:all, :order => 'position') Chris@0: @back = params[:back_url] || request.env['HTTP_REFERER'] Chris@0: Chris@0: render :layout => false Chris@0: end Chris@0: Chris@0: def update_form Chris@0: if params[:id].blank? Chris@0: @issue = Issue.new Chris@0: @issue.project = @project Chris@0: else Chris@0: @issue = @project.issues.visible.find(params[:id]) Chris@0: end Chris@0: @issue.attributes = params[:issue] Chris@0: @allowed_statuses = ([@issue.status] + @issue.status.find_new_statuses_allowed_to(User.current.roles_for_project(@project), @issue.tracker)).uniq Chris@0: @priorities = IssuePriority.all Chris@0: Chris@0: render :partial => 'attributes' Chris@0: end Chris@0: Chris@0: def preview Chris@0: @issue = @project.issues.find_by_id(params[:id]) unless params[:id].blank? Chris@0: if @issue Chris@0: @attachements = @issue.attachments Chris@0: @description = params[:issue] && params[:issue][:description] Chris@0: if @description && @description.gsub(/(\r?\n|\n\r?)/, "\n") == @issue.description.to_s.gsub(/(\r?\n|\n\r?)/, "\n") Chris@0: @description = nil Chris@0: end Chris@0: @notes = params[:notes] Chris@0: else Chris@0: @description = (params[:issue] ? params[:issue][:description] : nil) Chris@0: end Chris@0: render :layout => false Chris@0: end Chris@0: Chris@0: def auto_complete Chris@0: @issues = [] Chris@0: q = params[:q].to_s Chris@0: if q.match(/^\d+$/) Chris@0: @issues << @project.issues.visible.find_by_id(q.to_i) Chris@0: end Chris@0: unless q.blank? Chris@0: @issues += @project.issues.visible.find(:all, :conditions => ["LOWER(#{Issue.table_name}.subject) LIKE ?", "%#{q.downcase}%"], :limit => 10) Chris@0: end Chris@0: render :layout => false Chris@0: end Chris@0: Chris@0: private Chris@0: def find_issue Chris@0: @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category]) Chris@0: @project = @issue.project Chris@0: rescue ActiveRecord::RecordNotFound Chris@0: render_404 Chris@0: end Chris@0: Chris@0: # Filter for bulk operations Chris@0: def find_issues Chris@0: @issues = Issue.find_all_by_id(params[:id] || params[:ids]) Chris@0: raise ActiveRecord::RecordNotFound if @issues.empty? Chris@0: projects = @issues.collect(&:project).compact.uniq Chris@0: if projects.size == 1 Chris@0: @project = projects.first Chris@0: else Chris@0: # TODO: let users bulk edit/move/destroy issues from different projects Chris@0: render_error 'Can not bulk edit/move/destroy issues from different projects' Chris@0: return false Chris@0: end Chris@0: rescue ActiveRecord::RecordNotFound Chris@0: render_404 Chris@0: end Chris@0: Chris@0: def find_project Chris@0: project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id] Chris@0: @project = Project.find(project_id) Chris@0: rescue ActiveRecord::RecordNotFound Chris@0: render_404 Chris@0: end Chris@0: Chris@0: # Used by #edit and #update to set some common instance variables Chris@0: # from the params Chris@0: # TODO: Refactor, not everything in here is needed by #edit Chris@0: def update_issue_from_params Chris@0: @allowed_statuses = @issue.new_statuses_allowed_to(User.current) Chris@0: @priorities = IssuePriority.all Chris@0: @edit_allowed = User.current.allowed_to?(:edit_issues, @project) Chris@0: @time_entry = TimeEntry.new Chris@0: Chris@0: @notes = params[:notes] Chris@0: @issue.init_journal(User.current, @notes) Chris@0: # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed Chris@0: if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue] Chris@0: attrs = params[:issue].dup Chris@0: attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed Chris@0: attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s} Chris@0: @issue.safe_attributes = attrs Chris@0: end Chris@0: Chris@0: end Chris@0: Chris@0: # TODO: Refactor, lots of extra code in here Chris@0: def build_new_issue_from_params Chris@0: @issue = Issue.new Chris@0: @issue.copy_from(params[:copy_from]) if params[:copy_from] Chris@0: @issue.project = @project Chris@0: # Tracker must be set before custom field values Chris@0: @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first) Chris@0: if @issue.tracker.nil? Chris@0: render_error l(:error_no_tracker_in_project) Chris@0: return false Chris@0: end Chris@0: if params[:issue].is_a?(Hash) Chris@0: @issue.safe_attributes = params[:issue] Chris@0: @issue.watcher_user_ids = params[:issue]['watcher_user_ids'] if User.current.allowed_to?(:add_issue_watchers, @project) Chris@0: end Chris@0: @issue.author = User.current Chris@0: @issue.start_date ||= Date.today Chris@0: @priorities = IssuePriority.all Chris@0: @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true) Chris@0: end Chris@0: Chris@0: def set_flash_from_bulk_issue_save(issues, unsaved_issue_ids) Chris@0: if unsaved_issue_ids.empty? Chris@0: flash[:notice] = l(:notice_successful_update) unless issues.empty? Chris@0: else Chris@0: flash[:error] = l(:notice_failed_to_save_issues, Chris@0: :count => unsaved_issue_ids.size, Chris@0: :total => issues.size, Chris@0: :ids => '#' + unsaved_issue_ids.join(', #')) Chris@0: end Chris@0: end Chris@0: Chris@0: def check_for_default_issue_status Chris@0: if IssueStatus.default.nil? Chris@0: render_error l(:error_no_default_issue_status) Chris@0: return false Chris@0: end Chris@0: end Chris@0: end