socialforge/app/controllers/versions_controller.rb

330 lines
12 KiB
Ruby
Raw Normal View History

# Redmine - project management software
# Copyright (C) 2006-2013 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
class VersionsController < ApplicationController
layout "base_projects"
menu_item :roadmap
model_object Version
before_filter :find_model_object, :except => [:index, :new, :create, :close_completed]
#before_filter :find_model_object_contest, :except => [:index, :new, :create]
before_filter :find_project_from_association, :except => [:index, :new, :create, :close_completed]
before_filter :find_project_by_project_id, :only => [:index, :new, :create, :close_completed]
before_filter :authorize
accept_api_auth :index, :show, :create, :update, :destroy
helper :custom_fields
helper :projects
helper :project_score
def index
2016-10-10 10:41:41 +08:00
# 顶部导航
@project_menu_type = 7
2016-10-20 18:08:24 +08:00
type = params[:type]
case type
when nil,"1"
@versions = @project.versions
when "2"
@versions = @project.versions.where(:status => 'open')
when "3"
@versions = @project.versions.where(:status => 'locked')
when "4"
@versions = @project.versions.where(:status => 'closed')
end
@versions_count = Version.where(:project_id => @project.id).count
@versions_open_count = Version.where(:project_id => @project.id, :status => "open").count
@versions_locked_count = Version.where(:project_id => @project.id, :status => "locked").count
@versions_closed_count = Version.where(:project_id => @project.id, :status => "closed").count
@versions_count = version_type_count(type, @versions_count, @versions_open_count, @versions_locked_count, @versions_closed_count)
@limit = 10
@is_remote = true
@version_pages = Paginator.new @versions_count, @limit, params['page'] || 1
@offset ||= @version_pages.offset
@versions = paginateHelper @versions, @limit
respond_to do |format|
2016-10-20 18:08:24 +08:00
format.html
format.js
format.api
# format.html {
# @trackers = @project.trackers.sorted.all
# retrieve_selected_tracker_ids(@trackers, @trackers.select {|t| t.is_in_roadmap?})
# @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
# project_ids = @with_subprojects ? @project.self_and_descendants.collect(&:id) : [@project.id]
#
# @versions = @project.shared_versions || []
# @versions += @project.rolled_up_versions.visible if @with_subprojects
# #added by young
# @versions = @versions.uniq.reverse#Modified by young
# unless params[:completed]
# @completed_versions = @versions.select {|version| version.closed? || version.completed? }
# @versions -= @completed_versions
# end
# @offset, @limit = api_offset_and_limit({:limit => 4})
# @versions_count = @versions.count
# @versions_pages = Paginator.new @versions_count, @limit, params['page']
# @offset ||= @versions_pages.offset
# @versions = @versions.slice(@offset, @limit)
# #end by young
#
# @issues_by_version = {}
# if @selected_tracker_ids.any? && @versions.any?
# issues = Issue.visible.all(
# :include => [:project, :status, :tracker, :priority, :fixed_version],
# :conditions => {:tracker_id => @selected_tracker_ids, :project_id => project_ids, :fixed_version_id => @versions.map(&:id)},
# :order => "#{Project.table_name}.lft, #{Tracker.table_name}.position, #{Issue.table_name}.id"
# )
# @issues_by_version = issues.group_by(&:fixed_version)
# end
# @versions.reject! {|version| !project_ids.include?(version.project_id) && @issues_by_version[version].blank?}
# }
# format.api {
# @versions = @project.shared_versions.all
# }
end
end
2016-10-20 18:08:24 +08:00
# 统计各种类型数量
def version_type_count type, all_count, opened_count, locked_count, closed_count
case type
when nil, "1"
all_count
when "2"
opened_count
when "3"
locked_count
when "4"
closed_count
end
end
def show
2016-10-26 18:39:14 +08:00
# 顶部导航
@project_menu_type = 7
respond_to do |format|
@version_issues = @version.fixed_issues.visible.
2016-10-21 14:22:14 +08:00
includes(:status, :tracker, :priority).
reorder("#{Tracker.table_name}.position, #{Issue.table_name}.id").
all
@issue_count = @version_issues.count
2016-10-21 14:22:14 +08:00
@limit = 20
@issue_pages = Paginator.new @issue_count, @limit, params['page'] || 1
# @offset ||= @issue_pages.offset
@issues = paginateHelper @version_issues, @limit
@version_issue_assigned_name = @version_issues.group_by(&:assigned_to_id)
format.html {
2016-10-21 14:22:14 +08:00
# @issues = @version.fixed_issues.visible.
# includes(:status, :tracker, :priority).
# reorder("#{Tracker.table_name}.position, #{Issue.table_name}.id").
# all
}
format.api
2016-10-24 15:42:22 +08:00
format.xls {
@issues = @version.fixed_issues.visible.includes(:status, :tracker, :priority).reorder("#{Tracker.table_name}.position, #{Issue.table_name}.id").all
filename = "#{@version.name.to_s}_#{l(:label_issue_list_xls)}.xls"
send_data(issue_list_xls(@issues), :type => 'application/octet-stream', :filename => filename_for_content_disposition(filename))
}
end
end
def new
# @version = @project.versions.build
# @version.safe_attributes = params[:version]
#
2016-10-26 18:04:12 +08:00
respond_to do |format|
2016-10-27 16:33:00 +08:00
@is_setting = params[:is_setting]
2016-10-26 18:04:12 +08:00
@is_create = params[:is_create]
2016-10-27 16:33:00 +08:00
@is_issue = params[:is_issue]
@issue_project_id = params[:issue_project_id]
2016-10-27 11:15:28 +08:00
#@@issue = Issue.find(params[:issue].to_i)
2016-10-27 16:33:00 +08:00
format.js
2016-10-26 18:04:12 +08:00
end
end
def create
2016-10-27 11:15:28 +08:00
# 项目配置中新建
@is_setting = params[:is_setting]
@is_issue = params[:is_issue]
@is_create = params[:is_create]
2016-10-27 16:33:00 +08:00
@issue_project_id = params[:issue_project_id]
# @issue = current_issue
2016-10-27 11:15:28 +08:00
@version = @project.versions.build
2016-10-27 11:15:28 +08:00
if params[:version]
attributes = params[:version].dup
attributes.delete('sharing') unless attributes.nil? || @version.allowed_sharings.include?(attributes['sharing'])
@version.safe_attributes = attributes
end
if request.post?
if @version.save
respond_to do |format|
2016-10-27 16:33:00 +08:00
format.html{
if @is_create
redirect_to project_versions_path(@project)
elsif @is_issue
redirect_to new_project_issue_path(@project)
end
}
format.js
format.api do
render :action => 'show', :status => :created, :location => version_url(@version)
end
end
else
respond_to do |format|
2015-04-17 14:39:30 +08:00
format.html { flash[:error] = @version.errors.full_messages.flatten.to_s
2015-04-17 14:21:18 +08:00
redirect_to settings_project_url(@project, :tab => 'versions') }
format.js { render :action => 'new' }
format.api { render_validation_errors(@version) }
end
end
end
end
def edit
2016-10-26 15:18:07 +08:00
@@is_index = params[:is_index]
end
def update
if request.put? && params[:version]
# 处理里程碑里面的更新
if params[:flag].to_i == 1
@version.update_attribute(:status, params[:status])
if @version.save
respond_to do |format|
format.html {
flash[:notice] = l(:notice_successful_update)
redirect_to settings_project_path(@project, :tab => 'versions')
}
format.js
format.api { render_api_ok }
end
else
respond_to do |format|
format.html { render :action => 'edit' }
format.api { render_validation_errors(@version) }
end
end
else
attributes = params[:version].dup
attributes.delete('sharing') unless @version.allowed_sharings.include?(attributes['sharing'])
@version.safe_attributes = attributes
@is_setting = params[:is_setting]
if @version.save
# 为了再setting里面局部刷新
if @is_setting
@versions = @version.project.shared_versions.sort
end
respond_to do |format|
format.html {
flash[:notice] = l(:notice_successful_update)
redirect_to settings_project_path(@project, :tab => 'versions')
}
2016-10-26 15:18:07 +08:00
format.js{
if @@is_index
redirect_to project_versions_path(@project)
end
}
format.api { render_api_ok }
end
else
respond_to do |format|
format.html { render :action => 'edit' }
format.js
format.api { render_validation_errors(@version) }
end
end
end
end
end
def close_completed
if request.put?
@project.close_completed_versions
end
redirect_to settings_project_url(@project, :tab => 'versions')
end
def close_completed_contest
if request.put?
@contest.close_completed_versions
end
redirect_to settings_contest_url(@contest, :tab => 'versions')
end
def destroy
2016-10-26 18:39:14 +08:00
begin
project = @version.project
issues = Issue.where(:fixed_version_id => @version.id)
@version.destroy
2016-10-26 18:39:14 +08:00
issues.update_all(:fixed_version_id, nil)
rescue Exception => e
puts e
end
respond_to do |format|
format.js
format.html { redirect_to project_versions_path(project)}
format.api { render_api_ok }
end
end
def status_by
respond_to do |format|
format.html { render :action => 'show' }
format.js
end
end
private
2016-10-24 15:42:22 +08:00
def issue_list_xls issues
xls_report = StringIO.new
book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet :name => "issues"
blue = Spreadsheet::Format.new :color => :blue, :weight => :bold, :size => 10
sheet1.row(0).default_format = blue
sheet1.row(0).concat([l(:issue_xls_id),l(:issue_xls_tracker_id),l(:issue_xls_title),l(:issue_xls_description),l(:issue_xls_status),l(:issue_xls_assign),l(:issue_xls_priority),l(:issue_xls_author),l(:issue_xls_created_at),l(:milestone),l(:issue_xls_start),l(:issue_xls_due),l(:issue_xls_ratio)])
count_row = 1
issues.each do |issue|
sheet1[count_row,0] = issue.id
sheet1[count_row,1] = issue_tracker_change(issue.tracker_id)
sheet1[count_row,2] = issue.subject
sheet1[count_row,3] = (issue.description.gsub(/<\/?.*?>/,"")).html_safe
sheet1[count_row,4] = issue_status_change(issue.status_id)
sheet1[count_row,5] = issue.assigned_to.try(:show_name)
sheet1[count_row,6] = issue_priority_change(issue.priority_id)
sheet1[count_row,7] = issue.author.show_name
sheet1[count_row,8] = issue.created_on.nil? ? issue.created_on : issue.created_on.strftime('%Y-%m-%d %H:%M:%S')
sheet1[count_row,9] = issue.fixed_version.try(:name)
sheet1[count_row,10] = issue.start_date.nil? ? issue.start_date : issue.start_date.strftime('%Y-%m-%d')
sheet1[count_row,11] = issue.due_date.nil? ? issue.due_date : issue.due_date.strftime('%Y-%m-%d')
sheet1[count_row,12] = issue_ratio_change(issue.done_ratio, issue.status_id)
count_row += 1
end
book.write xls_report
xls_report.string
end
def retrieve_selected_tracker_ids(selectable_trackers, default_trackers=nil)
if ids = params[:tracker_ids]
@selected_tracker_ids = (ids.is_a? Array) ? ids.collect { |id| id.to_i.to_s } : ids.split('/').collect { |id| id.to_i.to_s }
else
@selected_tracker_ids = (selectable_trackers).collect {|t| t.id.to_s }
end
end
end