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