Project

General

Profile

I am trying to store the gantt months_from, and year_from in the session

Added by Shivam Barthwal over 3 years ago

I want to store the gantt months from, year in the session when we hit apply to view the gantt. Currently in Redmine 4.1, when we select the months and year in the gantt view and later visit the gantt page the year and month selection is not stored to the value we selected on previous visit to the gantt page, instead it hovers back to the default. So I was trying to store it on the session, but I do not have any success. Can someone help me. Here is my code

Gantt controller

class GanttsController < ApplicationController
  menu_item :gantt
  before_action :find_optional_project

  rescue_from Query::StatementInvalid, :with => :query_statement_invalid

  helper :gantt
  helper :issues
  helper :projects
  helper :queries
  include QueriesHelper
  include Redmine::Export::PDF

  def show
    @gantt = Redmine::Helpers::Gantt.new(params)
    set_gantt_dates(@gantt)
    @gantt.project = @project    
    retrieve_query
    @query.group_by = nil
    @gantt.query = @query if @query.valid?

    basename = (@project ? "#{@project.identifier}-" : '') + 'gantt'

    respond_to do |format|
      format.html { render :action => "show", :layout => !request.xhr? }
      format.png  { send_data(@gantt.to_image, :disposition => 'inline', :type => 'image/png', :filename => "#{basename}.png") } if @gantt.respond_to?('to_image')
      format.pdf  { send_data(@gantt.to_pdf, :type => 'application/pdf', :filename => "#{basename}.pdf") }
    end
  end
end

I implemented a method in application controller where I pass the gantt variable to the method to store the month and year in session as below.

  def set_gantt_dates(gantt)
    session[:user_id] = User.current.id
    session[:month_from] = gantt.month_from
    session[:year_from] = gantt.year_from
    session[:months] = gantt.months 
  end
  helper_method :set_gantt_dates

I am accessing this helper method in my show view page gantt, but it does not make any difference

<p class="buttons">
  <% set_gantt_dates(@gantt) %>
  <%= number_field_tag 'months', @gantt.months, :min => 1, :max => Setting.gantt_months_limit.to_i, :autocomplete => false %>
  <%= l(:label_months_from) %>
  <%= select_month(session[:month_from], :prefix => "month", :discard_type => true) %>
  <%= select_year(session[:year_from], :prefix => "year", :discard_type => true) %>
  <%= hidden_field_tag 'zoom', @gantt.zoom %>

  <!--<%= link_to_function l(:button_apply), '$("#query_form").submit()',
                       :class => 'icon icon-checked' %> !-->
  <%= button_tag "Apply", :class => 'icon icon-checked', :onclick => "submitQuery()" %>
  <%= link_to l(:button_clear), { :project_id => @project, :set_filter => 1 },
              :class => 'icon icon-reload' %>
  <% if @query.new_record? && User.current.allowed_to?(:save_queries, @project, :global => true) %>
    <%= link_to_function l(:button_save),
                         "$('#query_form').attr('action', '#{ @project ? new_project_query_path(@project) : new_query_path }').submit();",
                         :class => 'icon icon-save' %>
  <% end %>
<% if !@query.new_record? && @query.editable_by?(User.current) %>
  <%= link_to l(:button_edit), edit_query_path(@query, :gantt => 1), :class => 'icon icon-edit' %>
  <%= delete_link query_path(@query, :gantt => 1) %>
<% end %>
</p>
</div>
<% end %>

Please help