annotate lib/redmine/helpers/gantt.rb @ 1174:928c0d0f624e bug_365

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