annotate lib/redmine/helpers/gantt.rb @ 252:adc8466df404 cannam

* Restore branch menu in repo
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 03 Mar 2011 15:08:45 +0000
parents 051f544170fe
children cbce1fd3b1b7
rev   line source
Chris@0 1 # Redmine - project management software
Chris@0 2 # Copyright (C) 2006-2008 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@0 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@0 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 module Redmine
Chris@0 19 module Helpers
Chris@0 20 # Simple class to handle gantt chart data
Chris@0 21 class Gantt
chris@22 22 include ERB::Util
chris@22 23 include Redmine::I18n
chris@22 24
chris@22 25 # :nodoc:
chris@22 26 # Some utility methods for the PDF export
chris@22 27 class PDF
chris@22 28 MaxCharactorsForSubject = 45
chris@22 29 TotalWidth = 280
chris@22 30 LeftPaneWidth = 100
chris@22 31
chris@22 32 def self.right_pane_width
chris@22 33 TotalWidth - LeftPaneWidth
chris@22 34 end
chris@22 35 end
chris@22 36
Chris@119 37 attr_reader :year_from, :month_from, :date_from, :date_to, :zoom, :months, :truncated, :max_rows
chris@22 38 attr_accessor :query
chris@22 39 attr_accessor :project
chris@22 40 attr_accessor :view
chris@22 41
Chris@0 42 def initialize(options={})
Chris@0 43 options = options.dup
Chris@0 44
Chris@0 45 if options[:year] && options[:year].to_i >0
Chris@0 46 @year_from = options[:year].to_i
Chris@0 47 if options[:month] && options[:month].to_i >=1 && options[:month].to_i <= 12
Chris@0 48 @month_from = options[:month].to_i
Chris@0 49 else
Chris@0 50 @month_from = 1
Chris@0 51 end
Chris@0 52 else
Chris@0 53 @month_from ||= Date.today.month
Chris@0 54 @year_from ||= Date.today.year
Chris@0 55 end
Chris@0 56
Chris@0 57 zoom = (options[:zoom] || User.current.pref[:gantt_zoom]).to_i
Chris@0 58 @zoom = (zoom > 0 && zoom < 5) ? zoom : 2
Chris@0 59 months = (options[:months] || User.current.pref[:gantt_months]).to_i
Chris@0 60 @months = (months > 0 && months < 25) ? months : 6
Chris@0 61
Chris@0 62 # Save gantt parameters as user preference (zoom and months count)
Chris@0 63 if (User.current.logged? && (@zoom != User.current.pref[:gantt_zoom] || @months != User.current.pref[:gantt_months]))
Chris@0 64 User.current.pref[:gantt_zoom], User.current.pref[:gantt_months] = @zoom, @months
Chris@0 65 User.current.preference.save
Chris@0 66 end
Chris@0 67
Chris@0 68 @date_from = Date.civil(@year_from, @month_from, 1)
Chris@0 69 @date_to = (@date_from >> @months) - 1
Chris@119 70
Chris@119 71 @subjects = ''
Chris@119 72 @lines = ''
Chris@119 73 @number_of_rows = nil
Chris@119 74
Chris@119 75 @issue_ancestors = []
Chris@119 76
Chris@119 77 @truncated = false
Chris@119 78 if options.has_key?(:max_rows)
Chris@119 79 @max_rows = options[:max_rows]
Chris@119 80 else
Chris@119 81 @max_rows = Setting.gantt_items_limit.blank? ? nil : Setting.gantt_items_limit.to_i
Chris@119 82 end
Chris@0 83 end
chris@22 84
chris@22 85 def common_params
chris@22 86 { :controller => 'gantts', :action => 'show', :project_id => @project }
Chris@0 87 end
Chris@0 88
Chris@0 89 def params
chris@22 90 common_params.merge({ :zoom => zoom, :year => year_from, :month => month_from, :months => months })
Chris@0 91 end
Chris@0 92
Chris@0 93 def params_previous
chris@22 94 common_params.merge({:year => (date_from << months).year, :month => (date_from << months).month, :zoom => zoom, :months => months })
Chris@0 95 end
Chris@0 96
Chris@0 97 def params_next
chris@22 98 common_params.merge({:year => (date_from >> months).year, :month => (date_from >> months).month, :zoom => zoom, :months => months })
Chris@0 99 end
chris@22 100
chris@22 101 ### Extracted from the HTML view/helpers
chris@22 102 # Returns the number of rows that will be rendered on the Gantt chart
chris@22 103 def number_of_rows
Chris@119 104 return @number_of_rows if @number_of_rows
Chris@119 105
Chris@119 106 rows = if @project
Chris@119 107 number_of_rows_on_project(@project)
chris@22 108 else
Chris@119 109 Project.roots.visible.has_module('issue_tracking').inject(0) do |total, project|
chris@22 110 total += number_of_rows_on_project(project)
chris@22 111 end
chris@22 112 end
Chris@119 113
Chris@119 114 rows > @max_rows ? @max_rows : rows
chris@22 115 end
chris@22 116
chris@22 117 # Returns the number of rows that will be used to list a project on
chris@22 118 # the Gantt chart. This will recurse for each subproject.
chris@22 119 def number_of_rows_on_project(project)
chris@22 120 # Remove the project requirement for Versions because it will
chris@22 121 # restrict issues to only be on the current project. This
chris@22 122 # ends up missing issues which are assigned to shared versions.
chris@22 123 @query.project = nil if @query.project
chris@22 124
chris@22 125 # One Root project
chris@22 126 count = 1
chris@22 127 # Issues without a Version
chris@22 128 count += project.issues.for_gantt.without_version.with_query(@query).count
chris@22 129
chris@22 130 # Versions
chris@22 131 count += project.versions.count
chris@22 132
chris@22 133 # Issues on the Versions
chris@22 134 project.versions.each do |version|
chris@22 135 count += version.fixed_issues.for_gantt.with_query(@query).count
chris@22 136 end
chris@22 137
chris@22 138 # Subprojects
Chris@119 139 project.children.visible.has_module('issue_tracking').each do |subproject|
chris@22 140 count += number_of_rows_on_project(subproject)
chris@22 141 end
chris@22 142
chris@22 143 count
chris@22 144 end
chris@22 145
chris@22 146 # Renders the subjects of the Gantt chart, the left side.
chris@22 147 def subjects(options={})
Chris@119 148 render(options.merge(:only => :subjects)) unless @subjects_rendered
Chris@119 149 @subjects
chris@22 150 end
chris@22 151
chris@22 152 # Renders the lines of the Gantt chart, the right side
chris@22 153 def lines(options={})
Chris@119 154 render(options.merge(:only => :lines)) unless @lines_rendered
Chris@119 155 @lines
Chris@119 156 end
Chris@119 157
Chris@119 158 def render(options={})
Chris@119 159 options = {:indent => 4, :render => :subject, :format => :html}.merge(options)
Chris@119 160
Chris@119 161 @subjects = '' unless options[:only] == :lines
Chris@119 162 @lines = '' unless options[:only] == :subjects
Chris@119 163 @number_of_rows = 0
Chris@119 164
chris@22 165 if @project
Chris@119 166 render_project(@project, options)
chris@22 167 else
Chris@119 168 Project.roots.visible.has_module('issue_tracking').each do |project|
Chris@119 169 render_project(project, options)
Chris@119 170 break if abort?
chris@22 171 end
chris@22 172 end
chris@22 173
Chris@119 174 @subjects_rendered = true unless options[:only] == :lines
Chris@119 175 @lines_rendered = true unless options[:only] == :subjects
Chris@119 176
Chris@119 177 render_end(options)
chris@22 178 end
chris@22 179
chris@22 180 def render_project(project, options={})
chris@22 181 options[:top] = 0 unless options.key? :top
chris@22 182 options[:indent_increment] = 20 unless options.key? :indent_increment
chris@22 183 options[:top_increment] = 20 unless options.key? :top_increment
chris@22 184
Chris@119 185 subject_for_project(project, options) unless options[:only] == :lines
Chris@119 186 line_for_project(project, options) unless options[:only] == :subjects
chris@22 187
chris@22 188 options[:top] += options[:top_increment]
chris@22 189 options[:indent] += options[:indent_increment]
Chris@119 190 @number_of_rows += 1
Chris@119 191 return if abort?
chris@22 192
chris@22 193 # Second, Issues without a version
Chris@119 194 issues = project.issues.for_gantt.without_version.with_query(@query).all(:limit => current_limit)
Chris@119 195 sort_issues!(issues)
chris@22 196 if issues
Chris@119 197 render_issues(issues, options)
Chris@119 198 return if abort?
chris@22 199 end
chris@22 200
chris@22 201 # Third, Versions
chris@22 202 project.versions.sort.each do |version|
Chris@119 203 render_version(version, options)
Chris@119 204 return if abort?
chris@22 205 end
chris@22 206
chris@22 207 # Fourth, subprojects
Chris@119 208 project.children.visible.has_module('issue_tracking').each do |project|
Chris@119 209 render_project(project, options)
Chris@119 210 return if abort?
Chris@119 211 end unless project.leaf?
chris@22 212
chris@22 213 # Remove indent to hit the next sibling
chris@22 214 options[:indent] -= options[:indent_increment]
chris@22 215 end
chris@22 216
chris@22 217 def render_issues(issues, options={})
Chris@119 218 @issue_ancestors = []
Chris@119 219
chris@22 220 issues.each do |i|
Chris@119 221 subject_for_issue(i, options) unless options[:only] == :lines
Chris@119 222 line_for_issue(i, options) unless options[:only] == :subjects
Chris@119 223
chris@22 224 options[:top] += options[:top_increment]
Chris@119 225 @number_of_rows += 1
Chris@119 226 break if abort?
chris@22 227 end
Chris@119 228
Chris@119 229 options[:indent] -= (options[:indent_increment] * @issue_ancestors.size)
chris@22 230 end
chris@22 231
chris@22 232 def render_version(version, options={})
chris@22 233 # Version header
Chris@119 234 subject_for_version(version, options) unless options[:only] == :lines
Chris@119 235 line_for_version(version, options) unless options[:only] == :subjects
chris@22 236
chris@22 237 options[:top] += options[:top_increment]
Chris@119 238 @number_of_rows += 1
Chris@119 239 return if abort?
Chris@119 240
chris@22 241 # Remove the project requirement for Versions because it will
chris@22 242 # restrict issues to only be on the current project. This
chris@22 243 # ends up missing issues which are assigned to shared versions.
chris@22 244 @query.project = nil if @query.project
chris@22 245
Chris@119 246 issues = version.fixed_issues.for_gantt.with_query(@query).all(:limit => current_limit)
chris@22 247 if issues
Chris@119 248 sort_issues!(issues)
chris@22 249 # Indent issues
chris@22 250 options[:indent] += options[:indent_increment]
Chris@119 251 render_issues(issues, options)
chris@22 252 options[:indent] -= options[:indent_increment]
chris@22 253 end
Chris@119 254 end
Chris@119 255
Chris@119 256 def render_end(options={})
Chris@119 257 case options[:format]
Chris@119 258 when :pdf
Chris@119 259 options[:pdf].Line(15, options[:top], PDF::TotalWidth, options[:top])
Chris@119 260 end
chris@22 261 end
chris@22 262
chris@22 263 def subject_for_project(project, options)
chris@22 264 case options[:format]
chris@22 265 when :html
Chris@119 266 subject = "<span class='icon icon-projects #{project.overdue? ? 'project-overdue' : ''}'>"
Chris@119 267 subject << view.link_to_project(project)
Chris@119 268 subject << '</span>'
Chris@119 269 html_subject(options, subject, :css => "project-name")
chris@22 270 when :image
Chris@119 271 image_subject(options, project.name)
chris@22 272 when :pdf
Chris@119 273 pdf_new_page?(options)
Chris@119 274 pdf_subject(options, project.name)
chris@22 275 end
chris@22 276 end
chris@22 277
chris@22 278 def line_for_project(project, options)
chris@37 279 # Skip versions that don't have a start_date or due date
chris@37 280 if project.is_a?(Project) && project.start_date && project.due_date
chris@22 281 options[:zoom] ||= 1
chris@22 282 options[:g_width] ||= (self.date_to - self.date_from + 1) * options[:zoom]
Chris@119 283
Chris@119 284 coords = coordinates(project.start_date, project.due_date, nil, options[:zoom])
Chris@119 285 label = h(project)
chris@22 286
chris@22 287 case options[:format]
chris@22 288 when :html
Chris@119 289 html_task(options, coords, :css => "project task", :label => label, :markers => true)
chris@22 290 when :image
Chris@119 291 image_task(options, coords, :label => label, :markers => true, :height => 3)
chris@22 292 when :pdf
Chris@119 293 pdf_task(options, coords, :label => label, :markers => true, :height => 0.8)
chris@22 294 end
chris@22 295 else
chris@22 296 ActiveRecord::Base.logger.debug "Gantt#line_for_project was not given a project with a start_date"
chris@22 297 ''
chris@22 298 end
chris@22 299 end
chris@22 300
chris@22 301 def subject_for_version(version, options)
chris@22 302 case options[:format]
chris@22 303 when :html
Chris@119 304 subject = "<span class='icon icon-package #{version.behind_schedule? ? 'version-behind-schedule' : ''} #{version.overdue? ? 'version-overdue' : ''}'>"
Chris@119 305 subject << view.link_to_version(version)
Chris@119 306 subject << '</span>'
Chris@119 307 html_subject(options, subject, :css => "version-name")
chris@22 308 when :image
Chris@119 309 image_subject(options, version.to_s_with_project)
chris@22 310 when :pdf
Chris@119 311 pdf_new_page?(options)
Chris@119 312 pdf_subject(options, version.to_s_with_project)
chris@22 313 end
chris@22 314 end
chris@22 315
chris@22 316 def line_for_version(version, options)
chris@22 317 # Skip versions that don't have a start_date
chris@37 318 if version.is_a?(Version) && version.start_date && version.due_date
chris@22 319 options[:zoom] ||= 1
chris@22 320 options[:g_width] ||= (self.date_to - self.date_from + 1) * options[:zoom]
Chris@119 321
Chris@119 322 coords = coordinates(version.start_date, version.due_date, version.completed_pourcent, options[:zoom])
Chris@119 323 label = "#{h version } #{h version.completed_pourcent.to_i.to_s}%"
Chris@119 324 label = h("#{version.project} -") + label unless @project && @project == version.project
chris@22 325
chris@22 326 case options[:format]
chris@22 327 when :html
Chris@119 328 html_task(options, coords, :css => "version task", :label => label, :markers => true)
chris@22 329 when :image
Chris@119 330 image_task(options, coords, :label => label, :markers => true, :height => 3)
chris@22 331 when :pdf
Chris@119 332 pdf_task(options, coords, :label => label, :markers => true, :height => 0.8)
chris@22 333 end
chris@22 334 else
chris@22 335 ActiveRecord::Base.logger.debug "Gantt#line_for_version was not given a version with a start_date"
chris@22 336 ''
chris@22 337 end
chris@22 338 end
chris@22 339
chris@22 340 def subject_for_issue(issue, options)
Chris@119 341 while @issue_ancestors.any? && !issue.is_descendant_of?(@issue_ancestors.last)
Chris@119 342 @issue_ancestors.pop
Chris@119 343 options[:indent] -= options[:indent_increment]
Chris@119 344 end
Chris@119 345
Chris@119 346 output = case options[:format]
chris@22 347 when :html
Chris@119 348 css_classes = ''
Chris@119 349 css_classes << ' issue-overdue' if issue.overdue?
Chris@119 350 css_classes << ' issue-behind-schedule' if issue.behind_schedule?
Chris@119 351 css_classes << ' icon icon-issue' unless Setting.gravatar_enabled? && issue.assigned_to
Chris@119 352
Chris@119 353 subject = "<span class='#{css_classes}'>"
Chris@119 354 if issue.assigned_to.present?
Chris@119 355 assigned_string = l(:field_assigned_to) + ": " + issue.assigned_to.name
Chris@245 356 subject << view.avatar(issue.assigned_to, :class => 'gravatar icon-gravatar', :size => 10, :title => assigned_string).to_s
Chris@119 357 end
Chris@119 358 subject << view.link_to_issue(issue)
Chris@119 359 subject << '</span>'
Chris@245 360 html_subject(options, subject, :css => "issue-subject", :title => issue.subject) + "\n"
Chris@119 361 when :image
Chris@119 362 image_subject(options, issue.subject)
Chris@119 363 when :pdf
Chris@119 364 pdf_new_page?(options)
Chris@119 365 pdf_subject(options, issue.subject)
Chris@119 366 end
chris@22 367
Chris@119 368 unless issue.leaf?
Chris@119 369 @issue_ancestors << issue
Chris@119 370 options[:indent] += options[:indent_increment]
Chris@119 371 end
chris@22 372
Chris@119 373 output
chris@22 374 end
chris@22 375
chris@22 376 def line_for_issue(issue, options)
chris@22 377 # Skip issues that don't have a due_before (due_date or version's due_date)
chris@22 378 if issue.is_a?(Issue) && issue.due_before
Chris@119 379 coords = coordinates(issue.start_date, issue.due_before, issue.done_ratio, options[:zoom])
Chris@119 380 label = "#{ issue.status.name } #{ issue.done_ratio }%"
Chris@119 381
chris@22 382 case options[:format]
chris@22 383 when :html
Chris@119 384 html_task(options, coords, :css => "task " + (issue.leaf? ? 'leaf' : 'parent'), :label => label, :issue => issue, :markers => !issue.leaf?)
chris@22 385 when :image
Chris@119 386 image_task(options, coords, :label => label)
chris@22 387 when :pdf
Chris@119 388 pdf_task(options, coords, :label => label)
Chris@119 389 end
chris@22 390 else
chris@22 391 ActiveRecord::Base.logger.debug "GanttHelper#line_for_issue was not given an issue with a due_before"
chris@22 392 ''
chris@22 393 end
chris@22 394 end
chris@22 395
Chris@0 396 # Generates a gantt image
Chris@0 397 # Only defined if RMagick is avalaible
chris@22 398 def to_image(format='PNG')
Chris@0 399 date_to = (@date_from >> @months)-1
Chris@0 400 show_weeks = @zoom > 1
Chris@0 401 show_days = @zoom > 2
Chris@0 402
Chris@1 403 subject_width = 400
Chris@0 404 header_heigth = 18
Chris@0 405 # width of one day in pixels
Chris@0 406 zoom = @zoom*2
Chris@0 407 g_width = (@date_to - @date_from + 1)*zoom
chris@22 408 g_height = 20 * number_of_rows + 30
Chris@0 409 headers_heigth = (show_weeks ? 2*header_heigth : header_heigth)
Chris@0 410 height = g_height + headers_heigth
Chris@0 411
Chris@0 412 imgl = Magick::ImageList.new
Chris@0 413 imgl.new_image(subject_width+g_width+1, height)
Chris@0 414 gc = Magick::Draw.new
Chris@0 415
Chris@0 416 # Subjects
Chris@119 417 gc.stroke('transparent')
chris@22 418 subjects(:image => gc, :top => (headers_heigth + 20), :indent => 4, :format => :image)
Chris@0 419
Chris@0 420 # Months headers
Chris@0 421 month_f = @date_from
Chris@0 422 left = subject_width
Chris@0 423 @months.times do
Chris@0 424 width = ((month_f >> 1) - month_f) * zoom
Chris@0 425 gc.fill('white')
Chris@0 426 gc.stroke('grey')
Chris@0 427 gc.stroke_width(1)
Chris@0 428 gc.rectangle(left, 0, left + width, height)
Chris@0 429 gc.fill('black')
Chris@0 430 gc.stroke('transparent')
Chris@0 431 gc.stroke_width(1)
Chris@0 432 gc.text(left.round + 8, 14, "#{month_f.year}-#{month_f.month}")
Chris@0 433 left = left + width
Chris@0 434 month_f = month_f >> 1
Chris@0 435 end
Chris@0 436
Chris@0 437 # Weeks headers
Chris@0 438 if show_weeks
Chris@0 439 left = subject_width
Chris@0 440 height = header_heigth
Chris@0 441 if @date_from.cwday == 1
Chris@0 442 # date_from is monday
Chris@0 443 week_f = date_from
Chris@0 444 else
Chris@0 445 # find next monday after date_from
Chris@0 446 week_f = @date_from + (7 - @date_from.cwday + 1)
Chris@0 447 width = (7 - @date_from.cwday + 1) * zoom
Chris@0 448 gc.fill('white')
Chris@0 449 gc.stroke('grey')
Chris@0 450 gc.stroke_width(1)
Chris@0 451 gc.rectangle(left, header_heigth, left + width, 2*header_heigth + g_height-1)
Chris@0 452 left = left + width
Chris@0 453 end
Chris@0 454 while week_f <= date_to
Chris@0 455 width = (week_f + 6 <= date_to) ? 7 * zoom : (date_to - week_f + 1) * zoom
Chris@0 456 gc.fill('white')
Chris@0 457 gc.stroke('grey')
Chris@0 458 gc.stroke_width(1)
Chris@0 459 gc.rectangle(left.round, header_heigth, left.round + width, 2*header_heigth + g_height-1)
Chris@0 460 gc.fill('black')
Chris@0 461 gc.stroke('transparent')
Chris@0 462 gc.stroke_width(1)
Chris@0 463 gc.text(left.round + 2, header_heigth + 14, week_f.cweek.to_s)
Chris@0 464 left = left + width
Chris@0 465 week_f = week_f+7
Chris@0 466 end
Chris@0 467 end
Chris@0 468
Chris@0 469 # Days details (week-end in grey)
Chris@0 470 if show_days
Chris@0 471 left = subject_width
Chris@0 472 height = g_height + header_heigth - 1
Chris@0 473 wday = @date_from.cwday
Chris@0 474 (date_to - @date_from + 1).to_i.times do
Chris@0 475 width = zoom
Chris@0 476 gc.fill(wday == 6 || wday == 7 ? '#eee' : 'white')
Chris@119 477 gc.stroke('#ddd')
Chris@0 478 gc.stroke_width(1)
Chris@0 479 gc.rectangle(left, 2*header_heigth, left + width, 2*header_heigth + g_height-1)
Chris@0 480 left = left + width
Chris@0 481 wday = wday + 1
Chris@0 482 wday = 1 if wday > 7
Chris@0 483 end
Chris@0 484 end
Chris@0 485
Chris@0 486 # border
Chris@0 487 gc.fill('transparent')
Chris@0 488 gc.stroke('grey')
Chris@0 489 gc.stroke_width(1)
Chris@0 490 gc.rectangle(0, 0, subject_width+g_width, headers_heigth)
Chris@0 491 gc.stroke('black')
Chris@0 492 gc.rectangle(0, 0, subject_width+g_width, g_height+ headers_heigth-1)
Chris@0 493
Chris@0 494 # content
Chris@0 495 top = headers_heigth + 20
Chris@119 496
Chris@119 497 gc.stroke('transparent')
chris@22 498 lines(:image => gc, :top => top, :zoom => zoom, :subject_width => subject_width, :format => :image)
Chris@0 499
Chris@0 500 # today red line
Chris@0 501 if Date.today >= @date_from and Date.today <= date_to
Chris@0 502 gc.stroke('red')
Chris@0 503 x = (Date.today-@date_from+1)*zoom + subject_width
Chris@0 504 gc.line(x, headers_heigth, x, headers_heigth + g_height-1)
Chris@0 505 end
Chris@0 506
Chris@0 507 gc.draw(imgl)
Chris@0 508 imgl.format = format
Chris@0 509 imgl.to_blob
Chris@0 510 end if Object.const_defined?(:Magick)
chris@22 511
chris@22 512 def to_pdf
chris@22 513 pdf = ::Redmine::Export::PDF::IFPDF.new(current_language)
chris@22 514 pdf.SetTitle("#{l(:label_gantt)} #{project}")
chris@22 515 pdf.AliasNbPages
chris@22 516 pdf.footer_date = format_date(Date.today)
chris@22 517 pdf.AddPage("L")
chris@22 518 pdf.SetFontStyle('B',12)
chris@22 519 pdf.SetX(15)
chris@22 520 pdf.Cell(PDF::LeftPaneWidth, 20, project.to_s)
chris@22 521 pdf.Ln
chris@22 522 pdf.SetFontStyle('B',9)
chris@22 523
chris@22 524 subject_width = PDF::LeftPaneWidth
chris@22 525 header_heigth = 5
chris@22 526
chris@22 527 headers_heigth = header_heigth
chris@22 528 show_weeks = false
chris@22 529 show_days = false
chris@22 530
chris@22 531 if self.months < 7
chris@22 532 show_weeks = true
chris@22 533 headers_heigth = 2*header_heigth
chris@22 534 if self.months < 3
chris@22 535 show_days = true
chris@22 536 headers_heigth = 3*header_heigth
chris@22 537 end
chris@22 538 end
chris@22 539
chris@22 540 g_width = PDF.right_pane_width
chris@22 541 zoom = (g_width) / (self.date_to - self.date_from + 1)
chris@22 542 g_height = 120
chris@22 543 t_height = g_height + headers_heigth
chris@22 544
chris@22 545 y_start = pdf.GetY
chris@22 546
chris@22 547 # Months headers
chris@22 548 month_f = self.date_from
chris@22 549 left = subject_width
chris@22 550 height = header_heigth
chris@22 551 self.months.times do
chris@22 552 width = ((month_f >> 1) - month_f) * zoom
chris@22 553 pdf.SetY(y_start)
chris@22 554 pdf.SetX(left)
chris@22 555 pdf.Cell(width, height, "#{month_f.year}-#{month_f.month}", "LTR", 0, "C")
chris@22 556 left = left + width
chris@22 557 month_f = month_f >> 1
chris@22 558 end
chris@22 559
chris@22 560 # Weeks headers
chris@22 561 if show_weeks
chris@22 562 left = subject_width
chris@22 563 height = header_heigth
chris@22 564 if self.date_from.cwday == 1
chris@22 565 # self.date_from is monday
chris@22 566 week_f = self.date_from
chris@22 567 else
chris@22 568 # find next monday after self.date_from
chris@22 569 week_f = self.date_from + (7 - self.date_from.cwday + 1)
chris@22 570 width = (7 - self.date_from.cwday + 1) * zoom-1
chris@22 571 pdf.SetY(y_start + header_heigth)
chris@22 572 pdf.SetX(left)
chris@22 573 pdf.Cell(width + 1, height, "", "LTR")
chris@22 574 left = left + width+1
chris@22 575 end
chris@22 576 while week_f <= self.date_to
chris@22 577 width = (week_f + 6 <= self.date_to) ? 7 * zoom : (self.date_to - week_f + 1) * zoom
chris@22 578 pdf.SetY(y_start + header_heigth)
chris@22 579 pdf.SetX(left)
chris@22 580 pdf.Cell(width, height, (width >= 5 ? week_f.cweek.to_s : ""), "LTR", 0, "C")
chris@22 581 left = left + width
chris@22 582 week_f = week_f+7
chris@22 583 end
chris@22 584 end
chris@22 585
chris@22 586 # Days headers
chris@22 587 if show_days
chris@22 588 left = subject_width
chris@22 589 height = header_heigth
chris@22 590 wday = self.date_from.cwday
chris@22 591 pdf.SetFontStyle('B',7)
chris@22 592 (self.date_to - self.date_from + 1).to_i.times do
chris@22 593 width = zoom
chris@22 594 pdf.SetY(y_start + 2 * header_heigth)
chris@22 595 pdf.SetX(left)
chris@22 596 pdf.Cell(width, height, day_name(wday).first, "LTR", 0, "C")
chris@22 597 left = left + width
chris@22 598 wday = wday + 1
chris@22 599 wday = 1 if wday > 7
chris@22 600 end
chris@22 601 end
chris@22 602
chris@22 603 pdf.SetY(y_start)
chris@22 604 pdf.SetX(15)
chris@22 605 pdf.Cell(subject_width+g_width-15, headers_heigth, "", 1)
chris@22 606
chris@22 607 # Tasks
chris@22 608 top = headers_heigth + y_start
Chris@119 609 options = {
Chris@119 610 :top => top,
Chris@119 611 :zoom => zoom,
Chris@119 612 :subject_width => subject_width,
Chris@119 613 :g_width => g_width,
Chris@119 614 :indent => 0,
Chris@119 615 :indent_increment => 5,
Chris@119 616 :top_increment => 5,
Chris@119 617 :format => :pdf,
Chris@119 618 :pdf => pdf
Chris@119 619 }
Chris@119 620 render(options)
chris@22 621 pdf.Output
chris@22 622 end
Chris@0 623
Chris@0 624 private
Chris@119 625
Chris@119 626 def coordinates(start_date, end_date, progress, zoom=nil)
Chris@119 627 zoom ||= @zoom
Chris@119 628
Chris@119 629 coords = {}
Chris@119 630 if start_date && end_date && start_date < self.date_to && end_date > self.date_from
Chris@119 631 if start_date > self.date_from
Chris@119 632 coords[:start] = start_date - self.date_from
Chris@119 633 coords[:bar_start] = start_date - self.date_from
Chris@119 634 else
Chris@119 635 coords[:bar_start] = 0
Chris@119 636 end
Chris@119 637 if end_date < self.date_to
Chris@119 638 coords[:end] = end_date - self.date_from
Chris@119 639 coords[:bar_end] = end_date - self.date_from + 1
Chris@119 640 else
Chris@119 641 coords[:bar_end] = self.date_to - self.date_from + 1
Chris@119 642 end
Chris@119 643
Chris@119 644 if progress
Chris@119 645 progress_date = start_date + (end_date - start_date) * (progress / 100.0)
Chris@119 646 if progress_date > self.date_from && progress_date > start_date
Chris@119 647 if progress_date < self.date_to
Chris@119 648 coords[:bar_progress_end] = progress_date - self.date_from + 1
Chris@119 649 else
Chris@119 650 coords[:bar_progress_end] = self.date_to - self.date_from + 1
Chris@119 651 end
Chris@119 652 end
Chris@119 653
Chris@119 654 if progress_date < Date.today
Chris@119 655 late_date = [Date.today, end_date].min
Chris@119 656 if late_date > self.date_from && late_date > start_date
Chris@119 657 if late_date < self.date_to
Chris@119 658 coords[:bar_late_end] = late_date - self.date_from + 1
Chris@119 659 else
Chris@119 660 coords[:bar_late_end] = self.date_to - self.date_from + 1
Chris@119 661 end
Chris@119 662 end
Chris@119 663 end
Chris@119 664 end
Chris@119 665 end
Chris@119 666
Chris@119 667 # Transforms dates into pixels witdh
Chris@119 668 coords.keys.each do |key|
Chris@119 669 coords[key] = (coords[key] * zoom).floor
Chris@119 670 end
Chris@119 671 coords
Chris@119 672 end
chris@22 673
Chris@119 674 # Sorts a collection of issues by start_date, due_date, id for gantt rendering
Chris@119 675 def sort_issues!(issues)
Chris@119 676 issues.sort! { |a, b| gantt_issue_compare(a, b, issues) }
Chris@119 677 end
Chris@119 678
Chris@119 679 # TODO: top level issues should be sorted by start date
Chris@119 680 def gantt_issue_compare(x, y, issues)
Chris@119 681 if x.root_id == y.root_id
Chris@119 682 x.lft <=> y.lft
Chris@0 683 else
Chris@119 684 x.root_id <=> y.root_id
Chris@119 685 end
Chris@119 686 end
Chris@119 687
Chris@119 688 def current_limit
Chris@119 689 if @max_rows
Chris@119 690 @max_rows - @number_of_rows
Chris@119 691 else
Chris@119 692 nil
Chris@119 693 end
Chris@119 694 end
Chris@119 695
Chris@119 696 def abort?
Chris@119 697 if @max_rows && @number_of_rows >= @max_rows
Chris@119 698 @truncated = true
Chris@119 699 end
Chris@119 700 end
Chris@119 701
Chris@119 702 def pdf_new_page?(options)
Chris@119 703 if options[:top] > 180
Chris@119 704 options[:pdf].Line(15, options[:top], PDF::TotalWidth, options[:top])
Chris@119 705 options[:pdf].AddPage("L")
Chris@119 706 options[:top] = 15
Chris@119 707 options[:pdf].Line(15, options[:top] - 0.1, PDF::TotalWidth, options[:top] - 0.1)
Chris@119 708 end
Chris@119 709 end
Chris@119 710
Chris@119 711 def html_subject(params, subject, options={})
Chris@245 712 style = "position: absolute;top:#{params[:top]}px;left:#{params[:indent]}px;"
Chris@245 713 style << "width:#{params[:subject_width] - params[:indent]}px;" if params[:subject_width]
Chris@245 714
Chris@245 715 output = view.content_tag 'div', subject, :class => options[:css], :style => style, :title => options[:title]
Chris@119 716 @subjects << output
Chris@119 717 output
Chris@119 718 end
Chris@119 719
Chris@119 720 def pdf_subject(params, subject, options={})
Chris@119 721 params[:pdf].SetY(params[:top])
Chris@119 722 params[:pdf].SetX(15)
Chris@119 723
Chris@119 724 char_limit = PDF::MaxCharactorsForSubject - params[:indent]
Chris@119 725 params[:pdf].Cell(params[:subject_width]-15, 5, (" " * params[:indent]) + subject.to_s.sub(/^(.{#{char_limit}}[^\s]*\s).*$/, '\1 (...)'), "LR")
Chris@119 726
Chris@119 727 params[:pdf].SetY(params[:top])
Chris@119 728 params[:pdf].SetX(params[:subject_width])
Chris@119 729 params[:pdf].Cell(params[:g_width], 5, "", "LR")
Chris@119 730 end
Chris@119 731
Chris@119 732 def image_subject(params, subject, options={})
Chris@119 733 params[:image].fill('black')
Chris@119 734 params[:image].stroke('transparent')
Chris@119 735 params[:image].stroke_width(1)
Chris@119 736 params[:image].text(params[:indent], params[:top] + 2, subject)
Chris@119 737 end
Chris@119 738
Chris@119 739 def html_task(params, coords, options={})
Chris@119 740 output = ''
Chris@119 741 # Renders the task bar, with progress and late
Chris@119 742 if coords[:bar_start] && coords[:bar_end]
Chris@119 743 output << "<div style='top:#{ params[:top] }px;left:#{ coords[:bar_start] }px;width:#{ coords[:bar_end] - coords[:bar_start] - 2}px;' class='#{options[:css]} task_todo'>&nbsp;</div>"
Chris@119 744
Chris@119 745 if coords[:bar_late_end]
Chris@119 746 output << "<div style='top:#{ params[:top] }px;left:#{ coords[:bar_start] }px;width:#{ coords[:bar_late_end] - coords[:bar_start] - 2}px;' class='#{options[:css]} task_late'>&nbsp;</div>"
Chris@0 747 end
Chris@119 748 if coords[:bar_progress_end]
Chris@119 749 output << "<div style='top:#{ params[:top] }px;left:#{ coords[:bar_start] }px;width:#{ coords[:bar_progress_end] - coords[:bar_start] - 2}px;' class='#{options[:css]} task_done'>&nbsp;</div>"
Chris@119 750 end
Chris@119 751 end
Chris@119 752 # Renders the markers
Chris@119 753 if options[:markers]
Chris@119 754 if coords[:start]
Chris@119 755 output << "<div style='top:#{ params[:top] }px;left:#{ coords[:start] }px;width:15px;' class='#{options[:css]} marker starting'>&nbsp;</div>"
Chris@119 756 end
Chris@119 757 if coords[:end]
Chris@119 758 output << "<div style='top:#{ params[:top] }px;left:#{ coords[:end] + params[:zoom] }px;width:15px;' class='#{options[:css]} marker ending'>&nbsp;</div>"
Chris@119 759 end
Chris@119 760 end
Chris@119 761 # Renders the label on the right
Chris@119 762 if options[:label]
Chris@119 763 output << "<div style='top:#{ params[:top] }px;left:#{ (coords[:bar_end] || 0) + 8 }px;' class='#{options[:css]} label'>"
Chris@119 764 output << options[:label]
Chris@119 765 output << "</div>"
Chris@119 766 end
Chris@119 767 # Renders the tooltip
Chris@119 768 if options[:issue] && coords[:bar_start] && coords[:bar_end]
Chris@119 769 output << "<div class='tooltip' style='position: absolute;top:#{ params[:top] }px;left:#{ coords[:bar_start] }px;width:#{ coords[:bar_end] - coords[:bar_start] }px;height:12px;'>"
Chris@119 770 output << '<span class="tip">'
Chris@119 771 output << view.render_issue_tooltip(options[:issue])
Chris@119 772 output << "</span></div>"
Chris@119 773 end
Chris@119 774 @lines << output
Chris@119 775 output
Chris@119 776 end
Chris@119 777
Chris@119 778 def pdf_task(params, coords, options={})
Chris@119 779 height = options[:height] || 2
Chris@119 780
Chris@119 781 # Renders the task bar, with progress and late
Chris@119 782 if coords[:bar_start] && coords[:bar_end]
Chris@119 783 params[:pdf].SetY(params[:top]+1.5)
Chris@119 784 params[:pdf].SetX(params[:subject_width] + coords[:bar_start])
Chris@119 785 params[:pdf].SetFillColor(200,200,200)
Chris@119 786 params[:pdf].Cell(coords[:bar_end] - coords[:bar_start], height, "", 0, 0, "", 1)
Chris@119 787
Chris@119 788 if coords[:bar_late_end]
Chris@119 789 params[:pdf].SetY(params[:top]+1.5)
Chris@119 790 params[:pdf].SetX(params[:subject_width] + coords[:bar_start])
Chris@119 791 params[:pdf].SetFillColor(255,100,100)
Chris@119 792 params[:pdf].Cell(coords[:bar_late_end] - coords[:bar_start], height, "", 0, 0, "", 1)
Chris@119 793 end
Chris@119 794 if coords[:bar_progress_end]
Chris@119 795 params[:pdf].SetY(params[:top]+1.5)
Chris@119 796 params[:pdf].SetX(params[:subject_width] + coords[:bar_start])
Chris@119 797 params[:pdf].SetFillColor(90,200,90)
Chris@119 798 params[:pdf].Cell(coords[:bar_progress_end] - coords[:bar_start], height, "", 0, 0, "", 1)
Chris@119 799 end
Chris@119 800 end
Chris@119 801 # Renders the markers
Chris@119 802 if options[:markers]
Chris@119 803 if coords[:start]
Chris@119 804 params[:pdf].SetY(params[:top] + 1)
Chris@119 805 params[:pdf].SetX(params[:subject_width] + coords[:start] - 1)
Chris@119 806 params[:pdf].SetFillColor(50,50,200)
Chris@119 807 params[:pdf].Cell(2, 2, "", 0, 0, "", 1)
Chris@119 808 end
Chris@119 809 if coords[:end]
Chris@119 810 params[:pdf].SetY(params[:top] + 1)
Chris@119 811 params[:pdf].SetX(params[:subject_width] + coords[:end] - 1)
Chris@119 812 params[:pdf].SetFillColor(50,50,200)
Chris@119 813 params[:pdf].Cell(2, 2, "", 0, 0, "", 1)
Chris@119 814 end
Chris@119 815 end
Chris@119 816 # Renders the label on the right
Chris@119 817 if options[:label]
Chris@119 818 params[:pdf].SetX(params[:subject_width] + (coords[:bar_end] || 0) + 5)
Chris@119 819 params[:pdf].Cell(30, 2, options[:label])
Chris@0 820 end
Chris@0 821 end
chris@22 822
Chris@119 823 def image_task(params, coords, options={})
Chris@119 824 height = options[:height] || 6
Chris@119 825
Chris@119 826 # Renders the task bar, with progress and late
Chris@119 827 if coords[:bar_start] && coords[:bar_end]
Chris@119 828 params[:image].fill('#aaa')
Chris@119 829 params[:image].rectangle(params[:subject_width] + coords[:bar_start], params[:top], params[:subject_width] + coords[:bar_end], params[:top] - height)
Chris@119 830
Chris@119 831 if coords[:bar_late_end]
Chris@119 832 params[:image].fill('#f66')
Chris@119 833 params[:image].rectangle(params[:subject_width] + coords[:bar_start], params[:top], params[:subject_width] + coords[:bar_late_end], params[:top] - height)
Chris@119 834 end
Chris@119 835 if coords[:bar_progress_end]
Chris@119 836 params[:image].fill('#00c600')
Chris@119 837 params[:image].rectangle(params[:subject_width] + coords[:bar_start], params[:top], params[:subject_width] + coords[:bar_progress_end], params[:top] - height)
Chris@119 838 end
Chris@119 839 end
Chris@119 840 # Renders the markers
Chris@119 841 if options[:markers]
Chris@119 842 if coords[:start]
Chris@119 843 x = params[:subject_width] + coords[:start]
Chris@119 844 y = params[:top] - height / 2
Chris@119 845 params[:image].fill('blue')
Chris@119 846 params[:image].polygon(x-4, y, x, y-4, x+4, y, x, y+4)
Chris@119 847 end
Chris@119 848 if coords[:end]
Chris@119 849 x = params[:subject_width] + coords[:end] + params[:zoom]
Chris@119 850 y = params[:top] - height / 2
Chris@119 851 params[:image].fill('blue')
Chris@119 852 params[:image].polygon(x-4, y, x, y-4, x+4, y, x, y+4)
Chris@119 853 end
Chris@119 854 end
Chris@119 855 # Renders the label on the right
Chris@119 856 if options[:label]
Chris@119 857 params[:image].fill('black')
Chris@119 858 params[:image].text(params[:subject_width] + (coords[:bar_end] || 0) + 5,params[:top] + 1, options[:label])
Chris@119 859 end
Chris@119 860 end
Chris@0 861 end
Chris@0 862 end
Chris@0 863 end