annotate .svn/pristine/40/40a074fa4b99359e32098a2704385ed2f567928a.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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