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