comparison app/controllers/projects_controller.rb @ 511:107d36338b70 live

Merge from branch "cannam"
author Chris Cannam
date Thu, 14 Jul 2011 10:43:07 +0100
parents 851510f1b535
children b2a9e64b8283
comparison
equal deleted inserted replaced
451:a9f6345cb43d 511:107d36338b70
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2009 Jean-Philippe Lang 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
22 22
23 before_filter :find_project, :except => [ :index, :list, :new, :create, :copy ] 23 before_filter :find_project, :except => [ :index, :list, :new, :create, :copy ]
24 before_filter :authorize, :except => [ :index, :list, :new, :create, :copy, :archive, :unarchive, :destroy] 24 before_filter :authorize, :except => [ :index, :list, :new, :create, :copy, :archive, :unarchive, :destroy]
25 before_filter :authorize_global, :only => [:new, :create] 25 before_filter :authorize_global, :only => [:new, :create]
26 before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ] 26 before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ]
27 accept_key_auth :index 27 accept_rss_auth :index
28 accept_api_auth :index, :show, :create, :update, :destroy
28 29
29 after_filter :only => [:create, :edit, :update, :archive, :unarchive, :destroy] do |controller| 30 after_filter :only => [:create, :edit, :update, :archive, :unarchive, :destroy] do |controller|
30 if controller.request.post? 31 if controller.request.post?
31 controller.send :expire_action, :controller => 'welcome', :action => 'robots.txt' 32 controller.send :expire_action, :controller => 'welcome', :action => 'robots.txt'
32 end 33 end
33 end 34 end
34
35 # TODO: convert to PUT only
36 verify :method => [:post, :put], :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
37 35
38 helper :sort 36 helper :sort
39 include SortHelper 37 include SortHelper
40 helper :custom_fields 38 helper :custom_fields
41 include CustomFieldsHelper 39 include CustomFieldsHelper
63 # @user_projects = User.current.projects.sort_by(&:name) 61 # @user_projects = User.current.projects.sort_by(&:name)
64 @user_projects = User.current.projects.all(:order => :name) 62 @user_projects = User.current.projects.all(:order => :name)
65 end 63 end
66 render :template => 'projects/index.rhtml', :layout => !request.xhr? 64 render :template => 'projects/index.rhtml', :layout => !request.xhr?
67 } 65 }
68 format.xml { 66 format.api {
69 @projects = Project.visible.find(:all, :order => 'lft') 67 @offset, @limit = api_offset_and_limit
68 @project_count = Project.visible.count
69 @projects = Project.visible.all(:offset => @offset, :limit => @limit, :order => 'lft')
70 } 70 }
71 format.atom { 71 format.atom {
72 projects = Project.visible.find(:all, :order => 'created_on DESC', 72 projects = Project.visible.find(:all, :order => 'created_on DESC',
73 :limit => Setting.feeds_limit.to_i) 73 :limit => Setting.feeds_limit.to_i)
74 render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}") 74 render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
78 78
79 def new 79 def new
80 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") 80 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
81 @trackers = Tracker.all 81 @trackers = Tracker.all
82 @project = Project.new(params[:project]) 82 @project = Project.new(params[:project])
83 83 end
84 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers? 84
85 @project.trackers = Tracker.all 85 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
86 @project.is_public = Setting.default_projects_public?
87 @project.enabled_module_names = Setting.default_projects_modules
88 end
89
90 def create 86 def create
91 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") 87 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
92 @trackers = Tracker.all 88 @trackers = Tracker.all
93 @project = Project.new(params[:project]) 89 @project = Project.new
94 90 @project.safe_attributes = params[:project]
95 @project.enabled_module_names = params[:enabled_modules] 91
96 if validate_parent_id && @project.save 92 if validate_parent_id && @project.save
97 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') 93 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
98 # Add current user as a project member if he is not admin 94 # Add current user as a project member if he is not admin
99 unless User.current.admin? 95 unless User.current.admin?
100 r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first 96 r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
104 respond_to do |format| 100 respond_to do |format|
105 format.html { 101 format.html {
106 flash[:notice] = l(:notice_successful_create) 102 flash[:notice] = l(:notice_successful_create)
107 redirect_to :controller => 'projects', :action => 'settings', :id => @project 103 redirect_to :controller => 'projects', :action => 'settings', :id => @project
108 } 104 }
109 format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) } 105 format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
110 end 106 end
111 else 107 else
112 respond_to do |format| 108 respond_to do |format|
113 format.html { render :action => 'new' } 109 format.html { render :action => 'new' }
114 format.xml { render :xml => @project.errors, :status => :unprocessable_entity } 110 format.api { render_validation_errors(@project) }
115 end 111 end
116 end 112 end
117 113
118 end 114 end
119 115
131 else 127 else
132 redirect_to :controller => 'admin', :action => 'projects' 128 redirect_to :controller => 'admin', :action => 'projects'
133 end 129 end
134 else 130 else
135 Mailer.with_deliveries(params[:notifications] == '1') do 131 Mailer.with_deliveries(params[:notifications] == '1') do
136 @project = Project.new(params[:project]) 132 @project = Project.new
137 @project.enabled_module_names = params[:enabled_modules] 133 @project.safe_attributes = params[:project]
138 if validate_parent_id && @project.copy(@source_project, :only => params[:only]) 134 if validate_parent_id && @project.copy(@source_project, :only => params[:only])
139 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') 135 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
140 flash[:notice] = l(:notice_successful_create) 136 flash[:notice] = l(:notice_successful_create)
141 redirect_to :controller => 'projects', :action => 'settings' 137 redirect_to :controller => 'projects', :action => 'settings', :id => @project
142 elsif !@project.new_record? 138 elsif !@project.new_record?
143 # Project was created 139 # Project was created
144 # But some objects were not copied due to validation failures 140 # But some objects were not copied due to validation failures
145 # (eg. issues from disabled trackers) 141 # (eg. issues from disabled trackers)
146 # TODO: inform about that 142 # TODO: inform about that
147 redirect_to :controller => 'projects', :action => 'settings' 143 redirect_to :controller => 'projects', :action => 'settings', :id => @project
148 end 144 end
149 end 145 end
150 end 146 end
151 rescue ActiveRecord::RecordNotFound 147 rescue ActiveRecord::RecordNotFound
152 redirect_to :controller => 'admin', :action => 'projects' 148 redirect_to :controller => 'admin', :action => 'projects'
158 # try to redirect to the requested menu item 154 # try to redirect to the requested menu item
159 redirect_to_project_menu_item(@project, params[:jump]) && return 155 redirect_to_project_menu_item(@project, params[:jump]) && return
160 end 156 end
161 157
162 @users_by_role = @project.users_by_role 158 @users_by_role = @project.users_by_role
163 @subprojects = @project.children.visible 159 @subprojects = @project.children.visible.all
164 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC") 160 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
165 @trackers = @project.rolled_up_trackers 161 @trackers = @project.rolled_up_trackers
166 162
167 cond = @project.project_condition(Setting.display_subprojects_issues?) 163 cond = @project.project_condition(Setting.display_subprojects_issues?)
168 164
171 :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false]) 167 :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
172 @total_issues_by_tracker = Issue.visible.count(:group => :tracker, 168 @total_issues_by_tracker = Issue.visible.count(:group => :tracker,
173 :include => [:project, :status, :tracker], 169 :include => [:project, :status, :tracker],
174 :conditions => cond) 170 :conditions => cond)
175 171
176 TimeEntry.visible_by(User.current) do 172 if User.current.allowed_to?(:view_time_entries, @project)
177 @total_hours = TimeEntry.sum(:hours, 173 @total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f
178 :include => :project, 174 end
179 :conditions => cond).to_f 175
180 end
181 @key = User.current.rss_key 176 @key = User.current.rss_key
182 177
183 respond_to do |format| 178 respond_to do |format|
184 format.html 179 format.html
185 format.xml 180 format.api
186 end 181 end
187 end 182 end
188 183
189 def settings 184 def settings
190 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") 185 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
196 end 191 end
197 192
198 def edit 193 def edit
199 end 194 end
200 195
196 # TODO: convert to PUT only
197 verify :method => [:post, :put], :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
201 def update 198 def update
202 @project.attributes = params[:project] 199 @project.safe_attributes = params[:project]
203 if validate_parent_id && @project.save 200 if validate_parent_id && @project.save
204 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') 201 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
205 respond_to do |format| 202 respond_to do |format|
206 format.html { 203 format.html {
207 flash[:notice] = l(:notice_successful_update) 204 flash[:notice] = l(:notice_successful_update)
208 redirect_to :action => 'settings', :id => @project 205 redirect_to :action => 'settings', :id => @project
209 } 206 }
210 format.xml { head :ok } 207 format.api { head :ok }
211 end 208 end
212 else 209 else
213 respond_to do |format| 210 respond_to do |format|
214 format.html { 211 format.html {
215 settings 212 settings
216 render :action => 'settings' 213 render :action => 'settings'
217 } 214 }
218 format.xml { render :xml => @project.errors, :status => :unprocessable_entity } 215 format.api { render_validation_errors(@project) }
219 end 216 end
220 end 217 end
221 end 218 end
222 219
220 verify :method => :post, :only => :modules, :render => {:nothing => true, :status => :method_not_allowed }
221
223 def overview 222 def overview
224 @project.has_welcome_page = params[:has_welcome_page] 223 @project.has_welcome_page = params[:has_welcome_page]
225 if @project.save 224 if @project.save
226 flash[:notice] = l(:notice_successful_update) 225 flash[:notice] = l(:notice_successful_update)
227 end 226 end
228 redirect_to :action => 'settings', :id => @project, :tab => 'overview' 227 redirect_to :action => 'settings', :id => @project, :tab => 'overview'
229 end 228 end
230 229
231 def modules 230 def modules
232 @project.enabled_module_names = params[:enabled_modules] 231 @project.enabled_module_names = params[:enabled_module_names]
233 flash[:notice] = l(:notice_successful_update) 232 flash[:notice] = l(:notice_successful_update)
234 redirect_to :action => 'settings', :id => @project, :tab => 'modules' 233 redirect_to :action => 'settings', :id => @project, :tab => 'modules'
235 end 234 end
236 235
237 def archive 236 def archive
252 def destroy 251 def destroy
253 @project_to_destroy = @project 252 @project_to_destroy = @project
254 if request.get? 253 if request.get?
255 # display confirmation view 254 # display confirmation view
256 else 255 else
257 if params[:format] == 'xml' || params[:confirm] 256 if api_request? || params[:confirm]
258 @project_to_destroy.destroy 257 @project_to_destroy.destroy
259 respond_to do |format| 258 respond_to do |format|
260 format.html { redirect_to :controller => 'admin', :action => 'projects' } 259 format.html { redirect_to :controller => 'admin', :action => 'projects' }
261 format.xml { head :ok } 260 format.api { head :ok }
262 end 261 end
263 end 262 end
264 end 263 end
265 # hide project in layout 264 # hide project in layout
266 @project = nil 265 @project = nil