To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / lib / redmine / helpers / gantt.rb @ 1568:bc47b68a9487

History | View | Annotate | Download (36.5 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2014  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

    
18
module Redmine
19
  module Helpers
20
    # Simple class to handle gantt chart data
21
    class Gantt
22
      include ERB::Util
23
      include Redmine::I18n
24
      include Redmine::Utils::DateCalculation
25

    
26
      # Relation types that are rendered
27
      DRAW_TYPES = {
28
        IssueRelation::TYPE_BLOCKS   => { :landscape_margin => 16, :color => '#F34F4F' },
29
        IssueRelation::TYPE_PRECEDES => { :landscape_margin => 20, :color => '#628FEA' }
30
      }.freeze
31

    
32
      # :nodoc:
33
      # Some utility methods for the PDF export
34
      class PDF
35
        MaxCharactorsForSubject = 45
36
        TotalWidth = 280
37
        LeftPaneWidth = 100
38

    
39
        def self.right_pane_width
40
          TotalWidth - LeftPaneWidth
41
        end
42
      end
43

    
44
      attr_reader :year_from, :month_from, :date_from, :date_to, :zoom, :months, :truncated, :max_rows
45
      attr_accessor :query
46
      attr_accessor :project
47
      attr_accessor :view
48

    
49
      def initialize(options={})
50
        options = options.dup
51
        if options[:year] && options[:year].to_i >0
52
          @year_from = options[:year].to_i
53
          if options[:month] && options[:month].to_i >=1 && options[:month].to_i <= 12
54
            @month_from = options[:month].to_i
55
          else
56
            @month_from = 1
57
          end
58
        else
59
          @month_from ||= Date.today.month
60
          @year_from ||= Date.today.year
61
        end
62
        zoom = (options[:zoom] || User.current.pref[:gantt_zoom]).to_i
63
        @zoom = (zoom > 0 && zoom < 5) ? zoom : 2
64
        months = (options[:months] || User.current.pref[:gantt_months]).to_i
65
        @months = (months > 0 && months < 25) ? months : 6
66
        # Save gantt parameters as user preference (zoom and months count)
67
        if (User.current.logged? && (@zoom != User.current.pref[:gantt_zoom] ||
68
              @months != User.current.pref[:gantt_months]))
69
          User.current.pref[:gantt_zoom], User.current.pref[:gantt_months] = @zoom, @months
70
          User.current.preference.save
71
        end
72
        @date_from = Date.civil(@year_from, @month_from, 1)
73
        @date_to = (@date_from >> @months) - 1
74
        @subjects = ''
75
        @lines = ''
76
        @number_of_rows = nil
77
        @issue_ancestors = []
78
        @truncated = false
79
        if options.has_key?(:max_rows)
80
          @max_rows = options[:max_rows]
81
        else
82
          @max_rows = Setting.gantt_items_limit.blank? ? nil : Setting.gantt_items_limit.to_i
83
        end
84
      end
85

    
86
      def common_params
87
        { :controller => 'gantts', :action => 'show', :project_id => @project }
88
      end
89

    
90
      def params
91
        common_params.merge({:zoom => zoom, :year => year_from,
92
                             :month => month_from, :months => months})
93
      end
94

    
95
      def params_previous
96
        common_params.merge({:year => (date_from << months).year,
97
                             :month => (date_from << months).month,
98
                             :zoom => zoom, :months => months})
99
      end
100

    
101
      def params_next
102
        common_params.merge({:year => (date_from >> months).year,
103
                             :month => (date_from >> months).month,
104
                             :zoom => zoom, :months => months})
105
      end
106

    
107
      # Returns the number of rows that will be rendered on the Gantt chart
108
      def number_of_rows
109
        return @number_of_rows if @number_of_rows
110
        rows = projects.inject(0) {|total, p| total += number_of_rows_on_project(p)}
111
        rows > @max_rows ? @max_rows : rows
112
      end
113

    
114
      # Returns the number of rows that will be used to list a project on
115
      # the Gantt chart.  This will recurse for each subproject.
116
      def number_of_rows_on_project(project)
117
        return 0 unless projects.include?(project)
118
        count = 1
119
        count += project_issues(project).size
120
        count += project_versions(project).size
121
        count
122
      end
123

    
124
      # Renders the subjects of the Gantt chart, the left side.
125
      def subjects(options={})
126
        render(options.merge(:only => :subjects)) unless @subjects_rendered
127
        @subjects
128
      end
129

    
130
      # Renders the lines of the Gantt chart, the right side
131
      def lines(options={})
132
        render(options.merge(:only => :lines)) unless @lines_rendered
133
        @lines
134
      end
135

    
136
      # Returns issues that will be rendered
137
      def issues
138
        @issues ||= @query.issues(
139
          :include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
140
          :order => "#{Project.table_name}.lft ASC, #{Issue.table_name}.id ASC",
141
          :limit => @max_rows
142
        )
143
      end
144

    
145
      # Returns a hash of the relations between the issues that are present on the gantt
146
      # and that should be displayed, grouped by issue ids.
147
      def relations
148
        return @relations if @relations
149
        if issues.any?
150
          issue_ids = issues.map(&:id)
151
          @relations = IssueRelation.
152
            where(:issue_from_id => issue_ids, :issue_to_id => issue_ids, :relation_type => DRAW_TYPES.keys).
153
            group_by(&:issue_from_id)
154
        else
155
          @relations = {}
156
        end
157
      end
158

    
159
      # Return all the project nodes that will be displayed
160
      def projects
161
        return @projects if @projects
162
        ids = issues.collect(&:project).uniq.collect(&:id)
163
        if ids.any?
164
          # All issues projects and their visible ancestors
165
          @projects = Project.visible.
166
            joins("LEFT JOIN #{Project.table_name} child ON #{Project.table_name}.lft <= child.lft AND #{Project.table_name}.rgt >= child.rgt").
167
            where("child.id IN (?)", ids).
168
            order("#{Project.table_name}.lft ASC").
169
            uniq.
170
            all
171
        else
172
          @projects = []
173
        end
174
      end
175

    
176
      # Returns the issues that belong to +project+
177
      def project_issues(project)
178
        @issues_by_project ||= issues.group_by(&:project)
179
        @issues_by_project[project] || []
180
      end
181

    
182
      # Returns the distinct versions of the issues that belong to +project+
183
      def project_versions(project)
184
        project_issues(project).collect(&:fixed_version).compact.uniq
185
      end
186

    
187
      # Returns the issues that belong to +project+ and are assigned to +version+
188
      def version_issues(project, version)
189
        project_issues(project).select {|issue| issue.fixed_version == version}
190
      end
191

    
192
      def render(options={})
193
        options = {:top => 0, :top_increment => 20,
194
                   :indent_increment => 20, :render => :subject,
195
                   :format => :html}.merge(options)
196
        indent = options[:indent] || 4
197
        @subjects = '' unless options[:only] == :lines
198
        @lines = '' unless options[:only] == :subjects
199
        @number_of_rows = 0
200
        Project.project_tree(projects) do |project, level|
201
          options[:indent] = indent + level * options[:indent_increment]
202
          render_project(project, options)
203
          break if abort?
204
        end
205
        @subjects_rendered = true unless options[:only] == :lines
206
        @lines_rendered = true unless options[:only] == :subjects
207
        render_end(options)
208
      end
209

    
210
      def render_project(project, options={})
211
        subject_for_project(project, options) unless options[:only] == :lines
212
        line_for_project(project, options) unless options[:only] == :subjects
213
        options[:top] += options[:top_increment]
214
        options[:indent] += options[:indent_increment]
215
        @number_of_rows += 1
216
        return if abort?
217
        issues = project_issues(project).select {|i| i.fixed_version.nil?}
218
        self.class.sort_issues!(issues)
219
        if issues
220
          render_issues(issues, options)
221
          return if abort?
222
        end
223
        versions = project_versions(project)
224
        self.class.sort_versions!(versions)
225
        versions.each do |version|
226
          render_version(project, version, options)
227
        end
228
        # Remove indent to hit the next sibling
229
        options[:indent] -= options[:indent_increment]
230
      end
231

    
232
      def render_issues(issues, options={})
233
        @issue_ancestors = []
234
        issues.each do |i|
235
          subject_for_issue(i, options) unless options[:only] == :lines
236
          line_for_issue(i, options) unless options[:only] == :subjects
237
          options[:top] += options[:top_increment]
238
          @number_of_rows += 1
239
          break if abort?
240
        end
241
        options[:indent] -= (options[:indent_increment] * @issue_ancestors.size)
242
      end
243

    
244
      def render_version(project, version, options={})
245
        # Version header
246
        subject_for_version(version, options) unless options[:only] == :lines
247
        line_for_version(version, options) unless options[:only] == :subjects
248
        options[:top] += options[:top_increment]
249
        @number_of_rows += 1
250
        return if abort?
251
        issues = version_issues(project, version)
252
        if issues
253
          self.class.sort_issues!(issues)
254
          # Indent issues
255
          options[:indent] += options[:indent_increment]
256
          render_issues(issues, options)
257
          options[:indent] -= options[:indent_increment]
258
        end
259
      end
260

    
261
      def render_end(options={})
262
        case options[:format]
263
        when :pdf
264
          options[:pdf].Line(15, options[:top], PDF::TotalWidth, options[:top])
265
        end
266
      end
267

    
268
      def subject_for_project(project, options)
269
        case options[:format]
270
        when :html
271
          html_class = ""
272
          html_class << 'icon icon-projects '
273
          html_class << (project.overdue? ? 'project-overdue' : '')
274
          s = view.link_to_project(project).html_safe
275
          subject = view.content_tag(:span, s,
276
                                     :class => html_class).html_safe
277
          html_subject(options, subject, :css => "project-name")
278
        when :image
279
          image_subject(options, project.name)
280
        when :pdf
281
          pdf_new_page?(options)
282
          pdf_subject(options, project.name)
283
        end
284
      end
285

    
286
      def line_for_project(project, options)
287
        # Skip versions that don't have a start_date or due date
288
        if project.is_a?(Project) && project.start_date && project.due_date
289
          options[:zoom] ||= 1
290
          options[:g_width] ||= (self.date_to - self.date_from + 1) * options[:zoom]
291
          coords = coordinates(project.start_date, project.due_date, nil, options[:zoom])
292
          label = h(project)
293
          case options[:format]
294
          when :html
295
            html_task(options, coords, :css => "project task", :label => label, :markers => true)
296
          when :image
297
            image_task(options, coords, :label => label, :markers => true, :height => 3)
298
          when :pdf
299
            pdf_task(options, coords, :label => label, :markers => true, :height => 0.8)
300
          end
301
        else
302
          ''
303
        end
304
      end
305

    
306
      def subject_for_version(version, options)
307
        case options[:format]
308
        when :html
309
          html_class = ""
310
          html_class << 'icon icon-package '
311
          html_class << (version.behind_schedule? ? 'version-behind-schedule' : '') << " "
312
          html_class << (version.overdue? ? 'version-overdue' : '')
313
          html_class << ' version-closed' unless version.open?
314
          if version.start_date && version.due_date && version.completed_percent
315
            progress_date = calc_progress_date(version.start_date,
316
                                               version.due_date, version.completed_percent)
317
            html_class << ' behind-start-date' if progress_date < self.date_from
318
            html_class << ' over-end-date' if progress_date > self.date_to
319
          end
320
          s = view.link_to_version(version).html_safe
321
          subject = view.content_tag(:span, s,
322
                                     :class => html_class).html_safe
323
          html_subject(options, subject, :css => "version-name",
324
                       :id => "version-#{version.id}")
325
        when :image
326
          image_subject(options, version.to_s_with_project)
327
        when :pdf
328
          pdf_new_page?(options)
329
          pdf_subject(options, version.to_s_with_project)
330
        end
331
      end
332

    
333
      def line_for_version(version, options)
334
        # Skip versions that don't have a start_date
335
        if version.is_a?(Version) && version.due_date && version.start_date
336
          options[:zoom] ||= 1
337
          options[:g_width] ||= (self.date_to - self.date_from + 1) * options[:zoom]
338
          coords = coordinates(version.start_date,
339
                               version.due_date, version.completed_percent,
340
                               options[:zoom])
341
          label = "#{h version} #{h version.completed_percent.to_i.to_s}%"
342
          label = h("#{version.project} -") + label unless @project && @project == version.project
343
          case options[:format]
344
          when :html
345
            html_task(options, coords, :css => "version task",
346
                      :label => label, :markers => true, :version => version)
347
          when :image
348
            image_task(options, coords, :label => label, :markers => true, :height => 3)
349
          when :pdf
350
            pdf_task(options, coords, :label => label, :markers => true, :height => 0.8)
351
          end
352
        else
353
          ''
354
        end
355
      end
356

    
357
      def subject_for_issue(issue, options)
358
        while @issue_ancestors.any? && !issue.is_descendant_of?(@issue_ancestors.last)
359
          @issue_ancestors.pop
360
          options[:indent] -= options[:indent_increment]
361
        end
362
        output = case options[:format]
363
        when :html
364
          css_classes = ''
365
          css_classes << ' issue-overdue' if issue.overdue?
366
          css_classes << ' issue-behind-schedule' if issue.behind_schedule?
367
          css_classes << ' icon icon-issue' unless Setting.gravatar_enabled? && issue.assigned_to
368
          css_classes << ' issue-closed' if issue.closed?
369
          if issue.start_date && issue.due_before && issue.done_ratio
370
            progress_date = calc_progress_date(issue.start_date,
371
                                               issue.due_before, issue.done_ratio)
372
            css_classes << ' behind-start-date' if progress_date < self.date_from
373
            css_classes << ' over-end-date' if progress_date > self.date_to
374
          end
375
          s = "".html_safe
376
          if issue.assigned_to.present?
377
            assigned_string = l(:field_assigned_to) + ": " + issue.assigned_to.name
378
            s << view.avatar(issue.assigned_to,
379
                             :class => 'gravatar icon-gravatar',
380
                             :size => 10,
381
                             :title => assigned_string).to_s.html_safe
382
          end
383
          s << view.link_to_issue(issue).html_safe
384
          subject = view.content_tag(:span, s, :class => css_classes).html_safe
385
          html_subject(options, subject, :css => "issue-subject",
386
                       :title => issue.subject, :id => "issue-#{issue.id}") + "\n"
387
        when :image
388
          image_subject(options, issue.subject)
389
        when :pdf
390
          pdf_new_page?(options)
391
          pdf_subject(options, issue.subject)
392
        end
393
        unless issue.leaf?
394
          @issue_ancestors << issue
395
          options[:indent] += options[:indent_increment]
396
        end
397
        output
398
      end
399

    
400
      def line_for_issue(issue, options)
401
        # Skip issues that don't have a due_before (due_date or version's due_date)
402
        if issue.is_a?(Issue) && issue.due_before
403
          coords = coordinates(issue.start_date, issue.due_before, issue.done_ratio, options[:zoom])
404
          label = "#{issue.status.name} #{issue.done_ratio}%"
405
          case options[:format]
406
          when :html
407
            html_task(options, coords,
408
                      :css => "task " + (issue.leaf? ? 'leaf' : 'parent'),
409
                      :label => label, :issue => issue,
410
                      :markers => !issue.leaf?)
411
          when :image
412
            image_task(options, coords, :label => label)
413
          when :pdf
414
            pdf_task(options, coords, :label => label)
415
        end
416
        else
417
          ''
418
        end
419
      end
420

    
421
      # Generates a gantt image
422
      # Only defined if RMagick is avalaible
423
      def to_image(format='PNG')
424
        date_to = (@date_from >> @months) - 1
425
        show_weeks = @zoom > 1
426
        show_days = @zoom > 2
427
        subject_width = 400
428
        header_height = 18
429
        # width of one day in pixels
430
        zoom = @zoom * 2
431
        g_width = (@date_to - @date_from + 1) * zoom
432
        g_height = 20 * number_of_rows + 30
433
        headers_height = (show_weeks ? 2 * header_height : header_height)
434
        height = g_height + headers_height
435
        imgl = Magick::ImageList.new
436
        imgl.new_image(subject_width + g_width + 1, height)
437
        gc = Magick::Draw.new
438
        gc.font = Redmine::Configuration['rmagick_font_path'] || ""
439
        # Subjects
440
        gc.stroke('transparent')
441
        subjects(:image => gc, :top => (headers_height + 20), :indent => 4, :format => :image)
442
        # Months headers
443
        month_f = @date_from
444
        left = subject_width
445
        @months.times do
446
          width = ((month_f >> 1) - month_f) * zoom
447
          gc.fill('white')
448
          gc.stroke('grey')
449
          gc.stroke_width(1)
450
          gc.rectangle(left, 0, left + width, height)
451
          gc.fill('black')
452
          gc.stroke('transparent')
453
          gc.stroke_width(1)
454
          gc.text(left.round + 8, 14, "#{month_f.year}-#{month_f.month}")
455
          left = left + width
456
          month_f = month_f >> 1
457
        end
458
        # Weeks headers
459
        if show_weeks
460
          left = subject_width
461
          height = header_height
462
          if @date_from.cwday == 1
463
            # date_from is monday
464
            week_f = date_from
465
          else
466
            # find next monday after date_from
467
            week_f = @date_from + (7 - @date_from.cwday + 1)
468
            width = (7 - @date_from.cwday + 1) * zoom
469
            gc.fill('white')
470
            gc.stroke('grey')
471
            gc.stroke_width(1)
472
            gc.rectangle(left, header_height, left + width, 2 * header_height + g_height - 1)
473
            left = left + width
474
          end
475
          while week_f <= date_to
476
            width = (week_f + 6 <= date_to) ? 7 * zoom : (date_to - week_f + 1) * zoom
477
            gc.fill('white')
478
            gc.stroke('grey')
479
            gc.stroke_width(1)
480
            gc.rectangle(left.round, header_height, left.round + width, 2 * header_height + g_height - 1)
481
            gc.fill('black')
482
            gc.stroke('transparent')
483
            gc.stroke_width(1)
484
            gc.text(left.round + 2, header_height + 14, week_f.cweek.to_s)
485
            left = left + width
486
            week_f = week_f + 7
487
          end
488
        end
489
        # Days details (week-end in grey)
490
        if show_days
491
          left = subject_width
492
          height = g_height + header_height - 1
493
          wday = @date_from.cwday
494
          (date_to - @date_from + 1).to_i.times do
495
            width =  zoom
496
            gc.fill(non_working_week_days.include?(wday) ? '#eee' : 'white')
497
            gc.stroke('#ddd')
498
            gc.stroke_width(1)
499
            gc.rectangle(left, 2 * header_height, left + width, 2 * header_height + g_height - 1)
500
            left = left + width
501
            wday = wday + 1
502
            wday = 1 if wday > 7
503
          end
504
        end
505
        # border
506
        gc.fill('transparent')
507
        gc.stroke('grey')
508
        gc.stroke_width(1)
509
        gc.rectangle(0, 0, subject_width + g_width, headers_height)
510
        gc.stroke('black')
511
        gc.rectangle(0, 0, subject_width + g_width, g_height + headers_height - 1)
512
        # content
513
        top = headers_height + 20
514
        gc.stroke('transparent')
515
        lines(:image => gc, :top => top, :zoom => zoom,
516
              :subject_width => subject_width, :format => :image)
517
        # today red line
518
        if Date.today >= @date_from and Date.today <= date_to
519
          gc.stroke('red')
520
          x = (Date.today - @date_from + 1) * zoom + subject_width
521
          gc.line(x, headers_height, x, headers_height + g_height - 1)
522
        end
523
        gc.draw(imgl)
524
        imgl.format = format
525
        imgl.to_blob
526
      end if Object.const_defined?(:Magick)
527

    
528
      def to_pdf
529
        pdf = ::Redmine::Export::PDF::ITCPDF.new(current_language)
530
        pdf.SetTitle("#{l(:label_gantt)} #{project}")
531
        pdf.alias_nb_pages
532
        pdf.footer_date = format_date(Date.today)
533
        pdf.AddPage("L")
534
        pdf.SetFontStyle('B', 12)
535
        pdf.SetX(15)
536
        pdf.RDMCell(PDF::LeftPaneWidth, 20, project.to_s)
537
        pdf.Ln
538
        pdf.SetFontStyle('B', 9)
539
        subject_width = PDF::LeftPaneWidth
540
        header_height = 5
541
        headers_height = header_height
542
        show_weeks = false
543
        show_days = false
544
        if self.months < 7
545
          show_weeks = true
546
          headers_height = 2 * header_height
547
          if self.months < 3
548
            show_days = true
549
            headers_height = 3 * header_height
550
          end
551
        end
552
        g_width = PDF.right_pane_width
553
        zoom = (g_width) / (self.date_to - self.date_from + 1)
554
        g_height = 120
555
        t_height = g_height + headers_height
556
        y_start = pdf.GetY
557
        # Months headers
558
        month_f = self.date_from
559
        left = subject_width
560
        height = header_height
561
        self.months.times do
562
          width = ((month_f >> 1) - month_f) * zoom
563
          pdf.SetY(y_start)
564
          pdf.SetX(left)
565
          pdf.RDMCell(width, height, "#{month_f.year}-#{month_f.month}", "LTR", 0, "C")
566
          left = left + width
567
          month_f = month_f >> 1
568
        end
569
        # Weeks headers
570
        if show_weeks
571
          left = subject_width
572
          height = header_height
573
          if self.date_from.cwday == 1
574
            # self.date_from is monday
575
            week_f = self.date_from
576
          else
577
            # find next monday after self.date_from
578
            week_f = self.date_from + (7 - self.date_from.cwday + 1)
579
            width = (7 - self.date_from.cwday + 1) * zoom-1
580
            pdf.SetY(y_start + header_height)
581
            pdf.SetX(left)
582
            pdf.RDMCell(width + 1, height, "", "LTR")
583
            left = left + width + 1
584
          end
585
          while week_f <= self.date_to
586
            width = (week_f + 6 <= self.date_to) ? 7 * zoom : (self.date_to - week_f + 1) * zoom
587
            pdf.SetY(y_start + header_height)
588
            pdf.SetX(left)
589
            pdf.RDMCell(width, height, (width >= 5 ? week_f.cweek.to_s : ""), "LTR", 0, "C")
590
            left = left + width
591
            week_f = week_f + 7
592
          end
593
        end
594
        # Days headers
595
        if show_days
596
          left = subject_width
597
          height = header_height
598
          wday = self.date_from.cwday
599
          pdf.SetFontStyle('B', 7)
600
          (self.date_to - self.date_from + 1).to_i.times do
601
            width = zoom
602
            pdf.SetY(y_start + 2 * header_height)
603
            pdf.SetX(left)
604
            pdf.RDMCell(width, height, day_name(wday).first, "LTR", 0, "C")
605
            left = left + width
606
            wday = wday + 1
607
            wday = 1 if wday > 7
608
          end
609
        end
610
        pdf.SetY(y_start)
611
        pdf.SetX(15)
612
        pdf.RDMCell(subject_width + g_width - 15, headers_height, "", 1)
613
        # Tasks
614
        top = headers_height + y_start
615
        options = {
616
          :top => top,
617
          :zoom => zoom,
618
          :subject_width => subject_width,
619
          :g_width => g_width,
620
          :indent => 0,
621
          :indent_increment => 5,
622
          :top_increment => 5,
623
          :format => :pdf,
624
          :pdf => pdf
625
        }
626
        render(options)
627
        pdf.Output
628
      end
629

    
630
      private
631

    
632
      def coordinates(start_date, end_date, progress, zoom=nil)
633
        zoom ||= @zoom
634
        coords = {}
635
        if start_date && end_date && start_date < self.date_to && end_date > self.date_from
636
          if start_date > self.date_from
637
            coords[:start] = start_date - self.date_from
638
            coords[:bar_start] = start_date - self.date_from
639
          else
640
            coords[:bar_start] = 0
641
          end
642
          if end_date < self.date_to
643
            coords[:end] = end_date - self.date_from
644
            coords[:bar_end] = end_date - self.date_from + 1
645
          else
646
            coords[:bar_end] = self.date_to - self.date_from + 1
647
          end
648
          if progress
649
            progress_date = calc_progress_date(start_date, end_date, progress)
650
            if progress_date > self.date_from && progress_date > start_date
651
              if progress_date < self.date_to
652
                coords[:bar_progress_end] = progress_date - self.date_from
653
              else
654
                coords[:bar_progress_end] = self.date_to - self.date_from + 1
655
              end
656
            end
657
            if progress_date < Date.today
658
              late_date = [Date.today, end_date].min
659
              if late_date > self.date_from && late_date > start_date
660
                if late_date < self.date_to
661
                  coords[:bar_late_end] = late_date - self.date_from + 1
662
                else
663
                  coords[:bar_late_end] = self.date_to - self.date_from + 1
664
                end
665
              end
666
            end
667
          end
668
        end
669
        # Transforms dates into pixels witdh
670
        coords.keys.each do |key|
671
          coords[key] = (coords[key] * zoom).floor
672
        end
673
        coords
674
      end
675

    
676
      def calc_progress_date(start_date, end_date, progress)
677
        start_date + (end_date - start_date + 1) * (progress / 100.0)
678
      end
679

    
680
      def self.sort_issues!(issues)
681
        issues.sort! {|a, b| sort_issue_logic(a) <=> sort_issue_logic(b)}
682
      end
683

    
684
      def self.sort_issue_logic(issue)
685
        julian_date = Date.new()
686
        ancesters_start_date = []
687
        current_issue = issue
688
        begin
689
          ancesters_start_date.unshift([current_issue.start_date || julian_date, current_issue.id])
690
          current_issue = current_issue.parent
691
        end while (current_issue)
692
        ancesters_start_date
693
      end
694

    
695
      def self.sort_versions!(versions)
696
        versions.sort!
697
      end
698

    
699
      def current_limit
700
        if @max_rows
701
          @max_rows - @number_of_rows
702
        else
703
          nil
704
        end
705
      end
706

    
707
      def abort?
708
        if @max_rows && @number_of_rows >= @max_rows
709
          @truncated = true
710
        end
711
      end
712

    
713
      def pdf_new_page?(options)
714
        if options[:top] > 180
715
          options[:pdf].Line(15, options[:top], PDF::TotalWidth, options[:top])
716
          options[:pdf].AddPage("L")
717
          options[:top] = 15
718
          options[:pdf].Line(15, options[:top] - 0.1, PDF::TotalWidth, options[:top] - 0.1)
719
        end
720
      end
721

    
722
      def html_subject(params, subject, options={})
723
        style = "position: absolute;top:#{params[:top]}px;left:#{params[:indent]}px;"
724
        style << "width:#{params[:subject_width] - params[:indent]}px;" if params[:subject_width]
725
        output = view.content_tag(:div, subject,
726
                                  :class => options[:css], :style => style,
727
                                  :title => options[:title],
728
                                  :id => options[:id])
729
        @subjects << output
730
        output
731
      end
732

    
733
      def pdf_subject(params, subject, options={})
734
        params[:pdf].SetY(params[:top])
735
        params[:pdf].SetX(15)
736
        char_limit = PDF::MaxCharactorsForSubject - params[:indent]
737
        params[:pdf].RDMCell(params[:subject_width] - 15, 5,
738
                             (" " * params[:indent]) +
739
                               subject.to_s.sub(/^(.{#{char_limit}}[^\s]*\s).*$/, '\1 (...)'),
740
                              "LR")
741
        params[:pdf].SetY(params[:top])
742
        params[:pdf].SetX(params[:subject_width])
743
        params[:pdf].RDMCell(params[:g_width], 5, "", "LR")
744
      end
745

    
746
      def image_subject(params, subject, options={})
747
        params[:image].fill('black')
748
        params[:image].stroke('transparent')
749
        params[:image].stroke_width(1)
750
        params[:image].text(params[:indent], params[:top] + 2, subject)
751
      end
752

    
753
      def issue_relations(issue)
754
        rels = {}
755
        if relations[issue.id]
756
          relations[issue.id].each do |relation|
757
            (rels[relation.relation_type] ||= []) << relation.issue_to_id
758
          end
759
        end
760
        rels
761
      end
762

    
763
      def html_task(params, coords, options={})
764
        output = ''
765
        # Renders the task bar, with progress and late
766
        if coords[:bar_start] && coords[:bar_end]
767
          width = coords[:bar_end] - coords[:bar_start] - 2
768
          style = ""
769
          style << "top:#{params[:top]}px;"
770
          style << "left:#{coords[:bar_start]}px;"
771
          style << "width:#{width}px;"
772
          html_id = "task-todo-issue-#{options[:issue].id}" if options[:issue]
773
          html_id = "task-todo-version-#{options[:version].id}" if options[:version]
774
          content_opt = {:style => style,
775
                         :class => "#{options[:css]} task_todo",
776
                         :id => html_id}
777
          if options[:issue]
778
            rels = issue_relations(options[:issue])
779
            if rels.present?
780
              content_opt[:data] = {"rels" => rels.to_json}
781
            end
782
          end
783
          output << view.content_tag(:div, '&nbsp;'.html_safe, content_opt)
784
          if coords[:bar_late_end]
785
            width = coords[:bar_late_end] - coords[:bar_start] - 2
786
            style = ""
787
            style << "top:#{params[:top]}px;"
788
            style << "left:#{coords[:bar_start]}px;"
789
            style << "width:#{width}px;"
790
            output << view.content_tag(:div, '&nbsp;'.html_safe,
791
                                       :style => style,
792
                                       :class => "#{options[:css]} task_late")
793
          end
794
          if coords[:bar_progress_end]
795
            width = coords[:bar_progress_end] - coords[:bar_start] - 2
796
            style = ""
797
            style << "top:#{params[:top]}px;"
798
            style << "left:#{coords[:bar_start]}px;"
799
            style << "width:#{width}px;"
800
            html_id = "task-done-issue-#{options[:issue].id}" if options[:issue]
801
            html_id = "task-done-version-#{options[:version].id}" if options[:version]
802
            output << view.content_tag(:div, '&nbsp;'.html_safe,
803
                                       :style => style,
804
                                       :class => "#{options[:css]} task_done",
805
                                       :id => html_id)
806
          end
807
        end
808
        # Renders the markers
809
        if options[:markers]
810
          if coords[:start]
811
            style = ""
812
            style << "top:#{params[:top]}px;"
813
            style << "left:#{coords[:start]}px;"
814
            style << "width:15px;"
815
            output << view.content_tag(:div, '&nbsp;'.html_safe,
816
                                       :style => style,
817
                                       :class => "#{options[:css]} marker starting")
818
          end
819
          if coords[:end]
820
            style = ""
821
            style << "top:#{params[:top]}px;"
822
            style << "left:#{coords[:end] + params[:zoom]}px;"
823
            style << "width:15px;"
824
            output << view.content_tag(:div, '&nbsp;'.html_safe,
825
                                       :style => style,
826
                                       :class => "#{options[:css]} marker ending")
827
          end
828
        end
829
        # Renders the label on the right
830
        if options[:label]
831
          style = ""
832
          style << "top:#{params[:top]}px;"
833
          style << "left:#{(coords[:bar_end] || 0) + 8}px;"
834
          style << "width:15px;"
835
          output << view.content_tag(:div, options[:label],
836
                                     :style => style,
837
                                     :class => "#{options[:css]} label")
838
        end
839
        # Renders the tooltip
840
        if options[:issue] && coords[:bar_start] && coords[:bar_end]
841
          s = view.content_tag(:span,
842
                               view.render_issue_tooltip(options[:issue]).html_safe,
843
                               :class => "tip")
844
          style = ""
845
          style << "position: absolute;"
846
          style << "top:#{params[:top]}px;"
847
          style << "left:#{coords[:bar_start]}px;"
848
          style << "width:#{coords[:bar_end] - coords[:bar_start]}px;"
849
          style << "height:12px;"
850
          output << view.content_tag(:div, s.html_safe,
851
                                     :style => style,
852
                                     :class => "tooltip")
853
        end
854
        @lines << output
855
        output
856
      end
857

    
858
      def pdf_task(params, coords, options={})
859
        height = options[:height] || 2
860
        # Renders the task bar, with progress and late
861
        if coords[:bar_start] && coords[:bar_end]
862
          params[:pdf].SetY(params[:top] + 1.5)
863
          params[:pdf].SetX(params[:subject_width] + coords[:bar_start])
864
          params[:pdf].SetFillColor(200, 200, 200)
865
          params[:pdf].RDMCell(coords[:bar_end] - coords[:bar_start], height, "", 0, 0, "", 1)
866
          if coords[:bar_late_end]
867
            params[:pdf].SetY(params[:top] + 1.5)
868
            params[:pdf].SetX(params[:subject_width] + coords[:bar_start])
869
            params[:pdf].SetFillColor(255, 100, 100)
870
            params[:pdf].RDMCell(coords[:bar_late_end] - coords[:bar_start], height, "", 0, 0, "", 1)
871
          end
872
          if coords[:bar_progress_end]
873
            params[:pdf].SetY(params[:top] + 1.5)
874
            params[:pdf].SetX(params[:subject_width] + coords[:bar_start])
875
            params[:pdf].SetFillColor(90, 200, 90)
876
            params[:pdf].RDMCell(coords[:bar_progress_end] - coords[:bar_start], height, "", 0, 0, "", 1)
877
          end
878
        end
879
        # Renders the markers
880
        if options[:markers]
881
          if coords[:start]
882
            params[:pdf].SetY(params[:top] + 1)
883
            params[:pdf].SetX(params[:subject_width] + coords[:start] - 1)
884
            params[:pdf].SetFillColor(50, 50, 200)
885
            params[:pdf].RDMCell(2, 2, "", 0, 0, "", 1)
886
          end
887
          if coords[:end]
888
            params[:pdf].SetY(params[:top] + 1)
889
            params[:pdf].SetX(params[:subject_width] + coords[:end] - 1)
890
            params[:pdf].SetFillColor(50, 50, 200)
891
            params[:pdf].RDMCell(2, 2, "", 0, 0, "", 1)
892
          end
893
        end
894
        # Renders the label on the right
895
        if options[:label]
896
          params[:pdf].SetX(params[:subject_width] + (coords[:bar_end] || 0) + 5)
897
          params[:pdf].RDMCell(30, 2, options[:label])
898
        end
899
      end
900

    
901
      def image_task(params, coords, options={})
902
        height = options[:height] || 6
903
        # Renders the task bar, with progress and late
904
        if coords[:bar_start] && coords[:bar_end]
905
          params[:image].fill('#aaa')
906
          params[:image].rectangle(params[:subject_width] + coords[:bar_start],
907
                                   params[:top],
908
                                   params[:subject_width] + coords[:bar_end],
909
                                   params[:top] - height)
910
          if coords[:bar_late_end]
911
            params[:image].fill('#f66')
912
            params[:image].rectangle(params[:subject_width] + coords[:bar_start],
913
                                     params[:top],
914
                                     params[:subject_width] + coords[:bar_late_end],
915
                                     params[:top] - height)
916
          end
917
          if coords[:bar_progress_end]
918
            params[:image].fill('#00c600')
919
            params[:image].rectangle(params[:subject_width] + coords[:bar_start],
920
                                     params[:top],
921
                                     params[:subject_width] + coords[:bar_progress_end],
922
                                     params[:top] - height)
923
          end
924
        end
925
        # Renders the markers
926
        if options[:markers]
927
          if coords[:start]
928
            x = params[:subject_width] + coords[:start]
929
            y = params[:top] - height / 2
930
            params[:image].fill('blue')
931
            params[:image].polygon(x - 4, y, x, y - 4, x + 4, y, x, y + 4)
932
          end
933
          if coords[:end]
934
            x = params[:subject_width] + coords[:end] + params[:zoom]
935
            y = params[:top] - height / 2
936
            params[:image].fill('blue')
937
            params[:image].polygon(x - 4, y, x, y - 4, x + 4, y, x, y + 4)
938
          end
939
        end
940
        # Renders the label on the right
941
        if options[:label]
942
          params[:image].fill('black')
943
          params[:image].text(params[:subject_width] + (coords[:bar_end] || 0) + 5,
944
                              params[:top] + 1,
945
                              options[:label])
946
        end
947
      end
948
    end
949
  end
950
end