diff --git a/app/controllers/gantts_controller.rb b/app/controllers/gantts_controller.rb index bc2d635..7ccafd1 100644 --- a/app/controllers/gantts_controller.rb +++ b/app/controllers/gantts_controller.rb @@ -15,23 +15,7 @@ class GanttsController < ApplicationController @gantt = Redmine::Helpers::Gantt.new(params) retrieve_query @query.group_by = nil - if @query.valid? - events = [] - # Issues that have start and due dates - events += @query.issues(:include => [:tracker, :assigned_to, :priority], - :order => "start_date, due_date", - :conditions => ["(((start_date>=? and start_date<=?) or (due_date>=? and due_date<=?) or (start_date?)) and start_date is not null and due_date is not null)", @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to] - ) - # Issues that don't have a due date but that are assigned to a version with a date - events += @query.issues(:include => [:tracker, :assigned_to, :priority, :fixed_version], - :order => "start_date, effective_date", - :conditions => ["(((start_date>=? and start_date<=?) or (effective_date>=? and effective_date<=?) or (start_date?)) and start_date is not null and due_date is null and effective_date is not null)", @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to] - ) - # Versions - events += @query.versions(:conditions => ["effective_date BETWEEN ? AND ?", @gantt.date_from, @gantt.date_to]) - - @gantt.events = events - end + @gantt.fill_events_with_query(@query) if @query.valid? basename = (@project ? "#{@project.identifier}-" : '') + 'gantt' diff --git a/lib/redmine/helpers/gantt.rb b/lib/redmine/helpers/gantt.rb index 330b58e..18d8881 100644 --- a/lib/redmine/helpers/gantt.rb +++ b/lib/redmine/helpers/gantt.rb @@ -51,7 +51,24 @@ module Redmine @date_from = Date.civil(@year_from, @month_from, 1) @date_to = (@date_from >> @months) - 1 end - + + def fill_events_with_query(query) + events = [] + # Issues that have start and due dates + events += query.issues(:include => [:tracker, :assigned_to, :priority], + :order => "#{Issue.quoted_table_name}.start_date, #{Issue.quoted_table_name}.due_date", + :conditions => conditions_for_issues_with_dates) + + # Issues that don't have a due date but that are assigned to a version with a date + events += query.issues(:include => [:tracker, :assigned_to, :priority, :fixed_version], + :order => "#{Issue.quoted_table_name}.start_date, #{Version.quoted_table_name}.effective_date", + :conditions => conditions_for_issues_with_milestone_dates) + + # Versions + events += query.versions(:conditions => ["#{Version.quoted_table_name}.effective_date BETWEEN ? AND ?", date_from, date_to]) + + self.events = events + end def events=(e) @events = e @@ -269,6 +286,59 @@ module Redmine x.start_date <=> y.start_date end end + + def conditions_for_issues_with_dates + [<= ? AND + #{Issue.quoted_table_name}.start_date <= ?) + OR + (#{Issue.quoted_table_name}.due_date >= ? AND + #{Issue.quoted_table_name}.due_date <= ?) + OR + (#{Issue.quoted_table_name}.start_date < ? AND + #{Issue.quoted_table_name}.due_date > ?) + ) + AND + #{Issue.quoted_table_name}.start_date IS NOT NULL + AND + #{Issue.quoted_table_name}.due_date IS NOT NULL + ) +SQL + end + + def conditions_for_issues_with_milestone_dates + [<= ? + AND + #{Issue.quoted_table_name}.start_date <= ? + ) + OR + ( + #{Version.quoted_table_name}.effective_date >= ? + AND + #{Version.quoted_table_name}.effective_date <= ? + ) + OR + ( + #{Issue.quoted_table_name}.start_date < ? + AND + #{Version.quoted_table_name}.effective_date > ? + ) + ) + AND + #{Issue.quoted_table_name}.start_date IS NOT NULL + AND + #{Issue.quoted_table_name}.due_date IS NULL + AND + #{Version.quoted_table_name}.effective_date IS NOT NULL + ) +SQL + end end end end -- 1.7.1