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