Chris@1295: # Redmine - project management software Chris@1295: # Copyright (C) 2006-2013 Jean-Philippe Lang Chris@1295: # Chris@1295: # This program is free software; you can redistribute it and/or Chris@1295: # modify it under the terms of the GNU General Public License Chris@1295: # as published by the Free Software Foundation; either version 2 Chris@1295: # of the License, or (at your option) any later version. Chris@1295: # Chris@1295: # This program is distributed in the hope that it will be useful, Chris@1295: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1295: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1295: # GNU General Public License for more details. Chris@1295: # Chris@1295: # You should have received a copy of the GNU General Public License Chris@1295: # along with this program; if not, write to the Free Software Chris@1295: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1295: Chris@1295: class VersionsController < ApplicationController Chris@1295: menu_item :roadmap Chris@1295: model_object Version Chris@1295: before_filter :find_model_object, :except => [:index, :new, :create, :close_completed] Chris@1295: before_filter :find_project_from_association, :except => [:index, :new, :create, :close_completed] Chris@1295: before_filter :find_project_by_project_id, :only => [:index, :new, :create, :close_completed] Chris@1295: before_filter :authorize Chris@1295: Chris@1295: accept_api_auth :index, :show, :create, :update, :destroy Chris@1295: Chris@1295: helper :custom_fields Chris@1295: helper :projects Chris@1295: Chris@1295: def index Chris@1295: respond_to do |format| Chris@1295: format.html { Chris@1295: @trackers = @project.trackers.sorted.all Chris@1295: retrieve_selected_tracker_ids(@trackers, @trackers.select {|t| t.is_in_roadmap?}) Chris@1295: @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1') Chris@1295: project_ids = @with_subprojects ? @project.self_and_descendants.collect(&:id) : [@project.id] Chris@1295: Chris@1295: @versions = @project.shared_versions || [] Chris@1295: @versions += @project.rolled_up_versions.visible if @with_subprojects Chris@1295: @versions = @versions.uniq.sort Chris@1295: unless params[:completed] Chris@1295: @completed_versions = @versions.select {|version| version.closed? || version.completed? } Chris@1295: @versions -= @completed_versions Chris@1295: end Chris@1295: Chris@1295: @issues_by_version = {} Chris@1295: if @selected_tracker_ids.any? && @versions.any? Chris@1295: issues = Issue.visible.all( Chris@1295: :include => [:project, :status, :tracker, :priority, :fixed_version], Chris@1295: :conditions => {:tracker_id => @selected_tracker_ids, :project_id => project_ids, :fixed_version_id => @versions.map(&:id)}, Chris@1295: :order => "#{Project.table_name}.lft, #{Tracker.table_name}.position, #{Issue.table_name}.id" Chris@1295: ) Chris@1295: @issues_by_version = issues.group_by(&:fixed_version) Chris@1295: end Chris@1295: @versions.reject! {|version| !project_ids.include?(version.project_id) && @issues_by_version[version].blank?} Chris@1295: } Chris@1295: format.api { Chris@1295: @versions = @project.shared_versions.all Chris@1295: } Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def show Chris@1295: respond_to do |format| Chris@1295: format.html { Chris@1295: @issues = @version.fixed_issues.visible. Chris@1295: includes(:status, :tracker, :priority). Chris@1295: reorder("#{Tracker.table_name}.position, #{Issue.table_name}.id"). Chris@1295: all Chris@1295: } Chris@1295: format.api Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def new Chris@1295: @version = @project.versions.build Chris@1295: @version.safe_attributes = params[:version] Chris@1295: Chris@1295: respond_to do |format| Chris@1295: format.html Chris@1295: format.js Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def create Chris@1295: @version = @project.versions.build Chris@1295: if params[:version] Chris@1295: attributes = params[:version].dup Chris@1295: attributes.delete('sharing') unless attributes.nil? || @version.allowed_sharings.include?(attributes['sharing']) Chris@1295: @version.safe_attributes = attributes Chris@1295: end Chris@1295: Chris@1295: if request.post? Chris@1295: if @version.save Chris@1295: respond_to do |format| Chris@1295: format.html do Chris@1295: flash[:notice] = l(:notice_successful_create) Chris@1295: redirect_back_or_default settings_project_path(@project, :tab => 'versions') Chris@1295: end Chris@1295: format.js Chris@1295: format.api do Chris@1295: render :action => 'show', :status => :created, :location => version_url(@version) Chris@1295: end Chris@1295: end Chris@1295: else Chris@1295: respond_to do |format| Chris@1295: format.html { render :action => 'new' } Chris@1295: format.js { render :action => 'new' } Chris@1295: format.api { render_validation_errors(@version) } Chris@1295: end Chris@1295: end Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def edit Chris@1295: end Chris@1295: Chris@1295: def update Chris@1295: if request.put? && params[:version] Chris@1295: attributes = params[:version].dup Chris@1295: attributes.delete('sharing') unless @version.allowed_sharings.include?(attributes['sharing']) Chris@1295: @version.safe_attributes = attributes Chris@1295: if @version.save Chris@1295: respond_to do |format| Chris@1295: format.html { Chris@1295: flash[:notice] = l(:notice_successful_update) Chris@1295: redirect_back_or_default settings_project_path(@project, :tab => 'versions') Chris@1295: } Chris@1295: format.api { render_api_ok } Chris@1295: end Chris@1295: else Chris@1295: respond_to do |format| Chris@1295: format.html { render :action => 'edit' } Chris@1295: format.api { render_validation_errors(@version) } Chris@1295: end Chris@1295: end Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def close_completed Chris@1295: if request.put? Chris@1295: @project.close_completed_versions Chris@1295: end Chris@1295: redirect_to settings_project_path(@project, :tab => 'versions') Chris@1295: end Chris@1295: Chris@1295: def destroy Chris@1295: if @version.fixed_issues.empty? Chris@1295: @version.destroy Chris@1295: respond_to do |format| Chris@1295: format.html { redirect_back_or_default settings_project_path(@project, :tab => 'versions') } Chris@1295: format.api { render_api_ok } Chris@1295: end Chris@1295: else Chris@1295: respond_to do |format| Chris@1295: format.html { Chris@1295: flash[:error] = l(:notice_unable_delete_version) Chris@1295: redirect_to settings_project_path(@project, :tab => 'versions') Chris@1295: } Chris@1295: format.api { head :unprocessable_entity } Chris@1295: end Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def status_by Chris@1295: respond_to do |format| Chris@1295: format.html { render :action => 'show' } Chris@1295: format.js Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: private Chris@1295: Chris@1295: def retrieve_selected_tracker_ids(selectable_trackers, default_trackers=nil) Chris@1295: if ids = params[:tracker_ids] Chris@1295: @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@1295: else Chris@1295: @selected_tracker_ids = (default_trackers || selectable_trackers).collect {|t| t.id.to_s } Chris@1295: end Chris@1295: end Chris@1295: end