annotate app/controllers/projects_controller.rb @ 1471:d65e60e20a50 bug_531

Close obsolete branch bug_531
author Chris Cannam
date Fri, 23 Nov 2012 18:10:33 +0000
parents ea5d9652c6f6
children bb32da3bea34 91db8e091f10
rev   line source
Chris@0 1 # Redmine - project management software
Chris@441 2 # Copyright (C) 2006-2011 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@1009 23 before_filter :find_project, :except => [ :index, :list, :explore, :new, :create, :copy ]
chris@1009 24 before_filter :authorize, :except => [ :index, :list, :explore, :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@0 32 controller.send :expire_action, :controller => 'welcome', :action => 'robots.txt'
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@1011 46 include ActivitiesHelper
chris@1011 47 helper :activities
Chris@909 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@909 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
luis@767 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@919 66 render :template => 'projects/index.html.erb', :layout => !request.xhr?
Chris@0 67 }
Chris@117 68 format.api {
Chris@117 69 @offset, @limit = api_offset_and_limit
Chris@117 70 @project_count = Project.visible.count
Chris@117 71 @projects = Project.visible.all(:offset => @offset, :limit => @limit, :order => 'lft')
Chris@0 72 }
Chris@0 73 format.atom {
Chris@0 74 projects = Project.visible.find(:all, :order => 'created_on DESC',
Chris@0 75 :limit => Setting.feeds_limit.to_i)
Chris@0 76 render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
Chris@0 77 }
Chris@0 78 end
Chris@0 79 end
Chris@909 80
chris@1009 81 # A different view of projects using explore boxes
chris@1009 82 def explore
chris@1007 83 respond_to do |format|
chris@1007 84 format.html {
chris@1007 85 @projects = Project.visible
chris@1009 86 render :template => 'projects/explore.html.erb', :layout => !request.xhr?
chris@1007 87 }
chris@1007 88 end
chris@1007 89 end
chris@1007 90
chris@22 91 def new
Chris@0 92 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
Chris@0 93 @trackers = Tracker.all
Chris@929 94 @project = Project.new
Chris@929 95 @project.safe_attributes = params[:project]
chris@22 96 end
chris@22 97
Chris@117 98 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
chris@22 99 def create
chris@22 100 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
chris@22 101 @trackers = Tracker.all
Chris@117 102 @project = Project.new
Chris@117 103 @project.safe_attributes = params[:project]
chris@22 104
luis@831 105
luis@831 106 # todo: luisf: this should be removed from here...
luis@831 107 if params && params[:project] && !params[:project][:tag_list].nil?
luis@831 108 new_tags = params[:project][:tag_list].to_s.downcase
luis@831 109
luis@831 110 @project.tag_list = ActionController::Base.helpers.strip_tags(new_tags)
luis@831 111 end
luis@831 112 # end of code to be removed
luis@831 113
chris@1034 114 if validate_is_public_key && validate_parent_id && @project.save
chris@22 115 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
chris@22 116 # Add current user as a project member if he is not admin
chris@22 117 unless User.current.admin?
chris@22 118 r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
chris@22 119 m = Member.new(:user => User.current, :roles => [r])
chris@22 120 @project.members << m
chris@22 121 end
chris@22 122 respond_to do |format|
Chris@909 123 format.html {
chris@22 124 flash[:notice] = l(:notice_successful_create)
Chris@909 125 redirect_to(params[:continue] ?
Chris@909 126 {:controller => 'projects', :action => 'new', :project => {:parent_id => @project.parent_id}.reject {|k,v| v.nil?}} :
Chris@909 127 {:controller => 'projects', :action => 'settings', :id => @project}
Chris@909 128 )
chris@22 129 }
Chris@117 130 format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
chris@22 131 end
Chris@0 132 else
chris@22 133 respond_to do |format|
chris@22 134 format.html { render :action => 'new' }
Chris@117 135 format.api { render_validation_errors(@project) }
Chris@0 136 end
chris@22 137 end
Chris@909 138
Chris@0 139 end
Chris@909 140
Chris@0 141 def copy
Chris@0 142 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
Chris@0 143 @trackers = Tracker.all
Chris@0 144 @root_projects = Project.find(:all,
Chris@0 145 :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}",
Chris@0 146 :order => 'name')
Chris@0 147 @source_project = Project.find(params[:id])
Chris@0 148 if request.get?
Chris@0 149 @project = Project.copy_from(@source_project)
Chris@0 150 if @project
Chris@0 151 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
Chris@0 152 else
Chris@0 153 redirect_to :controller => 'admin', :action => 'projects'
Chris@909 154 end
Chris@0 155 else
Chris@0 156 Mailer.with_deliveries(params[:notifications] == '1') do
Chris@117 157 @project = Project.new
Chris@117 158 @project.safe_attributes = params[:project]
Chris@0 159 if validate_parent_id && @project.copy(@source_project, :only => params[:only])
Chris@0 160 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
Chris@0 161 flash[:notice] = l(:notice_successful_create)
Chris@117 162 redirect_to :controller => 'projects', :action => 'settings', :id => @project
Chris@0 163 elsif !@project.new_record?
Chris@0 164 # Project was created
Chris@0 165 # But some objects were not copied due to validation failures
Chris@0 166 # (eg. issues from disabled trackers)
Chris@0 167 # TODO: inform about that
Chris@117 168 redirect_to :controller => 'projects', :action => 'settings', :id => @project
Chris@0 169 end
Chris@0 170 end
Chris@0 171 end
Chris@0 172 rescue ActiveRecord::RecordNotFound
Chris@0 173 redirect_to :controller => 'admin', :action => 'projects'
Chris@0 174 end
Chris@0 175
Chris@0 176 # Show @project
Chris@0 177 def show
Chris@0 178 if params[:jump]
Chris@0 179 # try to redirect to the requested menu item
Chris@0 180 redirect_to_project_menu_item(@project, params[:jump]) && return
Chris@0 181 end
Chris@909 182
Chris@0 183 @users_by_role = @project.users_by_role
Chris@441 184 @subprojects = @project.children.visible.all
Chris@0 185 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
Chris@0 186 @trackers = @project.rolled_up_trackers
Chris@909 187
Chris@0 188 cond = @project.project_condition(Setting.display_subprojects_issues?)
Chris@909 189
Chris@0 190 @open_issues_by_tracker = Issue.visible.count(:group => :tracker,
Chris@0 191 :include => [:project, :status, :tracker],
Chris@0 192 :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
Chris@0 193 @total_issues_by_tracker = Issue.visible.count(:group => :tracker,
Chris@0 194 :include => [:project, :status, :tracker],
Chris@0 195 :conditions => cond)
Chris@909 196
Chris@441 197 if User.current.allowed_to?(:view_time_entries, @project)
Chris@441 198 @total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f
Chris@0 199 end
Chris@909 200
Chris@0 201 @key = User.current.rss_key
Chris@909 202
Chris@0 203 respond_to do |format|
Chris@0 204 format.html
Chris@117 205 format.api
Chris@0 206 end
Chris@0 207 end
Chris@0 208
Chris@0 209 def settings
Chris@0 210 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
Chris@0 211 @issue_category ||= IssueCategory.new
Chris@0 212 @member ||= @project.members.new
Chris@0 213 @trackers = Tracker.all
Chris@0 214 @repository ||= @project.repository
Chris@0 215 @wiki ||= @project.wiki
Chris@0 216 end
Chris@909 217
Chris@0 218 def edit
chris@22 219 end
chris@22 220
Chris@117 221 # TODO: convert to PUT only
Chris@117 222 verify :method => [:post, :put], :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
chris@22 223 def update
Chris@117 224 @project.safe_attributes = params[:project]
chris@22 225 if validate_parent_id && @project.save
chris@22 226 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
chris@22 227 respond_to do |format|
Chris@909 228 format.html {
chris@22 229 flash[:notice] = l(:notice_successful_update)
chris@22 230 redirect_to :action => 'settings', :id => @project
chris@22 231 }
Chris@117 232 format.api { head :ok }
chris@22 233 end
Chris@0 234 else
chris@22 235 respond_to do |format|
Chris@909 236 format.html {
chris@22 237 settings
chris@22 238 render :action => 'settings'
chris@22 239 }
Chris@117 240 format.api { render_validation_errors(@project) }
Chris@0 241 end
Chris@0 242 end
Chris@0 243 end
Chris@117 244
Chris@117 245 verify :method => :post, :only => :modules, :render => {:nothing => true, :status => :method_not_allowed }
Chris@443 246
chris@351 247 def overview
chris@351 248 @project.has_welcome_page = params[:has_welcome_page]
chris@351 249 if @project.save
chris@351 250 flash[:notice] = l(:notice_successful_update)
chris@351 251 end
chris@351 252 redirect_to :action => 'settings', :id => @project, :tab => 'overview'
chris@351 253 end
chris@351 254
Chris@0 255 def modules
Chris@117 256 @project.enabled_module_names = params[:enabled_module_names]
Chris@0 257 flash[:notice] = l(:notice_successful_update)
Chris@0 258 redirect_to :action => 'settings', :id => @project, :tab => 'modules'
Chris@0 259 end
Chris@0 260
Chris@0 261 def archive
Chris@0 262 if request.post?
Chris@0 263 unless @project.archive
Chris@0 264 flash[:error] = l(:error_can_not_archive_project)
Chris@0 265 end
Chris@0 266 end
Chris@0 267 redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
Chris@0 268 end
Chris@909 269
Chris@0 270 def unarchive
Chris@0 271 @project.unarchive if request.post? && !@project.active?
Chris@0 272 redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
Chris@0 273 end
Chris@909 274
Chris@0 275 # Delete @project
Chris@0 276 def destroy
Chris@0 277 @project_to_destroy = @project
Chris@0 278 if request.get?
Chris@0 279 # display confirmation view
Chris@0 280 else
Chris@117 281 if api_request? || params[:confirm]
Chris@0 282 @project_to_destroy.destroy
Chris@0 283 respond_to do |format|
Chris@0 284 format.html { redirect_to :controller => 'admin', :action => 'projects' }
Chris@117 285 format.api { head :ok }
Chris@0 286 end
Chris@0 287 end
Chris@0 288 end
Chris@0 289 # hide project in layout
Chris@0 290 @project = nil
Chris@0 291 end
Chris@0 292
Chris@0 293 private
Chris@0 294 def find_optional_project
Chris@0 295 return true unless params[:id]
Chris@0 296 @project = Project.find(params[:id])
Chris@0 297 authorize
Chris@0 298 rescue ActiveRecord::RecordNotFound
Chris@0 299 render_404
Chris@0 300 end
Chris@0 301
chris@1034 302 def validate_is_public_key
chris@1034 303 # Although is_public isn't mandatory in the project model (it gets
chris@1034 304 # defaulted), it must be present in params -- it can be true or
chris@1034 305 # false, but it must be there. This permits us to make forms in
chris@1034 306 # which the user _has_ to select public or private (rather than
chris@1034 307 # defaulting it) if we want to
chris@1034 308 if params.nil? || params[:project].nil? || !params[:project].has_key?(:is_public)
chris@1034 309 @project.errors.add :is_public, :public_or_private
chris@1034 310 return false
chris@1034 311 end
chris@1034 312 true
chris@1034 313 end
chris@1034 314
Chris@0 315 # Validates parent_id param according to user's permissions
Chris@0 316 # TODO: move it to Project model in a validation that depends on User.current
Chris@0 317 def validate_parent_id
Chris@0 318 return true if User.current.admin?
Chris@0 319 parent_id = params[:project] && params[:project][:parent_id]
Chris@0 320 if parent_id || @project.new_record?
Chris@0 321 parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
Chris@0 322 unless @project.allowed_parents.include?(parent)
Chris@0 323 @project.errors.add :parent_id, :invalid
Chris@0 324 return false
Chris@0 325 end
Chris@0 326 end
Chris@0 327 true
Chris@0 328 end
Chris@0 329 end