@@ -1,413 +1,419 | |||
|
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 | 30 | accept_key_auth :activity |
|
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 | @project = Project.new(params[:project]) |
|
106 | 106 | @project.enabled_module_names = params[:enabled_modules] |
|
107 | 107 | if validate_parent_id && @project.copy(@source_project, :only => params[:only]) |
|
108 | 108 | @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') |
|
109 | 109 | flash[:notice] = l(:notice_successful_create) |
|
110 | 110 | redirect_to :controller => 'admin', :action => 'projects' |
|
111 | end | |
|
111 | elsif !@project.new_record? | |
|
112 | # Project was created | |
|
113 | # But some objects were not copied due to validation failures | |
|
114 | # (eg. issues from disabled trackers) | |
|
115 | # TODO: inform about that | |
|
116 | redirect_to :controller => 'admin', :action => 'projects' | |
|
117 | end | |
|
112 | 118 | end |
|
113 | 119 | rescue ActiveRecord::RecordNotFound |
|
114 | 120 | redirect_to :controller => 'admin', :action => 'projects' |
|
115 | 121 | end |
|
116 | 122 | |
|
117 | 123 | # Show @project |
|
118 | 124 | def show |
|
119 | 125 | if params[:jump] |
|
120 | 126 | # try to redirect to the requested menu item |
|
121 | 127 | redirect_to_project_menu_item(@project, params[:jump]) && return |
|
122 | 128 | end |
|
123 | 129 | |
|
124 | 130 | @users_by_role = @project.users_by_role |
|
125 | 131 | @subprojects = @project.children.visible |
|
126 | 132 | @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC") |
|
127 | 133 | @trackers = @project.rolled_up_trackers |
|
128 | 134 | |
|
129 | 135 | cond = @project.project_condition(Setting.display_subprojects_issues?) |
|
130 | 136 | |
|
131 | 137 | @open_issues_by_tracker = Issue.visible.count(:group => :tracker, |
|
132 | 138 | :include => [:project, :status, :tracker], |
|
133 | 139 | :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false]) |
|
134 | 140 | @total_issues_by_tracker = Issue.visible.count(:group => :tracker, |
|
135 | 141 | :include => [:project, :status, :tracker], |
|
136 | 142 | :conditions => cond) |
|
137 | 143 | |
|
138 | 144 | TimeEntry.visible_by(User.current) do |
|
139 | 145 | @total_hours = TimeEntry.sum(:hours, |
|
140 | 146 | :include => :project, |
|
141 | 147 | :conditions => cond).to_f |
|
142 | 148 | end |
|
143 | 149 | @key = User.current.rss_key |
|
144 | 150 | end |
|
145 | 151 | |
|
146 | 152 | def settings |
|
147 | 153 | @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") |
|
148 | 154 | @issue_category ||= IssueCategory.new |
|
149 | 155 | @member ||= @project.members.new |
|
150 | 156 | @trackers = Tracker.all |
|
151 | 157 | @repository ||= @project.repository |
|
152 | 158 | @wiki ||= @project.wiki |
|
153 | 159 | end |
|
154 | 160 | |
|
155 | 161 | # Edit @project |
|
156 | 162 | def edit |
|
157 | 163 | if request.post? |
|
158 | 164 | @project.attributes = params[:project] |
|
159 | 165 | if validate_parent_id && @project.save |
|
160 | 166 | @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') |
|
161 | 167 | flash[:notice] = l(:notice_successful_update) |
|
162 | 168 | redirect_to :action => 'settings', :id => @project |
|
163 | 169 | else |
|
164 | 170 | settings |
|
165 | 171 | render :action => 'settings' |
|
166 | 172 | end |
|
167 | 173 | end |
|
168 | 174 | end |
|
169 | 175 | |
|
170 | 176 | def modules |
|
171 | 177 | @project.enabled_module_names = params[:enabled_modules] |
|
172 | 178 | redirect_to :action => 'settings', :id => @project, :tab => 'modules' |
|
173 | 179 | end |
|
174 | 180 | |
|
175 | 181 | def archive |
|
176 | 182 | if request.post? |
|
177 | 183 | unless @project.archive |
|
178 | 184 | flash[:error] = l(:error_can_not_archive_project) |
|
179 | 185 | end |
|
180 | 186 | end |
|
181 | 187 | redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status])) |
|
182 | 188 | end |
|
183 | 189 | |
|
184 | 190 | def unarchive |
|
185 | 191 | @project.unarchive if request.post? && !@project.active? |
|
186 | 192 | redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status])) |
|
187 | 193 | end |
|
188 | 194 | |
|
189 | 195 | # Delete @project |
|
190 | 196 | def destroy |
|
191 | 197 | @project_to_destroy = @project |
|
192 | 198 | if request.post? and params[:confirm] |
|
193 | 199 | @project_to_destroy.destroy |
|
194 | 200 | redirect_to :controller => 'admin', :action => 'projects' |
|
195 | 201 | end |
|
196 | 202 | # hide project in layout |
|
197 | 203 | @project = nil |
|
198 | 204 | end |
|
199 | 205 | |
|
200 | 206 | # Add a new issue category to @project |
|
201 | 207 | def add_issue_category |
|
202 | 208 | @category = @project.issue_categories.build(params[:category]) |
|
203 | 209 | if request.post? |
|
204 | 210 | if @category.save |
|
205 | 211 | respond_to do |format| |
|
206 | 212 | format.html do |
|
207 | 213 | flash[:notice] = l(:notice_successful_create) |
|
208 | 214 | redirect_to :action => 'settings', :tab => 'categories', :id => @project |
|
209 | 215 | end |
|
210 | 216 | format.js do |
|
211 | 217 | # IE doesn't support the replace_html rjs method for select box options |
|
212 | 218 | render(:update) {|page| page.replace "issue_category_id", |
|
213 | 219 | 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]') |
|
214 | 220 | } |
|
215 | 221 | end |
|
216 | 222 | end |
|
217 | 223 | else |
|
218 | 224 | respond_to do |format| |
|
219 | 225 | format.html |
|
220 | 226 | format.js do |
|
221 | 227 | render(:update) {|page| page.alert(@category.errors.full_messages.join('\n')) } |
|
222 | 228 | end |
|
223 | 229 | end |
|
224 | 230 | end |
|
225 | 231 | end |
|
226 | 232 | end |
|
227 | 233 | |
|
228 | 234 | # Add a new version to @project |
|
229 | 235 | def add_version |
|
230 | 236 | @version = @project.versions.build |
|
231 | 237 | if params[:version] |
|
232 | 238 | attributes = params[:version].dup |
|
233 | 239 | attributes.delete('sharing') unless attributes.nil? || @version.allowed_sharings.include?(attributes['sharing']) |
|
234 | 240 | @version.attributes = attributes |
|
235 | 241 | end |
|
236 | 242 | if request.post? |
|
237 | 243 | if @version.save |
|
238 | 244 | respond_to do |format| |
|
239 | 245 | format.html do |
|
240 | 246 | flash[:notice] = l(:notice_successful_create) |
|
241 | 247 | redirect_to :action => 'settings', :tab => 'versions', :id => @project |
|
242 | 248 | end |
|
243 | 249 | format.js do |
|
244 | 250 | # IE doesn't support the replace_html rjs method for select box options |
|
245 | 251 | render(:update) {|page| page.replace "issue_fixed_version_id", |
|
246 | 252 | content_tag('select', '<option></option>' + version_options_for_select(@project.shared_versions.open, @version), :id => 'issue_fixed_version_id', :name => 'issue[fixed_version_id]') |
|
247 | 253 | } |
|
248 | 254 | end |
|
249 | 255 | end |
|
250 | 256 | else |
|
251 | 257 | respond_to do |format| |
|
252 | 258 | format.html |
|
253 | 259 | format.js do |
|
254 | 260 | render(:update) {|page| page.alert(@version.errors.full_messages.join('\n')) } |
|
255 | 261 | end |
|
256 | 262 | end |
|
257 | 263 | end |
|
258 | 264 | end |
|
259 | 265 | end |
|
260 | 266 | |
|
261 | 267 | def add_file |
|
262 | 268 | if request.post? |
|
263 | 269 | container = (params[:version_id].blank? ? @project : @project.versions.find_by_id(params[:version_id])) |
|
264 | 270 | attachments = attach_files(container, params[:attachments]) |
|
265 | 271 | if !attachments.empty? && Setting.notified_events.include?('file_added') |
|
266 | 272 | Mailer.deliver_attachments_added(attachments) |
|
267 | 273 | end |
|
268 | 274 | redirect_to :controller => 'projects', :action => 'list_files', :id => @project |
|
269 | 275 | return |
|
270 | 276 | end |
|
271 | 277 | @versions = @project.versions.sort |
|
272 | 278 | end |
|
273 | 279 | |
|
274 | 280 | def save_activities |
|
275 | 281 | if request.post? && params[:enumerations] |
|
276 | 282 | Project.transaction do |
|
277 | 283 | params[:enumerations].each do |id, activity| |
|
278 | 284 | @project.update_or_create_time_entry_activity(id, activity) |
|
279 | 285 | end |
|
280 | 286 | end |
|
281 | 287 | end |
|
282 | 288 | |
|
283 | 289 | redirect_to :controller => 'projects', :action => 'settings', :tab => 'activities', :id => @project |
|
284 | 290 | end |
|
285 | 291 | |
|
286 | 292 | def reset_activities |
|
287 | 293 | @project.time_entry_activities.each do |time_entry_activity| |
|
288 | 294 | time_entry_activity.destroy(time_entry_activity.parent) |
|
289 | 295 | end |
|
290 | 296 | redirect_to :controller => 'projects', :action => 'settings', :tab => 'activities', :id => @project |
|
291 | 297 | end |
|
292 | 298 | |
|
293 | 299 | def list_files |
|
294 | 300 | sort_init 'filename', 'asc' |
|
295 | 301 | sort_update 'filename' => "#{Attachment.table_name}.filename", |
|
296 | 302 | 'created_on' => "#{Attachment.table_name}.created_on", |
|
297 | 303 | 'size' => "#{Attachment.table_name}.filesize", |
|
298 | 304 | 'downloads' => "#{Attachment.table_name}.downloads" |
|
299 | 305 | |
|
300 | 306 | @containers = [ Project.find(@project.id, :include => :attachments, :order => sort_clause)] |
|
301 | 307 | @containers += @project.versions.find(:all, :include => :attachments, :order => sort_clause).sort.reverse |
|
302 | 308 | render :layout => !request.xhr? |
|
303 | 309 | end |
|
304 | 310 | |
|
305 | 311 | def roadmap |
|
306 | 312 | @trackers = @project.trackers.find(:all, :order => 'position') |
|
307 | 313 | retrieve_selected_tracker_ids(@trackers, @trackers.select {|t| t.is_in_roadmap?}) |
|
308 | 314 | @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1') |
|
309 | 315 | project_ids = @with_subprojects ? @project.self_and_descendants.collect(&:id) : [@project.id] |
|
310 | 316 | |
|
311 | 317 | @versions = @project.shared_versions.sort |
|
312 | 318 | @versions.reject! {|version| version.closed? || version.completed? } unless params[:completed] |
|
313 | 319 | |
|
314 | 320 | @issues_by_version = {} |
|
315 | 321 | unless @selected_tracker_ids.empty? |
|
316 | 322 | @versions.each do |version| |
|
317 | 323 | conditions = {:tracker_id => @selected_tracker_ids} |
|
318 | 324 | if !@project.versions.include?(version) |
|
319 | 325 | conditions.merge!(:project_id => project_ids) |
|
320 | 326 | end |
|
321 | 327 | issues = version.fixed_issues.visible.find(:all, |
|
322 | 328 | :include => [:project, :status, :tracker, :priority], |
|
323 | 329 | :conditions => conditions, |
|
324 | 330 | :order => "#{Project.table_name}.lft, #{Tracker.table_name}.position, #{Issue.table_name}.id") |
|
325 | 331 | @issues_by_version[version] = issues |
|
326 | 332 | end |
|
327 | 333 | end |
|
328 | 334 | @versions.reject! {|version| !project_ids.include?(version.project_id) && @issues_by_version[version].empty?} |
|
329 | 335 | end |
|
330 | 336 | |
|
331 | 337 | def activity |
|
332 | 338 | @days = Setting.activity_days_default.to_i |
|
333 | 339 | |
|
334 | 340 | if params[:from] |
|
335 | 341 | begin; @date_to = params[:from].to_date + 1; rescue; end |
|
336 | 342 | end |
|
337 | 343 | |
|
338 | 344 | @date_to ||= Date.today + 1 |
|
339 | 345 | @date_from = @date_to - @days |
|
340 | 346 | @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1') |
|
341 | 347 | @author = (params[:user_id].blank? ? nil : User.active.find(params[:user_id])) |
|
342 | 348 | |
|
343 | 349 | @activity = Redmine::Activity::Fetcher.new(User.current, :project => @project, |
|
344 | 350 | :with_subprojects => @with_subprojects, |
|
345 | 351 | :author => @author) |
|
346 | 352 | @activity.scope_select {|t| !params["show_#{t}"].nil?} |
|
347 | 353 | @activity.scope = (@author.nil? ? :default : :all) if @activity.scope.empty? |
|
348 | 354 | |
|
349 | 355 | events = @activity.events(@date_from, @date_to) |
|
350 | 356 | |
|
351 | 357 | if events.empty? || stale?(:etag => [events.first, User.current]) |
|
352 | 358 | respond_to do |format| |
|
353 | 359 | format.html { |
|
354 | 360 | @events_by_day = events.group_by(&:event_date) |
|
355 | 361 | render :layout => false if request.xhr? |
|
356 | 362 | } |
|
357 | 363 | format.atom { |
|
358 | 364 | title = l(:label_activity) |
|
359 | 365 | if @author |
|
360 | 366 | title = @author.name |
|
361 | 367 | elsif @activity.scope.size == 1 |
|
362 | 368 | title = l("label_#{@activity.scope.first.singularize}_plural") |
|
363 | 369 | end |
|
364 | 370 | render_feed(events, :title => "#{@project || Setting.app_title}: #{title}") |
|
365 | 371 | } |
|
366 | 372 | end |
|
367 | 373 | end |
|
368 | 374 | |
|
369 | 375 | rescue ActiveRecord::RecordNotFound |
|
370 | 376 | render_404 |
|
371 | 377 | end |
|
372 | 378 | |
|
373 | 379 | private |
|
374 | 380 | # Find project of id params[:id] |
|
375 | 381 | # if not found, redirect to project list |
|
376 | 382 | # Used as a before_filter |
|
377 | 383 | def find_project |
|
378 | 384 | @project = Project.find(params[:id]) |
|
379 | 385 | rescue ActiveRecord::RecordNotFound |
|
380 | 386 | render_404 |
|
381 | 387 | end |
|
382 | 388 | |
|
383 | 389 | def find_optional_project |
|
384 | 390 | return true unless params[:id] |
|
385 | 391 | @project = Project.find(params[:id]) |
|
386 | 392 | authorize |
|
387 | 393 | rescue ActiveRecord::RecordNotFound |
|
388 | 394 | render_404 |
|
389 | 395 | end |
|
390 | 396 | |
|
391 | 397 | def retrieve_selected_tracker_ids(selectable_trackers, default_trackers=nil) |
|
392 | 398 | if ids = params[:tracker_ids] |
|
393 | 399 | @selected_tracker_ids = (ids.is_a? Array) ? ids.collect { |id| id.to_i.to_s } : ids.split('/').collect { |id| id.to_i.to_s } |
|
394 | 400 | else |
|
395 | 401 | @selected_tracker_ids = (default_trackers || selectable_trackers).collect {|t| t.id.to_s } |
|
396 | 402 | end |
|
397 | 403 | end |
|
398 | 404 | |
|
399 | 405 | # Validates parent_id param according to user's permissions |
|
400 | 406 | # TODO: move it to Project model in a validation that depends on User.current |
|
401 | 407 | def validate_parent_id |
|
402 | 408 | return true if User.current.admin? |
|
403 | 409 | parent_id = params[:project] && params[:project][:parent_id] |
|
404 | 410 | if parent_id || @project.new_record? |
|
405 | 411 | parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i) |
|
406 | 412 | unless @project.allowed_parents.include?(parent) |
|
407 | 413 | @project.errors.add :parent_id, :invalid |
|
408 | 414 | return false |
|
409 | 415 | end |
|
410 | 416 | end |
|
411 | 417 | true |
|
412 | 418 | end |
|
413 | 419 | end |
General Comments 0
You need to be logged in to leave comments.
Login now