##// END OF EJS Templates
Code cleanup....
Jean-Philippe Lang -
r9722:a8e5ba00077b
parent child
Show More
@@ -1,271 +1,267
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
2 # Copyright (C) 2006-2012 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'
32 controller.send :expire_action, :controller => 'welcome', :action => 'robots'
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 scope = Project
51 scope = Project
52 unless params[:closed]
52 unless params[:closed]
53 scope = scope.active
53 scope = scope.active
54 end
54 end
55 @projects = scope.visible.order('lft').all
55 @projects = scope.visible.order('lft').all
56 }
56 }
57 format.api {
57 format.api {
58 @offset, @limit = api_offset_and_limit
58 @offset, @limit = api_offset_and_limit
59 @project_count = Project.visible.count
59 @project_count = Project.visible.count
60 @projects = Project.visible.all(:offset => @offset, :limit => @limit, :order => 'lft')
60 @projects = Project.visible.all(:offset => @offset, :limit => @limit, :order => 'lft')
61 }
61 }
62 format.atom {
62 format.atom {
63 projects = Project.visible.find(:all, :order => 'created_on DESC',
63 projects = Project.visible.find(:all, :order => 'created_on DESC',
64 :limit => Setting.feeds_limit.to_i)
64 :limit => Setting.feeds_limit.to_i)
65 render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
65 render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
66 }
66 }
67 end
67 end
68 end
68 end
69
69
70 def new
70 def new
71 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
71 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
72 @trackers = Tracker.sorted.all
72 @trackers = Tracker.sorted.all
73 @project = Project.new
73 @project = Project.new
74 @project.safe_attributes = params[:project]
74 @project.safe_attributes = params[:project]
75 end
75 end
76
76
77 def create
77 def create
78 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
78 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
79 @trackers = Tracker.sorted.all
79 @trackers = Tracker.sorted.all
80 @project = Project.new
80 @project = Project.new
81 @project.safe_attributes = params[:project]
81 @project.safe_attributes = params[:project]
82
82
83 if validate_parent_id && @project.save
83 if validate_parent_id && @project.save
84 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
84 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
85 # Add current user as a project member if he is not admin
85 # Add current user as a project member if he is not admin
86 unless User.current.admin?
86 unless User.current.admin?
87 r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
87 r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
88 m = Member.new(:user => User.current, :roles => [r])
88 m = Member.new(:user => User.current, :roles => [r])
89 @project.members << m
89 @project.members << m
90 end
90 end
91 respond_to do |format|
91 respond_to do |format|
92 format.html {
92 format.html {
93 flash[:notice] = l(:notice_successful_create)
93 flash[:notice] = l(:notice_successful_create)
94 redirect_to(params[:continue] ?
94 redirect_to(params[:continue] ?
95 {:controller => 'projects', :action => 'new', :project => {:parent_id => @project.parent_id}.reject {|k,v| v.nil?}} :
95 {:controller => 'projects', :action => 'new', :project => {:parent_id => @project.parent_id}.reject {|k,v| v.nil?}} :
96 {:controller => 'projects', :action => 'settings', :id => @project}
96 {:controller => 'projects', :action => 'settings', :id => @project}
97 )
97 )
98 }
98 }
99 format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
99 format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
100 end
100 end
101 else
101 else
102 respond_to do |format|
102 respond_to do |format|
103 format.html { render :action => 'new' }
103 format.html { render :action => 'new' }
104 format.api { render_validation_errors(@project) }
104 format.api { render_validation_errors(@project) }
105 end
105 end
106 end
106 end
107
107
108 end
108 end
109
109
110 def copy
110 def copy
111 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
111 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
112 @trackers = Tracker.sorted.all
112 @trackers = Tracker.sorted.all
113 @root_projects = Project.find(:all,
113 @root_projects = Project.find(:all,
114 :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}",
114 :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}",
115 :order => 'name')
115 :order => 'name')
116 @source_project = Project.find(params[:id])
116 @source_project = Project.find(params[:id])
117 if request.get?
117 if request.get?
118 @project = Project.copy_from(@source_project)
118 @project = Project.copy_from(@source_project)
119 if @project
119 if @project
120 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
120 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
121 else
121 else
122 redirect_to :controller => 'admin', :action => 'projects'
122 redirect_to :controller => 'admin', :action => 'projects'
123 end
123 end
124 else
124 else
125 Mailer.with_deliveries(params[:notifications] == '1') do
125 Mailer.with_deliveries(params[:notifications] == '1') do
126 @project = Project.new
126 @project = Project.new
127 @project.safe_attributes = params[:project]
127 @project.safe_attributes = params[:project]
128 if validate_parent_id && @project.copy(@source_project, :only => params[:only])
128 if validate_parent_id && @project.copy(@source_project, :only => params[:only])
129 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
129 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
130 flash[:notice] = l(:notice_successful_create)
130 flash[:notice] = l(:notice_successful_create)
131 redirect_to :controller => 'projects', :action => 'settings', :id => @project
131 redirect_to :controller => 'projects', :action => 'settings', :id => @project
132 elsif !@project.new_record?
132 elsif !@project.new_record?
133 # Project was created
133 # Project was created
134 # But some objects were not copied due to validation failures
134 # But some objects were not copied due to validation failures
135 # (eg. issues from disabled trackers)
135 # (eg. issues from disabled trackers)
136 # TODO: inform about that
136 # TODO: inform about that
137 redirect_to :controller => 'projects', :action => 'settings', :id => @project
137 redirect_to :controller => 'projects', :action => 'settings', :id => @project
138 end
138 end
139 end
139 end
140 end
140 end
141 rescue ActiveRecord::RecordNotFound
141 rescue ActiveRecord::RecordNotFound
142 redirect_to :controller => 'admin', :action => 'projects'
142 redirect_to :controller => 'admin', :action => 'projects'
143 end
143 end
144
144
145 # Show @project
145 # Show @project
146 def show
146 def show
147 if params[:jump]
147 if params[:jump]
148 # try to redirect to the requested menu item
148 # try to redirect to the requested menu item
149 redirect_to_project_menu_item(@project, params[:jump]) && return
149 redirect_to_project_menu_item(@project, params[:jump]) && return
150 end
150 end
151
151
152 @users_by_role = @project.users_by_role
152 @users_by_role = @project.users_by_role
153 @subprojects = @project.children.visible.all
153 @subprojects = @project.children.visible.all
154 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
154 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
155 @trackers = @project.rolled_up_trackers
155 @trackers = @project.rolled_up_trackers
156
156
157 cond = @project.project_condition(Setting.display_subprojects_issues?)
157 cond = @project.project_condition(Setting.display_subprojects_issues?)
158
158
159 @open_issues_by_tracker = Issue.visible.count(:group => :tracker,
159 @open_issues_by_tracker = Issue.visible.open.where(cond).count(:group => :tracker)
160 :include => [:project, :status, :tracker],
160 @total_issues_by_tracker = Issue.visible.where(cond).count(:group => :tracker)
161 :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
162 @total_issues_by_tracker = Issue.visible.count(:group => :tracker,
163 :include => [:project, :status, :tracker],
164 :conditions => cond)
165
161
166 if User.current.allowed_to?(:view_time_entries, @project)
162 if User.current.allowed_to?(:view_time_entries, @project)
167 @total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f
163 @total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f
168 end
164 end
169
165
170 @key = User.current.rss_key
166 @key = User.current.rss_key
171
167
172 respond_to do |format|
168 respond_to do |format|
173 format.html
169 format.html
174 format.api
170 format.api
175 end
171 end
176 end
172 end
177
173
178 def settings
174 def settings
179 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
175 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
180 @issue_category ||= IssueCategory.new
176 @issue_category ||= IssueCategory.new
181 @member ||= @project.members.new
177 @member ||= @project.members.new
182 @trackers = Tracker.sorted.all
178 @trackers = Tracker.sorted.all
183 @wiki ||= @project.wiki
179 @wiki ||= @project.wiki
184 end
180 end
185
181
186 def edit
182 def edit
187 end
183 end
188
184
189 def update
185 def update
190 @project.safe_attributes = params[:project]
186 @project.safe_attributes = params[:project]
191 if validate_parent_id && @project.save
187 if validate_parent_id && @project.save
192 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
188 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
193 respond_to do |format|
189 respond_to do |format|
194 format.html {
190 format.html {
195 flash[:notice] = l(:notice_successful_update)
191 flash[:notice] = l(:notice_successful_update)
196 redirect_to :action => 'settings', :id => @project
192 redirect_to :action => 'settings', :id => @project
197 }
193 }
198 format.api { head :ok }
194 format.api { head :ok }
199 end
195 end
200 else
196 else
201 respond_to do |format|
197 respond_to do |format|
202 format.html {
198 format.html {
203 settings
199 settings
204 render :action => 'settings'
200 render :action => 'settings'
205 }
201 }
206 format.api { render_validation_errors(@project) }
202 format.api { render_validation_errors(@project) }
207 end
203 end
208 end
204 end
209 end
205 end
210
206
211 def modules
207 def modules
212 @project.enabled_module_names = params[:enabled_module_names]
208 @project.enabled_module_names = params[:enabled_module_names]
213 flash[:notice] = l(:notice_successful_update)
209 flash[:notice] = l(:notice_successful_update)
214 redirect_to :action => 'settings', :id => @project, :tab => 'modules'
210 redirect_to :action => 'settings', :id => @project, :tab => 'modules'
215 end
211 end
216
212
217 def archive
213 def archive
218 if request.post?
214 if request.post?
219 unless @project.archive
215 unless @project.archive
220 flash[:error] = l(:error_can_not_archive_project)
216 flash[:error] = l(:error_can_not_archive_project)
221 end
217 end
222 end
218 end
223 redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
219 redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
224 end
220 end
225
221
226 def unarchive
222 def unarchive
227 @project.unarchive if request.post? && !@project.active?
223 @project.unarchive if request.post? && !@project.active?
228 redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
224 redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
229 end
225 end
230
226
231 def close
227 def close
232 @project.close
228 @project.close
233 redirect_to project_path(@project)
229 redirect_to project_path(@project)
234 end
230 end
235
231
236 def reopen
232 def reopen
237 @project.reopen
233 @project.reopen
238 redirect_to project_path(@project)
234 redirect_to project_path(@project)
239 end
235 end
240
236
241 # Delete @project
237 # Delete @project
242 def destroy
238 def destroy
243 @project_to_destroy = @project
239 @project_to_destroy = @project
244 if api_request? || params[:confirm]
240 if api_request? || params[:confirm]
245 @project_to_destroy.destroy
241 @project_to_destroy.destroy
246 respond_to do |format|
242 respond_to do |format|
247 format.html { redirect_to :controller => 'admin', :action => 'projects' }
243 format.html { redirect_to :controller => 'admin', :action => 'projects' }
248 format.api { head :ok }
244 format.api { head :ok }
249 end
245 end
250 end
246 end
251 # hide project in layout
247 # hide project in layout
252 @project = nil
248 @project = nil
253 end
249 end
254
250
255 private
251 private
256
252
257 # Validates parent_id param according to user's permissions
253 # Validates parent_id param according to user's permissions
258 # TODO: move it to Project model in a validation that depends on User.current
254 # TODO: move it to Project model in a validation that depends on User.current
259 def validate_parent_id
255 def validate_parent_id
260 return true if User.current.admin?
256 return true if User.current.admin?
261 parent_id = params[:project] && params[:project][:parent_id]
257 parent_id = params[:project] && params[:project][:parent_id]
262 if parent_id || @project.new_record?
258 if parent_id || @project.new_record?
263 parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
259 parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
264 unless @project.allowed_parents.include?(parent)
260 unless @project.allowed_parents.include?(parent)
265 @project.errors.add :parent_id, :invalid
261 @project.errors.add :parent_id, :invalid
266 return false
262 return false
267 end
263 end
268 end
264 end
269 true
265 true
270 end
266 end
271 end
267 end
General Comments 0
You need to be logged in to leave comments. Login now