##// END OF EJS Templates
Skip a few SQL queries for cross project gantt....
Jean-Philippe Lang -
r4401:9f18426ca7b1
parent child
Show More
@@ -1,1004 +1,1004
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2008 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 module Redmine
19 19 module Helpers
20 20 # Simple class to handle gantt chart data
21 21 class Gantt
22 22 include ERB::Util
23 23 include Redmine::I18n
24 24
25 25 # :nodoc:
26 26 # Some utility methods for the PDF export
27 27 class PDF
28 28 MaxCharactorsForSubject = 45
29 29 TotalWidth = 280
30 30 LeftPaneWidth = 100
31 31
32 32 def self.right_pane_width
33 33 TotalWidth - LeftPaneWidth
34 34 end
35 35 end
36 36
37 37 attr_reader :year_from, :month_from, :date_from, :date_to, :zoom, :months, :truncated, :max_rows
38 38 attr_accessor :query
39 39 attr_accessor :project
40 40 attr_accessor :view
41 41
42 42 def initialize(options={})
43 43 options = options.dup
44 44
45 45 if options[:year] && options[:year].to_i >0
46 46 @year_from = options[:year].to_i
47 47 if options[:month] && options[:month].to_i >=1 && options[:month].to_i <= 12
48 48 @month_from = options[:month].to_i
49 49 else
50 50 @month_from = 1
51 51 end
52 52 else
53 53 @month_from ||= Date.today.month
54 54 @year_from ||= Date.today.year
55 55 end
56 56
57 57 zoom = (options[:zoom] || User.current.pref[:gantt_zoom]).to_i
58 58 @zoom = (zoom > 0 && zoom < 5) ? zoom : 2
59 59 months = (options[:months] || User.current.pref[:gantt_months]).to_i
60 60 @months = (months > 0 && months < 25) ? months : 6
61 61
62 62 # Save gantt parameters as user preference (zoom and months count)
63 63 if (User.current.logged? && (@zoom != User.current.pref[:gantt_zoom] || @months != User.current.pref[:gantt_months]))
64 64 User.current.pref[:gantt_zoom], User.current.pref[:gantt_months] = @zoom, @months
65 65 User.current.preference.save
66 66 end
67 67
68 68 @date_from = Date.civil(@year_from, @month_from, 1)
69 69 @date_to = (@date_from >> @months) - 1
70 70
71 71 @subjects = ''
72 72 @lines = ''
73 73 @number_of_rows = nil
74 74
75 75 @truncated = false
76 76 if options.has_key?(:max_rows)
77 77 @max_rows = options[:max_rows]
78 78 else
79 79 @max_rows = Setting.gantt_items_limit.blank? ? nil : Setting.gantt_items_limit.to_i
80 80 end
81 81 end
82 82
83 83 def common_params
84 84 { :controller => 'gantts', :action => 'show', :project_id => @project }
85 85 end
86 86
87 87 def params
88 88 common_params.merge({ :zoom => zoom, :year => year_from, :month => month_from, :months => months })
89 89 end
90 90
91 91 def params_previous
92 92 common_params.merge({:year => (date_from << months).year, :month => (date_from << months).month, :zoom => zoom, :months => months })
93 93 end
94 94
95 95 def params_next
96 96 common_params.merge({:year => (date_from >> months).year, :month => (date_from >> months).month, :zoom => zoom, :months => months })
97 97 end
98 98
99 99 ### Extracted from the HTML view/helpers
100 100 # Returns the number of rows that will be rendered on the Gantt chart
101 101 def number_of_rows
102 102 return @number_of_rows if @number_of_rows
103 103
104 104 rows = if @project
105 105 number_of_rows_on_project(@project)
106 106 else
107 107 Project.roots.visible.has_module('issue_tracking').inject(0) do |total, project|
108 108 total += number_of_rows_on_project(project)
109 109 end
110 110 end
111 111
112 112 rows > @max_rows ? @max_rows : rows
113 113 end
114 114
115 115 # Returns the number of rows that will be used to list a project on
116 116 # the Gantt chart. This will recurse for each subproject.
117 117 def number_of_rows_on_project(project)
118 118 # Remove the project requirement for Versions because it will
119 119 # restrict issues to only be on the current project. This
120 120 # ends up missing issues which are assigned to shared versions.
121 121 @query.project = nil if @query.project
122 122
123 123 # One Root project
124 124 count = 1
125 125 # Issues without a Version
126 126 count += project.issues.for_gantt.without_version.with_query(@query).count
127 127
128 128 # Versions
129 129 count += project.versions.count
130 130
131 131 # Issues on the Versions
132 132 project.versions.each do |version|
133 133 count += version.fixed_issues.for_gantt.with_query(@query).count
134 134 end
135 135
136 136 # Subprojects
137 137 project.children.visible.has_module('issue_tracking').each do |subproject|
138 138 count += number_of_rows_on_project(subproject)
139 139 end
140 140
141 141 count
142 142 end
143 143
144 144 # Renders the subjects of the Gantt chart, the left side.
145 145 def subjects(options={})
146 146 render(options.merge(:only => :subjects)) unless @subjects_rendered
147 147 @subjects
148 148 end
149 149
150 150 # Renders the lines of the Gantt chart, the right side
151 151 def lines(options={})
152 152 render(options.merge(:only => :lines)) unless @lines_rendered
153 153 @lines
154 154 end
155 155
156 156 def render(options={})
157 157 options = {:indent => 4, :render => :subject, :format => :html}.merge(options)
158 158
159 159 @subjects = '' unless options[:only] == :lines
160 160 @lines = '' unless options[:only] == :subjects
161 161 @number_of_rows = 0
162 162
163 163 if @project
164 164 render_project(@project, options)
165 165 else
166 166 Project.roots.visible.has_module('issue_tracking').each do |project|
167 167 render_project(project, options)
168 168 break if abort?
169 169 end
170 170 end
171 171
172 172 @subjects_rendered = true unless options[:only] == :lines
173 173 @lines_rendered = true unless options[:only] == :subjects
174 174
175 175 render_end(options)
176 176 end
177 177
178 178 def render_project(project, options={})
179 179 options[:top] = 0 unless options.key? :top
180 180 options[:indent_increment] = 20 unless options.key? :indent_increment
181 181 options[:top_increment] = 20 unless options.key? :top_increment
182 182
183 183 subject_for_project(project, options) unless options[:only] == :lines
184 184 line_for_project(project, options) unless options[:only] == :subjects
185 185
186 186 options[:top] += options[:top_increment]
187 187 options[:indent] += options[:indent_increment]
188 188 @number_of_rows += 1
189 189 return if abort?
190 190
191 191 # Second, Issues without a version
192 192 issues = project.issues.for_gantt.without_version.with_query(@query).all(:limit => current_limit)
193 193 sort_issues!(issues)
194 194 if issues
195 195 render_issues(issues, options)
196 196 return if abort?
197 197 end
198 198
199 199 # Third, Versions
200 200 project.versions.sort.each do |version|
201 201 render_version(version, options)
202 202 return if abort?
203 203 end
204 204
205 205 # Fourth, subprojects
206 206 project.children.visible.has_module('issue_tracking').each do |project|
207 207 render_project(project, options)
208 208 return if abort?
209 end
209 end unless project.leaf?
210 210
211 211 # Remove indent to hit the next sibling
212 212 options[:indent] -= options[:indent_increment]
213 213 end
214 214
215 215 def render_issues(issues, options={})
216 216 issues.each do |i|
217 217 subject_for_issue(i, options) unless options[:only] == :lines
218 218 line_for_issue(i, options) unless options[:only] == :subjects
219 219
220 220 options[:top] += options[:top_increment]
221 221 @number_of_rows += 1
222 222 return if abort?
223 223 end
224 224 end
225 225
226 226 def render_version(version, options={})
227 227 # Version header
228 228 subject_for_version(version, options) unless options[:only] == :lines
229 229 line_for_version(version, options) unless options[:only] == :subjects
230 230
231 231 options[:top] += options[:top_increment]
232 232 @number_of_rows += 1
233 233 return if abort?
234 234
235 235 # Remove the project requirement for Versions because it will
236 236 # restrict issues to only be on the current project. This
237 237 # ends up missing issues which are assigned to shared versions.
238 238 @query.project = nil if @query.project
239 239
240 240 issues = version.fixed_issues.for_gantt.with_query(@query).all(:limit => current_limit)
241 241 if issues
242 242 sort_issues!(issues)
243 243 # Indent issues
244 244 options[:indent] += options[:indent_increment]
245 245 render_issues(issues, options)
246 246 options[:indent] -= options[:indent_increment]
247 247 end
248 248 end
249 249
250 250 def render_end(options={})
251 251 case options[:format]
252 252 when :pdf
253 253 options[:pdf].Line(15, options[:top], PDF::TotalWidth, options[:top])
254 254 end
255 255 end
256 256
257 257 def subject_for_project(project, options)
258 258 case options[:format]
259 259 when :html
260 260 output = ''
261 261
262 262 output << "<div class='project-name' style='position: absolute;line-height:1.2em;height:16px;top:#{options[:top]}px;left:#{options[:indent]}px;overflow:hidden;'><small> "
263 263 if project.is_a? Project
264 264 output << "<span class='icon icon-projects #{project.overdue? ? 'project-overdue' : ''}'>"
265 265 output << view.link_to_project(project)
266 266 output << '</span>'
267 267 else
268 268 ActiveRecord::Base.logger.debug "Gantt#subject_for_project was not given a project"
269 269 ''
270 270 end
271 271 output << "</small></div>"
272 272 @subjects << output
273 273 output
274 274 when :image
275 275
276 276 options[:image].fill('black')
277 277 options[:image].stroke('transparent')
278 278 options[:image].stroke_width(1)
279 279 options[:image].text(options[:indent], options[:top] + 2, project.name)
280 280 when :pdf
281 281 pdf_new_page?(options)
282 282 options[:pdf].SetY(options[:top])
283 283 options[:pdf].SetX(15)
284 284
285 285 char_limit = PDF::MaxCharactorsForSubject - options[:indent]
286 286 options[:pdf].Cell(options[:subject_width]-15, 5, (" " * options[:indent]) +"#{project.name}".sub(/^(.{#{char_limit}}[^\s]*\s).*$/, '\1 (...)'), "LR")
287 287
288 288 options[:pdf].SetY(options[:top])
289 289 options[:pdf].SetX(options[:subject_width])
290 290 options[:pdf].Cell(options[:g_width], 5, "", "LR")
291 291 end
292 292 end
293 293
294 294 def line_for_project(project, options)
295 295 # Skip versions that don't have a start_date or due date
296 296 if project.is_a?(Project) && project.start_date && project.due_date
297 297 options[:zoom] ||= 1
298 298 options[:g_width] ||= (self.date_to - self.date_from + 1) * options[:zoom]
299 299
300 300
301 301 case options[:format]
302 302 when :html
303 303 output = ''
304 304 i_left = ((project.start_date - self.date_from)*options[:zoom]).floor
305 305
306 306 start_date = project.start_date
307 307 start_date ||= self.date_from
308 308 start_left = ((start_date - self.date_from)*options[:zoom]).floor
309 309
310 310 i_end_date = ((project.due_date <= self.date_to) ? project.due_date : self.date_to )
311 311 i_done_date = start_date + ((project.due_date - start_date+1)* project.completed_percent(:include_subprojects => true)/100).floor
312 312 i_done_date = (i_done_date <= self.date_from ? self.date_from : i_done_date )
313 313 i_done_date = (i_done_date >= self.date_to ? self.date_to : i_done_date )
314 314
315 315 i_late_date = [i_end_date, Date.today].min if start_date < Date.today
316 316 i_end = ((i_end_date - self.date_from) * options[:zoom]).floor
317 317
318 318 i_width = (i_end - i_left + 1).floor - 2 # total width of the issue (- 2 for left and right borders)
319 319 d_width = ((i_done_date - start_date)*options[:zoom]).floor - 2 # done width
320 320 l_width = i_late_date ? ((i_late_date - start_date+1)*options[:zoom]).floor - 2 : 0 # delay width
321 321
322 322 # Bar graphic
323 323
324 324 # Make sure that negative i_left and i_width don't
325 325 # overflow the subject
326 326 if i_end > 0 && i_left <= options[:g_width]
327 327 output << "<div style='top:#{ options[:top] }px;left:#{ start_left }px;width:#{ i_width }px;' class='task project_todo'>&nbsp;</div>"
328 328 end
329 329
330 330 if l_width > 0 && i_left <= options[:g_width]
331 331 output << "<div style='top:#{ options[:top] }px;left:#{ start_left }px;width:#{ l_width }px;' class='task project_late'>&nbsp;</div>"
332 332 end
333 333 if d_width > 0 && i_left <= options[:g_width]
334 334 output<< "<div style='top:#{ options[:top] }px;left:#{ start_left }px;width:#{ d_width }px;' class='task project_done'>&nbsp;</div>"
335 335 end
336 336
337 337
338 338 # Starting diamond
339 339 if start_left <= options[:g_width] && start_left > 0
340 340 output << "<div style='top:#{ options[:top] }px;left:#{ start_left }px;width:15px;' class='task project-line starting'>&nbsp;</div>"
341 341 output << "<div style='top:#{ options[:top] }px;left:#{ start_left + 12 }px;' class='task label'>"
342 342 output << "</div>"
343 343 end
344 344
345 345 # Ending diamond
346 346 # Don't show items too far ahead
347 347 if i_end <= options[:g_width] && i_end > 0
348 348 output << "<div style='top:#{ options[:top] }px;left:#{ i_end }px;width:15px;' class='task project-line ending'>&nbsp;</div>"
349 349 end
350 350
351 351 # DIsplay the Project name and %
352 352 if i_end <= options[:g_width]
353 353 # Display the status even if it's floated off to the left
354 354 status_px = i_end + 12 # 12px for the diamond
355 355 status_px = 0 if status_px <= 0
356 356
357 357 output << "<div style='top:#{ options[:top] }px;left:#{ status_px }px;' class='task label project-name'>"
358 358 output << "<strong>#{h project } #{h project.completed_percent(:include_subprojects => true).to_i.to_s}%</strong>"
359 359 output << "</div>"
360 360 end
361 361 @lines << output
362 362 output
363 363 when :image
364 364 options[:image].stroke('transparent')
365 365 i_left = options[:subject_width] + ((project.due_date - self.date_from)*options[:zoom]).floor
366 366
367 367 # Make sure negative i_left doesn't overflow the subject
368 368 if i_left > options[:subject_width]
369 369 options[:image].fill('blue')
370 370 options[:image].rectangle(i_left, options[:top], i_left + 6, options[:top] - 6)
371 371 options[:image].fill('black')
372 372 options[:image].text(i_left + 11, options[:top] + 1, project.name)
373 373 end
374 374 when :pdf
375 375 options[:pdf].SetY(options[:top]+1.5)
376 376 i_left = ((project.due_date - @date_from)*options[:zoom])
377 377
378 378 # Make sure negative i_left doesn't overflow the subject
379 379 if i_left > 0
380 380 options[:pdf].SetX(options[:subject_width] + i_left)
381 381 options[:pdf].SetFillColor(50,50,200)
382 382 options[:pdf].Cell(2, 2, "", 0, 0, "", 1)
383 383
384 384 options[:pdf].SetY(options[:top]+1.5)
385 385 options[:pdf].SetX(options[:subject_width] + i_left + 3)
386 386 options[:pdf].Cell(30, 2, "#{project.name}")
387 387 end
388 388 end
389 389 else
390 390 ActiveRecord::Base.logger.debug "Gantt#line_for_project was not given a project with a start_date"
391 391 ''
392 392 end
393 393 end
394 394
395 395 def subject_for_version(version, options)
396 396 case options[:format]
397 397 when :html
398 398 output = ''
399 399 output << "<div class='version-name' style='position: absolute;line-height:1.2em;height:16px;top:#{options[:top]}px;left:#{options[:indent]}px;overflow:hidden;'><small> "
400 400 if version.is_a? Version
401 401 output << "<span class='icon icon-package #{version.behind_schedule? ? 'version-behind-schedule' : ''} #{version.overdue? ? 'version-overdue' : ''}'>"
402 402 output << view.link_to_version(version)
403 403 output << '</span>'
404 404 else
405 405 ActiveRecord::Base.logger.debug "Gantt#subject_for_version was not given a version"
406 406 ''
407 407 end
408 408 output << "</small></div>"
409 409 @subjects << output
410 410 output
411 411 when :image
412 412 options[:image].fill('black')
413 413 options[:image].stroke('transparent')
414 414 options[:image].stroke_width(1)
415 415 options[:image].text(options[:indent], options[:top] + 2, version.to_s_with_project)
416 416 when :pdf
417 417 pdf_new_page?(options)
418 418 options[:pdf].SetY(options[:top])
419 419 options[:pdf].SetX(15)
420 420
421 421 char_limit = PDF::MaxCharactorsForSubject - options[:indent]
422 422 options[:pdf].Cell(options[:subject_width]-15, 5, (" " * options[:indent]) +"#{version.to_s_with_project}".sub(/^(.{#{char_limit}}[^\s]*\s).*$/, '\1 (...)'), "LR")
423 423
424 424 options[:pdf].SetY(options[:top])
425 425 options[:pdf].SetX(options[:subject_width])
426 426 options[:pdf].Cell(options[:g_width], 5, "", "LR")
427 427 end
428 428 end
429 429
430 430 def line_for_version(version, options)
431 431 # Skip versions that don't have a start_date
432 432 if version.is_a?(Version) && version.start_date && version.due_date
433 433 options[:zoom] ||= 1
434 434 options[:g_width] ||= (self.date_to - self.date_from + 1) * options[:zoom]
435 435
436 436 case options[:format]
437 437 when :html
438 438 output = ''
439 439 i_left = ((version.start_date - self.date_from)*options[:zoom]).floor
440 440 # TODO: or version.fixed_issues.collect(&:start_date).min
441 441 start_date = version.fixed_issues.minimum('start_date') if version.fixed_issues.present?
442 442 start_date ||= self.date_from
443 443 start_left = ((start_date - self.date_from)*options[:zoom]).floor
444 444
445 445 i_end_date = ((version.due_date <= self.date_to) ? version.due_date : self.date_to )
446 446 i_done_date = start_date + ((version.due_date - start_date+1)* version.completed_pourcent/100).floor
447 447 i_done_date = (i_done_date <= self.date_from ? self.date_from : i_done_date )
448 448 i_done_date = (i_done_date >= self.date_to ? self.date_to : i_done_date )
449 449
450 450 i_late_date = [i_end_date, Date.today].min if start_date < Date.today
451 451
452 452 i_width = (i_left - start_left + 1).floor - 2 # total width of the issue (- 2 for left and right borders)
453 453 d_width = ((i_done_date - start_date)*options[:zoom]).floor - 2 # done width
454 454 l_width = i_late_date ? ((i_late_date - start_date+1)*options[:zoom]).floor - 2 : 0 # delay width
455 455
456 456 i_end = ((i_end_date - self.date_from) * options[:zoom]).floor # Ending pixel
457 457
458 458 # Bar graphic
459 459
460 460 # Make sure that negative i_left and i_width don't
461 461 # overflow the subject
462 462 if i_width > 0 && i_left <= options[:g_width]
463 463 output << "<div style='top:#{ options[:top] }px;left:#{ start_left }px;width:#{ i_width }px;' class='task milestone_todo'>&nbsp;</div>"
464 464 end
465 465 if l_width > 0 && i_left <= options[:g_width]
466 466 output << "<div style='top:#{ options[:top] }px;left:#{ start_left }px;width:#{ l_width }px;' class='task milestone_late'>&nbsp;</div>"
467 467 end
468 468 if d_width > 0 && i_left <= options[:g_width]
469 469 output<< "<div style='top:#{ options[:top] }px;left:#{ start_left }px;width:#{ d_width }px;' class='task milestone_done'>&nbsp;</div>"
470 470 end
471 471
472 472
473 473 # Starting diamond
474 474 if start_left <= options[:g_width] && start_left > 0
475 475 output << "<div style='top:#{ options[:top] }px;left:#{ start_left }px;width:15px;' class='task milestone starting'>&nbsp;</div>"
476 476 output << "<div style='top:#{ options[:top] }px;left:#{ start_left + 12 }px;background:#fff;' class='task'>"
477 477 output << "</div>"
478 478 end
479 479
480 480 # Ending diamond
481 481 # Don't show items too far ahead
482 482 if i_left <= options[:g_width] && i_end > 0
483 483 output << "<div style='top:#{ options[:top] }px;left:#{ i_end }px;width:15px;' class='task milestone ending'>&nbsp;</div>"
484 484 end
485 485
486 486 # Display the Version name and %
487 487 if i_end <= options[:g_width]
488 488 # Display the status even if it's floated off to the left
489 489 status_px = i_end + 12 # 12px for the diamond
490 490 status_px = 0 if status_px <= 0
491 491
492 492 output << "<div style='top:#{ options[:top] }px;left:#{ status_px }px;' class='task label version-name'>"
493 493 output << h("#{version.project} -") unless @project && @project == version.project
494 494 output << "<strong>#{h version } #{h version.completed_pourcent.to_i.to_s}%</strong>"
495 495 output << "</div>"
496 496 end
497 497 @lines << output
498 498 output
499 499 when :image
500 500 options[:image].stroke('transparent')
501 501 i_left = options[:subject_width] + ((version.start_date - @date_from)*options[:zoom]).floor
502 502
503 503 # Make sure negative i_left doesn't overflow the subject
504 504 if i_left > options[:subject_width]
505 505 options[:image].fill('green')
506 506 options[:image].rectangle(i_left, options[:top], i_left + 6, options[:top] - 6)
507 507 options[:image].fill('black')
508 508 options[:image].text(i_left + 11, options[:top] + 1, version.name)
509 509 end
510 510 when :pdf
511 511 options[:pdf].SetY(options[:top]+1.5)
512 512 i_left = ((version.start_date - @date_from)*options[:zoom])
513 513
514 514 # Make sure negative i_left doesn't overflow the subject
515 515 if i_left > 0
516 516 options[:pdf].SetX(options[:subject_width] + i_left)
517 517 options[:pdf].SetFillColor(50,200,50)
518 518 options[:pdf].Cell(2, 2, "", 0, 0, "", 1)
519 519
520 520 options[:pdf].SetY(options[:top]+1.5)
521 521 options[:pdf].SetX(options[:subject_width] + i_left + 3)
522 522 options[:pdf].Cell(30, 2, "#{version.name}")
523 523 end
524 524 end
525 525 else
526 526 ActiveRecord::Base.logger.debug "Gantt#line_for_version was not given a version with a start_date"
527 527 ''
528 528 end
529 529 end
530 530
531 531 def subject_for_issue(issue, options)
532 532 case options[:format]
533 533 when :html
534 534 output = ''
535 535 output << "<div class='tooltip'>"
536 536 output << "<div class='issue-subject' style='position: absolute;line-height:1.2em;height:16px;top:#{options[:top]}px;left:#{options[:indent]}px;overflow:hidden;'><small> "
537 537 if issue.is_a? Issue
538 538 css_classes = []
539 539 css_classes << 'issue-overdue' if issue.overdue?
540 540 css_classes << 'issue-behind-schedule' if issue.behind_schedule?
541 541 css_classes << 'icon icon-issue' unless Setting.gravatar_enabled? && issue.assigned_to
542 542
543 543 if issue.assigned_to.present?
544 544 assigned_string = l(:field_assigned_to) + ": " + issue.assigned_to.name
545 545 output << view.avatar(issue.assigned_to, :class => 'gravatar icon-gravatar', :size => 10, :title => assigned_string)
546 546 end
547 547 output << "<span class='#{css_classes.join(' ')}'>"
548 548 output << view.link_to_issue(issue)
549 549 output << '</span>'
550 550 else
551 551 ActiveRecord::Base.logger.debug "Gantt#subject_for_issue was not given an issue"
552 552 ''
553 553 end
554 554 output << "</small></div>"
555 555
556 556 # Tooltip
557 557 if issue.is_a? Issue
558 558 output << "<span class='tip' style='position: absolute;top:#{ options[:top].to_i + 16 }px;left:#{ options[:indent].to_i + 20 }px;'>"
559 559 output << view.render_issue_tooltip(issue)
560 560 output << "</span>"
561 561 end
562 562
563 563 output << "</div>"
564 564 @subjects << output
565 565 output
566 566 when :image
567 567 options[:image].fill('black')
568 568 options[:image].stroke('transparent')
569 569 options[:image].stroke_width(1)
570 570 options[:image].text(options[:indent], options[:top] + 2, issue.subject)
571 571 when :pdf
572 572 pdf_new_page?(options)
573 573 options[:pdf].SetY(options[:top])
574 574 options[:pdf].SetX(15)
575 575
576 576 char_limit = PDF::MaxCharactorsForSubject - options[:indent]
577 577 options[:pdf].Cell(options[:subject_width]-15, 5, (" " * options[:indent]) +"#{issue.tracker} #{issue.id}: #{issue.subject}".sub(/^(.{#{char_limit}}[^\s]*\s).*$/, '\1 (...)'), "LR")
578 578
579 579 options[:pdf].SetY(options[:top])
580 580 options[:pdf].SetX(options[:subject_width])
581 581 options[:pdf].Cell(options[:g_width], 5, "", "LR")
582 582 end
583 583 end
584 584
585 585 def line_for_issue(issue, options)
586 586 # Skip issues that don't have a due_before (due_date or version's due_date)
587 587 if issue.is_a?(Issue) && issue.due_before
588 588 case options[:format]
589 589 when :html
590 590 output = ''
591 591 # Handle nil start_dates, rare but can happen.
592 592 i_start_date = if issue.start_date && issue.start_date >= self.date_from
593 593 issue.start_date
594 594 else
595 595 self.date_from
596 596 end
597 597
598 598 i_end_date = ((issue.due_before && issue.due_before <= self.date_to) ? issue.due_before : self.date_to )
599 599 i_done_date = i_start_date + ((issue.due_before - i_start_date+1)*issue.done_ratio/100).floor
600 600 i_done_date = (i_done_date <= self.date_from ? self.date_from : i_done_date )
601 601 i_done_date = (i_done_date >= self.date_to ? self.date_to : i_done_date )
602 602
603 603 i_late_date = [i_end_date, Date.today].min if i_start_date < Date.today
604 604
605 605 i_left = ((i_start_date - self.date_from)*options[:zoom]).floor
606 606 i_width = ((i_end_date - i_start_date + 1)*options[:zoom]).floor - 2 # total width of the issue (- 2 for left and right borders)
607 607 d_width = ((i_done_date - i_start_date)*options[:zoom]).floor - 2 # done width
608 608 l_width = i_late_date ? ((i_late_date - i_start_date+1)*options[:zoom]).floor - 2 : 0 # delay width
609 609 css = "task " + (issue.leaf? ? 'leaf' : 'parent')
610 610
611 611 # Make sure that negative i_left and i_width don't
612 612 # overflow the subject
613 613 if i_width > 0
614 614 output << "<div style='top:#{ options[:top] }px;left:#{ i_left }px;width:#{ i_width }px;' class='#{css} task_todo'>&nbsp;</div>"
615 615 end
616 616 if l_width > 0
617 617 output << "<div style='top:#{ options[:top] }px;left:#{ i_left }px;width:#{ l_width }px;' class='#{css} task_late'>&nbsp;</div>"
618 618 end
619 619 if d_width > 0
620 620 output<< "<div style='top:#{ options[:top] }px;left:#{ i_left }px;width:#{ d_width }px;' class='#{css} task_done'>&nbsp;</div>"
621 621 end
622 622
623 623 # Display the status even if it's floated off to the left
624 624 status_px = i_left + i_width + 5
625 625 status_px = 5 if status_px <= 0
626 626
627 627 output << "<div style='top:#{ options[:top] }px;left:#{ status_px }px;' class='#{css} label issue-name'>"
628 628 output << issue.status.name
629 629 output << ' '
630 630 output << (issue.done_ratio).to_i.to_s
631 631 output << "%"
632 632 output << "</div>"
633 633
634 634 output << "<div class='tooltip' style='position: absolute;top:#{ options[:top] }px;left:#{ i_left }px;width:#{ i_width }px;height:12px;'>"
635 635 output << '<span class="tip">'
636 636 output << view.render_issue_tooltip(issue)
637 637 output << "</span></div>"
638 638 @lines << output
639 639 output
640 640
641 641 when :image
642 642 # Handle nil start_dates, rare but can happen.
643 643 i_start_date = if issue.start_date && issue.start_date >= @date_from
644 644 issue.start_date
645 645 else
646 646 @date_from
647 647 end
648 648
649 649 i_end_date = (issue.due_before <= date_to ? issue.due_before : date_to )
650 650 i_done_date = i_start_date + ((issue.due_before - i_start_date+1)*issue.done_ratio/100).floor
651 651 i_done_date = (i_done_date <= @date_from ? @date_from : i_done_date )
652 652 i_done_date = (i_done_date >= date_to ? date_to : i_done_date )
653 653 i_late_date = [i_end_date, Date.today].min if i_start_date < Date.today
654 654
655 655 i_left = options[:subject_width] + ((i_start_date - @date_from)*options[:zoom]).floor
656 656 i_width = ((i_end_date - i_start_date + 1)*options[:zoom]).floor # total width of the issue
657 657 d_width = ((i_done_date - i_start_date)*options[:zoom]).floor # done width
658 658 l_width = i_late_date ? ((i_late_date - i_start_date+1)*options[:zoom]).floor : 0 # delay width
659 659
660 660
661 661 # Make sure that negative i_left and i_width don't
662 662 # overflow the subject
663 663 if i_width > 0
664 664 options[:image].fill('grey')
665 665 options[:image].rectangle(i_left, options[:top], i_left + i_width, options[:top] - 6)
666 666 options[:image].fill('red')
667 667 options[:image].rectangle(i_left, options[:top], i_left + l_width, options[:top] - 6) if l_width > 0
668 668 options[:image].fill('blue')
669 669 options[:image].rectangle(i_left, options[:top], i_left + d_width, options[:top] - 6) if d_width > 0
670 670 end
671 671
672 672 # Show the status and % done next to the subject if it overflows
673 673 options[:image].fill('black')
674 674 if i_width > 0
675 675 options[:image].text(i_left + i_width + 5,options[:top] + 1, "#{issue.status.name} #{issue.done_ratio}%")
676 676 else
677 677 options[:image].text(options[:subject_width] + 5,options[:top] + 1, "#{issue.status.name} #{issue.done_ratio}%")
678 678 end
679 679
680 680 when :pdf
681 681 options[:pdf].SetY(options[:top]+1.5)
682 682 # Handle nil start_dates, rare but can happen.
683 683 i_start_date = if issue.start_date && issue.start_date >= @date_from
684 684 issue.start_date
685 685 else
686 686 @date_from
687 687 end
688 688
689 689 i_end_date = (issue.due_before <= @date_to ? issue.due_before : @date_to )
690 690
691 691 i_done_date = i_start_date + ((issue.due_before - i_start_date+1)*issue.done_ratio/100).floor
692 692 i_done_date = (i_done_date <= @date_from ? @date_from : i_done_date )
693 693 i_done_date = (i_done_date >= @date_to ? @date_to : i_done_date )
694 694
695 695 i_late_date = [i_end_date, Date.today].min if i_start_date < Date.today
696 696
697 697 i_left = ((i_start_date - @date_from)*options[:zoom])
698 698 i_width = ((i_end_date - i_start_date + 1)*options[:zoom])
699 699 d_width = ((i_done_date - i_start_date)*options[:zoom])
700 700 l_width = ((i_late_date - i_start_date+1)*options[:zoom]) if i_late_date
701 701 l_width ||= 0
702 702
703 703 # Make sure that negative i_left and i_width don't
704 704 # overflow the subject
705 705 if i_width > 0
706 706 options[:pdf].SetX(options[:subject_width] + i_left)
707 707 options[:pdf].SetFillColor(200,200,200)
708 708 options[:pdf].Cell(i_width, 2, "", 0, 0, "", 1)
709 709 end
710 710
711 711 if l_width > 0
712 712 options[:pdf].SetY(options[:top]+1.5)
713 713 options[:pdf].SetX(options[:subject_width] + i_left)
714 714 options[:pdf].SetFillColor(255,100,100)
715 715 options[:pdf].Cell(l_width, 2, "", 0, 0, "", 1)
716 716 end
717 717 if d_width > 0
718 718 options[:pdf].SetY(options[:top]+1.5)
719 719 options[:pdf].SetX(options[:subject_width] + i_left)
720 720 options[:pdf].SetFillColor(100,100,255)
721 721 options[:pdf].Cell(d_width, 2, "", 0, 0, "", 1)
722 722 end
723 723
724 724 options[:pdf].SetY(options[:top]+1.5)
725 725
726 726 # Make sure that negative i_left and i_width don't
727 727 # overflow the subject
728 728 if (i_left + i_width) >= 0
729 729 options[:pdf].SetX(options[:subject_width] + i_left + i_width)
730 730 else
731 731 options[:pdf].SetX(options[:subject_width])
732 732 end
733 733 options[:pdf].Cell(30, 2, "#{issue.status} #{issue.done_ratio}%")
734 734 end
735 735 else
736 736 ActiveRecord::Base.logger.debug "GanttHelper#line_for_issue was not given an issue with a due_before"
737 737 ''
738 738 end
739 739 end
740 740
741 741 # Generates a gantt image
742 742 # Only defined if RMagick is avalaible
743 743 def to_image(format='PNG')
744 744 date_to = (@date_from >> @months)-1
745 745 show_weeks = @zoom > 1
746 746 show_days = @zoom > 2
747 747
748 748 subject_width = 400
749 749 header_heigth = 18
750 750 # width of one day in pixels
751 751 zoom = @zoom*2
752 752 g_width = (@date_to - @date_from + 1)*zoom
753 753 g_height = 20 * number_of_rows + 30
754 754 headers_heigth = (show_weeks ? 2*header_heigth : header_heigth)
755 755 height = g_height + headers_heigth
756 756
757 757 imgl = Magick::ImageList.new
758 758 imgl.new_image(subject_width+g_width+1, height)
759 759 gc = Magick::Draw.new
760 760
761 761 # Subjects
762 762 subjects(:image => gc, :top => (headers_heigth + 20), :indent => 4, :format => :image)
763 763
764 764 # Months headers
765 765 month_f = @date_from
766 766 left = subject_width
767 767 @months.times do
768 768 width = ((month_f >> 1) - month_f) * zoom
769 769 gc.fill('white')
770 770 gc.stroke('grey')
771 771 gc.stroke_width(1)
772 772 gc.rectangle(left, 0, left + width, height)
773 773 gc.fill('black')
774 774 gc.stroke('transparent')
775 775 gc.stroke_width(1)
776 776 gc.text(left.round + 8, 14, "#{month_f.year}-#{month_f.month}")
777 777 left = left + width
778 778 month_f = month_f >> 1
779 779 end
780 780
781 781 # Weeks headers
782 782 if show_weeks
783 783 left = subject_width
784 784 height = header_heigth
785 785 if @date_from.cwday == 1
786 786 # date_from is monday
787 787 week_f = date_from
788 788 else
789 789 # find next monday after date_from
790 790 week_f = @date_from + (7 - @date_from.cwday + 1)
791 791 width = (7 - @date_from.cwday + 1) * zoom
792 792 gc.fill('white')
793 793 gc.stroke('grey')
794 794 gc.stroke_width(1)
795 795 gc.rectangle(left, header_heigth, left + width, 2*header_heigth + g_height-1)
796 796 left = left + width
797 797 end
798 798 while week_f <= date_to
799 799 width = (week_f + 6 <= date_to) ? 7 * zoom : (date_to - week_f + 1) * zoom
800 800 gc.fill('white')
801 801 gc.stroke('grey')
802 802 gc.stroke_width(1)
803 803 gc.rectangle(left.round, header_heigth, left.round + width, 2*header_heigth + g_height-1)
804 804 gc.fill('black')
805 805 gc.stroke('transparent')
806 806 gc.stroke_width(1)
807 807 gc.text(left.round + 2, header_heigth + 14, week_f.cweek.to_s)
808 808 left = left + width
809 809 week_f = week_f+7
810 810 end
811 811 end
812 812
813 813 # Days details (week-end in grey)
814 814 if show_days
815 815 left = subject_width
816 816 height = g_height + header_heigth - 1
817 817 wday = @date_from.cwday
818 818 (date_to - @date_from + 1).to_i.times do
819 819 width = zoom
820 820 gc.fill(wday == 6 || wday == 7 ? '#eee' : 'white')
821 821 gc.stroke('grey')
822 822 gc.stroke_width(1)
823 823 gc.rectangle(left, 2*header_heigth, left + width, 2*header_heigth + g_height-1)
824 824 left = left + width
825 825 wday = wday + 1
826 826 wday = 1 if wday > 7
827 827 end
828 828 end
829 829
830 830 # border
831 831 gc.fill('transparent')
832 832 gc.stroke('grey')
833 833 gc.stroke_width(1)
834 834 gc.rectangle(0, 0, subject_width+g_width, headers_heigth)
835 835 gc.stroke('black')
836 836 gc.rectangle(0, 0, subject_width+g_width, g_height+ headers_heigth-1)
837 837
838 838 # content
839 839 top = headers_heigth + 20
840 840
841 841 lines(:image => gc, :top => top, :zoom => zoom, :subject_width => subject_width, :format => :image)
842 842
843 843 # today red line
844 844 if Date.today >= @date_from and Date.today <= date_to
845 845 gc.stroke('red')
846 846 x = (Date.today-@date_from+1)*zoom + subject_width
847 847 gc.line(x, headers_heigth, x, headers_heigth + g_height-1)
848 848 end
849 849
850 850 gc.draw(imgl)
851 851 imgl.format = format
852 852 imgl.to_blob
853 853 end if Object.const_defined?(:Magick)
854 854
855 855 def to_pdf
856 856 pdf = ::Redmine::Export::PDF::IFPDF.new(current_language)
857 857 pdf.SetTitle("#{l(:label_gantt)} #{project}")
858 858 pdf.AliasNbPages
859 859 pdf.footer_date = format_date(Date.today)
860 860 pdf.AddPage("L")
861 861 pdf.SetFontStyle('B',12)
862 862 pdf.SetX(15)
863 863 pdf.Cell(PDF::LeftPaneWidth, 20, project.to_s)
864 864 pdf.Ln
865 865 pdf.SetFontStyle('B',9)
866 866
867 867 subject_width = PDF::LeftPaneWidth
868 868 header_heigth = 5
869 869
870 870 headers_heigth = header_heigth
871 871 show_weeks = false
872 872 show_days = false
873 873
874 874 if self.months < 7
875 875 show_weeks = true
876 876 headers_heigth = 2*header_heigth
877 877 if self.months < 3
878 878 show_days = true
879 879 headers_heigth = 3*header_heigth
880 880 end
881 881 end
882 882
883 883 g_width = PDF.right_pane_width
884 884 zoom = (g_width) / (self.date_to - self.date_from + 1)
885 885 g_height = 120
886 886 t_height = g_height + headers_heigth
887 887
888 888 y_start = pdf.GetY
889 889
890 890 # Months headers
891 891 month_f = self.date_from
892 892 left = subject_width
893 893 height = header_heigth
894 894 self.months.times do
895 895 width = ((month_f >> 1) - month_f) * zoom
896 896 pdf.SetY(y_start)
897 897 pdf.SetX(left)
898 898 pdf.Cell(width, height, "#{month_f.year}-#{month_f.month}", "LTR", 0, "C")
899 899 left = left + width
900 900 month_f = month_f >> 1
901 901 end
902 902
903 903 # Weeks headers
904 904 if show_weeks
905 905 left = subject_width
906 906 height = header_heigth
907 907 if self.date_from.cwday == 1
908 908 # self.date_from is monday
909 909 week_f = self.date_from
910 910 else
911 911 # find next monday after self.date_from
912 912 week_f = self.date_from + (7 - self.date_from.cwday + 1)
913 913 width = (7 - self.date_from.cwday + 1) * zoom-1
914 914 pdf.SetY(y_start + header_heigth)
915 915 pdf.SetX(left)
916 916 pdf.Cell(width + 1, height, "", "LTR")
917 917 left = left + width+1
918 918 end
919 919 while week_f <= self.date_to
920 920 width = (week_f + 6 <= self.date_to) ? 7 * zoom : (self.date_to - week_f + 1) * zoom
921 921 pdf.SetY(y_start + header_heigth)
922 922 pdf.SetX(left)
923 923 pdf.Cell(width, height, (width >= 5 ? week_f.cweek.to_s : ""), "LTR", 0, "C")
924 924 left = left + width
925 925 week_f = week_f+7
926 926 end
927 927 end
928 928
929 929 # Days headers
930 930 if show_days
931 931 left = subject_width
932 932 height = header_heigth
933 933 wday = self.date_from.cwday
934 934 pdf.SetFontStyle('B',7)
935 935 (self.date_to - self.date_from + 1).to_i.times do
936 936 width = zoom
937 937 pdf.SetY(y_start + 2 * header_heigth)
938 938 pdf.SetX(left)
939 939 pdf.Cell(width, height, day_name(wday).first, "LTR", 0, "C")
940 940 left = left + width
941 941 wday = wday + 1
942 942 wday = 1 if wday > 7
943 943 end
944 944 end
945 945
946 946 pdf.SetY(y_start)
947 947 pdf.SetX(15)
948 948 pdf.Cell(subject_width+g_width-15, headers_heigth, "", 1)
949 949
950 950 # Tasks
951 951 top = headers_heigth + y_start
952 952 options = {
953 953 :top => top,
954 954 :zoom => zoom,
955 955 :subject_width => subject_width,
956 956 :g_width => g_width,
957 957 :indent => 0,
958 958 :indent_increment => 5,
959 959 :top_increment => 5,
960 960 :format => :pdf,
961 961 :pdf => pdf
962 962 }
963 963 render(options)
964 964 pdf.Output
965 965 end
966 966
967 967 private
968 968
969 969 # Sorts a collection of issues by start_date, due_date, id for gantt rendering
970 970 def sort_issues!(issues)
971 971 issues.sort! do |a, b|
972 972 cmp = 0
973 973 cmp = (a.start_date <=> b.start_date) if a.start_date? && b.start_date?
974 974 cmp = (a.due_date <=> b.due_date) if cmp == 0 && a.due_date? && b.due_date?
975 975 cmp = (a.id <=> b.id) if cmp == 0
976 976 cmp
977 977 end
978 978 end
979 979
980 980 def current_limit
981 981 if @max_rows
982 982 @max_rows - @number_of_rows
983 983 else
984 984 nil
985 985 end
986 986 end
987 987
988 988 def abort?
989 989 if @max_rows && @number_of_rows >= @max_rows
990 990 @truncated = true
991 991 end
992 992 end
993 993
994 994 def pdf_new_page?(options)
995 995 if options[:top] > 180
996 996 options[:pdf].Line(15, options[:top], PDF::TotalWidth, options[:top])
997 997 options[:pdf].AddPage("L")
998 998 options[:top] = 15
999 999 options[:pdf].Line(15, options[:top] - 0.1, PDF::TotalWidth, options[:top] - 0.1)
1000 1000 end
1001 1001 end
1002 1002 end
1003 1003 end
1004 1004 end
General Comments 0
You need to be logged in to leave comments. Login now