Chris@0: # Redmine - project management software Chris@441: # Copyright (C) 2006-2011 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@441: # 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@441: # 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@441: Chris@14: before_filter :find_issue, :only => [:show, :edit, :update] Chris@14: before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :move, :perform_move, :destroy] chris@37: before_filter :check_project_uniqueness, :only => [:move, :perform_move] Chris@14: before_filter :find_project, :only => [:new, :create] Chris@14: before_filter :authorize, :except => [:index] Chris@14: before_filter :find_optional_project, :only => [:index] 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@507: accept_rss_auth :index, :show Chris@507: accept_api_auth :index, :show, :create, :update, :destroy Chris@0: Chris@0: rescue_from Query::StatementInvalid, :with => :query_statement_invalid Chris@441: Chris@0: helper :journals Chris@0: helper :projects Chris@441: 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@119: helper :repositories Chris@119: include RepositoriesHelper Chris@0: helper :sort Chris@0: include SortHelper Chris@0: include IssuesHelper Chris@0: helper :timelog chris@22: helper :gantt 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@14: verify :method => :post, :only => :bulk_update, :render => {:nothing => true, :status => :method_not_allowed } Chris@0: verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } Chris@441: 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@441: Chris@0: if @query.valid? Chris@119: case params[:format] Chris@0: when 'csv', 'pdf' Chris@119: @limit = Setting.issues_export_limit.to_i Chris@0: when 'atom' Chris@119: @limit = Setting.feeds_limit.to_i Chris@119: when 'xml', 'json' Chris@119: @offset, @limit = api_offset_and_limit Chris@0: else Chris@119: @limit = per_page_option Chris@0: end Chris@441: Chris@0: @issue_count = @query.issue_count Chris@119: @issue_pages = Paginator.new self, @issue_count, @limit, params['page'] Chris@119: @offset ||= @issue_pages.current.offset Chris@0: @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version], Chris@441: :order => sort_clause, Chris@441: :offset => @offset, Chris@119: :limit => @limit) Chris@0: @issue_count_by_group = @query.issue_count_by_group Chris@441: Chris@0: respond_to do |format| Chris@0: format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? } Chris@119: format.api 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@441: 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@441: Chris@441: if User.current.allowed_to?(:view_changesets, @project) Chris@441: @changesets = @issue.changesets.visible.all Chris@441: @changesets.reverse! if User.current.wants_comments_in_reverse_order? Chris@441: end Chris@441: Chris@210: @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? } 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@441: @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project) Chris@0: respond_to do |format| Chris@0: format.html { render :template => 'issues/show.rhtml' } Chris@119: format.api Chris@14: format.atom { render :template => 'journals/index', :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@14: respond_to do |format| Chris@14: format.html { render :action => 'new', :layout => !request.xhr? } Chris@14: format.js { render :partial => 'attributes' } Chris@14: end 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@22: 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@0: { :action => 'show', :id => @issue }) Chris@0: } Chris@119: format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) } Chris@0: end Chris@0: return Chris@0: else Chris@0: respond_to do |format| Chris@0: format.html { render :action => 'new' } Chris@119: format.api { render_validation_errors(@issue) } Chris@0: end Chris@0: end Chris@0: end Chris@441: 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@119: format.api { 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@119: format.api { render_validation_errors(@issue) } Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: # Bulk edit a set of issues Chris@0: def bulk_edit Chris@0: @issues.sort! chris@37: @available_statuses = @projects.map{|p|Workflow.available_statuses(p)}.inject{|memo,w|memo & w} chris@37: @custom_fields = @projects.map{|p|p.all_issue_custom_fields}.inject{|memo,c|memo & c} chris@37: @assignables = @projects.map(&:assignable_users).inject{|memo,a| memo & a} chris@37: @trackers = @projects.map(&:trackers).inject{|memo,t| memo & t} Chris@0: end Chris@0: Chris@14: def bulk_update Chris@0: @issues.sort! Chris@14: attributes = parse_params_for_bulk_issue_attributes(params) Chris@14: Chris@14: unsaved_issue_ids = [] Chris@14: @issues.each do |issue| Chris@14: issue.reload Chris@14: journal = issue.init_journal(User.current, params[:notes]) Chris@14: issue.safe_attributes = attributes Chris@14: call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue }) Chris@14: unless issue.save Chris@14: # Keep unsaved issue ids to display them in flash error Chris@14: unsaved_issue_ids << issue.id Chris@0: end Chris@0: end Chris@14: set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids) Chris@14: redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project}) Chris@0: end Chris@441: 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@119: # display the destroy form if it's a user request Chris@119: return unless api_request? Chris@0: end Chris@0: end Chris@441: @issues.each do |issue| Chris@441: begin Chris@441: issue.reload.destroy Chris@441: rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists Chris@441: # nothing to do, issue was already deleted (eg. by a parent) Chris@441: end Chris@441: end Chris@0: respond_to do |format| chris@37: format.html { redirect_back_or_default(:action => 'index', :project_id => @project) } Chris@119: format.api { head :ok } Chris@0: end Chris@0: end Chris@0: Chris@0: private Chris@0: def find_issue Chris@441: # Issue.visible.find(...) can not be used to redirect user to the login form Chris@441: # if the issue actually exists but requires authentication Chris@0: @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category]) Chris@441: unless @issue.visible? Chris@441: deny_access Chris@441: return Chris@441: end Chris@0: @project = @issue.project Chris@0: rescue ActiveRecord::RecordNotFound Chris@0: render_404 Chris@0: end Chris@441: 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@441: 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@441: @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project) chris@37: @time_entry.attributes = params[:time_entry] Chris@441: chris@22: @notes = params[:notes] || (params[:issue].present? ? params[:issue][:notes] : nil) Chris@0: @issue.init_journal(User.current, @notes) chris@37: @issue.safe_attributes = params[:issue] Chris@0: end Chris@0: Chris@0: # TODO: Refactor, lots of extra code in here chris@37: # TODO: Changing tracker on an existing issue should not trigger this Chris@0: def build_new_issue_from_params Chris@14: if params[:id].blank? Chris@14: @issue = Issue.new Chris@14: @issue.copy_from(params[:copy_from]) if params[:copy_from] Chris@14: @issue.project = @project Chris@14: else Chris@14: @issue = @project.issues.visible.find(params[:id]) Chris@14: end Chris@441: Chris@0: @issue.project = @project Chris@507: @issue.author = User.current 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@37: @issue.start_date ||= Date.today Chris@0: if params[:issue].is_a?(Hash) Chris@0: @issue.safe_attributes = params[:issue] chris@37: if User.current.allowed_to?(:add_issue_watchers, @project) && @issue.new_record? chris@37: @issue.watcher_user_ids = params[:issue]['watcher_user_ids'] chris@37: end Chris@0: end 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 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@14: Chris@14: def parse_params_for_bulk_issue_attributes(params) Chris@14: attributes = (params[:issue] || {}).reject {|k,v| v.blank?} Chris@14: attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} Chris@14: attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values] Chris@14: attributes Chris@14: end Chris@0: end