annotate app/controllers/projects_controller.rb @ 1459:cf78a7ade302 luisf

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