Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: class IssuesController < ApplicationController Chris@909: menu_item :new_issue, :only => [:new, :create] Chris@909: default_search_scope :issues Chris@909: Chris@909: before_filter :find_issue, :only => [:show, :edit, :update] Chris@909: before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :move, :perform_move, :destroy] Chris@909: before_filter :check_project_uniqueness, :only => [:move, :perform_move] Chris@909: before_filter :find_project, :only => [:new, :create] Chris@909: before_filter :authorize, :except => [:index] Chris@909: before_filter :find_optional_project, :only => [:index] Chris@909: before_filter :check_for_default_issue_status, :only => [:new, :create] Chris@909: before_filter :build_new_issue_from_params, :only => [:new, :create] Chris@909: accept_rss_auth :index, :show Chris@909: accept_api_auth :index, :show, :create, :update, :destroy Chris@909: Chris@909: rescue_from Query::StatementInvalid, :with => :query_statement_invalid Chris@909: Chris@909: helper :journals Chris@909: helper :projects Chris@909: include ProjectsHelper Chris@909: helper :custom_fields Chris@909: include CustomFieldsHelper Chris@909: helper :issue_relations Chris@909: include IssueRelationsHelper Chris@909: helper :watchers Chris@909: include WatchersHelper Chris@909: helper :attachments Chris@909: include AttachmentsHelper Chris@909: helper :queries Chris@909: include QueriesHelper Chris@909: helper :repositories Chris@909: include RepositoriesHelper Chris@909: helper :sort Chris@909: include SortHelper Chris@909: include IssuesHelper Chris@909: helper :timelog Chris@909: helper :gantt Chris@909: include Redmine::Export::PDF Chris@909: Chris@909: verify :method => [:post, :delete], Chris@909: :only => :destroy, Chris@909: :render => { :nothing => true, :status => :method_not_allowed } Chris@909: Chris@909: verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed } Chris@909: verify :method => :post, :only => :bulk_update, :render => {:nothing => true, :status => :method_not_allowed } Chris@909: verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } Chris@909: Chris@909: def index Chris@909: retrieve_query Chris@909: sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) Chris@909: sort_update(@query.sortable_columns) Chris@909: Chris@909: if @query.valid? Chris@909: case params[:format] Chris@909: when 'csv', 'pdf' Chris@909: @limit = Setting.issues_export_limit.to_i Chris@909: when 'atom' Chris@909: @limit = Setting.feeds_limit.to_i Chris@909: when 'xml', 'json' Chris@909: @offset, @limit = api_offset_and_limit Chris@909: else Chris@909: @limit = per_page_option Chris@909: end Chris@909: Chris@909: @issue_count = @query.issue_count Chris@909: @issue_pages = Paginator.new self, @issue_count, @limit, params['page'] Chris@909: @offset ||= @issue_pages.current.offset Chris@909: @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version], Chris@909: :order => sort_clause, Chris@909: :offset => @offset, Chris@909: :limit => @limit) Chris@909: @issue_count_by_group = @query.issue_count_by_group Chris@909: Chris@909: respond_to do |format| Chris@909: format.html { render :template => 'issues/index', :layout => !request.xhr? } Chris@909: format.api { Chris@909: Issue.load_relations(@issues) if include_in_api_response?('relations') Chris@909: } Chris@909: format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") } Chris@909: format.csv { send_data(issues_to_csv(@issues, @project, @query, params), :type => 'text/csv; header=present', :filename => 'export.csv') } Chris@909: format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') } Chris@909: end Chris@909: else Chris@909: respond_to do |format| Chris@909: format.html { render(:template => 'issues/index', :layout => !request.xhr?) } Chris@909: format.any(:atom, :csv, :pdf) { render(:nothing => true) } Chris@909: format.api { render_validation_errors(@query) } Chris@909: end Chris@909: end Chris@909: rescue ActiveRecord::RecordNotFound Chris@909: render_404 Chris@909: end Chris@909: Chris@909: def show Chris@909: @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC") Chris@909: @journals.each_with_index {|j,i| j.indice = i+1} Chris@909: @journals.reverse! if User.current.wants_comments_in_reverse_order? Chris@909: Chris@909: if User.current.allowed_to?(:view_changesets, @project) Chris@909: @changesets = @issue.changesets.visible.all Chris@909: @changesets.reverse! if User.current.wants_comments_in_reverse_order? Chris@909: end Chris@909: Chris@909: @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? } Chris@909: @allowed_statuses = @issue.new_statuses_allowed_to(User.current) Chris@909: @edit_allowed = User.current.allowed_to?(:edit_issues, @project) Chris@909: @priorities = IssuePriority.active Chris@909: @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project) Chris@909: respond_to do |format| Chris@909: format.html { render :template => 'issues/show' } Chris@909: format.api Chris@909: format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' } Chris@909: format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") } Chris@909: end Chris@909: end Chris@909: Chris@909: # Add a new issue Chris@909: # The new issue will be created from an existing one if copy_from parameter is given Chris@909: def new Chris@909: respond_to do |format| Chris@909: format.html { render :action => 'new', :layout => !request.xhr? } Chris@909: format.js { render :partial => 'attributes' } Chris@909: end Chris@909: end Chris@909: Chris@909: def create Chris@909: call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue }) Chris@909: if @issue.save Chris@909: attachments = Attachment.attach_files(@issue, params[:attachments]) Chris@909: call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue}) Chris@909: respond_to do |format| Chris@909: format.html { Chris@909: render_attachment_warning_if_needed(@issue) Chris@909: flash[:notice] = l(:notice_issue_successful_create, :id => "##{@issue.id}") Chris@909: redirect_to(params[:continue] ? { :action => 'new', :project_id => @project, :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } : Chris@909: { :action => 'show', :id => @issue }) Chris@909: } Chris@909: format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) } Chris@909: end Chris@909: return Chris@909: else Chris@909: respond_to do |format| Chris@909: format.html { render :action => 'new' } Chris@909: format.api { render_validation_errors(@issue) } Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: def edit Chris@909: update_issue_from_params Chris@909: Chris@909: @journal = @issue.current_journal Chris@909: Chris@909: respond_to do |format| Chris@909: format.html { } Chris@909: format.xml { } Chris@909: end Chris@909: end Chris@909: Chris@909: def update Chris@909: update_issue_from_params Chris@909: Chris@909: if @issue.save_issue_with_child_records(params, @time_entry) Chris@909: render_attachment_warning_if_needed(@issue) Chris@909: flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? Chris@909: Chris@909: respond_to do |format| Chris@909: format.html { redirect_back_or_default({:action => 'show', :id => @issue}) } Chris@909: format.api { head :ok } Chris@909: end Chris@909: else Chris@909: render_attachment_warning_if_needed(@issue) Chris@909: flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? Chris@909: @journal = @issue.current_journal Chris@909: Chris@909: respond_to do |format| Chris@909: format.html { render :action => 'edit' } Chris@909: format.api { render_validation_errors(@issue) } Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: # Bulk edit a set of issues Chris@909: def bulk_edit Chris@909: @issues.sort! Chris@909: @available_statuses = @projects.map{|p|Workflow.available_statuses(p)}.inject{|memo,w|memo & w} Chris@909: @custom_fields = @projects.map{|p|p.all_issue_custom_fields}.inject{|memo,c|memo & c} Chris@909: @assignables = @projects.map(&:assignable_users).inject{|memo,a| memo & a} Chris@909: @trackers = @projects.map(&:trackers).inject{|memo,t| memo & t} Chris@909: end Chris@909: Chris@909: def bulk_update Chris@909: @issues.sort! Chris@909: attributes = parse_params_for_bulk_issue_attributes(params) Chris@909: Chris@909: unsaved_issue_ids = [] Chris@909: @issues.each do |issue| Chris@909: issue.reload Chris@909: journal = issue.init_journal(User.current, params[:notes]) Chris@909: issue.safe_attributes = attributes Chris@909: call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue }) Chris@909: unless issue.save Chris@909: # Keep unsaved issue ids to display them in flash error Chris@909: unsaved_issue_ids << issue.id Chris@909: end Chris@909: end Chris@909: set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids) Chris@909: redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project}) Chris@909: end Chris@909: Chris@909: def destroy Chris@909: @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f Chris@909: if @hours > 0 Chris@909: case params[:todo] Chris@909: when 'destroy' Chris@909: # nothing to do Chris@909: when 'nullify' Chris@909: TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues]) Chris@909: when 'reassign' Chris@909: reassign_to = @project.issues.find_by_id(params[:reassign_to_id]) Chris@909: if reassign_to.nil? Chris@909: flash.now[:error] = l(:error_issue_not_found_in_project) Chris@909: return Chris@909: else Chris@909: TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues]) Chris@909: end Chris@909: else Chris@909: # display the destroy form if it's a user request Chris@909: return unless api_request? Chris@909: end Chris@909: end Chris@909: @issues.each do |issue| Chris@909: begin Chris@909: issue.reload.destroy Chris@909: rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists Chris@909: # nothing to do, issue was already deleted (eg. by a parent) Chris@909: end Chris@909: end Chris@909: respond_to do |format| Chris@909: format.html { redirect_back_or_default(:action => 'index', :project_id => @project) } Chris@909: format.api { head :ok } Chris@909: end Chris@909: end Chris@909: Chris@909: private Chris@909: def find_issue Chris@909: # Issue.visible.find(...) can not be used to redirect user to the login form Chris@909: # if the issue actually exists but requires authentication Chris@909: @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category]) Chris@909: unless @issue.visible? Chris@909: deny_access Chris@909: return Chris@909: end Chris@909: @project = @issue.project Chris@909: rescue ActiveRecord::RecordNotFound Chris@909: render_404 Chris@909: end Chris@909: Chris@909: def find_project Chris@909: project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id] Chris@909: @project = Project.find(project_id) Chris@909: rescue ActiveRecord::RecordNotFound Chris@909: render_404 Chris@909: end Chris@909: Chris@909: # Used by #edit and #update to set some common instance variables Chris@909: # from the params Chris@909: # TODO: Refactor, not everything in here is needed by #edit Chris@909: def update_issue_from_params Chris@909: @allowed_statuses = @issue.new_statuses_allowed_to(User.current) Chris@909: @priorities = IssuePriority.active Chris@909: @edit_allowed = User.current.allowed_to?(:edit_issues, @project) Chris@909: @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project) Chris@909: @time_entry.attributes = params[:time_entry] Chris@909: Chris@909: @notes = params[:notes] || (params[:issue].present? ? params[:issue][:notes] : nil) Chris@909: @issue.init_journal(User.current, @notes) Chris@909: @issue.safe_attributes = params[:issue] Chris@909: end Chris@909: Chris@909: # TODO: Refactor, lots of extra code in here Chris@909: # TODO: Changing tracker on an existing issue should not trigger this Chris@909: def build_new_issue_from_params Chris@909: if params[:id].blank? Chris@909: @issue = Issue.new Chris@909: @issue.copy_from(params[:copy_from]) if params[:copy_from] Chris@909: @issue.project = @project Chris@909: else Chris@909: @issue = @project.issues.visible.find(params[:id]) Chris@909: end Chris@909: Chris@909: @issue.project = @project Chris@909: @issue.author = User.current Chris@909: # Tracker must be set before custom field values Chris@909: @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first) Chris@909: if @issue.tracker.nil? Chris@909: render_error l(:error_no_tracker_in_project) Chris@909: return false Chris@909: end Chris@909: @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date? Chris@909: if params[:issue].is_a?(Hash) Chris@909: @issue.safe_attributes = params[:issue] Chris@909: if User.current.allowed_to?(:add_issue_watchers, @project) && @issue.new_record? Chris@909: @issue.watcher_user_ids = params[:issue]['watcher_user_ids'] Chris@909: end Chris@909: end Chris@909: @priorities = IssuePriority.active Chris@909: @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true) Chris@909: end Chris@909: Chris@909: def check_for_default_issue_status Chris@909: if IssueStatus.default.nil? Chris@909: render_error l(:error_no_default_issue_status) Chris@909: return false Chris@909: end Chris@909: end Chris@909: Chris@909: def parse_params_for_bulk_issue_attributes(params) Chris@909: attributes = (params[:issue] || {}).reject {|k,v| v.blank?} Chris@909: attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} Chris@909: attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values] Chris@909: attributes Chris@909: end Chris@909: end