##// END OF EJS Templates
Merged r3777 from trunk....
Eric Davis -
r3666:90e23e4d5d55
parent child
Show More
@@ -1,417 +1,417
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2009 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class ProjectsController < ApplicationController
19 19 menu_item :overview
20 20 menu_item :activity, :only => :activity
21 21 menu_item :roadmap, :only => :roadmap
22 22 menu_item :files, :only => [:list_files, :add_file]
23 23 menu_item :settings, :only => :settings
24 24
25 25 before_filter :find_project, :except => [ :index, :list, :add, :copy, :activity ]
26 26 before_filter :find_optional_project, :only => :activity
27 27 before_filter :authorize, :except => [ :index, :list, :add, :copy, :archive, :unarchive, :destroy, :activity ]
28 28 before_filter :authorize_global, :only => :add
29 29 before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ]
30 accept_key_auth :activity
30 accept_key_auth :activity, :index
31 31
32 32 after_filter :only => [:add, :edit, :archive, :unarchive, :destroy] do |controller|
33 33 if controller.request.post?
34 34 controller.send :expire_action, :controller => 'welcome', :action => 'robots.txt'
35 35 end
36 36 end
37 37
38 38 helper :sort
39 39 include SortHelper
40 40 helper :custom_fields
41 41 include CustomFieldsHelper
42 42 helper :issues
43 43 helper IssuesHelper
44 44 helper :queries
45 45 include QueriesHelper
46 46 helper :repositories
47 47 include RepositoriesHelper
48 48 include ProjectsHelper
49 49
50 50 # Lists visible projects
51 51 def index
52 52 respond_to do |format|
53 53 format.html {
54 54 @projects = Project.visible.find(:all, :order => 'lft')
55 55 }
56 56 format.atom {
57 57 projects = Project.visible.find(:all, :order => 'created_on DESC',
58 58 :limit => Setting.feeds_limit.to_i)
59 59 render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
60 60 }
61 61 end
62 62 end
63 63
64 64 # Add a new project
65 65 def add
66 66 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
67 67 @trackers = Tracker.all
68 68 @project = Project.new(params[:project])
69 69 if request.get?
70 70 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
71 71 @project.trackers = Tracker.all
72 72 @project.is_public = Setting.default_projects_public?
73 73 @project.enabled_module_names = Setting.default_projects_modules
74 74 else
75 75 @project.enabled_module_names = params[:enabled_modules]
76 76 if validate_parent_id && @project.save
77 77 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
78 78 # Add current user as a project member if he is not admin
79 79 unless User.current.admin?
80 80 r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
81 81 m = Member.new(:user => User.current, :roles => [r])
82 82 @project.members << m
83 83 end
84 84 flash[:notice] = l(:notice_successful_create)
85 85 redirect_to :controller => 'projects', :action => 'settings', :id => @project
86 86 end
87 87 end
88 88 end
89 89
90 90 def copy
91 91 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
92 92 @trackers = Tracker.all
93 93 @root_projects = Project.find(:all,
94 94 :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}",
95 95 :order => 'name')
96 96 @source_project = Project.find(params[:id])
97 97 if request.get?
98 98 @project = Project.copy_from(@source_project)
99 99 if @project
100 100 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
101 101 else
102 102 redirect_to :controller => 'admin', :action => 'projects'
103 103 end
104 104 else
105 105 Mailer.with_deliveries(params[:notifications] == '1') do
106 106 @project = Project.new(params[:project])
107 107 @project.enabled_module_names = params[:enabled_modules]
108 108 if validate_parent_id && @project.copy(@source_project, :only => params[:only])
109 109 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
110 110 flash[:notice] = l(:notice_successful_create)
111 111 redirect_to :controller => 'admin', :action => 'projects'
112 112 elsif !@project.new_record?
113 113 # Project was created
114 114 # But some objects were not copied due to validation failures
115 115 # (eg. issues from disabled trackers)
116 116 # TODO: inform about that
117 117 redirect_to :controller => 'admin', :action => 'projects'
118 118 end
119 119 end
120 120 end
121 121 rescue ActiveRecord::RecordNotFound
122 122 redirect_to :controller => 'admin', :action => 'projects'
123 123 end
124 124
125 125 # Show @project
126 126 def show
127 127 if params[:jump]
128 128 # try to redirect to the requested menu item
129 129 redirect_to_project_menu_item(@project, params[:jump]) && return
130 130 end
131 131
132 132 @users_by_role = @project.users_by_role
133 133 @subprojects = @project.children.visible
134 134 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
135 135 @trackers = @project.rolled_up_trackers
136 136
137 137 cond = @project.project_condition(Setting.display_subprojects_issues?)
138 138
139 139 @open_issues_by_tracker = Issue.visible.count(:group => :tracker,
140 140 :include => [:project, :status, :tracker],
141 141 :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
142 142 @total_issues_by_tracker = Issue.visible.count(:group => :tracker,
143 143 :include => [:project, :status, :tracker],
144 144 :conditions => cond)
145 145
146 146 TimeEntry.visible_by(User.current) do
147 147 @total_hours = TimeEntry.sum(:hours,
148 148 :include => :project,
149 149 :conditions => cond).to_f
150 150 end
151 151 @key = User.current.rss_key
152 152 end
153 153
154 154 def settings
155 155 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
156 156 @issue_category ||= IssueCategory.new
157 157 @member ||= @project.members.new
158 158 @trackers = Tracker.all
159 159 @repository ||= @project.repository
160 160 @wiki ||= @project.wiki
161 161 end
162 162
163 163 # Edit @project
164 164 def edit
165 165 if request.post?
166 166 @project.attributes = params[:project]
167 167 if validate_parent_id && @project.save
168 168 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
169 169 flash[:notice] = l(:notice_successful_update)
170 170 redirect_to :action => 'settings', :id => @project
171 171 else
172 172 settings
173 173 render :action => 'settings'
174 174 end
175 175 end
176 176 end
177 177
178 178 def modules
179 179 @project.enabled_module_names = params[:enabled_modules]
180 180 redirect_to :action => 'settings', :id => @project, :tab => 'modules'
181 181 end
182 182
183 183 def archive
184 184 if request.post?
185 185 unless @project.archive
186 186 flash[:error] = l(:error_can_not_archive_project)
187 187 end
188 188 end
189 189 redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
190 190 end
191 191
192 192 def unarchive
193 193 @project.unarchive if request.post? && !@project.active?
194 194 redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
195 195 end
196 196
197 197 # Delete @project
198 198 def destroy
199 199 @project_to_destroy = @project
200 200 if request.post? and params[:confirm]
201 201 @project_to_destroy.destroy
202 202 redirect_to :controller => 'admin', :action => 'projects'
203 203 end
204 204 # hide project in layout
205 205 @project = nil
206 206 end
207 207
208 208 # Add a new issue category to @project
209 209 def add_issue_category
210 210 @category = @project.issue_categories.build(params[:category])
211 211 if request.post?
212 212 if @category.save
213 213 respond_to do |format|
214 214 format.html do
215 215 flash[:notice] = l(:notice_successful_create)
216 216 redirect_to :action => 'settings', :tab => 'categories', :id => @project
217 217 end
218 218 format.js do
219 219 # IE doesn't support the replace_html rjs method for select box options
220 220 render(:update) {|page| page.replace "issue_category_id",
221 221 content_tag('select', '<option></option>' + options_from_collection_for_select(@project.issue_categories, 'id', 'name', @category.id), :id => 'issue_category_id', :name => 'issue[category_id]')
222 222 }
223 223 end
224 224 end
225 225 else
226 226 respond_to do |format|
227 227 format.html
228 228 format.js do
229 229 render(:update) {|page| page.alert(@category.errors.full_messages.join('\n')) }
230 230 end
231 231 end
232 232 end
233 233 end
234 234 end
235 235
236 236 # Add a new version to @project
237 237 def add_version
238 238 @version = @project.versions.build
239 239 if params[:version]
240 240 attributes = params[:version].dup
241 241 attributes.delete('sharing') unless attributes.nil? || @version.allowed_sharings.include?(attributes['sharing'])
242 242 @version.attributes = attributes
243 243 end
244 244 if request.post?
245 245 if @version.save
246 246 respond_to do |format|
247 247 format.html do
248 248 flash[:notice] = l(:notice_successful_create)
249 249 redirect_to :action => 'settings', :tab => 'versions', :id => @project
250 250 end
251 251 format.js do
252 252 # IE doesn't support the replace_html rjs method for select box options
253 253 render(:update) {|page| page.replace "issue_fixed_version_id",
254 254 content_tag('select', '<option></option>' + version_options_for_select(@project.shared_versions.open, @version), :id => 'issue_fixed_version_id', :name => 'issue[fixed_version_id]')
255 255 }
256 256 end
257 257 end
258 258 else
259 259 respond_to do |format|
260 260 format.html
261 261 format.js do
262 262 render(:update) {|page| page.alert(@version.errors.full_messages.join('\n')) }
263 263 end
264 264 end
265 265 end
266 266 end
267 267 end
268 268
269 269 def add_file
270 270 if request.post?
271 271 container = (params[:version_id].blank? ? @project : @project.versions.find_by_id(params[:version_id]))
272 272 attachments = attach_files(container, params[:attachments])
273 273 if !attachments.empty? && Setting.notified_events.include?('file_added')
274 274 Mailer.deliver_attachments_added(attachments)
275 275 end
276 276 redirect_to :controller => 'projects', :action => 'list_files', :id => @project
277 277 return
278 278 end
279 279 @versions = @project.versions.sort
280 280 end
281 281
282 282 def save_activities
283 283 if request.post? && params[:enumerations]
284 284 Project.transaction do
285 285 params[:enumerations].each do |id, activity|
286 286 @project.update_or_create_time_entry_activity(id, activity)
287 287 end
288 288 end
289 289 end
290 290
291 291 redirect_to :controller => 'projects', :action => 'settings', :tab => 'activities', :id => @project
292 292 end
293 293
294 294 def reset_activities
295 295 @project.time_entry_activities.each do |time_entry_activity|
296 296 time_entry_activity.destroy(time_entry_activity.parent)
297 297 end
298 298 redirect_to :controller => 'projects', :action => 'settings', :tab => 'activities', :id => @project
299 299 end
300 300
301 301 def list_files
302 302 sort_init 'filename', 'asc'
303 303 sort_update 'filename' => "#{Attachment.table_name}.filename",
304 304 'created_on' => "#{Attachment.table_name}.created_on",
305 305 'size' => "#{Attachment.table_name}.filesize",
306 306 'downloads' => "#{Attachment.table_name}.downloads"
307 307
308 308 @containers = [ Project.find(@project.id, :include => :attachments, :order => sort_clause)]
309 309 @containers += @project.versions.find(:all, :include => :attachments, :order => sort_clause).sort.reverse
310 310 render :layout => !request.xhr?
311 311 end
312 312
313 313 def roadmap
314 314 @trackers = @project.trackers.find(:all, :order => 'position')
315 315 retrieve_selected_tracker_ids(@trackers, @trackers.select {|t| t.is_in_roadmap?})
316 316 @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
317 317 project_ids = @with_subprojects ? @project.self_and_descendants.collect(&:id) : [@project.id]
318 318
319 319 @versions = @project.shared_versions.sort
320 320 @versions.reject! {|version| version.closed? || version.completed? } unless params[:completed]
321 321
322 322 @issues_by_version = {}
323 323 unless @selected_tracker_ids.empty?
324 324 @versions.each do |version|
325 325 issues = version.fixed_issues.visible.find(:all,
326 326 :include => [:project, :status, :tracker, :priority],
327 327 :conditions => {:tracker_id => @selected_tracker_ids, :project_id => project_ids},
328 328 :order => "#{Project.table_name}.lft, #{Tracker.table_name}.position, #{Issue.table_name}.id")
329 329 @issues_by_version[version] = issues
330 330 end
331 331 end
332 332 @versions.reject! {|version| !project_ids.include?(version.project_id) && @issues_by_version[version].empty?}
333 333 end
334 334
335 335 def activity
336 336 @days = Setting.activity_days_default.to_i
337 337
338 338 if params[:from]
339 339 begin; @date_to = params[:from].to_date + 1; rescue; end
340 340 end
341 341
342 342 @date_to ||= Date.today + 1
343 343 @date_from = @date_to - @days
344 344 @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
345 345 @author = (params[:user_id].blank? ? nil : User.active.find(params[:user_id]))
346 346
347 347 @activity = Redmine::Activity::Fetcher.new(User.current, :project => @project,
348 348 :with_subprojects => @with_subprojects,
349 349 :author => @author)
350 350 @activity.scope_select {|t| !params["show_#{t}"].nil?}
351 351 @activity.scope = (@author.nil? ? :default : :all) if @activity.scope.empty?
352 352
353 353 events = @activity.events(@date_from, @date_to)
354 354
355 355 if events.empty? || stale?(:etag => [events.first, User.current])
356 356 respond_to do |format|
357 357 format.html {
358 358 @events_by_day = events.group_by(&:event_date)
359 359 render :layout => false if request.xhr?
360 360 }
361 361 format.atom {
362 362 title = l(:label_activity)
363 363 if @author
364 364 title = @author.name
365 365 elsif @activity.scope.size == 1
366 366 title = l("label_#{@activity.scope.first.singularize}_plural")
367 367 end
368 368 render_feed(events, :title => "#{@project || Setting.app_title}: #{title}")
369 369 }
370 370 end
371 371 end
372 372
373 373 rescue ActiveRecord::RecordNotFound
374 374 render_404
375 375 end
376 376
377 377 private
378 378 # Find project of id params[:id]
379 379 # if not found, redirect to project list
380 380 # Used as a before_filter
381 381 def find_project
382 382 @project = Project.find(params[:id])
383 383 rescue ActiveRecord::RecordNotFound
384 384 render_404
385 385 end
386 386
387 387 def find_optional_project
388 388 return true unless params[:id]
389 389 @project = Project.find(params[:id])
390 390 authorize
391 391 rescue ActiveRecord::RecordNotFound
392 392 render_404
393 393 end
394 394
395 395 def retrieve_selected_tracker_ids(selectable_trackers, default_trackers=nil)
396 396 if ids = params[:tracker_ids]
397 397 @selected_tracker_ids = (ids.is_a? Array) ? ids.collect { |id| id.to_i.to_s } : ids.split('/').collect { |id| id.to_i.to_s }
398 398 else
399 399 @selected_tracker_ids = (default_trackers || selectable_trackers).collect {|t| t.id.to_s }
400 400 end
401 401 end
402 402
403 403 # Validates parent_id param according to user's permissions
404 404 # TODO: move it to Project model in a validation that depends on User.current
405 405 def validate_parent_id
406 406 return true if User.current.admin?
407 407 parent_id = params[:project] && params[:project][:parent_id]
408 408 if parent_id || @project.new_record?
409 409 parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
410 410 unless @project.allowed_parents.include?(parent)
411 411 @project.errors.add :parent_id, :invalid
412 412 return false
413 413 end
414 414 end
415 415 true
416 416 end
417 417 end
General Comments 0
You need to be logged in to leave comments. Login now