annotate .svn/pristine/4d/4d303c92e6935bfe0bbc913277addc1ba08aef2e.svn-base @ 929:5f33065ddc4b redmine-1.3

Update to Redmine SVN rev 9414 on 1.3-stable branch
author Chris Cannam
date Wed, 27 Jun 2012 14:54:18 +0100
parents cbb26bc654de
children
rev   line source
Chris@909 1 # Redmine - project management software
Chris@909 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@909 3 #
Chris@909 4 # This program is free software; you can redistribute it and/or
Chris@909 5 # modify it under the terms of the GNU General Public License
Chris@909 6 # as published by the Free Software Foundation; either version 2
Chris@909 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@909 9 # This program is distributed in the hope that it will be useful,
Chris@909 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@909 14 # You should have received a copy of the GNU General Public License
Chris@909 15 # along with this program; if not, write to the Free Software
Chris@909 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@909 17
Chris@909 18 class ProjectsController < ApplicationController
Chris@909 19 menu_item :overview
Chris@909 20 menu_item :roadmap, :only => :roadmap
Chris@909 21 menu_item :settings, :only => :settings
Chris@909 22
Chris@909 23 before_filter :find_project, :except => [ :index, :list, :new, :create, :copy ]
Chris@909 24 before_filter :authorize, :except => [ :index, :list, :new, :create, :copy, :archive, :unarchive, :destroy]
Chris@909 25 before_filter :authorize_global, :only => [:new, :create]
Chris@909 26 before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ]
Chris@909 27 accept_rss_auth :index
Chris@909 28 accept_api_auth :index, :show, :create, :update, :destroy
Chris@909 29
Chris@909 30 after_filter :only => [:create, :edit, :update, :archive, :unarchive, :destroy] do |controller|
Chris@909 31 if controller.request.post?
Chris@909 32 controller.send :expire_action, :controller => 'welcome', :action => 'robots.txt'
Chris@909 33 end
Chris@909 34 end
Chris@909 35
Chris@909 36 helper :sort
Chris@909 37 include SortHelper
Chris@909 38 helper :custom_fields
Chris@909 39 include CustomFieldsHelper
Chris@909 40 helper :issues
Chris@909 41 helper :queries
Chris@909 42 include QueriesHelper
Chris@909 43 helper :repositories
Chris@909 44 include RepositoriesHelper
Chris@909 45 include ProjectsHelper
Chris@909 46
Chris@909 47 # Lists visible projects
Chris@909 48 def index
Chris@909 49 respond_to do |format|
Chris@909 50 format.html {
Chris@909 51 @projects = Project.visible.find(:all, :order => 'lft')
Chris@909 52 }
Chris@909 53 format.api {
Chris@909 54 @offset, @limit = api_offset_and_limit
Chris@909 55 @project_count = Project.visible.count
Chris@909 56 @projects = Project.visible.all(:offset => @offset, :limit => @limit, :order => 'lft')
Chris@909 57 }
Chris@909 58 format.atom {
Chris@909 59 projects = Project.visible.find(:all, :order => 'created_on DESC',
Chris@909 60 :limit => Setting.feeds_limit.to_i)
Chris@909 61 render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
Chris@909 62 }
Chris@909 63 end
Chris@909 64 end
Chris@909 65
Chris@909 66 def new
Chris@909 67 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
Chris@909 68 @trackers = Tracker.all
Chris@909 69 @project = Project.new(params[:project])
Chris@909 70 end
Chris@909 71
Chris@909 72 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
Chris@909 73 def create
Chris@909 74 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
Chris@909 75 @trackers = Tracker.all
Chris@909 76 @project = Project.new
Chris@909 77 @project.safe_attributes = params[:project]
Chris@909 78
Chris@909 79 if validate_parent_id && @project.save
Chris@909 80 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
Chris@909 81 # Add current user as a project member if he is not admin
Chris@909 82 unless User.current.admin?
Chris@909 83 r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
Chris@909 84 m = Member.new(:user => User.current, :roles => [r])
Chris@909 85 @project.members << m
Chris@909 86 end
Chris@909 87 respond_to do |format|
Chris@909 88 format.html {
Chris@909 89 flash[:notice] = l(:notice_successful_create)
Chris@909 90 redirect_to(params[:continue] ?
Chris@909 91 {:controller => 'projects', :action => 'new', :project => {:parent_id => @project.parent_id}.reject {|k,v| v.nil?}} :
Chris@909 92 {:controller => 'projects', :action => 'settings', :id => @project}
Chris@909 93 )
Chris@909 94 }
Chris@909 95 format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
Chris@909 96 end
Chris@909 97 else
Chris@909 98 respond_to do |format|
Chris@909 99 format.html { render :action => 'new' }
Chris@909 100 format.api { render_validation_errors(@project) }
Chris@909 101 end
Chris@909 102 end
Chris@909 103
Chris@909 104 end
Chris@909 105
Chris@909 106 def copy
Chris@909 107 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
Chris@909 108 @trackers = Tracker.all
Chris@909 109 @root_projects = Project.find(:all,
Chris@909 110 :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}",
Chris@909 111 :order => 'name')
Chris@909 112 @source_project = Project.find(params[:id])
Chris@909 113 if request.get?
Chris@909 114 @project = Project.copy_from(@source_project)
Chris@909 115 if @project
Chris@909 116 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
Chris@909 117 else
Chris@909 118 redirect_to :controller => 'admin', :action => 'projects'
Chris@909 119 end
Chris@909 120 else
Chris@909 121 Mailer.with_deliveries(params[:notifications] == '1') do
Chris@909 122 @project = Project.new
Chris@909 123 @project.safe_attributes = params[:project]
Chris@909 124 if validate_parent_id && @project.copy(@source_project, :only => params[:only])
Chris@909 125 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
Chris@909 126 flash[:notice] = l(:notice_successful_create)
Chris@909 127 redirect_to :controller => 'projects', :action => 'settings', :id => @project
Chris@909 128 elsif !@project.new_record?
Chris@909 129 # Project was created
Chris@909 130 # But some objects were not copied due to validation failures
Chris@909 131 # (eg. issues from disabled trackers)
Chris@909 132 # TODO: inform about that
Chris@909 133 redirect_to :controller => 'projects', :action => 'settings', :id => @project
Chris@909 134 end
Chris@909 135 end
Chris@909 136 end
Chris@909 137 rescue ActiveRecord::RecordNotFound
Chris@909 138 redirect_to :controller => 'admin', :action => 'projects'
Chris@909 139 end
Chris@909 140
Chris@909 141 # Show @project
Chris@909 142 def show
Chris@909 143 if params[:jump]
Chris@909 144 # try to redirect to the requested menu item
Chris@909 145 redirect_to_project_menu_item(@project, params[:jump]) && return
Chris@909 146 end
Chris@909 147
Chris@909 148 @users_by_role = @project.users_by_role
Chris@909 149 @subprojects = @project.children.visible.all
Chris@909 150 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
Chris@909 151 @trackers = @project.rolled_up_trackers
Chris@909 152
Chris@909 153 cond = @project.project_condition(Setting.display_subprojects_issues?)
Chris@909 154
Chris@909 155 @open_issues_by_tracker = Issue.visible.count(:group => :tracker,
Chris@909 156 :include => [:project, :status, :tracker],
Chris@909 157 :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
Chris@909 158 @total_issues_by_tracker = Issue.visible.count(:group => :tracker,
Chris@909 159 :include => [:project, :status, :tracker],
Chris@909 160 :conditions => cond)
Chris@909 161
Chris@909 162 if User.current.allowed_to?(:view_time_entries, @project)
Chris@909 163 @total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f
Chris@909 164 end
Chris@909 165
Chris@909 166 @key = User.current.rss_key
Chris@909 167
Chris@909 168 respond_to do |format|
Chris@909 169 format.html
Chris@909 170 format.api
Chris@909 171 end
Chris@909 172 end
Chris@909 173
Chris@909 174 def settings
Chris@909 175 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
Chris@909 176 @issue_category ||= IssueCategory.new
Chris@909 177 @member ||= @project.members.new
Chris@909 178 @trackers = Tracker.all
Chris@909 179 @repository ||= @project.repository
Chris@909 180 @wiki ||= @project.wiki
Chris@909 181 end
Chris@909 182
Chris@909 183 def edit
Chris@909 184 end
Chris@909 185
Chris@909 186 # TODO: convert to PUT only
Chris@909 187 verify :method => [:post, :put], :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
Chris@909 188 def update
Chris@909 189 @project.safe_attributes = params[:project]
Chris@909 190 if validate_parent_id && @project.save
Chris@909 191 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
Chris@909 192 respond_to do |format|
Chris@909 193 format.html {
Chris@909 194 flash[:notice] = l(:notice_successful_update)
Chris@909 195 redirect_to :action => 'settings', :id => @project
Chris@909 196 }
Chris@909 197 format.api { head :ok }
Chris@909 198 end
Chris@909 199 else
Chris@909 200 respond_to do |format|
Chris@909 201 format.html {
Chris@909 202 settings
Chris@909 203 render :action => 'settings'
Chris@909 204 }
Chris@909 205 format.api { render_validation_errors(@project) }
Chris@909 206 end
Chris@909 207 end
Chris@909 208 end
Chris@909 209
Chris@909 210 verify :method => :post, :only => :modules, :render => {:nothing => true, :status => :method_not_allowed }
Chris@909 211 def modules
Chris@909 212 @project.enabled_module_names = params[:enabled_module_names]
Chris@909 213 flash[:notice] = l(:notice_successful_update)
Chris@909 214 redirect_to :action => 'settings', :id => @project, :tab => 'modules'
Chris@909 215 end
Chris@909 216
Chris@909 217 def archive
Chris@909 218 if request.post?
Chris@909 219 unless @project.archive
Chris@909 220 flash[:error] = l(:error_can_not_archive_project)
Chris@909 221 end
Chris@909 222 end
Chris@909 223 redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
Chris@909 224 end
Chris@909 225
Chris@909 226 def unarchive
Chris@909 227 @project.unarchive if request.post? && !@project.active?
Chris@909 228 redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
Chris@909 229 end
Chris@909 230
Chris@909 231 # Delete @project
Chris@909 232 def destroy
Chris@909 233 @project_to_destroy = @project
Chris@909 234 if request.get?
Chris@909 235 # display confirmation view
Chris@909 236 else
Chris@909 237 if api_request? || params[:confirm]
Chris@909 238 @project_to_destroy.destroy
Chris@909 239 respond_to do |format|
Chris@909 240 format.html { redirect_to :controller => 'admin', :action => 'projects' }
Chris@909 241 format.api { head :ok }
Chris@909 242 end
Chris@909 243 end
Chris@909 244 end
Chris@909 245 # hide project in layout
Chris@909 246 @project = nil
Chris@909 247 end
Chris@909 248
Chris@909 249 private
Chris@909 250 def find_optional_project
Chris@909 251 return true unless params[:id]
Chris@909 252 @project = Project.find(params[:id])
Chris@909 253 authorize
Chris@909 254 rescue ActiveRecord::RecordNotFound
Chris@909 255 render_404
Chris@909 256 end
Chris@909 257
Chris@909 258 # Validates parent_id param according to user's permissions
Chris@909 259 # TODO: move it to Project model in a validation that depends on User.current
Chris@909 260 def validate_parent_id
Chris@909 261 return true if User.current.admin?
Chris@909 262 parent_id = params[:project] && params[:project][:parent_id]
Chris@909 263 if parent_id || @project.new_record?
Chris@909 264 parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
Chris@909 265 unless @project.allowed_parents.include?(parent)
Chris@909 266 @project.errors.add :parent_id, :invalid
Chris@909 267 return false
Chris@909 268 end
Chris@909 269 end
Chris@909 270 true
Chris@909 271 end
Chris@909 272 end