Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: module Redmine Chris@909: module Helpers Chris@909: # Simple class to handle gantt chart data Chris@909: class Gantt Chris@909: include ERB::Util Chris@909: include Redmine::I18n Chris@909: Chris@909: # :nodoc: Chris@909: # Some utility methods for the PDF export Chris@909: class PDF Chris@909: MaxCharactorsForSubject = 45 Chris@909: TotalWidth = 280 Chris@909: LeftPaneWidth = 100 Chris@909: Chris@909: def self.right_pane_width Chris@909: TotalWidth - LeftPaneWidth Chris@909: end Chris@909: end Chris@909: Chris@909: attr_reader :year_from, :month_from, :date_from, :date_to, :zoom, :months, :truncated, :max_rows Chris@909: attr_accessor :query Chris@909: attr_accessor :project Chris@909: attr_accessor :view Chris@909: Chris@909: def initialize(options={}) Chris@909: options = options.dup Chris@909: Chris@909: if options[:year] && options[:year].to_i >0 Chris@909: @year_from = options[:year].to_i Chris@909: if options[:month] && options[:month].to_i >=1 && options[:month].to_i <= 12 Chris@909: @month_from = options[:month].to_i Chris@909: else Chris@909: @month_from = 1 Chris@909: end Chris@909: else Chris@909: @month_from ||= Date.today.month Chris@909: @year_from ||= Date.today.year Chris@909: end Chris@909: Chris@909: zoom = (options[:zoom] || User.current.pref[:gantt_zoom]).to_i Chris@909: @zoom = (zoom > 0 && zoom < 5) ? zoom : 2 Chris@909: months = (options[:months] || User.current.pref[:gantt_months]).to_i Chris@909: @months = (months > 0 && months < 25) ? months : 6 Chris@909: Chris@909: # Save gantt parameters as user preference (zoom and months count) Chris@909: if (User.current.logged? && (@zoom != User.current.pref[:gantt_zoom] || @months != User.current.pref[:gantt_months])) Chris@909: User.current.pref[:gantt_zoom], User.current.pref[:gantt_months] = @zoom, @months Chris@909: User.current.preference.save Chris@909: end Chris@909: Chris@909: @date_from = Date.civil(@year_from, @month_from, 1) Chris@909: @date_to = (@date_from >> @months) - 1 Chris@909: Chris@909: @subjects = '' Chris@909: @lines = '' Chris@909: @number_of_rows = nil Chris@909: Chris@909: @issue_ancestors = [] Chris@909: Chris@909: @truncated = false Chris@909: if options.has_key?(:max_rows) Chris@909: @max_rows = options[:max_rows] Chris@909: else Chris@909: @max_rows = Setting.gantt_items_limit.blank? ? nil : Setting.gantt_items_limit.to_i Chris@909: end Chris@909: end Chris@909: Chris@909: def common_params Chris@909: { :controller => 'gantts', :action => 'show', :project_id => @project } Chris@909: end Chris@909: Chris@909: def params Chris@909: common_params.merge({ :zoom => zoom, :year => year_from, :month => month_from, :months => months }) Chris@909: end Chris@909: Chris@909: def params_previous Chris@909: common_params.merge({:year => (date_from << months).year, :month => (date_from << months).month, :zoom => zoom, :months => months }) Chris@909: end Chris@909: Chris@909: def params_next Chris@909: common_params.merge({:year => (date_from >> months).year, :month => (date_from >> months).month, :zoom => zoom, :months => months }) Chris@909: end Chris@909: Chris@909: # Returns the number of rows that will be rendered on the Gantt chart Chris@909: def number_of_rows Chris@909: return @number_of_rows if @number_of_rows Chris@909: Chris@909: rows = projects.inject(0) {|total, p| total += number_of_rows_on_project(p)} Chris@909: rows > @max_rows ? @max_rows : rows Chris@909: end Chris@909: Chris@909: # Returns the number of rows that will be used to list a project on Chris@909: # the Gantt chart. This will recurse for each subproject. Chris@909: def number_of_rows_on_project(project) Chris@909: return 0 unless projects.include?(project) Chris@909: Chris@909: count = 1 Chris@909: count += project_issues(project).size Chris@909: count += project_versions(project).size Chris@909: count Chris@909: end Chris@909: Chris@909: # Renders the subjects of the Gantt chart, the left side. Chris@909: def subjects(options={}) Chris@909: render(options.merge(:only => :subjects)) unless @subjects_rendered Chris@909: @subjects Chris@909: end Chris@909: Chris@909: # Renders the lines of the Gantt chart, the right side Chris@909: def lines(options={}) Chris@909: render(options.merge(:only => :lines)) unless @lines_rendered Chris@909: @lines Chris@909: end Chris@909: Chris@909: # Returns issues that will be rendered Chris@909: def issues Chris@909: @issues ||= @query.issues( Chris@909: :include => [:assigned_to, :tracker, :priority, :category, :fixed_version], Chris@909: :order => "#{Project.table_name}.lft ASC, #{Issue.table_name}.id ASC", Chris@909: :limit => @max_rows Chris@909: ) Chris@909: end Chris@909: Chris@909: # Return all the project nodes that will be displayed Chris@909: def projects Chris@909: return @projects if @projects Chris@909: Chris@909: ids = issues.collect(&:project).uniq.collect(&:id) Chris@909: if ids.any? Chris@909: # All issues projects and their visible ancestors Chris@909: @projects = Project.visible.all( Chris@909: :joins => "LEFT JOIN #{Project.table_name} child ON #{Project.table_name}.lft <= child.lft AND #{Project.table_name}.rgt >= child.rgt", Chris@909: :conditions => ["child.id IN (?)", ids], Chris@909: :order => "#{Project.table_name}.lft ASC" Chris@909: ).uniq Chris@909: else Chris@909: @projects = [] Chris@909: end Chris@909: end Chris@909: Chris@909: # Returns the issues that belong to +project+ Chris@909: def project_issues(project) Chris@909: @issues_by_project ||= issues.group_by(&:project) Chris@909: @issues_by_project[project] || [] Chris@909: end Chris@909: Chris@909: # Returns the distinct versions of the issues that belong to +project+ Chris@909: def project_versions(project) Chris@909: project_issues(project).collect(&:fixed_version).compact.uniq Chris@909: end Chris@909: Chris@909: # Returns the issues that belong to +project+ and are assigned to +version+ Chris@909: def version_issues(project, version) Chris@909: project_issues(project).select {|issue| issue.fixed_version == version} Chris@909: end Chris@909: Chris@909: def render(options={}) Chris@909: options = {:top => 0, :top_increment => 20, :indent_increment => 20, :render => :subject, :format => :html}.merge(options) Chris@909: indent = options[:indent] || 4 Chris@909: Chris@909: @subjects = '' unless options[:only] == :lines Chris@909: @lines = '' unless options[:only] == :subjects Chris@909: @number_of_rows = 0 Chris@909: Chris@909: Project.project_tree(projects) do |project, level| Chris@909: options[:indent] = indent + level * options[:indent_increment] Chris@909: render_project(project, options) Chris@909: break if abort? Chris@909: end Chris@909: Chris@909: @subjects_rendered = true unless options[:only] == :lines Chris@909: @lines_rendered = true unless options[:only] == :subjects Chris@909: Chris@909: render_end(options) Chris@909: end Chris@909: Chris@909: def render_project(project, options={}) Chris@909: subject_for_project(project, options) unless options[:only] == :lines Chris@909: line_for_project(project, options) unless options[:only] == :subjects Chris@909: Chris@909: options[:top] += options[:top_increment] Chris@909: options[:indent] += options[:indent_increment] Chris@909: @number_of_rows += 1 Chris@909: return if abort? Chris@909: Chris@909: issues = project_issues(project).select {|i| i.fixed_version.nil?} Chris@909: sort_issues!(issues) Chris@909: if issues Chris@909: render_issues(issues, options) Chris@909: return if abort? Chris@909: end Chris@909: Chris@909: versions = project_versions(project) Chris@909: versions.each do |version| Chris@909: render_version(project, version, options) Chris@909: end Chris@909: Chris@909: # Remove indent to hit the next sibling Chris@909: options[:indent] -= options[:indent_increment] Chris@909: end Chris@909: Chris@909: def render_issues(issues, options={}) Chris@909: @issue_ancestors = [] Chris@909: Chris@909: issues.each do |i| Chris@909: subject_for_issue(i, options) unless options[:only] == :lines Chris@909: line_for_issue(i, options) unless options[:only] == :subjects Chris@909: Chris@909: options[:top] += options[:top_increment] Chris@909: @number_of_rows += 1 Chris@909: break if abort? Chris@909: end Chris@909: Chris@909: options[:indent] -= (options[:indent_increment] * @issue_ancestors.size) Chris@909: end Chris@909: Chris@909: def render_version(project, version, options={}) Chris@909: # Version header Chris@909: subject_for_version(version, options) unless options[:only] == :lines Chris@909: line_for_version(version, options) unless options[:only] == :subjects Chris@909: Chris@909: options[:top] += options[:top_increment] Chris@909: @number_of_rows += 1 Chris@909: return if abort? Chris@909: Chris@909: issues = version_issues(project, version) Chris@909: if issues Chris@909: sort_issues!(issues) Chris@909: # Indent issues Chris@909: options[:indent] += options[:indent_increment] Chris@909: render_issues(issues, options) Chris@909: options[:indent] -= options[:indent_increment] Chris@909: end Chris@909: end Chris@909: Chris@909: def render_end(options={}) Chris@909: case options[:format] Chris@909: when :pdf Chris@909: options[:pdf].Line(15, options[:top], PDF::TotalWidth, options[:top]) Chris@909: end Chris@909: end Chris@909: Chris@909: def subject_for_project(project, options) Chris@909: case options[:format] Chris@909: when :html Chris@909: subject = "".html_safe Chris@909: subject << view.link_to_project(project).html_safe Chris@909: subject << ''.html_safe Chris@909: html_subject(options, subject, :css => "project-name") Chris@909: when :image Chris@909: image_subject(options, project.name) Chris@909: when :pdf Chris@909: pdf_new_page?(options) Chris@909: pdf_subject(options, project.name) Chris@909: end Chris@909: end Chris@909: Chris@909: def line_for_project(project, options) Chris@909: # Skip versions that don't have a start_date or due date Chris@909: if project.is_a?(Project) && project.start_date && project.due_date Chris@909: options[:zoom] ||= 1 Chris@909: options[:g_width] ||= (self.date_to - self.date_from + 1) * options[:zoom] Chris@909: Chris@909: coords = coordinates(project.start_date, project.due_date, nil, options[:zoom]) Chris@909: label = h(project) Chris@909: Chris@909: case options[:format] Chris@909: when :html Chris@909: html_task(options, coords, :css => "project task", :label => label, :markers => true) Chris@909: when :image Chris@909: image_task(options, coords, :label => label, :markers => true, :height => 3) Chris@909: when :pdf Chris@909: pdf_task(options, coords, :label => label, :markers => true, :height => 0.8) Chris@909: end Chris@909: else Chris@909: ActiveRecord::Base.logger.debug "Gantt#line_for_project was not given a project with a start_date" Chris@909: '' Chris@909: end Chris@909: end Chris@909: Chris@909: def subject_for_version(version, options) Chris@909: case options[:format] Chris@909: when :html Chris@909: subject = "".html_safe Chris@909: subject << view.link_to_version(version).html_safe Chris@909: subject << ''.html_safe Chris@909: html_subject(options, subject, :css => "version-name") Chris@909: when :image Chris@909: image_subject(options, version.to_s_with_project) Chris@909: when :pdf Chris@909: pdf_new_page?(options) Chris@909: pdf_subject(options, version.to_s_with_project) Chris@909: end Chris@909: end Chris@909: Chris@909: def line_for_version(version, options) Chris@909: # Skip versions that don't have a start_date Chris@909: if version.is_a?(Version) && version.start_date && version.due_date Chris@909: options[:zoom] ||= 1 Chris@909: options[:g_width] ||= (self.date_to - self.date_from + 1) * options[:zoom] Chris@909: Chris@909: coords = coordinates(version.start_date, version.due_date, version.completed_pourcent, options[:zoom]) Chris@909: label = "#{h version } #{h version.completed_pourcent.to_i.to_s}%" Chris@909: label = h("#{version.project} -") + label unless @project && @project == version.project Chris@909: Chris@909: case options[:format] Chris@909: when :html Chris@909: html_task(options, coords, :css => "version task", :label => label, :markers => true) Chris@909: when :image Chris@909: image_task(options, coords, :label => label, :markers => true, :height => 3) Chris@909: when :pdf Chris@909: pdf_task(options, coords, :label => label, :markers => true, :height => 0.8) Chris@909: end Chris@909: else Chris@909: ActiveRecord::Base.logger.debug "Gantt#line_for_version was not given a version with a start_date" Chris@909: '' Chris@909: end Chris@909: end Chris@909: Chris@909: def subject_for_issue(issue, options) Chris@909: while @issue_ancestors.any? && !issue.is_descendant_of?(@issue_ancestors.last) Chris@909: @issue_ancestors.pop Chris@909: options[:indent] -= options[:indent_increment] Chris@909: end Chris@909: Chris@909: output = case options[:format] Chris@909: when :html Chris@909: css_classes = '' Chris@909: css_classes << ' issue-overdue' if issue.overdue? Chris@909: css_classes << ' issue-behind-schedule' if issue.behind_schedule? Chris@909: css_classes << ' icon icon-issue' unless Setting.gravatar_enabled? && issue.assigned_to Chris@909: Chris@909: subject = "".html_safe Chris@909: if issue.assigned_to.present? Chris@909: assigned_string = l(:field_assigned_to) + ": " + issue.assigned_to.name Chris@909: subject << view.avatar(issue.assigned_to, :class => 'gravatar icon-gravatar', :size => 10, :title => assigned_string).to_s.html_safe Chris@909: end Chris@909: subject << view.link_to_issue(issue).html_safe Chris@909: subject << ''.html_safe Chris@909: html_subject(options, subject, :css => "issue-subject", :title => issue.subject) + "\n" Chris@909: when :image Chris@909: image_subject(options, issue.subject) Chris@909: when :pdf Chris@909: pdf_new_page?(options) Chris@909: pdf_subject(options, issue.subject) Chris@909: end Chris@909: Chris@909: unless issue.leaf? Chris@909: @issue_ancestors << issue Chris@909: options[:indent] += options[:indent_increment] Chris@909: end Chris@909: Chris@909: output Chris@909: end Chris@909: Chris@909: def line_for_issue(issue, options) Chris@909: # Skip issues that don't have a due_before (due_date or version's due_date) Chris@909: if issue.is_a?(Issue) && issue.due_before Chris@909: coords = coordinates(issue.start_date, issue.due_before, issue.done_ratio, options[:zoom]) Chris@909: label = "#{ issue.status.name } #{ issue.done_ratio }%" Chris@909: Chris@909: case options[:format] Chris@909: when :html Chris@909: html_task(options, coords, :css => "task " + (issue.leaf? ? 'leaf' : 'parent'), :label => label, :issue => issue, :markers => !issue.leaf?) Chris@909: when :image Chris@909: image_task(options, coords, :label => label) Chris@909: when :pdf Chris@909: pdf_task(options, coords, :label => label) Chris@909: end Chris@909: else Chris@909: ActiveRecord::Base.logger.debug "GanttHelper#line_for_issue was not given an issue with a due_before" Chris@909: '' Chris@909: end Chris@909: end Chris@909: Chris@909: # Generates a gantt image Chris@909: # Only defined if RMagick is avalaible Chris@909: def to_image(format='PNG') Chris@909: date_to = (@date_from >> @months)-1 Chris@909: show_weeks = @zoom > 1 Chris@909: show_days = @zoom > 2 Chris@909: Chris@909: subject_width = 400 Chris@909: header_height = 18 Chris@909: # width of one day in pixels Chris@909: zoom = @zoom*2 Chris@909: g_width = (@date_to - @date_from + 1)*zoom Chris@909: g_height = 20 * number_of_rows + 30 Chris@909: headers_height = (show_weeks ? 2*header_height : header_height) Chris@909: height = g_height + headers_height Chris@909: Chris@909: imgl = Magick::ImageList.new Chris@909: imgl.new_image(subject_width+g_width+1, height) Chris@909: gc = Magick::Draw.new Chris@909: Chris@909: # Subjects Chris@909: gc.stroke('transparent') Chris@909: subjects(:image => gc, :top => (headers_height + 20), :indent => 4, :format => :image) Chris@909: Chris@909: # Months headers Chris@909: month_f = @date_from Chris@909: left = subject_width Chris@909: @months.times do Chris@909: width = ((month_f >> 1) - month_f) * zoom Chris@909: gc.fill('white') Chris@909: gc.stroke('grey') Chris@909: gc.stroke_width(1) Chris@909: gc.rectangle(left, 0, left + width, height) Chris@909: gc.fill('black') Chris@909: gc.stroke('transparent') Chris@909: gc.stroke_width(1) Chris@909: gc.text(left.round + 8, 14, "#{month_f.year}-#{month_f.month}") Chris@909: left = left + width Chris@909: month_f = month_f >> 1 Chris@909: end Chris@909: Chris@909: # Weeks headers Chris@909: if show_weeks Chris@909: left = subject_width Chris@909: height = header_height Chris@909: if @date_from.cwday == 1 Chris@909: # date_from is monday Chris@909: week_f = date_from Chris@909: else Chris@909: # find next monday after date_from Chris@909: week_f = @date_from + (7 - @date_from.cwday + 1) Chris@909: width = (7 - @date_from.cwday + 1) * zoom Chris@909: gc.fill('white') Chris@909: gc.stroke('grey') Chris@909: gc.stroke_width(1) Chris@909: gc.rectangle(left, header_height, left + width, 2*header_height + g_height-1) Chris@909: left = left + width Chris@909: end Chris@909: while week_f <= date_to Chris@909: width = (week_f + 6 <= date_to) ? 7 * zoom : (date_to - week_f + 1) * zoom Chris@909: gc.fill('white') Chris@909: gc.stroke('grey') Chris@909: gc.stroke_width(1) Chris@909: gc.rectangle(left.round, header_height, left.round + width, 2*header_height + g_height-1) Chris@909: gc.fill('black') Chris@909: gc.stroke('transparent') Chris@909: gc.stroke_width(1) Chris@909: gc.text(left.round + 2, header_height + 14, week_f.cweek.to_s) Chris@909: left = left + width Chris@909: week_f = week_f+7 Chris@909: end Chris@909: end Chris@909: Chris@909: # Days details (week-end in grey) Chris@909: if show_days Chris@909: left = subject_width Chris@909: height = g_height + header_height - 1 Chris@909: wday = @date_from.cwday Chris@909: (date_to - @date_from + 1).to_i.times do Chris@909: width = zoom Chris@909: gc.fill(wday == 6 || wday == 7 ? '#eee' : 'white') Chris@909: gc.stroke('#ddd') Chris@909: gc.stroke_width(1) Chris@909: gc.rectangle(left, 2*header_height, left + width, 2*header_height + g_height-1) Chris@909: left = left + width Chris@909: wday = wday + 1 Chris@909: wday = 1 if wday > 7 Chris@909: end Chris@909: end Chris@909: Chris@909: # border Chris@909: gc.fill('transparent') Chris@909: gc.stroke('grey') Chris@909: gc.stroke_width(1) Chris@909: gc.rectangle(0, 0, subject_width+g_width, headers_height) Chris@909: gc.stroke('black') Chris@909: gc.rectangle(0, 0, subject_width+g_width, g_height+ headers_height-1) Chris@909: Chris@909: # content Chris@909: top = headers_height + 20 Chris@909: Chris@909: gc.stroke('transparent') Chris@909: lines(:image => gc, :top => top, :zoom => zoom, :subject_width => subject_width, :format => :image) Chris@909: Chris@909: # today red line Chris@909: if Date.today >= @date_from and Date.today <= date_to Chris@909: gc.stroke('red') Chris@909: x = (Date.today-@date_from+1)*zoom + subject_width Chris@909: gc.line(x, headers_height, x, headers_height + g_height-1) Chris@909: end Chris@909: Chris@909: gc.draw(imgl) Chris@909: imgl.format = format Chris@909: imgl.to_blob Chris@909: end if Object.const_defined?(:Magick) Chris@909: Chris@909: def to_pdf Chris@909: pdf = ::Redmine::Export::PDF::ITCPDF.new(current_language) Chris@909: pdf.SetTitle("#{l(:label_gantt)} #{project}") Chris@909: pdf.alias_nb_pages Chris@909: pdf.footer_date = format_date(Date.today) Chris@909: pdf.AddPage("L") Chris@909: pdf.SetFontStyle('B',12) Chris@909: pdf.SetX(15) Chris@909: pdf.RDMCell(PDF::LeftPaneWidth, 20, project.to_s) Chris@909: pdf.Ln Chris@909: pdf.SetFontStyle('B',9) Chris@909: Chris@909: subject_width = PDF::LeftPaneWidth Chris@909: header_height = 5 Chris@909: Chris@909: headers_height = header_height Chris@909: show_weeks = false Chris@909: show_days = false Chris@909: Chris@909: if self.months < 7 Chris@909: show_weeks = true Chris@909: headers_height = 2*header_height Chris@909: if self.months < 3 Chris@909: show_days = true Chris@909: headers_height = 3*header_height Chris@909: end Chris@909: end Chris@909: Chris@909: g_width = PDF.right_pane_width Chris@909: zoom = (g_width) / (self.date_to - self.date_from + 1) Chris@909: g_height = 120 Chris@909: t_height = g_height + headers_height Chris@909: Chris@909: y_start = pdf.GetY Chris@909: Chris@909: # Months headers Chris@909: month_f = self.date_from Chris@909: left = subject_width Chris@909: height = header_height Chris@909: self.months.times do Chris@909: width = ((month_f >> 1) - month_f) * zoom Chris@909: pdf.SetY(y_start) Chris@909: pdf.SetX(left) Chris@909: pdf.RDMCell(width, height, "#{month_f.year}-#{month_f.month}", "LTR", 0, "C") Chris@909: left = left + width Chris@909: month_f = month_f >> 1 Chris@909: end Chris@909: Chris@909: # Weeks headers Chris@909: if show_weeks Chris@909: left = subject_width Chris@909: height = header_height Chris@909: if self.date_from.cwday == 1 Chris@909: # self.date_from is monday Chris@909: week_f = self.date_from Chris@909: else Chris@909: # find next monday after self.date_from Chris@909: week_f = self.date_from + (7 - self.date_from.cwday + 1) Chris@909: width = (7 - self.date_from.cwday + 1) * zoom-1 Chris@909: pdf.SetY(y_start + header_height) Chris@909: pdf.SetX(left) Chris@909: pdf.RDMCell(width + 1, height, "", "LTR") Chris@909: left = left + width+1 Chris@909: end Chris@909: while week_f <= self.date_to Chris@909: width = (week_f + 6 <= self.date_to) ? 7 * zoom : (self.date_to - week_f + 1) * zoom Chris@909: pdf.SetY(y_start + header_height) Chris@909: pdf.SetX(left) Chris@909: pdf.RDMCell(width, height, (width >= 5 ? week_f.cweek.to_s : ""), "LTR", 0, "C") Chris@909: left = left + width Chris@909: week_f = week_f+7 Chris@909: end Chris@909: end Chris@909: Chris@909: # Days headers Chris@909: if show_days Chris@909: left = subject_width Chris@909: height = header_height Chris@909: wday = self.date_from.cwday Chris@909: pdf.SetFontStyle('B',7) Chris@909: (self.date_to - self.date_from + 1).to_i.times do Chris@909: width = zoom Chris@909: pdf.SetY(y_start + 2 * header_height) Chris@909: pdf.SetX(left) Chris@909: pdf.RDMCell(width, height, day_name(wday).first, "LTR", 0, "C") Chris@909: left = left + width Chris@909: wday = wday + 1 Chris@909: wday = 1 if wday > 7 Chris@909: end Chris@909: end Chris@909: Chris@909: pdf.SetY(y_start) Chris@909: pdf.SetX(15) Chris@909: pdf.RDMCell(subject_width+g_width-15, headers_height, "", 1) Chris@909: Chris@909: # Tasks Chris@909: top = headers_height + y_start Chris@909: options = { Chris@909: :top => top, Chris@909: :zoom => zoom, Chris@909: :subject_width => subject_width, Chris@909: :g_width => g_width, Chris@909: :indent => 0, Chris@909: :indent_increment => 5, Chris@909: :top_increment => 5, Chris@909: :format => :pdf, Chris@909: :pdf => pdf Chris@909: } Chris@909: render(options) Chris@909: pdf.Output Chris@909: end Chris@909: Chris@909: private Chris@909: Chris@909: def coordinates(start_date, end_date, progress, zoom=nil) Chris@909: zoom ||= @zoom Chris@909: Chris@909: coords = {} Chris@909: if start_date && end_date && start_date < self.date_to && end_date > self.date_from Chris@909: if start_date > self.date_from Chris@909: coords[:start] = start_date - self.date_from Chris@909: coords[:bar_start] = start_date - self.date_from Chris@909: else Chris@909: coords[:bar_start] = 0 Chris@909: end Chris@909: if end_date < self.date_to Chris@909: coords[:end] = end_date - self.date_from Chris@909: coords[:bar_end] = end_date - self.date_from + 1 Chris@909: else Chris@909: coords[:bar_end] = self.date_to - self.date_from + 1 Chris@909: end Chris@909: Chris@909: if progress Chris@909: progress_date = start_date + (end_date - start_date + 1) * (progress / 100.0) Chris@909: if progress_date > self.date_from && progress_date > start_date Chris@909: if progress_date < self.date_to Chris@909: coords[:bar_progress_end] = progress_date - self.date_from Chris@909: else Chris@909: coords[:bar_progress_end] = self.date_to - self.date_from + 1 Chris@909: end Chris@909: end Chris@909: Chris@909: if progress_date < Date.today Chris@909: late_date = [Date.today, end_date].min Chris@909: if late_date > self.date_from && late_date > start_date Chris@909: if late_date < self.date_to Chris@909: coords[:bar_late_end] = late_date - self.date_from + 1 Chris@909: else Chris@909: coords[:bar_late_end] = self.date_to - self.date_from + 1 Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: # Transforms dates into pixels witdh Chris@909: coords.keys.each do |key| Chris@909: coords[key] = (coords[key] * zoom).floor Chris@909: end Chris@909: coords Chris@909: end Chris@909: Chris@909: # Sorts a collection of issues by start_date, due_date, id for gantt rendering Chris@909: def sort_issues!(issues) Chris@909: issues.sort! { |a, b| gantt_issue_compare(a, b, issues) } Chris@909: end Chris@909: Chris@909: # TODO: top level issues should be sorted by start date Chris@909: def gantt_issue_compare(x, y, issues) Chris@909: if x.root_id == y.root_id Chris@909: x.lft <=> y.lft Chris@909: else Chris@909: x.root_id <=> y.root_id Chris@909: end Chris@909: end Chris@909: Chris@909: def current_limit Chris@909: if @max_rows Chris@909: @max_rows - @number_of_rows Chris@909: else Chris@909: nil Chris@909: end Chris@909: end Chris@909: Chris@909: def abort? Chris@909: if @max_rows && @number_of_rows >= @max_rows Chris@909: @truncated = true Chris@909: end Chris@909: end Chris@909: Chris@909: def pdf_new_page?(options) Chris@909: if options[:top] > 180 Chris@909: options[:pdf].Line(15, options[:top], PDF::TotalWidth, options[:top]) Chris@909: options[:pdf].AddPage("L") Chris@909: options[:top] = 15 Chris@909: options[:pdf].Line(15, options[:top] - 0.1, PDF::TotalWidth, options[:top] - 0.1) Chris@909: end Chris@909: end Chris@909: Chris@909: def html_subject(params, subject, options={}) Chris@909: style = "position: absolute;top:#{params[:top]}px;left:#{params[:indent]}px;" Chris@909: style << "width:#{params[:subject_width] - params[:indent]}px;" if params[:subject_width] Chris@909: Chris@909: output = view.content_tag 'div', subject, :class => options[:css], :style => style, :title => options[:title] Chris@909: @subjects << output Chris@909: output Chris@909: end Chris@909: Chris@909: def pdf_subject(params, subject, options={}) Chris@909: params[:pdf].SetY(params[:top]) Chris@909: params[:pdf].SetX(15) Chris@909: Chris@909: char_limit = PDF::MaxCharactorsForSubject - params[:indent] Chris@909: params[:pdf].RDMCell(params[:subject_width]-15, 5, (" " * params[:indent]) + subject.to_s.sub(/^(.{#{char_limit}}[^\s]*\s).*$/, '\1 (...)'), "LR") Chris@909: Chris@909: params[:pdf].SetY(params[:top]) Chris@909: params[:pdf].SetX(params[:subject_width]) Chris@909: params[:pdf].RDMCell(params[:g_width], 5, "", "LR") Chris@909: end Chris@909: Chris@909: def image_subject(params, subject, options={}) Chris@909: params[:image].fill('black') Chris@909: params[:image].stroke('transparent') Chris@909: params[:image].stroke_width(1) Chris@909: params[:image].text(params[:indent], params[:top] + 2, subject) Chris@909: end Chris@909: Chris@909: def html_task(params, coords, options={}) Chris@909: output = '' Chris@909: # Renders the task bar, with progress and late Chris@909: if coords[:bar_start] && coords[:bar_end] Chris@909: output << "
".html_safe Chris@909: Chris@909: if coords[:bar_late_end] Chris@909: output << " ".html_safe Chris@909: end Chris@909: if coords[:bar_progress_end] Chris@909: output << " ".html_safe Chris@909: end Chris@909: end Chris@909: # Renders the markers Chris@909: if options[:markers] Chris@909: if coords[:start] Chris@909: output << " ".html_safe Chris@909: end Chris@909: if coords[:end] Chris@909: output << " ".html_safe Chris@909: end Chris@909: end Chris@909: # Renders the label on the right Chris@909: if options[:label] Chris@909: output << " ".html_safe Chris@909: end Chris@909: # Renders the tooltip Chris@909: if options[:issue] && coords[:bar_start] && coords[:bar_end] Chris@909: output << "