##// END OF EJS Templates
If 'Display subprojects issues on main projects' is set to false:...
Jean-Philippe Lang -
r1283:89602004098d
parent child
Show More
@@ -1,426 +1,428
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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 layout 'base'
20 20 menu_item :overview
21 21 menu_item :activity, :only => :activity
22 22 menu_item :roadmap, :only => :roadmap
23 23 menu_item :files, :only => [:list_files, :add_file]
24 24 menu_item :settings, :only => :settings
25 25 menu_item :issues, :only => [:changelog]
26 26
27 27 before_filter :find_project, :except => [ :index, :list, :add, :activity ]
28 28 before_filter :find_optional_project, :only => :activity
29 29 before_filter :authorize, :except => [ :index, :list, :add, :archive, :unarchive, :destroy, :activity ]
30 30 before_filter :require_admin, :only => [ :add, :archive, :unarchive, :destroy ]
31 31 accept_key_auth :activity, :calendar
32 32
33 33 helper :sort
34 34 include SortHelper
35 35 helper :custom_fields
36 36 include CustomFieldsHelper
37 37 helper :ifpdf
38 38 include IfpdfHelper
39 39 helper :issues
40 40 helper IssuesHelper
41 41 helper :queries
42 42 include QueriesHelper
43 43 helper :repositories
44 44 include RepositoriesHelper
45 45 include ProjectsHelper
46 46
47 47 def index
48 48 list
49 49 render :action => 'list' unless request.xhr?
50 50 end
51 51
52 52 # Lists visible projects
53 53 def list
54 54 projects = Project.find :all,
55 55 :conditions => Project.visible_by(User.current),
56 56 :include => :parent
57 57 @project_tree = projects.group_by {|p| p.parent || p}
58 58 @project_tree.each_key {|p| @project_tree[p] -= [p]}
59 59 end
60 60
61 61 # Add a new project
62 62 def add
63 63 @custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
64 64 @trackers = Tracker.all
65 65 @root_projects = Project.find(:all,
66 66 :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}",
67 67 :order => 'name')
68 68 @project = Project.new(params[:project])
69 69 @project.enabled_module_names = Redmine::AccessControl.available_project_modules
70 70 if request.get?
71 71 @custom_values = ProjectCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| CustomValue.new(:custom_field => x, :customized => @project) }
72 72 @project.trackers = Tracker.all
73 73 @project.is_public = Setting.default_projects_public?
74 74 else
75 75 @project.custom_fields = CustomField.find(params[:custom_field_ids]) if params[:custom_field_ids]
76 76 @custom_values = ProjectCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| CustomValue.new(:custom_field => x, :customized => @project, :value => (params[:custom_fields] ? params["custom_fields"][x.id.to_s] : nil)) }
77 77 @project.custom_values = @custom_values
78 78 if @project.save
79 79 @project.enabled_module_names = params[:enabled_modules]
80 80 flash[:notice] = l(:notice_successful_create)
81 81 redirect_to :controller => 'admin', :action => 'projects'
82 82 end
83 83 end
84 84 end
85 85
86 86 # Show @project
87 87 def show
88 88 @custom_values = @project.custom_values.find(:all, :include => :custom_field, :order => "#{CustomField.table_name}.position")
89 89 @members_by_role = @project.members.find(:all, :include => [:user, :role], :order => 'position').group_by {|m| m.role}
90 90 @subprojects = @project.active_children
91 91 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
92 92 @trackers = @project.rolled_up_trackers
93
94 cond = @project.project_condition(Setting.display_subprojects_issues?)
93 95 Issue.visible_by(User.current) do
94 96 @open_issues_by_tracker = Issue.count(:group => :tracker,
95 97 :include => [:project, :status, :tracker],
96 :conditions => ["(#{Project.table_name}.id=? OR #{Project.table_name}.parent_id=?) and #{IssueStatus.table_name}.is_closed=?", @project.id, @project.id, false])
98 :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
97 99 @total_issues_by_tracker = Issue.count(:group => :tracker,
98 100 :include => [:project, :status, :tracker],
99 :conditions => ["#{Project.table_name}.id=? OR #{Project.table_name}.parent_id=?", @project.id, @project.id])
101 :conditions => cond)
100 102 end
101 103 TimeEntry.visible_by(User.current) do
102 104 @total_hours = TimeEntry.sum(:hours,
103 105 :include => :project,
104 :conditions => ["(#{Project.table_name}.id = ? OR #{Project.table_name}.parent_id = ?)", @project.id, @project.id]).to_f
106 :conditions => cond).to_f
105 107 end
106 108 @key = User.current.rss_key
107 109 end
108 110
109 111 def settings
110 112 @root_projects = Project.find(:all,
111 113 :conditions => ["parent_id IS NULL AND status = #{Project::STATUS_ACTIVE} AND id <> ?", @project.id],
112 114 :order => 'name')
113 115 @custom_fields = IssueCustomField.find(:all)
114 116 @issue_category ||= IssueCategory.new
115 117 @member ||= @project.members.new
116 118 @trackers = Tracker.all
117 119 @custom_values ||= ProjectCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| @project.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x) }
118 120 @repository ||= @project.repository
119 121 @wiki ||= @project.wiki
120 122 end
121 123
122 124 # Edit @project
123 125 def edit
124 126 if request.post?
125 127 if params[:custom_fields]
126 128 @custom_values = ProjectCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| CustomValue.new(:custom_field => x, :customized => @project, :value => params["custom_fields"][x.id.to_s]) }
127 129 @project.custom_values = @custom_values
128 130 end
129 131 @project.attributes = params[:project]
130 132 if @project.save
131 133 flash[:notice] = l(:notice_successful_update)
132 134 redirect_to :action => 'settings', :id => @project
133 135 else
134 136 settings
135 137 render :action => 'settings'
136 138 end
137 139 end
138 140 end
139 141
140 142 def modules
141 143 @project.enabled_module_names = params[:enabled_modules]
142 144 redirect_to :action => 'settings', :id => @project, :tab => 'modules'
143 145 end
144 146
145 147 def archive
146 148 @project.archive if request.post? && @project.active?
147 149 redirect_to :controller => 'admin', :action => 'projects'
148 150 end
149 151
150 152 def unarchive
151 153 @project.unarchive if request.post? && !@project.active?
152 154 redirect_to :controller => 'admin', :action => 'projects'
153 155 end
154 156
155 157 # Delete @project
156 158 def destroy
157 159 @project_to_destroy = @project
158 160 if request.post? and params[:confirm]
159 161 @project_to_destroy.destroy
160 162 redirect_to :controller => 'admin', :action => 'projects'
161 163 end
162 164 # hide project in layout
163 165 @project = nil
164 166 end
165 167
166 168 # Add a new issue category to @project
167 169 def add_issue_category
168 170 @category = @project.issue_categories.build(params[:category])
169 171 if request.post? and @category.save
170 172 respond_to do |format|
171 173 format.html do
172 174 flash[:notice] = l(:notice_successful_create)
173 175 redirect_to :action => 'settings', :tab => 'categories', :id => @project
174 176 end
175 177 format.js do
176 178 # IE doesn't support the replace_html rjs method for select box options
177 179 render(:update) {|page| page.replace "issue_category_id",
178 180 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]')
179 181 }
180 182 end
181 183 end
182 184 end
183 185 end
184 186
185 187 # Add a new version to @project
186 188 def add_version
187 189 @version = @project.versions.build(params[:version])
188 190 if request.post? and @version.save
189 191 flash[:notice] = l(:notice_successful_create)
190 192 redirect_to :action => 'settings', :tab => 'versions', :id => @project
191 193 end
192 194 end
193 195
194 196 def add_file
195 197 if request.post?
196 198 @version = @project.versions.find_by_id(params[:version_id])
197 199 attachments = attach_files(@version, params[:attachments])
198 200 Mailer.deliver_attachments_added(attachments) if !attachments.empty? && Setting.notified_events.include?('file_added')
199 201 redirect_to :controller => 'projects', :action => 'list_files', :id => @project
200 202 end
201 203 @versions = @project.versions.sort
202 204 end
203 205
204 206 def list_files
205 207 @versions = @project.versions.sort.reverse
206 208 end
207 209
208 210 # Show changelog for @project
209 211 def changelog
210 212 @trackers = @project.trackers.find(:all, :conditions => ["is_in_chlog=?", true], :order => 'position')
211 213 retrieve_selected_tracker_ids(@trackers)
212 214 @versions = @project.versions.sort
213 215 end
214 216
215 217 def roadmap
216 218 @trackers = @project.trackers.find(:all, :conditions => ["is_in_roadmap=?", true])
217 219 retrieve_selected_tracker_ids(@trackers)
218 220 @versions = @project.versions.sort
219 221 @versions = @versions.select {|v| !v.completed? } unless params[:completed]
220 222 end
221 223
222 224 def activity
223 225 @days = Setting.activity_days_default.to_i
224 226
225 227 if params[:from]
226 228 begin; @date_to = params[:from].to_date; rescue; end
227 229 end
228 230
229 231 @date_to ||= Date.today + 1
230 232 @date_from = @date_to - @days
231 233
232 234 @event_types = %w(issues news files documents changesets wiki_pages messages)
233 235 if @project
234 236 @event_types.delete('wiki_pages') unless @project.wiki
235 237 @event_types.delete('changesets') unless @project.repository
236 238 @event_types.delete('messages') unless @project.boards.any?
237 239 # only show what the user is allowed to view
238 240 @event_types = @event_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, @project)}
239 241 @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
240 242 end
241 243 @scope = @event_types.select {|t| params["show_#{t}"]}
242 244 # default events if none is specified in parameters
243 245 @scope = (@event_types - %w(wiki_pages messages))if @scope.empty?
244 246
245 247 @events = []
246 248
247 249 if @scope.include?('issues')
248 250 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_issues, :project => @project, :with_subprojects => @with_subprojects))
249 251 cond.add(["#{Issue.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
250 252 @events += Issue.find(:all, :include => [:project, :author, :tracker], :conditions => cond.conditions)
251 253
252 254 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_issues, :project => @project, :with_subprojects => @with_subprojects))
253 255 cond.add(["#{Journal.table_name}.journalized_type = 'Issue' AND #{JournalDetail.table_name}.prop_key = 'status_id' AND #{Journal.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
254 256 @events += Journal.find(:all, :include => [{:issue => :project}, :details, :user], :conditions => cond.conditions)
255 257 end
256 258
257 259 if @scope.include?('news')
258 260 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_news, :project => @project, :with_subprojects => @with_subprojects))
259 261 cond.add(["#{News.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
260 262 @events += News.find(:all, :include => [:project, :author], :conditions => cond.conditions)
261 263 end
262 264
263 265 if @scope.include?('files')
264 266 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_files, :project => @project, :with_subprojects => @with_subprojects))
265 267 cond.add(["#{Attachment.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
266 268 @events += Attachment.find(:all, :select => "#{Attachment.table_name}.*",
267 269 :joins => "LEFT JOIN #{Version.table_name} ON #{Attachment.table_name}.container_type='Version' AND #{Version.table_name}.id = #{Attachment.table_name}.container_id " +
268 270 "LEFT JOIN #{Project.table_name} ON #{Version.table_name}.project_id = #{Project.table_name}.id",
269 271 :conditions => cond.conditions)
270 272 end
271 273
272 274 if @scope.include?('documents')
273 275 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_documents, :project => @project, :with_subprojects => @with_subprojects))
274 276 cond.add(["#{Document.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
275 277 @events += Document.find(:all, :include => :project, :conditions => cond.conditions)
276 278
277 279 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_documents, :project => @project, :with_subprojects => @with_subprojects))
278 280 cond.add(["#{Attachment.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
279 281 @events += Attachment.find(:all, :select => "#{Attachment.table_name}.*",
280 282 :joins => "LEFT JOIN #{Document.table_name} ON #{Attachment.table_name}.container_type='Document' AND #{Document.table_name}.id = #{Attachment.table_name}.container_id " +
281 283 "LEFT JOIN #{Project.table_name} ON #{Document.table_name}.project_id = #{Project.table_name}.id",
282 284 :conditions => cond.conditions)
283 285 end
284 286
285 287 if @scope.include?('wiki_pages')
286 288 select = "#{WikiContent.versioned_table_name}.updated_on, #{WikiContent.versioned_table_name}.comments, " +
287 289 "#{WikiContent.versioned_table_name}.#{WikiContent.version_column}, #{WikiPage.table_name}.title, " +
288 290 "#{WikiContent.versioned_table_name}.page_id, #{WikiContent.versioned_table_name}.author_id, " +
289 291 "#{WikiContent.versioned_table_name}.id"
290 292 joins = "LEFT JOIN #{WikiPage.table_name} ON #{WikiPage.table_name}.id = #{WikiContent.versioned_table_name}.page_id " +
291 293 "LEFT JOIN #{Wiki.table_name} ON #{Wiki.table_name}.id = #{WikiPage.table_name}.wiki_id " +
292 294 "LEFT JOIN #{Project.table_name} ON #{Project.table_name}.id = #{Wiki.table_name}.project_id"
293 295
294 296 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_wiki_pages, :project => @project, :with_subprojects => @with_subprojects))
295 297 cond.add(["#{WikiContent.versioned_table_name}.updated_on BETWEEN ? AND ?", @date_from, @date_to])
296 298 @events += WikiContent.versioned_class.find(:all, :select => select, :joins => joins, :conditions => cond.conditions)
297 299 end
298 300
299 301 if @scope.include?('changesets')
300 302 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_changesets, :project => @project, :with_subprojects => @with_subprojects))
301 303 cond.add(["#{Changeset.table_name}.committed_on BETWEEN ? AND ?", @date_from, @date_to])
302 304 @events += Changeset.find(:all, :include => {:repository => :project}, :conditions => cond.conditions)
303 305 end
304 306
305 307 if @scope.include?('messages')
306 308 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_messages, :project => @project, :with_subprojects => @with_subprojects))
307 309 cond.add(["#{Message.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
308 310 @events += Message.find(:all, :include => [{:board => :project}, :author], :conditions => cond.conditions)
309 311 end
310 312
311 313 @events_by_day = @events.group_by(&:event_date)
312 314
313 315 respond_to do |format|
314 316 format.html { render :layout => false if request.xhr? }
315 317 format.atom { render_feed(@events, :title => "#{@project || Setting.app_title}: #{l(:label_activity)}") }
316 318 end
317 319 end
318 320
319 321 def calendar
320 322 @trackers = @project.rolled_up_trackers
321 323 retrieve_selected_tracker_ids(@trackers)
322 324
323 325 if params[:year] and params[:year].to_i > 1900
324 326 @year = params[:year].to_i
325 327 if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13
326 328 @month = params[:month].to_i
327 329 end
328 330 end
329 331 @year ||= Date.today.year
330 332 @month ||= Date.today.month
331 333 @calendar = Redmine::Helpers::Calendar.new(Date.civil(@year, @month, 1), current_language, :month)
332 334 @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
333 335 events = []
334 336 @project.issues_with_subprojects(@with_subprojects) do
335 337 events += Issue.find(:all,
336 338 :include => [:tracker, :status, :assigned_to, :priority, :project],
337 339 :conditions => ["((start_date BETWEEN ? AND ?) OR (due_date BETWEEN ? AND ?)) AND #{Issue.table_name}.tracker_id IN (#{@selected_tracker_ids.join(',')})", @calendar.startdt, @calendar.enddt, @calendar.startdt, @calendar.enddt]
338 340 ) unless @selected_tracker_ids.empty?
339 341 end
340 342 events += @project.versions.find(:all, :conditions => ["effective_date BETWEEN ? AND ?", @calendar.startdt, @calendar.enddt])
341 343 @calendar.events = events
342 344
343 345 render :layout => false if request.xhr?
344 346 end
345 347
346 348 def gantt
347 349 @trackers = @project.rolled_up_trackers
348 350 retrieve_selected_tracker_ids(@trackers)
349 351
350 352 if params[:year] and params[:year].to_i >0
351 353 @year_from = params[:year].to_i
352 354 if params[:month] and params[:month].to_i >=1 and params[:month].to_i <= 12
353 355 @month_from = params[:month].to_i
354 356 else
355 357 @month_from = 1
356 358 end
357 359 else
358 360 @month_from ||= Date.today.month
359 361 @year_from ||= Date.today.year
360 362 end
361 363
362 364 zoom = (params[:zoom] || User.current.pref[:gantt_zoom]).to_i
363 365 @zoom = (zoom > 0 && zoom < 5) ? zoom : 2
364 366 months = (params[:months] || User.current.pref[:gantt_months]).to_i
365 367 @months = (months > 0 && months < 25) ? months : 6
366 368
367 369 # Save gantt paramters as user preference (zoom and months count)
368 370 if (User.current.logged? && (@zoom != User.current.pref[:gantt_zoom] || @months != User.current.pref[:gantt_months]))
369 371 User.current.pref[:gantt_zoom], User.current.pref[:gantt_months] = @zoom, @months
370 372 User.current.preference.save
371 373 end
372 374
373 375 @date_from = Date.civil(@year_from, @month_from, 1)
374 376 @date_to = (@date_from >> @months) - 1
375 377 @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
376 378
377 379 @events = []
378 380 @project.issues_with_subprojects(@with_subprojects) do
379 381 @events += Issue.find(:all,
380 382 :order => "start_date, due_date",
381 383 :include => [:tracker, :status, :assigned_to, :priority, :project],
382 384 :conditions => ["(((start_date>=? and start_date<=?) or (due_date>=? and due_date<=?) or (start_date<? and due_date>?)) and start_date is not null and due_date is not null and #{Issue.table_name}.tracker_id in (#{@selected_tracker_ids.join(',')}))", @date_from, @date_to, @date_from, @date_to, @date_from, @date_to]
383 385 ) unless @selected_tracker_ids.empty?
384 386 end
385 387 @events += @project.versions.find(:all, :conditions => ["effective_date BETWEEN ? AND ?", @date_from, @date_to])
386 388 @events.sort! {|x,y| x.start_date <=> y.start_date }
387 389
388 390 if params[:format]=='pdf'
389 391 @options_for_rfpdf ||= {}
390 392 @options_for_rfpdf[:file_name] = "#{@project.identifier}-gantt.pdf"
391 393 render :template => "projects/gantt.rfpdf", :layout => false
392 394 elsif params[:format]=='png' && respond_to?('gantt_image')
393 395 image = gantt_image(@events, @date_from, @months, @zoom)
394 396 image.format = 'PNG'
395 397 send_data(image.to_blob, :disposition => 'inline', :type => 'image/png', :filename => "#{@project.identifier}-gantt.png")
396 398 else
397 399 render :template => "projects/gantt.rhtml"
398 400 end
399 401 end
400 402
401 403 private
402 404 # Find project of id params[:id]
403 405 # if not found, redirect to project list
404 406 # Used as a before_filter
405 407 def find_project
406 408 @project = Project.find(params[:id])
407 409 rescue ActiveRecord::RecordNotFound
408 410 render_404
409 411 end
410 412
411 413 def find_optional_project
412 414 return true unless params[:id]
413 415 @project = Project.find(params[:id])
414 416 authorize
415 417 rescue ActiveRecord::RecordNotFound
416 418 render_404
417 419 end
418 420
419 421 def retrieve_selected_tracker_ids(selectable_trackers)
420 422 if ids = params[:tracker_ids]
421 423 @selected_tracker_ids = (ids.is_a? Array) ? ids.collect { |id| id.to_i.to_s } : ids.split('/').collect { |id| id.to_i.to_s }
422 424 else
423 425 @selected_tracker_ids = selectable_trackers.collect {|t| t.id.to_s }
424 426 end
425 427 end
426 428 end
@@ -1,241 +1,241
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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 TimelogController < ApplicationController
19 19 layout 'base'
20 20 menu_item :issues
21 21 before_filter :find_project, :authorize
22 22
23 23 verify :method => :post, :only => :destroy, :redirect_to => { :action => :details }
24 24
25 25 helper :sort
26 26 include SortHelper
27 27 helper :issues
28 28 include TimelogHelper
29 29
30 30 def report
31 31 @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
32 32 :klass => Project,
33 33 :label => :label_project},
34 34 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
35 35 :klass => Version,
36 36 :label => :label_version},
37 37 'category' => {:sql => "#{Issue.table_name}.category_id",
38 38 :klass => IssueCategory,
39 39 :label => :field_category},
40 40 'member' => {:sql => "#{TimeEntry.table_name}.user_id",
41 41 :klass => User,
42 42 :label => :label_member},
43 43 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
44 44 :klass => Tracker,
45 45 :label => :label_tracker},
46 46 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
47 47 :klass => Enumeration,
48 48 :label => :label_activity}
49 49 }
50 50
51 51 @criterias = params[:criterias] || []
52 52 @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
53 53 @criterias.uniq!
54 54 @criterias = @criterias[0,3]
55 55
56 56 @columns = (params[:period] && %w(year month week).include?(params[:period])) ? params[:period] : 'month'
57 57
58 58 if params[:date_from]
59 59 begin; @date_from = params[:date_from].to_date; rescue; end
60 60 end
61 61 if params[:date_to]
62 62 begin; @date_to = params[:date_to].to_date; rescue; end
63 63 end
64 64 @date_from ||= Date.civil(Date.today.year, 1, 1)
65 65 @date_to ||= (Date.civil(Date.today.year, Date.today.month, 1) >> 1) - 1
66 66
67 67 unless @criterias.empty?
68 68 sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
69 69 sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
70 70
71 71 sql = "SELECT #{sql_select}, tyear, tmonth, tweek, SUM(hours) AS hours"
72 72 sql << " FROM #{TimeEntry.table_name}"
73 73 sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
74 74 sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
75 sql << " WHERE (#{Project.table_name}.id = %s OR #{Project.table_name}.parent_id = %s)" % [@project.id, @project.id]
75 sql << " WHERE (%s)" % @project.project_condition(Setting.display_subprojects_issues?)
76 76 sql << " AND (%s)" % Project.allowed_to_condition(User.current, :view_time_entries)
77 77 sql << " AND spent_on BETWEEN '%s' AND '%s'" % [ActiveRecord::Base.connection.quoted_date(@date_from.to_time), ActiveRecord::Base.connection.quoted_date(@date_to.to_time)]
78 78 sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek"
79 79
80 80 @hours = ActiveRecord::Base.connection.select_all(sql)
81 81
82 82 @hours.each do |row|
83 83 case @columns
84 84 when 'year'
85 85 row['year'] = row['tyear']
86 86 when 'month'
87 87 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
88 88 when 'week'
89 89 row['week'] = "#{row['tyear']}-#{row['tweek']}"
90 90 end
91 91 end
92 92
93 93 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
94 94 end
95 95
96 96 @periods = []
97 97 date_from = @date_from
98 98 # 100 columns max
99 99 while date_from < @date_to && @periods.length < 100
100 100 case @columns
101 101 when 'year'
102 102 @periods << "#{date_from.year}"
103 103 date_from = date_from >> 12
104 104 when 'month'
105 105 @periods << "#{date_from.year}-#{date_from.month}"
106 106 date_from = date_from >> 1
107 107 when 'week'
108 108 @periods << "#{date_from.year}-#{date_from.cweek}"
109 109 date_from = date_from + 7
110 110 end
111 111 end
112 112
113 113 render :layout => false if request.xhr?
114 114 end
115 115
116 116 def details
117 117 sort_init 'spent_on', 'desc'
118 118 sort_update
119 119
120 120 @free_period = false
121 121 @from, @to = nil, nil
122 122
123 123 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
124 124 case params[:period].to_s
125 125 when 'today'
126 126 @from = @to = Date.today
127 127 when 'yesterday'
128 128 @from = @to = Date.today - 1
129 129 when 'current_week'
130 130 @from = Date.today - (Date.today.cwday - 1)%7
131 131 @to = @from + 6
132 132 when 'last_week'
133 133 @from = Date.today - 7 - (Date.today.cwday - 1)%7
134 134 @to = @from + 6
135 135 when '7_days'
136 136 @from = Date.today - 7
137 137 @to = Date.today
138 138 when 'current_month'
139 139 @from = Date.civil(Date.today.year, Date.today.month, 1)
140 140 @to = (@from >> 1) - 1
141 141 when 'last_month'
142 142 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
143 143 @to = (@from >> 1) - 1
144 144 when '30_days'
145 145 @from = Date.today - 30
146 146 @to = Date.today
147 147 when 'current_year'
148 148 @from = Date.civil(Date.today.year, 1, 1)
149 149 @to = Date.civil(Date.today.year, 12, 31)
150 150 end
151 151 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
152 152 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
153 153 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
154 154 @free_period = true
155 155 else
156 156 # default
157 157 end
158 158
159 159 @from, @to = @to, @from if @from && @to && @from > @to
160 160
161 161 cond = ARCondition.new
162 cond << (@issue.nil? ? ["(#{Project.table_name}.id = ? OR #{Project.table_name}.parent_id = ?)", @project.id, @project.id] :
162 cond << (@issue.nil? ? @project.project_condition(Setting.display_subprojects_issues?) :
163 163 ["#{TimeEntry.table_name}.issue_id = ?", @issue.id])
164 164
165 165 if @from
166 166 if @to
167 167 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
168 168 else
169 169 cond << ['spent_on >= ?', @from]
170 170 end
171 171 elsif @to
172 172 cond << ['spent_on <= ?', @to]
173 173 end
174 174
175 175 TimeEntry.visible_by(User.current) do
176 176 respond_to do |format|
177 177 format.html {
178 178 # Paginate results
179 179 @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions)
180 180 @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
181 181 @entries = TimeEntry.find(:all,
182 182 :include => [:project, :activity, :user, {:issue => :tracker}],
183 183 :conditions => cond.conditions,
184 184 :order => sort_clause,
185 185 :limit => @entry_pages.items_per_page,
186 186 :offset => @entry_pages.current.offset)
187 187 @total_hours = TimeEntry.sum(:hours, :include => :project, :conditions => cond.conditions).to_f
188 188 render :layout => !request.xhr?
189 189 }
190 190 format.csv {
191 191 # Export all entries
192 192 @entries = TimeEntry.find(:all,
193 193 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
194 194 :conditions => cond.conditions,
195 195 :order => sort_clause)
196 196 send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv')
197 197 }
198 198 end
199 199 end
200 200 end
201 201
202 202 def edit
203 203 render_403 and return if @time_entry && !@time_entry.editable_by?(User.current)
204 204 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
205 205 @time_entry.attributes = params[:time_entry]
206 206 if request.post? and @time_entry.save
207 207 flash[:notice] = l(:notice_successful_update)
208 208 redirect_to :action => 'details', :project_id => @time_entry.project
209 209 return
210 210 end
211 211 @activities = Enumeration::get_values('ACTI')
212 212 end
213 213
214 214 def destroy
215 215 render_404 and return unless @time_entry
216 216 render_403 and return unless @time_entry.editable_by?(User.current)
217 217 @time_entry.destroy
218 218 flash[:notice] = l(:notice_successful_delete)
219 219 redirect_to :back
220 220 rescue RedirectBackError
221 221 redirect_to :action => 'details', :project_id => @time_entry.project
222 222 end
223 223
224 224 private
225 225 def find_project
226 226 if params[:id]
227 227 @time_entry = TimeEntry.find(params[:id])
228 228 @project = @time_entry.project
229 229 elsif params[:issue_id]
230 230 @issue = Issue.find(params[:issue_id])
231 231 @project = @issue.project
232 232 elsif params[:project_id]
233 233 @project = Project.find(params[:project_id])
234 234 else
235 235 render_404
236 236 return false
237 237 end
238 238 rescue ActiveRecord::RecordNotFound
239 239 render_404
240 240 end
241 241 end
@@ -1,248 +1,254
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 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 Project < ActiveRecord::Base
19 19 # Project statuses
20 20 STATUS_ACTIVE = 1
21 21 STATUS_ARCHIVED = 9
22 22
23 23 has_many :members, :include => :user, :conditions => "#{User.table_name}.status=#{User::STATUS_ACTIVE}"
24 24 has_many :users, :through => :members
25 25 has_many :custom_values, :dependent => :delete_all, :as => :customized
26 26 has_many :enabled_modules, :dependent => :delete_all
27 27 has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position"
28 28 has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker]
29 29 has_many :issue_changes, :through => :issues, :source => :journals
30 30 has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
31 31 has_many :time_entries, :dependent => :delete_all
32 32 has_many :queries, :dependent => :delete_all
33 33 has_many :documents, :dependent => :destroy
34 34 has_many :news, :dependent => :delete_all, :include => :author
35 35 has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name"
36 36 has_many :boards, :order => "position ASC"
37 37 has_one :repository, :dependent => :destroy
38 38 has_many :changesets, :through => :repository
39 39 has_one :wiki, :dependent => :destroy
40 40 # Custom field for the project issues
41 41 has_and_belongs_to_many :custom_fields,
42 42 :class_name => 'IssueCustomField',
43 43 :order => "#{CustomField.table_name}.position",
44 44 :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
45 45 :association_foreign_key => 'custom_field_id'
46 46
47 47 acts_as_tree :order => "name", :counter_cache => true
48 48
49 49 acts_as_searchable :columns => ['name', 'description'], :project_key => 'id'
50 50 acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
51 51 :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}}
52 52
53 53 attr_protected :status, :enabled_module_names
54 54
55 55 validates_presence_of :name, :identifier
56 56 validates_uniqueness_of :name, :identifier
57 57 validates_associated :custom_values, :on => :update
58 58 validates_associated :repository, :wiki
59 59 validates_length_of :name, :maximum => 30
60 60 validates_length_of :homepage, :maximum => 60
61 61 validates_length_of :identifier, :in => 3..20
62 62 validates_format_of :identifier, :with => /^[a-z0-9\-]*$/
63 63
64 64 before_destroy :delete_all_members
65 65
66 66 def identifier=(identifier)
67 67 super unless identifier_frozen?
68 68 end
69 69
70 70 def identifier_frozen?
71 71 errors[:identifier].nil? && !(new_record? || identifier.blank?)
72 72 end
73 73
74 74 def issues_with_subprojects(include_subprojects=false)
75 75 conditions = nil
76 76 if include_subprojects && !active_children.empty?
77 77 ids = [id] + active_children.collect {|c| c.id}
78 78 conditions = ["#{Issue.table_name}.project_id IN (#{ids.join(',')})"]
79 79 end
80 80 conditions ||= ["#{Issue.table_name}.project_id = ?", id]
81 81 # Quick and dirty fix for Rails 2 compatibility
82 82 Issue.send(:with_scope, :find => { :conditions => conditions }) do
83 83 yield
84 84 end
85 85 end
86 86
87 87 # returns latest created projects
88 88 # non public projects will be returned only if user is a member of those
89 89 def self.latest(user=nil, count=5)
90 90 find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC")
91 91 end
92 92
93 93 def self.visible_by(user=nil)
94 94 if user && user.admin?
95 95 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
96 96 elsif user && user.memberships.any?
97 97 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND (#{Project.table_name}.is_public = #{connection.quoted_true} or #{Project.table_name}.id IN (#{user.memberships.collect{|m| m.project_id}.join(',')}))"
98 98 else
99 99 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}"
100 100 end
101 101 end
102 102
103 103 def self.allowed_to_condition(user, permission, options={})
104 104 statements = []
105 105 base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
106 106 if options[:project]
107 107 project_statement = "#{Project.table_name}.id = #{options[:project].id}"
108 108 project_statement << " OR #{Project.table_name}.parent_id = #{options[:project].id}" if options[:with_subprojects]
109 109 base_statement = "(#{project_statement}) AND (#{base_statement})"
110 110 end
111 111 if user.admin?
112 112 # no restriction
113 113 elsif user.logged?
114 114 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" if Role.non_member.allowed_to?(permission)
115 115 allowed_project_ids = user.memberships.select {|m| m.role.allowed_to?(permission)}.collect {|m| m.project_id}
116 116 statements << "#{Project.table_name}.id IN (#{allowed_project_ids.join(',')})" if allowed_project_ids.any?
117 117 elsif Role.anonymous.allowed_to?(permission)
118 118 # anonymous user allowed on public project
119 119 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}"
120 120 else
121 121 # anonymous user is not authorized
122 122 statements << "1=0"
123 123 end
124 124 statements.empty? ? base_statement : "((#{base_statement}) AND (#{statements.join(' OR ')}))"
125 125 end
126 126
127 def project_condition(with_subprojects)
128 cond = "#{Project.table_name}.id = #{id}"
129 cond = "(#{cond} OR #{Project.table_name}.parent_id = #{id})" if with_subprojects
130 cond
131 end
132
127 133 def self.find(*args)
128 134 if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/)
129 135 project = find_by_identifier(*args)
130 136 raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil?
131 137 project
132 138 else
133 139 super
134 140 end
135 141 end
136 142
137 143 def to_param
138 144 identifier
139 145 end
140 146
141 147 def active?
142 148 self.status == STATUS_ACTIVE
143 149 end
144 150
145 151 def archive
146 152 # Archive subprojects if any
147 153 children.each do |subproject|
148 154 subproject.archive
149 155 end
150 156 update_attribute :status, STATUS_ARCHIVED
151 157 end
152 158
153 159 def unarchive
154 160 return false if parent && !parent.active?
155 161 update_attribute :status, STATUS_ACTIVE
156 162 end
157 163
158 164 def active_children
159 165 children.select {|child| child.active?}
160 166 end
161 167
162 168 # Returns an array of the trackers used by the project and its sub projects
163 169 def rolled_up_trackers
164 170 @rolled_up_trackers ||=
165 171 Tracker.find(:all, :include => :projects,
166 172 :select => "DISTINCT #{Tracker.table_name}.*",
167 173 :conditions => ["#{Project.table_name}.id = ? OR #{Project.table_name}.parent_id = ?", id, id],
168 174 :order => "#{Tracker.table_name}.position")
169 175 end
170 176
171 177 # Deletes all project's members
172 178 def delete_all_members
173 179 Member.delete_all(['project_id = ?', id])
174 180 end
175 181
176 182 # Users issues can be assigned to
177 183 def assignable_users
178 184 members.select {|m| m.role.assignable?}.collect {|m| m.user}.sort
179 185 end
180 186
181 187 # Returns the mail adresses of users that should be always notified on project events
182 188 def recipients
183 189 members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail}
184 190 end
185 191
186 192 # Returns an array of all custom fields enabled for project issues
187 193 # (explictly associated custom fields and custom fields enabled for all projects)
188 194 def custom_fields_for_issues(tracker)
189 195 all_custom_fields.select {|c| tracker.custom_fields.include? c }
190 196 end
191 197
192 198 def all_custom_fields
193 199 @all_custom_fields ||= (IssueCustomField.for_all + custom_fields).uniq
194 200 end
195 201
196 202 def <=>(project)
197 203 name.downcase <=> project.name.downcase
198 204 end
199 205
200 206 def to_s
201 207 name
202 208 end
203 209
204 210 # Returns a short description of the projects (first lines)
205 211 def short_description(length = 255)
206 212 description.gsub(/^(.{#{length}}[^\n]*).*$/m, '\1').strip if description
207 213 end
208 214
209 215 def allows_to?(action)
210 216 if action.is_a? Hash
211 217 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
212 218 else
213 219 allowed_permissions.include? action
214 220 end
215 221 end
216 222
217 223 def module_enabled?(module_name)
218 224 module_name = module_name.to_s
219 225 enabled_modules.detect {|m| m.name == module_name}
220 226 end
221 227
222 228 def enabled_module_names=(module_names)
223 229 enabled_modules.clear
224 230 module_names = [] unless module_names && module_names.is_a?(Array)
225 231 module_names.each do |name|
226 232 enabled_modules << EnabledModule.new(:name => name.to_s)
227 233 end
228 234 end
229 235
230 236 protected
231 237 def validate
232 238 errors.add(parent_id, " must be a root project") if parent and parent.parent
233 239 errors.add_to_base("A project with subprojects can't be a subproject") if parent and children.size > 0
234 240 errors.add(:identifier, :activerecord_error_invalid) if !identifier.blank? && identifier.match(/^\d*$/)
235 241 end
236 242
237 243 private
238 244 def allowed_permissions
239 245 @allowed_permissions ||= begin
240 246 module_names = enabled_modules.collect {|m| m.name}
241 247 Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name}
242 248 end
243 249 end
244 250
245 251 def allowed_actions
246 252 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
247 253 end
248 254 end
General Comments 0
You need to be logged in to leave comments. Login now