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