##// END OF EJS Templates
Removed debug messages....
Jean-Philippe Lang -
r10904:114537530f3a
parent child
Show More
@@ -1,921 +1,918
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 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 ActiveRecord::Base.logger.debug "Gantt#line_for_project was not given a project with a start_date"
301 300 ''
302 301 end
303 302 end
304 303
305 304 def subject_for_version(version, options)
306 305 case options[:format]
307 306 when :html
308 307 html_class = ""
309 308 html_class << 'icon icon-package '
310 309 html_class << (version.behind_schedule? ? 'version-behind-schedule' : '') << " "
311 310 html_class << (version.overdue? ? 'version-overdue' : '')
312 311 s = view.link_to_version(version).html_safe
313 312 subject = view.content_tag(:span, s,
314 313 :class => html_class).html_safe
315 314 html_subject(options, subject, :css => "version-name")
316 315 when :image
317 316 image_subject(options, version.to_s_with_project)
318 317 when :pdf
319 318 pdf_new_page?(options)
320 319 pdf_subject(options, version.to_s_with_project)
321 320 end
322 321 end
323 322
324 323 def line_for_version(version, options)
325 324 # Skip versions that don't have a start_date
326 325 if version.is_a?(Version) && version.start_date && version.due_date
327 326 options[:zoom] ||= 1
328 327 options[:g_width] ||= (self.date_to - self.date_from + 1) * options[:zoom]
329 328 coords = coordinates(version.start_date,
330 329 version.due_date, version.completed_percent,
331 330 options[:zoom])
332 331 label = "#{h version} #{h version.completed_percent.to_i.to_s}%"
333 332 label = h("#{version.project} -") + label unless @project && @project == version.project
334 333 case options[:format]
335 334 when :html
336 335 html_task(options, coords, :css => "version task", :label => label, :markers => true)
337 336 when :image
338 337 image_task(options, coords, :label => label, :markers => true, :height => 3)
339 338 when :pdf
340 339 pdf_task(options, coords, :label => label, :markers => true, :height => 0.8)
341 340 end
342 341 else
343 ActiveRecord::Base.logger.debug "Gantt#line_for_version was not given a version with a start_date"
344 342 ''
345 343 end
346 344 end
347 345
348 346 def subject_for_issue(issue, options)
349 347 while @issue_ancestors.any? && !issue.is_descendant_of?(@issue_ancestors.last)
350 348 @issue_ancestors.pop
351 349 options[:indent] -= options[:indent_increment]
352 350 end
353 351 output = case options[:format]
354 352 when :html
355 353 css_classes = ''
356 354 css_classes << ' issue-overdue' if issue.overdue?
357 355 css_classes << ' issue-behind-schedule' if issue.behind_schedule?
358 356 css_classes << ' icon icon-issue' unless Setting.gravatar_enabled? && issue.assigned_to
359 357 s = "".html_safe
360 358 if issue.assigned_to.present?
361 359 assigned_string = l(:field_assigned_to) + ": " + issue.assigned_to.name
362 360 s << view.avatar(issue.assigned_to,
363 361 :class => 'gravatar icon-gravatar',
364 362 :size => 10,
365 363 :title => assigned_string).to_s.html_safe
366 364 end
367 365 s << view.link_to_issue(issue).html_safe
368 366 subject = view.content_tag(:span, s, :class => css_classes).html_safe
369 367 html_subject(options, subject, :css => "issue-subject",
370 368 :title => issue.subject) + "\n"
371 369 when :image
372 370 image_subject(options, issue.subject)
373 371 when :pdf
374 372 pdf_new_page?(options)
375 373 pdf_subject(options, issue.subject)
376 374 end
377 375 unless issue.leaf?
378 376 @issue_ancestors << issue
379 377 options[:indent] += options[:indent_increment]
380 378 end
381 379 output
382 380 end
383 381
384 382 def line_for_issue(issue, options)
385 383 # Skip issues that don't have a due_before (due_date or version's due_date)
386 384 if issue.is_a?(Issue) && issue.due_before
387 385 coords = coordinates(issue.start_date, issue.due_before, issue.done_ratio, options[:zoom])
388 386 label = "#{issue.status.name} #{issue.done_ratio}%"
389 387 case options[:format]
390 388 when :html
391 389 html_task(options, coords,
392 390 :css => "task " + (issue.leaf? ? 'leaf' : 'parent'),
393 391 :label => label, :issue => issue,
394 392 :markers => !issue.leaf?)
395 393 when :image
396 394 image_task(options, coords, :label => label)
397 395 when :pdf
398 396 pdf_task(options, coords, :label => label)
399 397 end
400 398 else
401 ActiveRecord::Base.logger.debug "GanttHelper#line_for_issue was not given an issue with a due_before"
402 399 ''
403 400 end
404 401 end
405 402
406 403 # Generates a gantt image
407 404 # Only defined if RMagick is avalaible
408 405 def to_image(format='PNG')
409 406 date_to = (@date_from >> @months) - 1
410 407 show_weeks = @zoom > 1
411 408 show_days = @zoom > 2
412 409 subject_width = 400
413 410 header_height = 18
414 411 # width of one day in pixels
415 412 zoom = @zoom * 2
416 413 g_width = (@date_to - @date_from + 1) * zoom
417 414 g_height = 20 * number_of_rows + 30
418 415 headers_height = (show_weeks ? 2 * header_height : header_height)
419 416 height = g_height + headers_height
420 417 imgl = Magick::ImageList.new
421 418 imgl.new_image(subject_width + g_width + 1, height)
422 419 gc = Magick::Draw.new
423 420 gc.font = Redmine::Configuration['rmagick_font_path'] || ""
424 421 # Subjects
425 422 gc.stroke('transparent')
426 423 subjects(:image => gc, :top => (headers_height + 20), :indent => 4, :format => :image)
427 424 # Months headers
428 425 month_f = @date_from
429 426 left = subject_width
430 427 @months.times do
431 428 width = ((month_f >> 1) - month_f) * zoom
432 429 gc.fill('white')
433 430 gc.stroke('grey')
434 431 gc.stroke_width(1)
435 432 gc.rectangle(left, 0, left + width, height)
436 433 gc.fill('black')
437 434 gc.stroke('transparent')
438 435 gc.stroke_width(1)
439 436 gc.text(left.round + 8, 14, "#{month_f.year}-#{month_f.month}")
440 437 left = left + width
441 438 month_f = month_f >> 1
442 439 end
443 440 # Weeks headers
444 441 if show_weeks
445 442 left = subject_width
446 443 height = header_height
447 444 if @date_from.cwday == 1
448 445 # date_from is monday
449 446 week_f = date_from
450 447 else
451 448 # find next monday after date_from
452 449 week_f = @date_from + (7 - @date_from.cwday + 1)
453 450 width = (7 - @date_from.cwday + 1) * zoom
454 451 gc.fill('white')
455 452 gc.stroke('grey')
456 453 gc.stroke_width(1)
457 454 gc.rectangle(left, header_height, left + width, 2 * header_height + g_height - 1)
458 455 left = left + width
459 456 end
460 457 while week_f <= date_to
461 458 width = (week_f + 6 <= date_to) ? 7 * zoom : (date_to - week_f + 1) * zoom
462 459 gc.fill('white')
463 460 gc.stroke('grey')
464 461 gc.stroke_width(1)
465 462 gc.rectangle(left.round, header_height, left.round + width, 2 * header_height + g_height - 1)
466 463 gc.fill('black')
467 464 gc.stroke('transparent')
468 465 gc.stroke_width(1)
469 466 gc.text(left.round + 2, header_height + 14, week_f.cweek.to_s)
470 467 left = left + width
471 468 week_f = week_f + 7
472 469 end
473 470 end
474 471 # Days details (week-end in grey)
475 472 if show_days
476 473 left = subject_width
477 474 height = g_height + header_height - 1
478 475 wday = @date_from.cwday
479 476 (date_to - @date_from + 1).to_i.times do
480 477 width = zoom
481 478 gc.fill(non_working_week_days.include?(wday) ? '#eee' : 'white')
482 479 gc.stroke('#ddd')
483 480 gc.stroke_width(1)
484 481 gc.rectangle(left, 2 * header_height, left + width, 2 * header_height + g_height - 1)
485 482 left = left + width
486 483 wday = wday + 1
487 484 wday = 1 if wday > 7
488 485 end
489 486 end
490 487 # border
491 488 gc.fill('transparent')
492 489 gc.stroke('grey')
493 490 gc.stroke_width(1)
494 491 gc.rectangle(0, 0, subject_width + g_width, headers_height)
495 492 gc.stroke('black')
496 493 gc.rectangle(0, 0, subject_width + g_width, g_height + headers_height - 1)
497 494 # content
498 495 top = headers_height + 20
499 496 gc.stroke('transparent')
500 497 lines(:image => gc, :top => top, :zoom => zoom,
501 498 :subject_width => subject_width, :format => :image)
502 499 # today red line
503 500 if Date.today >= @date_from and Date.today <= date_to
504 501 gc.stroke('red')
505 502 x = (Date.today - @date_from + 1) * zoom + subject_width
506 503 gc.line(x, headers_height, x, headers_height + g_height - 1)
507 504 end
508 505 gc.draw(imgl)
509 506 imgl.format = format
510 507 imgl.to_blob
511 508 end if Object.const_defined?(:Magick)
512 509
513 510 def to_pdf
514 511 pdf = ::Redmine::Export::PDF::ITCPDF.new(current_language)
515 512 pdf.SetTitle("#{l(:label_gantt)} #{project}")
516 513 pdf.alias_nb_pages
517 514 pdf.footer_date = format_date(Date.today)
518 515 pdf.AddPage("L")
519 516 pdf.SetFontStyle('B', 12)
520 517 pdf.SetX(15)
521 518 pdf.RDMCell(PDF::LeftPaneWidth, 20, project.to_s)
522 519 pdf.Ln
523 520 pdf.SetFontStyle('B', 9)
524 521 subject_width = PDF::LeftPaneWidth
525 522 header_height = 5
526 523 headers_height = header_height
527 524 show_weeks = false
528 525 show_days = false
529 526 if self.months < 7
530 527 show_weeks = true
531 528 headers_height = 2 * header_height
532 529 if self.months < 3
533 530 show_days = true
534 531 headers_height = 3 * header_height
535 532 end
536 533 end
537 534 g_width = PDF.right_pane_width
538 535 zoom = (g_width) / (self.date_to - self.date_from + 1)
539 536 g_height = 120
540 537 t_height = g_height + headers_height
541 538 y_start = pdf.GetY
542 539 # Months headers
543 540 month_f = self.date_from
544 541 left = subject_width
545 542 height = header_height
546 543 self.months.times do
547 544 width = ((month_f >> 1) - month_f) * zoom
548 545 pdf.SetY(y_start)
549 546 pdf.SetX(left)
550 547 pdf.RDMCell(width, height, "#{month_f.year}-#{month_f.month}", "LTR", 0, "C")
551 548 left = left + width
552 549 month_f = month_f >> 1
553 550 end
554 551 # Weeks headers
555 552 if show_weeks
556 553 left = subject_width
557 554 height = header_height
558 555 if self.date_from.cwday == 1
559 556 # self.date_from is monday
560 557 week_f = self.date_from
561 558 else
562 559 # find next monday after self.date_from
563 560 week_f = self.date_from + (7 - self.date_from.cwday + 1)
564 561 width = (7 - self.date_from.cwday + 1) * zoom-1
565 562 pdf.SetY(y_start + header_height)
566 563 pdf.SetX(left)
567 564 pdf.RDMCell(width + 1, height, "", "LTR")
568 565 left = left + width + 1
569 566 end
570 567 while week_f <= self.date_to
571 568 width = (week_f + 6 <= self.date_to) ? 7 * zoom : (self.date_to - week_f + 1) * zoom
572 569 pdf.SetY(y_start + header_height)
573 570 pdf.SetX(left)
574 571 pdf.RDMCell(width, height, (width >= 5 ? week_f.cweek.to_s : ""), "LTR", 0, "C")
575 572 left = left + width
576 573 week_f = week_f + 7
577 574 end
578 575 end
579 576 # Days headers
580 577 if show_days
581 578 left = subject_width
582 579 height = header_height
583 580 wday = self.date_from.cwday
584 581 pdf.SetFontStyle('B', 7)
585 582 (self.date_to - self.date_from + 1).to_i.times do
586 583 width = zoom
587 584 pdf.SetY(y_start + 2 * header_height)
588 585 pdf.SetX(left)
589 586 pdf.RDMCell(width, height, day_name(wday).first, "LTR", 0, "C")
590 587 left = left + width
591 588 wday = wday + 1
592 589 wday = 1 if wday > 7
593 590 end
594 591 end
595 592 pdf.SetY(y_start)
596 593 pdf.SetX(15)
597 594 pdf.RDMCell(subject_width + g_width - 15, headers_height, "", 1)
598 595 # Tasks
599 596 top = headers_height + y_start
600 597 options = {
601 598 :top => top,
602 599 :zoom => zoom,
603 600 :subject_width => subject_width,
604 601 :g_width => g_width,
605 602 :indent => 0,
606 603 :indent_increment => 5,
607 604 :top_increment => 5,
608 605 :format => :pdf,
609 606 :pdf => pdf
610 607 }
611 608 render(options)
612 609 pdf.Output
613 610 end
614 611
615 612 private
616 613
617 614 def coordinates(start_date, end_date, progress, zoom=nil)
618 615 zoom ||= @zoom
619 616 coords = {}
620 617 if start_date && end_date && start_date < self.date_to && end_date > self.date_from
621 618 if start_date > self.date_from
622 619 coords[:start] = start_date - self.date_from
623 620 coords[:bar_start] = start_date - self.date_from
624 621 else
625 622 coords[:bar_start] = 0
626 623 end
627 624 if end_date < self.date_to
628 625 coords[:end] = end_date - self.date_from
629 626 coords[:bar_end] = end_date - self.date_from + 1
630 627 else
631 628 coords[:bar_end] = self.date_to - self.date_from + 1
632 629 end
633 630 if progress
634 631 progress_date = start_date + (end_date - start_date + 1) * (progress / 100.0)
635 632 if progress_date > self.date_from && progress_date > start_date
636 633 if progress_date < self.date_to
637 634 coords[:bar_progress_end] = progress_date - self.date_from
638 635 else
639 636 coords[:bar_progress_end] = self.date_to - self.date_from + 1
640 637 end
641 638 end
642 639 if progress_date < Date.today
643 640 late_date = [Date.today, end_date].min
644 641 if late_date > self.date_from && late_date > start_date
645 642 if late_date < self.date_to
646 643 coords[:bar_late_end] = late_date - self.date_from + 1
647 644 else
648 645 coords[:bar_late_end] = self.date_to - self.date_from + 1
649 646 end
650 647 end
651 648 end
652 649 end
653 650 end
654 651 # Transforms dates into pixels witdh
655 652 coords.keys.each do |key|
656 653 coords[key] = (coords[key] * zoom).floor
657 654 end
658 655 coords
659 656 end
660 657
661 658 # Sorts a collection of issues by start_date, due_date, id for gantt rendering
662 659 def sort_issues!(issues)
663 660 issues.sort! { |a, b| gantt_issue_compare(a, b) }
664 661 end
665 662
666 663 # TODO: top level issues should be sorted by start date
667 664 def gantt_issue_compare(x, y)
668 665 if x.root_id == y.root_id
669 666 x.lft <=> y.lft
670 667 else
671 668 x.root_id <=> y.root_id
672 669 end
673 670 end
674 671
675 672 def current_limit
676 673 if @max_rows
677 674 @max_rows - @number_of_rows
678 675 else
679 676 nil
680 677 end
681 678 end
682 679
683 680 def abort?
684 681 if @max_rows && @number_of_rows >= @max_rows
685 682 @truncated = true
686 683 end
687 684 end
688 685
689 686 def pdf_new_page?(options)
690 687 if options[:top] > 180
691 688 options[:pdf].Line(15, options[:top], PDF::TotalWidth, options[:top])
692 689 options[:pdf].AddPage("L")
693 690 options[:top] = 15
694 691 options[:pdf].Line(15, options[:top] - 0.1, PDF::TotalWidth, options[:top] - 0.1)
695 692 end
696 693 end
697 694
698 695 def html_subject(params, subject, options={})
699 696 style = "position: absolute;top:#{params[:top]}px;left:#{params[:indent]}px;"
700 697 style << "width:#{params[:subject_width] - params[:indent]}px;" if params[:subject_width]
701 698 output = view.content_tag('div', subject,
702 699 :class => options[:css], :style => style,
703 700 :title => options[:title])
704 701 @subjects << output
705 702 output
706 703 end
707 704
708 705 def pdf_subject(params, subject, options={})
709 706 params[:pdf].SetY(params[:top])
710 707 params[:pdf].SetX(15)
711 708 char_limit = PDF::MaxCharactorsForSubject - params[:indent]
712 709 params[:pdf].RDMCell(params[:subject_width] - 15, 5,
713 710 (" " * params[:indent]) +
714 711 subject.to_s.sub(/^(.{#{char_limit}}[^\s]*\s).*$/, '\1 (...)'),
715 712 "LR")
716 713 params[:pdf].SetY(params[:top])
717 714 params[:pdf].SetX(params[:subject_width])
718 715 params[:pdf].RDMCell(params[:g_width], 5, "", "LR")
719 716 end
720 717
721 718 def image_subject(params, subject, options={})
722 719 params[:image].fill('black')
723 720 params[:image].stroke('transparent')
724 721 params[:image].stroke_width(1)
725 722 params[:image].text(params[:indent], params[:top] + 2, subject)
726 723 end
727 724
728 725 def issue_relations(issue)
729 726 rels = {}
730 727 if relations[issue.id]
731 728 relations[issue.id].each do |relation|
732 729 (rels[relation.relation_type] ||= []) << relation.issue_to_id
733 730 end
734 731 end
735 732 rels
736 733 end
737 734
738 735 def html_task(params, coords, options={})
739 736 output = ''
740 737 # Renders the task bar, with progress and late
741 738 if coords[:bar_start] && coords[:bar_end]
742 739 width = coords[:bar_end] - coords[:bar_start] - 2
743 740 style = ""
744 741 style << "top:#{params[:top]}px;"
745 742 style << "left:#{coords[:bar_start]}px;"
746 743 style << "width:#{width}px;"
747 744 html_id = "task-todo-issue-#{options[:issue].id}" if options[:issue]
748 745 content_opt = {:style => style,
749 746 :class => "#{options[:css]} task_todo",
750 747 :id => html_id}
751 748 if options[:issue]
752 749 rels = issue_relations(options[:issue])
753 750 if rels.present?
754 751 content_opt[:data] = {"rels" => rels.to_json}
755 752 end
756 753 end
757 754 output << view.content_tag(:div, '&nbsp;'.html_safe, content_opt)
758 755 if coords[:bar_late_end]
759 756 width = coords[:bar_late_end] - coords[:bar_start] - 2
760 757 style = ""
761 758 style << "top:#{params[:top]}px;"
762 759 style << "left:#{coords[:bar_start]}px;"
763 760 style << "width:#{width}px;"
764 761 output << view.content_tag(:div, '&nbsp;'.html_safe,
765 762 :style => style,
766 763 :class => "#{options[:css]} task_late")
767 764 end
768 765 if coords[:bar_progress_end]
769 766 width = coords[:bar_progress_end] - coords[:bar_start] - 2
770 767 style = ""
771 768 style << "top:#{params[:top]}px;"
772 769 style << "left:#{coords[:bar_start]}px;"
773 770 style << "width:#{width}px;"
774 771 output << view.content_tag(:div, '&nbsp;'.html_safe,
775 772 :style => style,
776 773 :class => "#{options[:css]} task_done")
777 774 end
778 775 end
779 776 # Renders the markers
780 777 if options[:markers]
781 778 if coords[:start]
782 779 style = ""
783 780 style << "top:#{params[:top]}px;"
784 781 style << "left:#{coords[:start]}px;"
785 782 style << "width:15px;"
786 783 output << view.content_tag(:div, '&nbsp;'.html_safe,
787 784 :style => style,
788 785 :class => "#{options[:css]} marker starting")
789 786 end
790 787 if coords[:end]
791 788 style = ""
792 789 style << "top:#{params[:top]}px;"
793 790 style << "left:#{coords[:end] + params[:zoom]}px;"
794 791 style << "width:15px;"
795 792 output << view.content_tag(:div, '&nbsp;'.html_safe,
796 793 :style => style,
797 794 :class => "#{options[:css]} marker ending")
798 795 end
799 796 end
800 797 # Renders the label on the right
801 798 if options[:label]
802 799 style = ""
803 800 style << "top:#{params[:top]}px;"
804 801 style << "left:#{(coords[:bar_end] || 0) + 8}px;"
805 802 style << "width:15px;"
806 803 output << view.content_tag(:div, options[:label],
807 804 :style => style,
808 805 :class => "#{options[:css]} label")
809 806 end
810 807 # Renders the tooltip
811 808 if options[:issue] && coords[:bar_start] && coords[:bar_end]
812 809 s = view.content_tag(:span,
813 810 view.render_issue_tooltip(options[:issue]).html_safe,
814 811 :class => "tip")
815 812 style = ""
816 813 style << "position: absolute;"
817 814 style << "top:#{params[:top]}px;"
818 815 style << "left:#{coords[:bar_start]}px;"
819 816 style << "width:#{coords[:bar_end] - coords[:bar_start]}px;"
820 817 style << "height:12px;"
821 818 output << view.content_tag(:div, s.html_safe,
822 819 :style => style,
823 820 :class => "tooltip")
824 821 end
825 822 @lines << output
826 823 output
827 824 end
828 825
829 826 def pdf_task(params, coords, options={})
830 827 height = options[:height] || 2
831 828 # Renders the task bar, with progress and late
832 829 if coords[:bar_start] && coords[:bar_end]
833 830 params[:pdf].SetY(params[:top] + 1.5)
834 831 params[:pdf].SetX(params[:subject_width] + coords[:bar_start])
835 832 params[:pdf].SetFillColor(200, 200, 200)
836 833 params[:pdf].RDMCell(coords[:bar_end] - coords[:bar_start], height, "", 0, 0, "", 1)
837 834 if coords[:bar_late_end]
838 835 params[:pdf].SetY(params[:top] + 1.5)
839 836 params[:pdf].SetX(params[:subject_width] + coords[:bar_start])
840 837 params[:pdf].SetFillColor(255, 100, 100)
841 838 params[:pdf].RDMCell(coords[:bar_late_end] - coords[:bar_start], height, "", 0, 0, "", 1)
842 839 end
843 840 if coords[:bar_progress_end]
844 841 params[:pdf].SetY(params[:top] + 1.5)
845 842 params[:pdf].SetX(params[:subject_width] + coords[:bar_start])
846 843 params[:pdf].SetFillColor(90, 200, 90)
847 844 params[:pdf].RDMCell(coords[:bar_progress_end] - coords[:bar_start], height, "", 0, 0, "", 1)
848 845 end
849 846 end
850 847 # Renders the markers
851 848 if options[:markers]
852 849 if coords[:start]
853 850 params[:pdf].SetY(params[:top] + 1)
854 851 params[:pdf].SetX(params[:subject_width] + coords[:start] - 1)
855 852 params[:pdf].SetFillColor(50, 50, 200)
856 853 params[:pdf].RDMCell(2, 2, "", 0, 0, "", 1)
857 854 end
858 855 if coords[:end]
859 856 params[:pdf].SetY(params[:top] + 1)
860 857 params[:pdf].SetX(params[:subject_width] + coords[:end] - 1)
861 858 params[:pdf].SetFillColor(50, 50, 200)
862 859 params[:pdf].RDMCell(2, 2, "", 0, 0, "", 1)
863 860 end
864 861 end
865 862 # Renders the label on the right
866 863 if options[:label]
867 864 params[:pdf].SetX(params[:subject_width] + (coords[:bar_end] || 0) + 5)
868 865 params[:pdf].RDMCell(30, 2, options[:label])
869 866 end
870 867 end
871 868
872 869 def image_task(params, coords, options={})
873 870 height = options[:height] || 6
874 871 # Renders the task bar, with progress and late
875 872 if coords[:bar_start] && coords[:bar_end]
876 873 params[:image].fill('#aaa')
877 874 params[:image].rectangle(params[:subject_width] + coords[:bar_start],
878 875 params[:top],
879 876 params[:subject_width] + coords[:bar_end],
880 877 params[:top] - height)
881 878 if coords[:bar_late_end]
882 879 params[:image].fill('#f66')
883 880 params[:image].rectangle(params[:subject_width] + coords[:bar_start],
884 881 params[:top],
885 882 params[:subject_width] + coords[:bar_late_end],
886 883 params[:top] - height)
887 884 end
888 885 if coords[:bar_progress_end]
889 886 params[:image].fill('#00c600')
890 887 params[:image].rectangle(params[:subject_width] + coords[:bar_start],
891 888 params[:top],
892 889 params[:subject_width] + coords[:bar_progress_end],
893 890 params[:top] - height)
894 891 end
895 892 end
896 893 # Renders the markers
897 894 if options[:markers]
898 895 if coords[:start]
899 896 x = params[:subject_width] + coords[:start]
900 897 y = params[:top] - height / 2
901 898 params[:image].fill('blue')
902 899 params[:image].polygon(x - 4, y, x, y - 4, x + 4, y, x, y + 4)
903 900 end
904 901 if coords[:end]
905 902 x = params[:subject_width] + coords[:end] + params[:zoom]
906 903 y = params[:top] - height / 2
907 904 params[:image].fill('blue')
908 905 params[:image].polygon(x - 4, y, x, y - 4, x + 4, y, x, y + 4)
909 906 end
910 907 end
911 908 # Renders the label on the right
912 909 if options[:label]
913 910 params[:image].fill('black')
914 911 params[:image].text(params[:subject_width] + (coords[:bar_end] || 0) + 5,
915 912 params[:top] + 1,
916 913 options[:label])
917 914 end
918 915 end
919 916 end
920 917 end
921 918 end
General Comments 0
You need to be logged in to leave comments. Login now