annotate .svn/pristine/10/1048cea83e08be09288d325f5a2d1908b807f592.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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