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 VersionsController < ApplicationController Chris@909: menu_item :roadmap Chris@909: model_object Version Chris@909: before_filter :find_model_object, :except => [:index, :new, :create, :close_completed] Chris@909: before_filter :find_project_from_association, :except => [:index, :new, :create, :close_completed] Chris@909: before_filter :find_project, :only => [:index, :new, :create, :close_completed] Chris@909: before_filter :authorize Chris@909: Chris@909: accept_api_auth :index, :create, :update, :destroy Chris@909: Chris@909: helper :custom_fields Chris@909: helper :projects Chris@909: Chris@909: def index Chris@909: respond_to do |format| Chris@909: format.html { Chris@909: @trackers = @project.trackers.find(:all, :order => 'position') Chris@909: retrieve_selected_tracker_ids(@trackers, @trackers.select {|t| t.is_in_roadmap?}) Chris@909: @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1') Chris@909: project_ids = @with_subprojects ? @project.self_and_descendants.collect(&:id) : [@project.id] Chris@909: Chris@909: @versions = @project.shared_versions || [] Chris@909: @versions += @project.rolled_up_versions.visible if @with_subprojects Chris@909: @versions = @versions.uniq.sort Chris@909: @versions.reject! {|version| version.closed? || version.completed? } unless params[:completed] Chris@909: Chris@909: @issues_by_version = {} Chris@909: unless @selected_tracker_ids.empty? Chris@909: @versions.each do |version| Chris@909: issues = version.fixed_issues.visible.find(:all, Chris@909: :include => [:project, :status, :tracker, :priority], Chris@909: :conditions => {:tracker_id => @selected_tracker_ids, :project_id => project_ids}, Chris@909: :order => "#{Project.table_name}.lft, #{Tracker.table_name}.position, #{Issue.table_name}.id") Chris@909: @issues_by_version[version] = issues Chris@909: end Chris@909: end Chris@909: @versions.reject! {|version| !project_ids.include?(version.project_id) && @issues_by_version[version].blank?} Chris@909: } Chris@909: format.api { Chris@909: @versions = @project.shared_versions.all Chris@909: } Chris@909: end Chris@909: end Chris@909: Chris@909: def show Chris@909: respond_to do |format| Chris@909: format.html { Chris@909: @issues = @version.fixed_issues.visible.find(:all, Chris@909: :include => [:status, :tracker, :priority], Chris@909: :order => "#{Tracker.table_name}.position, #{Issue.table_name}.id") Chris@909: } Chris@909: format.api Chris@909: end Chris@909: end Chris@909: Chris@909: def new Chris@909: @version = @project.versions.build Chris@909: if params[:version] Chris@909: attributes = params[:version].dup Chris@909: attributes.delete('sharing') unless attributes.nil? || @version.allowed_sharings.include?(attributes['sharing']) Chris@909: @version.attributes = attributes Chris@909: end Chris@909: end Chris@909: Chris@909: def create Chris@909: # TODO: refactor with code above in #new Chris@909: @version = @project.versions.build Chris@909: if params[:version] Chris@909: attributes = params[:version].dup Chris@909: attributes.delete('sharing') unless attributes.nil? || @version.allowed_sharings.include?(attributes['sharing']) Chris@909: @version.attributes = attributes Chris@909: end Chris@909: Chris@909: if request.post? Chris@909: if @version.save Chris@909: respond_to do |format| Chris@909: format.html do Chris@909: flash[:notice] = l(:notice_successful_create) Chris@909: redirect_back_or_default :controller => 'projects', :action => 'settings', :tab => 'versions', :id => @project Chris@909: end Chris@909: format.js do Chris@909: # IE doesn't support the replace_html rjs method for select box options Chris@909: render(:update) {|page| page.replace "issue_fixed_version_id", Chris@909: content_tag('select', '' + version_options_for_select(@project.shared_versions.open, @version), :id => 'issue_fixed_version_id', :name => 'issue[fixed_version_id]') Chris@909: } Chris@909: end Chris@909: format.api do Chris@909: render :action => 'show', :status => :created, :location => version_url(@version) Chris@909: end Chris@909: end Chris@909: else Chris@909: respond_to do |format| Chris@909: format.html { render :action => 'new' } Chris@909: format.js do Chris@909: render(:update) {|page| page.alert(@version.errors.full_messages.join('\n')) } Chris@909: end Chris@909: format.api { render_validation_errors(@version) } Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: def edit Chris@909: end Chris@909: Chris@909: def update Chris@909: if request.put? && params[:version] Chris@909: attributes = params[:version].dup Chris@909: attributes.delete('sharing') unless @version.allowed_sharings.include?(attributes['sharing']) Chris@909: if @version.update_attributes(attributes) Chris@909: respond_to do |format| Chris@909: format.html { Chris@909: flash[:notice] = l(:notice_successful_update) Chris@909: redirect_back_or_default :controller => 'projects', :action => 'settings', :tab => 'versions', :id => @project Chris@909: } Chris@909: format.api { head :ok } Chris@909: end Chris@909: else Chris@909: respond_to do |format| Chris@909: format.html { render :action => 'edit' } Chris@909: format.api { render_validation_errors(@version) } Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: def close_completed Chris@909: if request.put? Chris@909: @project.close_completed_versions Chris@909: end Chris@909: redirect_to :controller => 'projects', :action => 'settings', :tab => 'versions', :id => @project Chris@909: end Chris@909: Chris@909: verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed } Chris@909: def destroy Chris@909: if @version.fixed_issues.empty? Chris@909: @version.destroy Chris@909: respond_to do |format| Chris@909: format.html { redirect_back_or_default :controller => 'projects', :action => 'settings', :tab => 'versions', :id => @project } Chris@909: format.api { head :ok } Chris@909: end Chris@909: else Chris@909: respond_to do |format| Chris@909: format.html { Chris@909: flash[:error] = l(:notice_unable_delete_version) Chris@909: redirect_to :controller => 'projects', :action => 'settings', :tab => 'versions', :id => @project Chris@909: } Chris@909: format.api { head :unprocessable_entity } Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: def status_by Chris@909: respond_to do |format| Chris@909: format.html { render :action => 'show' } Chris@909: format.js { render(:update) {|page| page.replace_html 'status_by', render_issue_status_by(@version, params[:status_by])} } Chris@909: end Chris@909: end Chris@909: Chris@909: private Chris@909: def find_project Chris@909: @project = Project.find(params[:project_id]) Chris@909: rescue ActiveRecord::RecordNotFound Chris@909: render_404 Chris@909: end Chris@909: Chris@909: def retrieve_selected_tracker_ids(selectable_trackers, default_trackers=nil) Chris@909: if ids = params[:tracker_ids] Chris@909: @selected_tracker_ids = (ids.is_a? Array) ? ids.collect { |id| id.to_i.to_s } : ids.split('/').collect { |id| id.to_i.to_s } Chris@909: else Chris@909: @selected_tracker_ids = (default_trackers || selectable_trackers).collect {|t| t.id.to_s } Chris@909: end Chris@909: end Chris@909: Chris@909: end