From 2986afc05ed5154b059e1408f32436a97e54f272 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Wed, 10 Sep 2008 18:26:13 +0000 Subject: [PATCH] Adds support for free ticket filtering and custom queries on Gantt chart. ProjectsController#gantt moved to IssuesController. git-svn-id: http://redmine.rubyforge.org/svn/trunk@1797 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/issues_controller.rb | 34 ++++- app/controllers/projects_controller.rb | 63 -------- app/helpers/projects_helper.rb | 150 ------------------- app/views/issues/_sidebar.rhtml | 12 +- app/views/{projects => issues}/gantt.rfpdf | 44 +++--- app/views/{projects => issues}/gantt.rhtml | 159 ++++++++++---------- app/views/projects/show.rhtml | 2 +- config/initializers/20-mime_types.rb | 1 + lib/redmine.rb | 2 +- lib/redmine/helpers/gantt.rb | 223 ++++++++++++++++++++++++++++ public/stylesheets/application.css | 3 +- test/functional/issues_controller_test.rb | 35 +++++ test/functional/projects_controller_test.rb | 41 ----- test/unit/helpers/projects_helper_test.rb | 41 ----- 14 files changed, 412 insertions(+), 398 deletions(-) rename app/views/{projects => issues}/gantt.rfpdf (72%) rename app/views/{projects => issues}/gantt.rhtml (54%) create mode 100644 lib/redmine/helpers/gantt.rb delete mode 100644 test/unit/helpers/projects_helper_test.rb diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index 7d572924..43a04f26 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -20,7 +20,7 @@ class IssuesController < ApplicationController before_filter :find_issue, :only => [:show, :edit, :reply, :destroy_attachment] before_filter :find_issues, :only => [:bulk_edit, :move, :destroy] - before_filter :find_project, :only => [:new, :update_form, :preview] + before_filter :find_project, :only => [:new, :update_form, :preview, :gantt] before_filter :authorize, :except => [:index, :changes, :preview, :update_form, :context_menu] before_filter :find_optional_project, :only => [:index, :changes] accept_key_auth :index, :changes @@ -322,6 +322,38 @@ class IssuesController < ApplicationController redirect_to :action => 'show', :id => @issue end + def gantt + @gantt = Redmine::Helpers::Gantt.new(params) + retrieve_query + if @query.valid? + events = [] + # Issues that have start and due dates + events += Issue.find(:all, + :order => "start_date, due_date", + :include => [:tracker, :status, :assigned_to, :priority, :project], + :conditions => ["(#{@query.statement}) AND (((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 += Issue.find(:all, + :order => "start_date, effective_date", + :include => [:tracker, :status, :assigned_to, :priority, :project, :fixed_version], + :conditions => ["(#{@query.statement}) AND (((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] + ) + # Related versions + version_ids = events.collect(&:fixed_version_id).compact.uniq + events += Version.find_all_by_id(version_ids, :include => :project, + :conditions => ["effective_date BETWEEN ? AND ?", @gantt.date_from, @gantt.date_to]) unless version_ids.empty? + + @gantt.events = events + end + + respond_to do |format| + format.html { render :template => "issues/gantt.rhtml", :layout => !request.xhr? } + format.png { send_data(@gantt.to_image, :disposition => 'inline', :type => 'image/png', :filename => "#{@project.identifier}-gantt.png") } if @gantt.respond_to?('to_image') + format.pdf { send_data(render(:template => "issues/gantt.rfpdf", :layout => false), :type => 'application/pdf', :filename => "#{@project.identifier}-gantt.pdf") } + end + end + def context_menu @issues = Issue.find_all_by_id(params[:ids], :include => :project) if (@issues.size == 1) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 0d83d81b..9e1df29a 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -272,69 +272,6 @@ class ProjectsController < ApplicationController @calendar.events = events render :layout => false if request.xhr? - end - - def gantt - @trackers = @project.rolled_up_trackers - retrieve_selected_tracker_ids(@trackers) - - if params[:year] and params[:year].to_i >0 - @year_from = params[:year].to_i - if params[:month] and params[:month].to_i >=1 and params[:month].to_i <= 12 - @month_from = params[:month].to_i - else - @month_from = 1 - end - else - @month_from ||= Date.today.month - @year_from ||= Date.today.year - end - - zoom = (params[:zoom] || User.current.pref[:gantt_zoom]).to_i - @zoom = (zoom > 0 && zoom < 5) ? zoom : 2 - months = (params[:months] || User.current.pref[:gantt_months]).to_i - @months = (months > 0 && months < 25) ? months : 6 - - # Save gantt paramters as user preference (zoom and months count) - if (User.current.logged? && (@zoom != User.current.pref[:gantt_zoom] || @months != User.current.pref[:gantt_months])) - User.current.pref[:gantt_zoom], User.current.pref[:gantt_months] = @zoom, @months - User.current.preference.save - end - - @date_from = Date.civil(@year_from, @month_from, 1) - @date_to = (@date_from >> @months) - 1 - @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1') - - @events = [] - @project.issues_with_subprojects(@with_subprojects) do - # Issues that have start and due dates - @events += Issue.find(:all, - :order => "start_date, due_date", - :include => [:tracker, :status, :assigned_to, :priority, :project], - :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 and #{Issue.table_name}.tracker_id in (#{@selected_tracker_ids.join(',')}))", @date_from, @date_to, @date_from, @date_to, @date_from, @date_to] - ) unless @selected_tracker_ids.empty? - # Issues that don't have a due date but that are assigned to a version with a date - @events += Issue.find(:all, - :order => "start_date, effective_date", - :include => [:tracker, :status, :assigned_to, :priority, :project, :fixed_version], - :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 and #{Issue.table_name}.tracker_id in (#{@selected_tracker_ids.join(',')}))", @date_from, @date_to, @date_from, @date_to, @date_from, @date_to] - ) unless @selected_tracker_ids.empty? - @events += Version.find(:all, :include => :project, - :conditions => ["effective_date BETWEEN ? AND ?", @date_from, @date_to]) - end - @events.sort! {|x,y| x.start_date <=> y.start_date } - - if params[:format]=='pdf' - @options_for_rfpdf ||= {} - @options_for_rfpdf[:file_name] = "#{@project.identifier}-gantt.pdf" - render :template => "projects/gantt.rfpdf", :layout => false - elsif params[:format]=='png' && respond_to?('gantt_image') - image = gantt_image(@events, @date_from, @months, @zoom) - image.format = 'PNG' - send_data(image.to_blob, :disposition => 'inline', :type => 'image/png', :filename => "#{@project.identifier}-gantt.png") - else - render :template => "projects/gantt.rhtml" - end end private diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb index 912482f1..cd2e743f 100644 --- a/app/helpers/projects_helper.rb +++ b/app/helpers/projects_helper.rb @@ -45,154 +45,4 @@ module ProjectsHelper ] tabs.select {|tab| User.current.allowed_to?(tab[:action], @project)} end - - # Generates a gantt image - # Only defined if RMagick is avalaible - def gantt_image(events, date_from, months, zoom) - date_to = (date_from >> months)-1 - show_weeks = zoom > 1 - show_days = zoom > 2 - - subject_width = 320 - header_heigth = 18 - # width of one day in pixels - zoom = zoom*2 - g_width = (date_to - date_from + 1)*zoom - g_height = 20 * events.length + 20 - headers_heigth = (show_weeks ? 2*header_heigth : header_heigth) - height = g_height + headers_heigth - - imgl = Magick::ImageList.new - imgl.new_image(subject_width+g_width+1, height) - gc = Magick::Draw.new - - # Subjects - top = headers_heigth + 20 - gc.fill('black') - gc.stroke('transparent') - gc.stroke_width(1) - events.each do |i| - gc.text(4, top + 2, (i.is_a?(Issue) ? i.subject : i.name)) - top = top + 20 - end - - # Months headers - month_f = date_from - left = subject_width - months.times do - width = ((month_f >> 1) - month_f) * zoom - gc.fill('white') - gc.stroke('grey') - gc.stroke_width(1) - gc.rectangle(left, 0, left + width, height) - gc.fill('black') - gc.stroke('transparent') - gc.stroke_width(1) - gc.text(left.round + 8, 14, "#{month_f.year}-#{month_f.month}") - left = left + width - month_f = month_f >> 1 - end - - # Weeks headers - if show_weeks - left = subject_width - height = header_heigth - if date_from.cwday == 1 - # date_from is monday - week_f = date_from - else - # find next monday after date_from - week_f = date_from + (7 - date_from.cwday + 1) - width = (7 - date_from.cwday + 1) * zoom - gc.fill('white') - gc.stroke('grey') - gc.stroke_width(1) - gc.rectangle(left, header_heigth, left + width, 2*header_heigth + g_height-1) - left = left + width - end - while week_f <= date_to - width = (week_f + 6 <= date_to) ? 7 * zoom : (date_to - week_f + 1) * zoom - gc.fill('white') - gc.stroke('grey') - gc.stroke_width(1) - gc.rectangle(left.round, header_heigth, left.round + width, 2*header_heigth + g_height-1) - gc.fill('black') - gc.stroke('transparent') - gc.stroke_width(1) - gc.text(left.round + 2, header_heigth + 14, week_f.cweek.to_s) - left = left + width - week_f = week_f+7 - end - end - - # Days details (week-end in grey) - if show_days - left = subject_width - height = g_height + header_heigth - 1 - wday = date_from.cwday - (date_to - date_from + 1).to_i.times do - width = zoom - gc.fill(wday == 6 || wday == 7 ? '#eee' : 'white') - gc.stroke('grey') - gc.stroke_width(1) - gc.rectangle(left, 2*header_heigth, left + width, 2*header_heigth + g_height-1) - left = left + width - wday = wday + 1 - wday = 1 if wday > 7 - end - end - - # border - gc.fill('transparent') - gc.stroke('grey') - gc.stroke_width(1) - gc.rectangle(0, 0, subject_width+g_width, headers_heigth) - gc.stroke('black') - gc.rectangle(0, 0, subject_width+g_width, g_height+ headers_heigth-1) - - # content - top = headers_heigth + 20 - gc.stroke('transparent') - events.each do |i| - if i.is_a?(Issue) - i_start_date = (i.start_date >= date_from ? i.start_date : date_from ) - i_end_date = (i.due_date <= date_to ? i.due_date : date_to ) - i_done_date = i.start_date + ((i.due_date - i.start_date+1)*i.done_ratio/100).floor - i_done_date = (i_done_date <= date_from ? date_from : i_done_date ) - i_done_date = (i_done_date >= date_to ? date_to : i_done_date ) - i_late_date = [i_end_date, Date.today].min if i_start_date < Date.today - - i_left = subject_width + ((i_start_date - date_from)*zoom).floor - i_width = ((i_end_date - i_start_date + 1)*zoom).floor # total width of the issue - d_width = ((i_done_date - i_start_date)*zoom).floor # done width - l_width = i_late_date ? ((i_late_date - i_start_date+1)*zoom).floor : 0 # delay width - - gc.fill('grey') - gc.rectangle(i_left, top, i_left + i_width, top - 6) - gc.fill('red') - gc.rectangle(i_left, top, i_left + l_width, top - 6) if l_width > 0 - gc.fill('blue') - gc.rectangle(i_left, top, i_left + d_width, top - 6) if d_width > 0 - gc.fill('black') - gc.text(i_left + i_width + 5,top + 1, "#{i.status.name} #{i.done_ratio}%") - else - i_left = subject_width + ((i.start_date - date_from)*zoom).floor - gc.fill('green') - gc.rectangle(i_left, top, i_left + 6, top - 6) - gc.fill('black') - gc.text(i_left + 11, top + 1, i.name) - end - top = top + 20 - end - - # today red line - if Date.today >= date_from and Date.today <= date_to - gc.stroke('red') - x = (Date.today-date_from+1)*zoom + subject_width - gc.line(x, headers_heigth, x, headers_heigth + g_height-1) - end - - gc.draw(imgl) - imgl - end if Object.const_defined?(:Magick) end diff --git a/app/views/issues/_sidebar.rhtml b/app/views/issues/_sidebar.rhtml index e94d4180..bf989493 100644 --- a/app/views/issues/_sidebar.rhtml +++ b/app/views/issues/_sidebar.rhtml @@ -3,12 +3,22 @@ <% if @project %> <%= link_to l(:field_summary), :controller => 'reports', :action => 'issue_report', :id => @project %>
<%= link_to l(:label_change_log), :controller => 'projects', :action => 'changelog', :id => @project %> + +<% planning_links = [] + planning_links << link_to_if_authorized(l(:label_calendar), :controller => 'projects', :action => 'calendar', :id => @project) + planning_links << link_to_if_authorized(l(:label_gantt), :action => 'gantt', :project_id => @project) + planning_links.compact! + unless planning_links.empty? %> +

<%= l(:label_planning) %>

+

<%= planning_links.join(' | ') %>

+<% end %> + <% end %> <% unless sidebar_queries.empty? -%>

<%= l(:label_query_plural) %>

<% sidebar_queries.each do |query| -%> -<%= link_to query.name, :controller => 'issues', :action => 'index', :project_id => @project, :query_id => query %>
+<%= link_to query.name, :query_id => query %>
<% end -%> <% end -%> diff --git a/app/views/projects/gantt.rfpdf b/app/views/issues/gantt.rfpdf similarity index 72% rename from app/views/projects/gantt.rfpdf rename to app/views/issues/gantt.rfpdf index e94fc581..692e0d26 100644 --- a/app/views/projects/gantt.rfpdf +++ b/app/views/issues/gantt.rfpdf @@ -17,17 +17,17 @@ headers_heigth = header_heigth show_weeks = false show_days = false -if @months < 7 +if @gantt.months < 7 show_weeks = true headers_heigth = 2*header_heigth - if @months < 3 + if @gantt.months < 3 show_days = true headers_heigth = 3*header_heigth end end g_width = 210 -zoom = (g_width) / (@date_to - @date_from + 1) +zoom = (g_width) / (@gantt.date_to - @gantt.date_from + 1) g_height = 120 t_height = g_height + headers_heigth @@ -37,10 +37,10 @@ y_start = pdf.GetY # # Months headers # -month_f = @date_from +month_f = @gantt.date_from left = subject_width height = header_heigth -@months.times do +@gantt.months.times do width = ((month_f >> 1) - month_f) * zoom pdf.SetY(y_start) pdf.SetX(left) @@ -55,20 +55,20 @@ end if show_weeks left = subject_width height = header_heigth - if @date_from.cwday == 1 - # @date_from is monday - week_f = @date_from + if @gantt.date_from.cwday == 1 + # @gantt.date_from is monday + week_f = @gantt.date_from else - # find next monday after @date_from - week_f = @date_from + (7 - @date_from.cwday + 1) - width = (7 - @date_from.cwday + 1) * zoom-1 + # find next monday after @gantt.date_from + week_f = @gantt.date_from + (7 - @gantt.date_from.cwday + 1) + width = (7 - @gantt.date_from.cwday + 1) * zoom-1 pdf.SetY(y_start + header_heigth) pdf.SetX(left) pdf.Cell(width + 1, height, "", "LTR") left = left + width+1 end - while week_f <= @date_to - width = (week_f + 6 <= @date_to) ? 7 * zoom : (@date_to - week_f + 1) * zoom + while week_f <= @gantt.date_to + width = (week_f + 6 <= @gantt.date_to) ? 7 * zoom : (@gantt.date_to - week_f + 1) * zoom pdf.SetY(y_start + header_heigth) pdf.SetX(left) pdf.Cell(width, height, (width >= 5 ? week_f.cweek.to_s : ""), "LTR", 0, "C") @@ -83,9 +83,9 @@ end if show_days left = subject_width height = header_heigth - wday = @date_from.cwday + wday = @gantt.date_from.cwday pdf.SetFontStyle('B',7) - (@date_to - @date_from + 1).to_i.times do + (@gantt.date_to - @gantt.date_from + 1).to_i.times do width = zoom pdf.SetY(y_start + 2 * header_heigth) pdf.SetX(left) @@ -106,7 +106,7 @@ pdf.Cell(subject_width+g_width-15, headers_heigth, "", 1) # top = headers_heigth + y_start pdf.SetFontStyle('B',7) -@events.each do |i| +@gantt.events.each do |i| pdf.SetY(top) pdf.SetX(15) @@ -123,16 +123,16 @@ pdf.SetFontStyle('B',7) pdf.SetY(top+1.5) if i.is_a? Issue - i_start_date = (i.start_date >= @date_from ? i.start_date : @date_from ) - i_end_date = (i.due_before <= @date_to ? i.due_before : @date_to ) + i_start_date = (i.start_date >= @gantt.date_from ? i.start_date : @gantt.date_from ) + i_end_date = (i.due_before <= @gantt.date_to ? i.due_before : @gantt.date_to ) i_done_date = i.start_date + ((i.due_before - i.start_date+1)*i.done_ratio/100).floor - i_done_date = (i_done_date <= @date_from ? @date_from : i_done_date ) - i_done_date = (i_done_date >= @date_to ? @date_to : i_done_date ) + i_done_date = (i_done_date <= @gantt.date_from ? @gantt.date_from : i_done_date ) + i_done_date = (i_done_date >= @gantt.date_to ? @gantt.date_to : i_done_date ) i_late_date = [i_end_date, Date.today].min if i_start_date < Date.today - i_left = ((i_start_date - @date_from)*zoom) + i_left = ((i_start_date - @gantt.date_from)*zoom) i_width = ((i_end_date - i_start_date + 1)*zoom) d_width = ((i_done_date - i_start_date)*zoom) l_width = ((i_late_date - i_start_date+1)*zoom) if i_late_date @@ -159,7 +159,7 @@ pdf.SetFontStyle('B',7) pdf.SetX(subject_width + i_left + i_width) pdf.Cell(30, 2, "#{i.status.name} #{i.done_ratio}%") else - i_left = ((i.start_date - @date_from)*zoom) + i_left = ((i.start_date - @gantt.date_from)*zoom) pdf.SetX(subject_width + i_left) pdf.SetFillColor(50,200,50) diff --git a/app/views/projects/gantt.rhtml b/app/views/issues/gantt.rhtml similarity index 54% rename from app/views/projects/gantt.rhtml rename to app/views/issues/gantt.rhtml index b18bca34..7da65771 100644 --- a/app/views/projects/gantt.rhtml +++ b/app/views/issues/gantt.rhtml @@ -1,5 +1,54 @@ +<% form_tag(params.merge(:month => nil, :year => nil, :months => nil), :id => 'query_form') do %> +<% if @query.new_record? %> +

<%=l(:label_gantt)%>

+
<%= l(:label_filter_plural) %> + <%= render :partial => 'queries/filters', :locals => {:query => @query} %> +
+<% else %> +

<%=h @query.name %>

+
+ <% html_title @query.name %> +<% end %> + +
<%= l(:label_date_range) %> + <%= text_field_tag 'months', @gantt.months, :size => 2 %> + <%= l(:label_months_from) %> + <%= select_month(@gantt.month_from, :prefix => "month", :discard_type => true) %> + <%= select_year(@gantt.year_from, :prefix => "year", :discard_type => true) %> + <%= hidden_field_tag 'zoom', @gantt.zoom %> +
+ +

+<%= if @gantt.zoom < 4 + link_to_remote image_tag('zoom_in.png'), {:url => @gantt.params.merge(:zoom => (@gantt.zoom+1)), :update => 'content'}, {:href => url_for(@gantt.params.merge(:zoom => (@gantt.zoom+1)))} + else + image_tag 'zoom_in_g.png' + end %> +<%= if @gantt.zoom > 1 + link_to_remote image_tag('zoom_out.png'), {:url => @gantt.params.merge(:zoom => (@gantt.zoom-1)), :update => 'content'}, {:href => url_for(@gantt.params.merge(:zoom => (@gantt.zoom-1)))} + else + image_tag 'zoom_out_g.png' + end %> +

+ +

+<%= link_to_remote l(:button_apply), + { :url => { :set_filter => (@query.new_record? ? 1 : nil) }, + :update => "content", + :with => "Form.serialize('query_form')" + }, :class => 'icon icon-checked' %> + +<%= link_to_remote l(:button_clear), + { :url => { :set_filter => (@query.new_record? ? 1 : nil) }, + :update => "content", + }, :class => 'icon icon-reload' if @query.new_record? %> +

+<% end %> + +<%= error_messages_for 'query' %> +<% if @query.valid? %> <% zoom = 1 -@zoom.times { zoom = zoom * 2 } +@gantt.zoom.times { zoom = zoom * 2 } subject_width = 330 header_heigth = 18 @@ -8,56 +57,23 @@ headers_height = header_heigth show_weeks = false show_days = false -if @zoom >1 +if @gantt.zoom >1 show_weeks = true headers_height = 2*header_heigth - if @zoom > 2 + if @gantt.zoom > 2 show_days = true headers_height = 3*header_heigth end end -g_width = (@date_to - @date_from + 1)*zoom -g_height = [(20 * @events.length + 6)+150, 206].max +g_width = (@gantt.date_to - @gantt.date_from + 1)*zoom +g_height = [(20 * @gantt.events.length + 6)+150, 206].max t_height = g_height + headers_height %> -
-
- -

<%= l(:label_gantt) %>

- -<% form_tag(params.merge(:month => nil, :year => nil, :months => nil)) do %> - - - - - - -
- - <%= l(:label_months_from) %> - <%= select_month(@month_from, :prefix => "month", :discard_type => true) %> - <%= select_year(@year_from, :prefix => "year", :discard_type => true) %> - <%= hidden_field_tag 'zoom', @zoom %> - <%= submit_tag l(:button_submit), :class => "button-small" %> - -<%= if @zoom < 4 - link_to image_tag('zoom_in.png'), {:zoom => (@zoom+1), :year => @year_from, :month => @month_from, :months => @months, :tracker_ids => @selected_tracker_ids, :with_subprojects => params[:with_subprojects]} - else - image_tag 'zoom_in_g.png' - end %> -<%= if @zoom > 1 - link_to image_tag('zoom_out.png'),{:zoom => (@zoom-1), :year => @year_from, :month => @month_from, :months => @months, :tracker_ids => @selected_tracker_ids, :with_subprojects => params[:with_subprojects]} - else - image_tag 'zoom_out_g.png' - end %> -
-<% end %> - -
+
@@ -67,7 +83,7 @@ t_height = g_height + headers_height # Tasks subjects # top = headers_height + 8 -@events.each do |i| %> +@gantt.events.each do |i| %>
<% if i.is_a? Issue %> <%= h("#{i.project} -") unless @project && @project == i.project %> @@ -91,14 +107,14 @@ end %> # # Months headers # -month_f = @date_from +month_f = @gantt.date_from left = 0 height = (show_weeks ? header_heigth : header_heigth + g_height) -@months.times do +@gantt.months.times do width = ((month_f >> 1) - month_f) * zoom - 1 %>
- <%= link_to "#{month_f.year}-#{month_f.month}", { :year => month_f.year, :month => month_f.month, :zoom => @zoom, :months => @months, :tracker_ids => @selected_tracker_ids, :with_subprojects => params[:with_subprojects] }, :title => "#{month_name(month_f.month)} #{month_f.year}"%> + <%= link_to "#{month_f.year}-#{month_f.month}", @gantt.params.merge(:year => month_f.year, :month => month_f.month), :title => "#{month_name(month_f.month)} #{month_f.year}"%>
<% left = left + width + 1 @@ -112,21 +128,21 @@ end %> if show_weeks left = 0 height = (show_days ? header_heigth-1 : header_heigth-1 + g_height) - if @date_from.cwday == 1 + if @gantt.date_from.cwday == 1 # @date_from is monday - week_f = @date_from + week_f = @gantt.date_from else # find next monday after @date_from - week_f = @date_from + (7 - @date_from.cwday + 1) - width = (7 - @date_from.cwday + 1) * zoom-1 + week_f = @gantt.date_from + (7 - @gantt.date_from.cwday + 1) + width = (7 - @gantt.date_from.cwday + 1) * zoom-1 %>
 
<% left = left + width+1 end %> <% - while week_f <= @date_to - width = (week_f + 6 <= @date_to) ? 7 * zoom -1 : (@date_to - week_f + 1) * zoom-1 + while week_f <= @gantt.date_to + width = (week_f + 6 <= @gantt.date_to) ? 7 * zoom -1 : (@gantt.date_to - week_f + 1) * zoom-1 %>
<%= week_f.cweek if width >= 16 %> @@ -144,8 +160,8 @@ end %> if show_days left = 0 height = g_height + header_heigth - 1 - wday = @date_from.cwday - (@date_to - @date_from + 1).to_i.times do + wday = @gantt.date_from.cwday + (@gantt.date_to - @gantt.date_from + 1).to_i.times do width = zoom - 1 %>
5 %>" class="gantt_hdr"> @@ -163,18 +179,18 @@ end %> # Tasks # top = headers_height + 10 -@events.each do |i| +@gantt.events.each do |i| if i.is_a? Issue - i_start_date = (i.start_date >= @date_from ? i.start_date : @date_from ) - i_end_date = (i.due_before <= @date_to ? i.due_before : @date_to ) + i_start_date = (i.start_date >= @gantt.date_from ? i.start_date : @gantt.date_from ) + i_end_date = (i.due_before <= @gantt.date_to ? i.due_before : @gantt.date_to ) i_done_date = i.start_date + ((i.due_before - i.start_date+1)*i.done_ratio/100).floor - i_done_date = (i_done_date <= @date_from ? @date_from : i_done_date ) - i_done_date = (i_done_date >= @date_to ? @date_to : i_done_date ) + i_done_date = (i_done_date <= @gantt.date_from ? @gantt.date_from : i_done_date ) + i_done_date = (i_done_date >= @gantt.date_to ? @gantt.date_to : i_done_date ) i_late_date = [i_end_date, Date.today].min if i_start_date < Date.today - i_left = ((i_start_date - @date_from)*zoom).floor + i_left = ((i_start_date - @gantt.date_from)*zoom).floor i_width = ((i_end_date - i_start_date + 1)*zoom).floor - 2 # total width of the issue (- 2 for left and right borders) d_width = ((i_done_date - i_start_date)*zoom).floor - 2 # done width l_width = i_late_date ? ((i_late_date - i_start_date+1)*zoom).floor - 2 : 0 # delay width @@ -195,7 +211,7 @@ top = headers_height + 10 <%= render_issue_tooltip i %>
<% else - i_left = ((i.start_date - @date_from)*zoom).floor + i_left = ((i.start_date - @gantt.date_from)*zoom).floor %>
 
@@ -210,8 +226,8 @@ end %> # # Today red line (excluded from cache) # -if Date.today >= @date_from and Date.today <= @date_to %> -
 
+if Date.today >= @gantt.date_from and Date.today <= @gantt.date_to %> +
 
<% end %>
@@ -221,29 +237,22 @@ if Date.today >= @date_from and Date.today <= @date_to %> - - + +
<%= link_to ('« ' + l(:label_previous)), :year => (@date_from << @months).year, :month => (@date_from << @months).month, :zoom => @zoom, :months => @months, :tracker_ids => @selected_tracker_ids, :with_subprojects => params[:with_subprojects] %><%= link_to (l(:label_next) + ' »'), :year => (@date_from >> @months).year, :month => (@date_from >> @months).month, :zoom => @zoom, :months => @months, :tracker_ids => @selected_tracker_ids, :with_subprojects => params[:with_subprojects] %><%= link_to_remote ('« ' + l(:label_previous)), {:url => @gantt.params_previous, :update => 'content', :complete => 'window.scrollTo(0,0)'}, {:href => url_for(@gantt.params_previous)} %><%= link_to_remote (l(:label_next) + ' »'), {:url => @gantt.params_next, :update => 'content', :complete => 'window.scrollTo(0,0)'}, {:href => url_for(@gantt.params_next)} %>

<%= l(:label_export_to) %> -<%= link_to 'PDF', {:zoom => @zoom, :year => @year_from, :month => @month_from, :months => @months, :tracker_ids => @selected_tracker_ids, :with_subprojects => params[:with_subprojects], :format => 'pdf'}, :class => 'pdf' %> -<%= content_tag('span', link_to('PNG', {:zoom => @zoom, :year => @year_from, :month => @month_from, :months => @months, :tracker_ids => @selected_tracker_ids, :with_subprojects => params[:with_subprojects], :format => 'png'}, :class => 'image')) if respond_to?('gantt_image') %> +<%= link_to 'PDF', @gantt.params.merge(:format => 'pdf'), :class => 'pdf' %> +<% if @gantt.respond_to?('to_image') %> +<%= link_to 'PNG', @gantt.params.merge(:format => 'png'), :class => 'image' %> +<% end %>

+<% end # query.valid? %> <% content_for :sidebar do %> -

<%= l(:label_gantt) %>

- <% form_tag(params.merge(:tracker_ids => nil, :with_subprojects => nil), :method => :get) do %> - <% @trackers.each do |tracker| %> -
- <% end %> - <% if @project.active_children.any? %> -
- <%= hidden_field_tag 'with_subprojects', 0 %> - <% end %> -

<%= submit_tag l(:button_apply), :class => 'button-small', :name => nil %>

- <% end %> + <%= render :partial => 'issues/sidebar' %> <% end %> <% html_title(l(:label_gantt)) -%> diff --git a/app/views/projects/show.rhtml b/app/views/projects/show.rhtml index d058ecf0..fa0a60cd 100644 --- a/app/views/projects/show.rhtml +++ b/app/views/projects/show.rhtml @@ -58,7 +58,7 @@ <% content_for :sidebar do %> <% planning_links = [] planning_links << link_to_if_authorized(l(:label_calendar), :action => 'calendar', :id => @project) - planning_links << link_to_if_authorized(l(:label_gantt), :action => 'gantt', :id => @project) + planning_links << link_to_if_authorized(l(:label_gantt), :controller => 'issues', :action => 'gantt', :project_id => @project) planning_links.compact! unless planning_links.empty? %>

<%= l(:label_planning) %>

diff --git a/config/initializers/20-mime_types.rb b/config/initializers/20-mime_types.rb index 269742b1..dabac346 100644 --- a/config/initializers/20-mime_types.rb +++ b/config/initializers/20-mime_types.rb @@ -2,3 +2,4 @@ Mime::SET << Mime::CSV unless Mime::SET.include?(Mime::CSV) Mime::Type.register 'application/pdf', :pdf +Mime::Type.register 'image/png', :png diff --git a/lib/redmine.rb b/lib/redmine.rb index 3ba2f2cc..882c1c91 100644 --- a/lib/redmine.rb +++ b/lib/redmine.rb @@ -45,7 +45,7 @@ Redmine::AccessControl.map do |map| map.permission :manage_public_queries, {:queries => [:new, :edit, :destroy]}, :require => :member map.permission :save_queries, {:queries => [:new, :edit, :destroy]}, :require => :loggedin # Gantt & calendar - map.permission :view_gantt, :projects => :gantt + map.permission :view_gantt, :issues => :gantt map.permission :view_calendar, :projects => :calendar # Watchers map.permission :view_issue_watchers, {} diff --git a/lib/redmine/helpers/gantt.rb b/lib/redmine/helpers/gantt.rb new file mode 100644 index 00000000..e24be1ca --- /dev/null +++ b/lib/redmine/helpers/gantt.rb @@ -0,0 +1,223 @@ +# Redmine - project management software +# Copyright (C) 2006-2008 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +module Redmine + module Helpers + # Simple class to handle gantt chart data + class Gantt + attr_reader :year_from, :month_from, :date_from, :date_to, :zoom, :months, :events + + def initialize(options={}) + options = options.dup + @events = [] + + if options[:year] && options[:year].to_i >0 + @year_from = options[:year].to_i + if options[:month] && options[:month].to_i >=1 && options[:month].to_i <= 12 + @month_from = options[:month].to_i + else + @month_from = 1 + end + else + @month_from ||= Date.today.month + @year_from ||= Date.today.year + end + + zoom = (options[:zoom] || User.current.pref[:gantt_zoom]).to_i + @zoom = (zoom > 0 && zoom < 5) ? zoom : 2 + months = (options[:months] || User.current.pref[:gantt_months]).to_i + @months = (months > 0 && months < 25) ? months : 6 + + # Save gantt parameters as user preference (zoom and months count) + if (User.current.logged? && (@zoom != User.current.pref[:gantt_zoom] || @months != User.current.pref[:gantt_months])) + User.current.pref[:gantt_zoom], User.current.pref[:gantt_months] = @zoom, @months + User.current.preference.save + end + + @date_from = Date.civil(@year_from, @month_from, 1) + @date_to = (@date_from >> @months) - 1 + end + + def events=(e) + @events = e.sort {|x,y| x.start_date <=> y.start_date } + end + + def params + { :zoom => zoom, :year => year_from, :month => month_from, :months => months } + end + + def params_previous + { :year => (date_from << months).year, :month => (date_from << months).month, :zoom => zoom, :months => months } + end + + def params_next + { :year => (date_from >> months).year, :month => (date_from >> months).month, :zoom => zoom, :months => months } + end + + # Generates a gantt image + # Only defined if RMagick is avalaible + def to_image(format='PNG') + date_to = (@date_from >> @months)-1 + show_weeks = @zoom > 1 + show_days = @zoom > 2 + + subject_width = 320 + header_heigth = 18 + # width of one day in pixels + zoom = @zoom*2 + g_width = (@date_to - @date_from + 1)*zoom + g_height = 20 * events.length + 20 + headers_heigth = (show_weeks ? 2*header_heigth : header_heigth) + height = g_height + headers_heigth + + imgl = Magick::ImageList.new + imgl.new_image(subject_width+g_width+1, height) + gc = Magick::Draw.new + + # Subjects + top = headers_heigth + 20 + gc.fill('black') + gc.stroke('transparent') + gc.stroke_width(1) + events.each do |i| + gc.text(4, top + 2, (i.is_a?(Issue) ? i.subject : i.name)) + top = top + 20 + end + + # Months headers + month_f = @date_from + left = subject_width + @months.times do + width = ((month_f >> 1) - month_f) * zoom + gc.fill('white') + gc.stroke('grey') + gc.stroke_width(1) + gc.rectangle(left, 0, left + width, height) + gc.fill('black') + gc.stroke('transparent') + gc.stroke_width(1) + gc.text(left.round + 8, 14, "#{month_f.year}-#{month_f.month}") + left = left + width + month_f = month_f >> 1 + end + + # Weeks headers + if show_weeks + left = subject_width + height = header_heigth + if @date_from.cwday == 1 + # date_from is monday + week_f = date_from + else + # find next monday after date_from + week_f = @date_from + (7 - @date_from.cwday + 1) + width = (7 - @date_from.cwday + 1) * zoom + gc.fill('white') + gc.stroke('grey') + gc.stroke_width(1) + gc.rectangle(left, header_heigth, left + width, 2*header_heigth + g_height-1) + left = left + width + end + while week_f <= date_to + width = (week_f + 6 <= date_to) ? 7 * zoom : (date_to - week_f + 1) * zoom + gc.fill('white') + gc.stroke('grey') + gc.stroke_width(1) + gc.rectangle(left.round, header_heigth, left.round + width, 2*header_heigth + g_height-1) + gc.fill('black') + gc.stroke('transparent') + gc.stroke_width(1) + gc.text(left.round + 2, header_heigth + 14, week_f.cweek.to_s) + left = left + width + week_f = week_f+7 + end + end + + # Days details (week-end in grey) + if show_days + left = subject_width + height = g_height + header_heigth - 1 + wday = @date_from.cwday + (date_to - @date_from + 1).to_i.times do + width = zoom + gc.fill(wday == 6 || wday == 7 ? '#eee' : 'white') + gc.stroke('grey') + gc.stroke_width(1) + gc.rectangle(left, 2*header_heigth, left + width, 2*header_heigth + g_height-1) + left = left + width + wday = wday + 1 + wday = 1 if wday > 7 + end + end + + # border + gc.fill('transparent') + gc.stroke('grey') + gc.stroke_width(1) + gc.rectangle(0, 0, subject_width+g_width, headers_heigth) + gc.stroke('black') + gc.rectangle(0, 0, subject_width+g_width, g_height+ headers_heigth-1) + + # content + top = headers_heigth + 20 + gc.stroke('transparent') + events.each do |i| + if i.is_a?(Issue) + i_start_date = (i.start_date >= @date_from ? i.start_date : @date_from ) + i_end_date = (i.due_before <= date_to ? i.due_before : date_to ) + i_done_date = i.start_date + ((i.due_before - i.start_date+1)*i.done_ratio/100).floor + i_done_date = (i_done_date <= @date_from ? @date_from : i_done_date ) + i_done_date = (i_done_date >= date_to ? date_to : i_done_date ) + i_late_date = [i_end_date, Date.today].min if i_start_date < Date.today + + i_left = subject_width + ((i_start_date - @date_from)*zoom).floor + i_width = ((i_end_date - i_start_date + 1)*zoom).floor # total width of the issue + d_width = ((i_done_date - i_start_date)*zoom).floor # done width + l_width = i_late_date ? ((i_late_date - i_start_date+1)*zoom).floor : 0 # delay width + + gc.fill('grey') + gc.rectangle(i_left, top, i_left + i_width, top - 6) + gc.fill('red') + gc.rectangle(i_left, top, i_left + l_width, top - 6) if l_width > 0 + gc.fill('blue') + gc.rectangle(i_left, top, i_left + d_width, top - 6) if d_width > 0 + gc.fill('black') + gc.text(i_left + i_width + 5,top + 1, "#{i.status.name} #{i.done_ratio}%") + else + i_left = subject_width + ((i.start_date - @date_from)*zoom).floor + gc.fill('green') + gc.rectangle(i_left, top, i_left + 6, top - 6) + gc.fill('black') + gc.text(i_left + 11, top + 1, i.name) + end + top = top + 20 + end + + # today red line + if Date.today >= @date_from and Date.today <= date_to + gc.stroke('red') + x = (Date.today-@date_from+1)*zoom + subject_width + gc.line(x, headers_heigth, x, headers_heigth + g_height-1) + end + + gc.draw(imgl) + imgl.format = format + imgl.to_blob + end if Object.const_defined?(:Magick) + end + end +end diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css index e5ca9e8f..32796528 100644 --- a/public/stylesheets/application.css +++ b/public/stylesheets/application.css @@ -84,7 +84,6 @@ table.list td { vertical-align: top; } table.list td.id { width: 2%; text-align: center;} table.list td.checkbox { width: 15px; padding: 0px;} -table.list.issues { margin-top: 10px; } tr.issue { text-align: center; white-space: nowrap; } tr.issue td.subject, tr.issue td.category { white-space: normal; } tr.issue td.subject { text-align: left; } @@ -165,7 +164,7 @@ div.issue {background:#ffffdd; padding:6px; margin-bottom:6px;border: 1px solid p.breadcrumb { font-size: 0.9em; margin: 4px 0 4px 0;} p.subtitle { font-size: 0.9em; margin: -6px 0 12px 0; font-style: italic; } -fieldset#filters { padding: 0.7em; } +fieldset#filters, fieldset#date-range { padding: 0.7em; margin-bottom: 8px; } fieldset#filters p { margin: 1.2em 0 0.8em 2px; } fieldset#filters .buttons { font-size: 0.9em; } fieldset#filters table { border-collapse: collapse; } diff --git a/test/functional/issues_controller_test.rb b/test/functional/issues_controller_test.rb index a248d8bd..67e7f1f9 100644 --- a/test/functional/issues_controller_test.rb +++ b/test/functional/issues_controller_test.rb @@ -125,6 +125,41 @@ class IssuesControllerTest < Test::Unit::TestCase assert_not_nil assigns(:issues) assert_equal 'application/pdf', @response.content_type end + + def test_gantt + get :gantt, :project_id => 1 + assert_response :success + assert_template 'gantt.rhtml' + assert_not_nil assigns(:gantt) + events = assigns(:gantt).events + assert_not_nil events + # Issue with start and due dates + i = Issue.find(1) + assert_not_nil i.due_date + assert events.include?(Issue.find(1)) + # Issue with without due date but targeted to a version with date + i = Issue.find(2) + assert_nil i.due_date + assert events.include?(i) + end + + def test_gantt_export_to_pdf + get :gantt, :project_id => 1, :format => 'pdf' + assert_response :success + assert_template 'gantt.rfpdf' + assert_equal 'application/pdf', @response.content_type + assert_not_nil assigns(:gantt) + end + + if Object.const_defined?(:Magick) + def test_gantt_image + get :gantt, :project_id => 1, :format => 'png' + assert_response :success + assert_equal 'image/png', @response.content_type + end + else + puts "RMagick not installed. Skipping tests !!!" + end def test_changes get :changes, :project_id => 1 diff --git a/test/functional/projects_controller_test.rb b/test/functional/projects_controller_test.rb index 8e57dc5f..3d969d11 100644 --- a/test/functional/projects_controller_test.rb +++ b/test/functional/projects_controller_test.rb @@ -231,47 +231,6 @@ class ProjectsControllerTest < Test::Unit::TestCase assert_not_nil assigns(:calendar) assert_tag :tag => 'a', :content => /#6/ end - - def test_gantt - get :gantt, :id => 1 - assert_response :success - assert_template 'gantt.rhtml' - events = assigns(:events) - assert_not_nil events - # Issue with start and due dates - i = Issue.find(1) - assert_not_nil i.due_date - assert events.include?(Issue.find(1)) - # Issue with without due date but targeted to a version with date - i = Issue.find(2) - assert_nil i.due_date - assert events.include?(i) - end - - def test_gantt_with_subprojects_should_not_show_private_subprojects - get :gantt, :id => 1, :with_subprojects => 1, :tracker_ids => [1, 2] - assert_response :success - assert_template 'gantt.rhtml' - assert_not_nil assigns(:events) - assert_no_tag :tag => 'a', :content => /#6/ - end - - def test_gantt_with_subprojects_should_show_private_subprojects - @request.session[:user_id] = 2 - get :gantt, :id => 1, :with_subprojects => 1, :tracker_ids => [1, 2] - assert_response :success - assert_template 'gantt.rhtml' - assert_not_nil assigns(:events) - assert_tag :tag => 'a', :content => /#6/ - end - - def test_gantt_export_to_pdf - get :gantt, :id => 1, :format => 'pdf' - assert_response :success - assert_template 'gantt.rfpdf' - assert_equal 'application/pdf', @response.content_type - assert_not_nil assigns(:events) - end def test_archive @request.session[:user_id] = 1 # admin diff --git a/test/unit/helpers/projects_helper_test.rb b/test/unit/helpers/projects_helper_test.rb deleted file mode 100644 index d76d92bc..00000000 --- a/test/unit/helpers/projects_helper_test.rb +++ /dev/null @@ -1,41 +0,0 @@ -# redMine - project management software -# Copyright (C) 2006-2007 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -require File.dirname(__FILE__) + '/../../test_helper' - -class ProjectsHelperTest < HelperTestCase - include ProjectsHelper - include ActionView::Helpers::TextHelper - fixtures :projects, :trackers, :issue_statuses, :issues, :enumerations, :users, :issue_categories - - def setup - super - end - - if Object.const_defined?(:Magick) - def test_gantt_image - assert gantt_image(Issue.find(:all, :conditions => "start_date IS NOT NULL AND due_date IS NOT NULL"), Date.today, 6, 2) - end - - def test_gantt_image_with_days - assert gantt_image(Issue.find(:all, :conditions => "start_date IS NOT NULL AND due_date IS NOT NULL"), Date.today, 3, 4) - end - else - puts "RMagick not installed. Skipping tests !!!" - def test_fake; assert true end - end -end -- 2.11.0