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