annotate app/controllers/projects_controller.rb @ 1621:3a510bf6a9bc

Merge from live branch
author Chris Cannam
date Fri, 13 Jul 2018 10:44:33 +0100
parents a1bdbf8a87d5
children
rev   line source
Chris@0 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 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@1464 46 helper :members
chris@1011 47 include ActivitiesHelper
chris@1011 48 helper :activities
Chris@909 49
chris@205 50 # Lists visible projects. Paginator is for top-level projects only
chris@205 51 # (subprojects belong to them)
Chris@0 52 def index
Chris@0 53 respond_to do |format|
Chris@909 54 format.html {
chris@205 55 sort_init 'name'
chris@205 56 sort_update %w(name lft created_on updated_on)
chris@131 57 @limit = per_page_option
chris@205 58 @project_count = Project.visible_roots.count
chris@131 59 @project_pages = Paginator.new self, @project_count, @limit, params['page']
chris@131 60 @offset ||= @project_pages.current.offset
luis@1128 61 @projects = Project.visible_roots.all(:offset => @offset, :limit => @limit, :order => sort_clause)
chris@1510 62 render :template => 'projects/index', :layout => !request.xhr?
Chris@1116 63
Chris@1116 64 ## Redmine 2.2:
Chris@1116 65 # scope = Project
Chris@1116 66 # unless params[:closed]
Chris@1116 67 # scope = scope.active
Chris@1116 68 # end
Chris@1116 69 # @projects = scope.visible.order('lft').all
Chris@0 70 }
Chris@117 71 format.api {
Chris@117 72 @offset, @limit = api_offset_and_limit
Chris@117 73 @project_count = Project.visible.count
Chris@1464 74 @projects = Project.visible.offset(@offset).limit(@limit).order('lft').all
Chris@0 75 }
Chris@0 76 format.atom {
Chris@1464 77 projects = Project.visible.order('created_on DESC').limit(Setting.feeds_limit.to_i).all
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@1510 88 render :template => 'projects/explore', :layout => !request.xhr?
chris@1007 89 }
chris@1007 90 end
chris@1007 91 end
chris@1007 92
chris@22 93 def new
Chris@1464 94 @issue_custom_fields = IssueCustomField.sorted.all
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@1464 101 @issue_custom_fields = IssueCustomField.sorted.all
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@1464 108 # Add current user as a project member if current user 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@1464 117 if params[:continue]
Chris@1464 118 attrs = {:parent_id => @project.parent_id}.reject {|k,v| v.nil?}
Chris@1464 119 redirect_to new_project_path(attrs)
Chris@1464 120 else
Chris@1464 121 redirect_to settings_project_path(@project)
Chris@1464 122 end
chris@22 123 }
Chris@117 124 format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
chris@22 125 end
Chris@0 126 else
chris@22 127 respond_to do |format|
chris@22 128 format.html { render :action => 'new' }
Chris@117 129 format.api { render_validation_errors(@project) }
Chris@0 130 end
chris@22 131 end
Chris@0 132 end
Chris@909 133
Chris@0 134 def copy
Chris@1464 135 @issue_custom_fields = IssueCustomField.sorted.all
Chris@1115 136 @trackers = Tracker.sorted.all
Chris@0 137 @source_project = Project.find(params[:id])
Chris@0 138 if request.get?
Chris@0 139 @project = Project.copy_from(@source_project)
Chris@1115 140 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
Chris@0 141 else
Chris@0 142 Mailer.with_deliveries(params[:notifications] == '1') do
Chris@117 143 @project = Project.new
Chris@117 144 @project.safe_attributes = params[:project]
Chris@0 145 if validate_parent_id && @project.copy(@source_project, :only => params[:only])
Chris@0 146 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
Chris@0 147 flash[:notice] = l(:notice_successful_create)
Chris@1464 148 redirect_to settings_project_path(@project)
Chris@0 149 elsif !@project.new_record?
Chris@0 150 # Project was created
Chris@0 151 # But some objects were not copied due to validation failures
Chris@0 152 # (eg. issues from disabled trackers)
Chris@0 153 # TODO: inform about that
Chris@1464 154 redirect_to settings_project_path(@project)
Chris@0 155 end
Chris@0 156 end
Chris@0 157 end
Chris@0 158 rescue ActiveRecord::RecordNotFound
Chris@1115 159 # source_project not found
Chris@1115 160 render_404
Chris@0 161 end
luis@1128 162
Chris@0 163 # Show @project
Chris@0 164 def show
Chris@1464 165 # try to redirect to the requested menu item
Chris@1464 166 if params[:jump] && redirect_to_project_menu_item(@project, params[:jump])
Chris@1464 167 return
Chris@0 168 end
Chris@909 169
Chris@0 170 @users_by_role = @project.users_by_role
Chris@441 171 @subprojects = @project.children.visible.all
Chris@1464 172 @news = @project.news.limit(5).includes(:author, :project).reorder("#{News.table_name}.created_on DESC").all
Chris@0 173 @trackers = @project.rolled_up_trackers
Chris@909 174
Chris@0 175 cond = @project.project_condition(Setting.display_subprojects_issues?)
Chris@909 176
Chris@1517 177 @open_issues_by_tracker = Issue.visible.open.where(cond).group(:tracker).count
Chris@1517 178 @total_issues_by_tracker = Issue.visible.where(cond).group(:tracker).count
Chris@909 179
Chris@441 180 if User.current.allowed_to?(:view_time_entries, @project)
Chris@1464 181 @total_hours = TimeEntry.visible.where(cond).sum(:hours).to_f
Chris@0 182 end
Chris@909 183
Chris@0 184 @key = User.current.rss_key
Chris@909 185
Chris@0 186 respond_to do |format|
Chris@0 187 format.html
Chris@117 188 format.api
Chris@0 189 end
Chris@0 190 end
Chris@0 191
Chris@0 192 def settings
Chris@1464 193 @issue_custom_fields = IssueCustomField.sorted.all
Chris@0 194 @issue_category ||= IssueCategory.new
Chris@0 195 @member ||= @project.members.new
Chris@1115 196 @trackers = Tracker.sorted.all
chris@1354 197 @repository ||= @project.repository
Chris@0 198 @wiki ||= @project.wiki
Chris@0 199 end
Chris@909 200
Chris@0 201 def edit
chris@22 202 end
chris@22 203
chris@22 204 def update
Chris@117 205 @project.safe_attributes = params[:project]
chris@22 206 if validate_parent_id && @project.save
chris@22 207 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
chris@22 208 respond_to do |format|
Chris@909 209 format.html {
chris@22 210 flash[:notice] = l(:notice_successful_update)
Chris@1464 211 redirect_to settings_project_path(@project)
chris@22 212 }
Chris@1115 213 format.api { render_api_ok }
chris@22 214 end
Chris@0 215 else
chris@22 216 respond_to do |format|
Chris@909 217 format.html {
chris@22 218 settings
chris@22 219 render :action => 'settings'
chris@22 220 }
Chris@117 221 format.api { render_validation_errors(@project) }
Chris@0 222 end
Chris@0 223 end
Chris@0 224 end
Chris@117 225
chris@351 226 def overview
chris@351 227 @project.has_welcome_page = params[:has_welcome_page]
chris@351 228 if @project.save
chris@351 229 flash[:notice] = l(:notice_successful_update)
chris@351 230 end
chris@351 231 redirect_to :action => 'settings', :id => @project, :tab => 'overview'
chris@351 232 end
chris@351 233
Chris@0 234 def modules
Chris@117 235 @project.enabled_module_names = params[:enabled_module_names]
Chris@0 236 flash[:notice] = l(:notice_successful_update)
Chris@1464 237 redirect_to settings_project_path(@project, :tab => 'modules')
Chris@0 238 end
Chris@0 239
Chris@0 240 def archive
Chris@0 241 if request.post?
Chris@0 242 unless @project.archive
Chris@0 243 flash[:error] = l(:error_can_not_archive_project)
Chris@0 244 end
Chris@0 245 end
Chris@1464 246 redirect_to admin_projects_path(:status => params[:status])
Chris@0 247 end
Chris@909 248
Chris@0 249 def unarchive
Chris@0 250 @project.unarchive if request.post? && !@project.active?
Chris@1464 251 redirect_to admin_projects_path(:status => params[:status])
Chris@0 252 end
Chris@909 253
Chris@1115 254 def close
Chris@1115 255 @project.close
Chris@1115 256 redirect_to project_path(@project)
Chris@1115 257 end
Chris@1115 258
Chris@1115 259 def reopen
Chris@1115 260 @project.reopen
Chris@1115 261 redirect_to project_path(@project)
Chris@1115 262 end
Chris@1115 263
Chris@0 264 # Delete @project
Chris@0 265 def destroy
Chris@0 266 @project_to_destroy = @project
Chris@1115 267 if api_request? || params[:confirm]
Chris@1115 268 @project_to_destroy.destroy
Chris@1115 269 respond_to do |format|
Chris@1464 270 format.html { redirect_to admin_projects_path }
Chris@1115 271 format.api { render_api_ok }
Chris@0 272 end
Chris@0 273 end
Chris@0 274 # hide project in layout
Chris@0 275 @project = nil
Chris@0 276 end
Chris@0 277
Chris@1115 278 private
Chris@0 279
chris@1034 280 def validate_is_public_key
chris@1034 281 # Although is_public isn't mandatory in the project model (it gets
chris@1034 282 # defaulted), it must be present in params -- it can be true or
chris@1034 283 # false, but it must be there. This permits us to make forms in
chris@1034 284 # which the user _has_ to select public or private (rather than
chris@1034 285 # defaulting it) if we want to
chris@1034 286 if params.nil? || params[:project].nil? || !params[:project].has_key?(:is_public)
chris@1034 287 @project.errors.add :is_public, :public_or_private
chris@1034 288 return false
chris@1034 289 end
chris@1034 290 true
chris@1034 291 end
chris@1034 292
Chris@0 293 # Validates parent_id param according to user's permissions
Chris@0 294 # TODO: move it to Project model in a validation that depends on User.current
Chris@0 295 def validate_parent_id
Chris@0 296 return true if User.current.admin?
Chris@0 297 parent_id = params[:project] && params[:project][:parent_id]
Chris@0 298 if parent_id || @project.new_record?
Chris@0 299 parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
Chris@0 300 unless @project.allowed_parents.include?(parent)
Chris@0 301 @project.errors.add :parent_id, :invalid
Chris@0 302 return false
Chris@0 303 end
Chris@0 304 end
Chris@0 305 true
Chris@0 306 end
Chris@0 307 end