start implementing filter application

git-svn-id: https://dev.finn.de/svn/cockpit/branches/reporting_merge@1262 7926756e-e54e-46e6-9721-ed318f58905e
pull/6827/head
t.felgentreff 15 years ago
parent 901a47480f
commit 8124c459fb
  1. 57
      app/controllers/cost_reports_controller.rb
  2. 70
      app/views/cost_reports/_cost_report_table.rhtml
  3. 64
      app/views/cost_reports/index.rhtml

@ -2,6 +2,63 @@ class CostReportsController < ApplicationController
before_filter :find_optional_project, :only => [:index]
helper :reporting
include ReportingHelper
##
# Determines if the request contains filters to set
def set_filter?
params[:set_filter].to_i == 1
end
##
# Determines whether the session-saved query can to be used
def session_query?
!session[:cost_query].nil?
end
# FIXME: Remove € symbol
def walker(query)
walker = CostQuery::Walker.new(query)
walker
end
##
# Find a query to search on
def query_params
filters = session[:cost_query] if session_query?
if set_filter?
filters = {}
filters[:operators] = params[:operators]
filters[:values] = params[:values]
end
filters ||= {:operators => {:user_id => "=", :tweek => "="},
:values => {:user_id => [User.current.id], :tweek => [Date.today.cweek]}}
end
##
# Build the query from the current request and save it to
# the session.
def query
session[:cost_query] = query_params
CostQuery.new.tap do |q|
(session[:cost_query][:operators] || []).each do |filter, operator|
unless (session[:cost_query][:values] || {})[filter].nil?
require 'ruby-debug'; debugger
q.filter(filter.to_sym,
:operator => operator,
:values => session[:cost_query][:values][filter])
end
end
end.
column(:tweek).column(:tyear).
row(:project_id).row(:user_id)
end
def index
@query = query
@walker = walker(@query)
render :layout => !request.xhr?
end
private
def find_optional_project

@ -0,0 +1,70 @@
<% # FIXME: Move this somewhere safe
##
# For a given row, determine how to render it's contents according to usability and
# localization rules
def show_row(row)
row.render do |key, value|
case key.to_sym
when :project_id then "Project ##{value}: #{Project.find(value.to_i).name}"
when :user_id then link_to_user User.find(value)
when :tyear then value
when :tweek then
if value.to_i == Date.today.cweek
l(:label_this_week).capitalize
elsif value.to_i == (Date.today.cweek - 1)
l(:label_last_week).capitalize
else
"#{l(:label_week)} ##{value}"
end
else "#{key}: #{value}"
end
end
end
walker.for_final_row do |row, cells|
"<td>#{show_row row}</td>" << cells.join << "<td>#{row.real_costs} €</td>"
end
walker.for_row do |row, subrows|
subrows.flatten!
unless row.fields.empty?
subrows[0] = "<td rowspan='#{subrows.size}'>#{show_row row}</td>"
"#{subrows[0]}<td rowspan='#{subrows.size}'>#{row.real_costs} €</td>"
end
subrows
end
walker.for_empty_cell { "<td></td>" }
walker.for_cell do |result|
"<td>#{result.real_costs} €</td>"
end
%>
<table style='text-align: center' border='1'>
<% walker.headers do |list, first| %>
<tr>
<%= "<td rowspan='#{query.depth_of(:column)}' colspan='#{query.depth_of(:row) - 1}'></td>" if first %>
<% list.each do |column| %>
<td colspan="<%= column.final_number(:column) %>"><%= show_row column %></td>
<% end %>
<%= "<td rowspan='#{@query.depth_of(:column)}' colspan='#{query.depth_of(:row) - 1}'></td>" if first %>
</tr>
<% end %>
<% walker.body do |line| %>
<tr><%= line %></tr>
<% end %>
<% walker.headers(:reverse) do |list, final| %>
<% first = list.first.final_column? %>
<tr>
<%= "<td rowspan='#{query.depth_of(:column) - 1}' colspan='#{query.depth_of(:row) - 1}'></td>" if first %>
<% if !final then list.each do |column| %>
<td colspan="<%= column.final_number(:column) %>"><%= column.real_costs %> €</td>
<% end end %>
<%= "<td rowspan='#{query.depth_of(:column) - 1}' colspan='#{query.depth_of(:row) - 1}'>#{query.real_costs} €</td>" if first %>
</tr>
<% end %>
</table>

@ -1,6 +1,3 @@
<%# things above this line should be deleted or go into the controller %>
<% @query = CostQuery.example %>
<% if @custom_error %>
<div class="flash error"><%= @custom_error %></div>
<% end %>
@ -41,63 +38,4 @@
<% end %>
<h1>Die sch&ouml;ne Tabelle:</h1>
<%
def show_row(row)
row.render do |key, value|
case key.to_sym
when :project_id then "Project ##{value}: #{Project.find(value.to_i).name}"
when :user_id then link_to_user User.find(value)
when :tyear then value
when :tweek then "Week ##{value}"
else "#{key}: #{value}"
end
end
end
@walker = CostQuery::Walker.new @query
@walker.for_final_row do |row, cells|
"<td>#{show_row row}</td>" << cells.join << "<td>#{row.real_costs} €</td>"
end
@walker.for_row do |row, subrows|
subrows.flatten!
unless row.fields.empty?
subrows[0] = "<td rowspan='#{subrows.size}'>#{show_row row}</td>" \
"#{subrows[0]}<td rowspan='#{subrows.size}'>#{row.real_costs} €</td>"
end
subrows
end
@walker.for_empty_cell { "<td></td>" }
@walker.for_cell do |result|
"<td>#{result.real_costs} €</td>"
end
%>
<table style='text-align: center' border='1'>
<% @walker.headers do |list, first| %>
<tr>
<%= "<td rowspan='#{@query.depth_of(:column)}' colspan='#{@query.depth_of(:row) - 1}'></td>" if first %>
<% list.each do |column| %>
<td colspan="<%= column.final_number(:column) %>"><%= show_row column %></td>
<% end %>
<%= "<td rowspan='#{@query.depth_of(:column)}' colspan='#{@query.depth_of(:row) - 1}'></td>" if first %>
</tr>
<% end %>
<% @walker.body do |line| %>
<tr><%= line %></tr>
<% end %>
<% @walker.headers(:reverse) do |list, final| %>
<% first = list.first.final_column? %>
<tr>
<%= "<td rowspan='#{@query.depth_of(:column) - 1}' colspan='#{@query.depth_of(:row) - 1}'></td>" if first %>
<% if !final then list.each do |column| %>
<td colspan="<%= column.final_number(:column) %>"><%= column.real_costs %> €</td>
<% end end %>
<%= "<td rowspan='#{@query.depth_of(:column) - 1}' colspan='#{@query.depth_of(:row) - 1}'>#{@query.real_costs} €</td>" if first %>
</tr>
<% end %>
</table>
<%= render :partial => "cost_report_table", :locals => {:query => @query, :walker => @walker} %>

Loading…
Cancel
Save