annotate app/controllers/projects_controller.rb @ 1296:038ba2d95de8 redmine-2.2

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