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