annotate app/controllers/projects_controller.rb @ 861:b8105f717bf7 bug_182

Close obsolete branch bug_182
author Chris Cannam
date Fri, 10 Jun 2011 16:49:58 +0100
parents a9921f3e9088
children 350acce374a2
rev   line source
Chris@0 1 # Redmine - project management software
Chris@0 2 # Copyright (C) 2006-2009 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@0 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@0 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@0 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@22 27 accept_key_auth :index
chris@22 28
chris@22 29 after_filter :only => [:create, :edit, :update, :archive, :unarchive, :destroy] do |controller|
Chris@0 30 if controller.request.post?
Chris@0 31 controller.send :expire_action, :controller => 'welcome', :action => 'robots.txt'
Chris@0 32 end
Chris@0 33 end
chris@22 34
chris@22 35 # TODO: convert to PUT only
chris@22 36 verify :method => [:post, :put], :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
chris@22 37
Chris@0 38 helper :sort
Chris@0 39 include SortHelper
Chris@0 40 helper :custom_fields
Chris@0 41 include CustomFieldsHelper
Chris@0 42 helper :issues
Chris@0 43 helper :queries
Chris@0 44 include QueriesHelper
Chris@0 45 helper :repositories
Chris@0 46 include RepositoriesHelper
Chris@0 47 include ProjectsHelper
chris@131 48
chris@205 49 # Lists visible projects. Paginator is for top-level projects only
chris@205 50 # (subprojects belong to them)
Chris@0 51 def index
Chris@0 52 respond_to do |format|
Chris@0 53 format.html {
chris@205 54 sort_init 'name'
chris@205 55 sort_update %w(name lft created_on updated_on)
chris@131 56 @limit = per_page_option
chris@205 57 @project_count = Project.visible_roots.count
chris@131 58 @project_pages = Paginator.new self, @project_count, @limit, params['page']
chris@131 59 @offset ||= @project_pages.current.offset
chris@205 60 @projects = Project.visible_roots.all(:offset => @offset, :limit => @limit, :order => sort_clause)
chris@131 61 if User.current.logged?
chris@418 62 # seems sort_by gives us case-sensitive ordering, which we don't want
chris@418 63 # @user_projects = User.current.projects.sort_by(&:name)
chris@418 64 @user_projects = User.current.projects.all(:order => :name)
chris@131 65 end
chris@131 66 render :template => 'projects/index.rhtml', :layout => !request.xhr?
Chris@0 67 }
Chris@0 68 format.xml {
Chris@0 69 @projects = Project.visible.find(:all, :order => 'lft')
Chris@0 70 }
Chris@0 71 format.atom {
Chris@0 72 projects = Project.visible.find(:all, :order => 'created_on DESC',
Chris@0 73 :limit => Setting.feeds_limit.to_i)
Chris@0 74 render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
Chris@0 75 }
Chris@0 76 end
Chris@0 77 end
Chris@0 78
chris@22 79 def new
Chris@0 80 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
Chris@0 81 @trackers = Tracker.all
Chris@0 82 @project = Project.new(params[:project])
chris@22 83
chris@22 84 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
chris@22 85 @project.trackers = Tracker.all
chris@22 86 @project.is_public = Setting.default_projects_public?
chris@22 87 @project.enabled_module_names = Setting.default_projects_modules
chris@22 88 end
chris@22 89
chris@22 90 def create
chris@22 91 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
chris@22 92 @trackers = Tracker.all
chris@22 93 @project = Project.new(params[:project])
chris@22 94
chris@22 95 @project.enabled_module_names = params[:enabled_modules]
chris@22 96 if validate_parent_id && @project.save
chris@22 97 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
chris@22 98 # Add current user as a project member if he is not admin
chris@22 99 unless User.current.admin?
chris@22 100 r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
chris@22 101 m = Member.new(:user => User.current, :roles => [r])
chris@22 102 @project.members << m
chris@22 103 end
chris@22 104 respond_to do |format|
chris@22 105 format.html {
chris@22 106 flash[:notice] = l(:notice_successful_create)
chris@22 107 redirect_to :controller => 'projects', :action => 'settings', :id => @project
chris@22 108 }
chris@37 109 format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
chris@22 110 end
Chris@0 111 else
chris@22 112 respond_to do |format|
chris@22 113 format.html { render :action => 'new' }
chris@22 114 format.xml { render :xml => @project.errors, :status => :unprocessable_entity }
Chris@0 115 end
chris@22 116 end
chris@22 117
Chris@0 118 end
Chris@0 119
Chris@0 120 def copy
Chris@0 121 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
Chris@0 122 @trackers = Tracker.all
Chris@0 123 @root_projects = Project.find(:all,
Chris@0 124 :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}",
Chris@0 125 :order => 'name')
Chris@0 126 @source_project = Project.find(params[:id])
Chris@0 127 if request.get?
Chris@0 128 @project = Project.copy_from(@source_project)
Chris@0 129 if @project
Chris@0 130 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
Chris@0 131 else
Chris@0 132 redirect_to :controller => 'admin', :action => 'projects'
Chris@0 133 end
Chris@0 134 else
Chris@0 135 Mailer.with_deliveries(params[:notifications] == '1') do
Chris@0 136 @project = Project.new(params[:project])
Chris@0 137 @project.enabled_module_names = params[:enabled_modules]
Chris@0 138 if validate_parent_id && @project.copy(@source_project, :only => params[:only])
Chris@0 139 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
Chris@0 140 flash[:notice] = l(:notice_successful_create)
chris@37 141 redirect_to :controller => 'projects', :action => 'settings'
Chris@0 142 elsif !@project.new_record?
Chris@0 143 # Project was created
Chris@0 144 # But some objects were not copied due to validation failures
Chris@0 145 # (eg. issues from disabled trackers)
Chris@0 146 # TODO: inform about that
chris@37 147 redirect_to :controller => 'projects', :action => 'settings'
Chris@0 148 end
Chris@0 149 end
Chris@0 150 end
Chris@0 151 rescue ActiveRecord::RecordNotFound
Chris@0 152 redirect_to :controller => 'admin', :action => 'projects'
Chris@0 153 end
Chris@0 154
Chris@0 155 # Show @project
Chris@0 156 def show
Chris@0 157 if params[:jump]
Chris@0 158 # try to redirect to the requested menu item
Chris@0 159 redirect_to_project_menu_item(@project, params[:jump]) && return
Chris@0 160 end
Chris@0 161
Chris@0 162 @users_by_role = @project.users_by_role
Chris@0 163 @subprojects = @project.children.visible
Chris@0 164 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
Chris@0 165 @trackers = @project.rolled_up_trackers
Chris@0 166
Chris@0 167 cond = @project.project_condition(Setting.display_subprojects_issues?)
Chris@0 168
Chris@0 169 @open_issues_by_tracker = Issue.visible.count(:group => :tracker,
Chris@0 170 :include => [:project, :status, :tracker],
Chris@0 171 :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
Chris@0 172 @total_issues_by_tracker = Issue.visible.count(:group => :tracker,
Chris@0 173 :include => [:project, :status, :tracker],
Chris@0 174 :conditions => cond)
Chris@0 175
Chris@0 176 TimeEntry.visible_by(User.current) do
Chris@0 177 @total_hours = TimeEntry.sum(:hours,
Chris@0 178 :include => :project,
Chris@0 179 :conditions => cond).to_f
Chris@0 180 end
Chris@0 181 @key = User.current.rss_key
Chris@0 182
Chris@0 183 respond_to do |format|
Chris@0 184 format.html
Chris@0 185 format.xml
Chris@0 186 end
Chris@0 187 end
Chris@0 188
Chris@0 189 def settings
Chris@0 190 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
Chris@0 191 @issue_category ||= IssueCategory.new
Chris@0 192 @member ||= @project.members.new
Chris@0 193 @trackers = Tracker.all
Chris@0 194 @repository ||= @project.repository
Chris@0 195 @wiki ||= @project.wiki
Chris@0 196 end
Chris@0 197
Chris@0 198 def edit
chris@22 199 end
chris@22 200
chris@22 201 def update
chris@22 202 @project.attributes = params[:project]
chris@22 203 if validate_parent_id && @project.save
chris@22 204 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
chris@22 205 respond_to do |format|
chris@22 206 format.html {
chris@22 207 flash[:notice] = l(:notice_successful_update)
chris@22 208 redirect_to :action => 'settings', :id => @project
chris@22 209 }
chris@22 210 format.xml { head :ok }
chris@22 211 end
Chris@0 212 else
chris@22 213 respond_to do |format|
chris@22 214 format.html {
chris@22 215 settings
chris@22 216 render :action => 'settings'
chris@22 217 }
chris@22 218 format.xml { render :xml => @project.errors, :status => :unprocessable_entity }
Chris@0 219 end
Chris@0 220 end
Chris@0 221 end
chris@351 222
chris@351 223 def overview
chris@351 224 @project.has_welcome_page = params[:has_welcome_page]
chris@351 225 if @project.save
chris@351 226 flash[:notice] = l(:notice_successful_update)
chris@351 227 end
chris@351 228 redirect_to :action => 'settings', :id => @project, :tab => 'overview'
chris@351 229 end
chris@351 230
Chris@0 231 def modules
Chris@0 232 @project.enabled_module_names = params[:enabled_modules]
Chris@0 233 flash[:notice] = l(:notice_successful_update)
Chris@0 234 redirect_to :action => 'settings', :id => @project, :tab => 'modules'
Chris@0 235 end
Chris@0 236
Chris@0 237 def archive
Chris@0 238 if request.post?
Chris@0 239 unless @project.archive
Chris@0 240 flash[:error] = l(:error_can_not_archive_project)
Chris@0 241 end
Chris@0 242 end
Chris@0 243 redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
Chris@0 244 end
Chris@0 245
Chris@0 246 def unarchive
Chris@0 247 @project.unarchive if request.post? && !@project.active?
Chris@0 248 redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
Chris@0 249 end
Chris@0 250
Chris@0 251 # Delete @project
Chris@0 252 def destroy
Chris@0 253 @project_to_destroy = @project
Chris@0 254 if request.get?
Chris@0 255 # display confirmation view
Chris@0 256 else
Chris@0 257 if params[:format] == 'xml' || params[:confirm]
Chris@0 258 @project_to_destroy.destroy
Chris@0 259 respond_to do |format|
Chris@0 260 format.html { redirect_to :controller => 'admin', :action => 'projects' }
Chris@0 261 format.xml { head :ok }
Chris@0 262 end
Chris@0 263 end
Chris@0 264 end
Chris@0 265 # hide project in layout
Chris@0 266 @project = nil
Chris@0 267 end
Chris@0 268
Chris@0 269 private
Chris@0 270 def find_optional_project
Chris@0 271 return true unless params[:id]
Chris@0 272 @project = Project.find(params[:id])
Chris@0 273 authorize
Chris@0 274 rescue ActiveRecord::RecordNotFound
Chris@0 275 render_404
Chris@0 276 end
Chris@0 277
Chris@0 278 # Validates parent_id param according to user's permissions
Chris@0 279 # TODO: move it to Project model in a validation that depends on User.current
Chris@0 280 def validate_parent_id
Chris@0 281 return true if User.current.admin?
Chris@0 282 parent_id = params[:project] && params[:project][:parent_id]
Chris@0 283 if parent_id || @project.new_record?
Chris@0 284 parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
Chris@0 285 unless @project.allowed_parents.include?(parent)
Chris@0 286 @project.errors.add :parent_id, :invalid
Chris@0 287 return false
Chris@0 288 end
Chris@0 289 end
Chris@0 290 true
Chris@0 291 end
Chris@0 292 end