Module: TimelogHelper

Includes:
ApplicationHelper
Included in:
TimelogController
Defined in:
app/helpers/timelog_helper.rb

Overview

redMine - project management software Copyright (C) 2006 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.

Instance Method Summary collapse

Methods included from ApplicationHelper

#accesskey, #authoring, #authorize_for, #avatar, #back_url_hidden_field_tag, #breadcrumb, #calendar_for, #check_all_links, #content_for, #context_menu_link, #current_role, #due_date_distance_in_words, #format_activity_day, #format_activity_description, #format_activity_title, #has_content?, #html_hours, #html_title, #image_to_function, #include_calendar_headers_tags, #label_tag_for, #labelled_tabular_form_for, #lang_options_for_select, #link_to_attachment, #link_to_if_authorized, #link_to_issue, #link_to_remote_if_authorized, #link_to_user, #other_formats_links, #page_header_title, #pagination_links_full, #path_to_stylesheet, #per_page_links, #progress_bar, #project_nested_ul, #project_tree, #project_tree_options_for_select, #prompt_to_remote, #render_flash_messages, #render_page_hierarchy, #render_project_jump_box, #reorder_links, #simple_format_without_paragraph, #stylesheet_path, #syntax_highlight, #textilizable, #to_path_param, #toggle_link, #truncate_single_line

Methods included from Redmine::I18n

#current_language, #day_name, #find_language, #format_date, #format_time, included, #l, #l_hours, #l_or_humanize, #ll, #month_name, #set_language_if_valid, #valid_languages

Methods included from Redmine::WikiFormatting::Macros::Definitions

#exec_macro, #extract_macro_options

Instance Method Details

#activity_collection_for_select_optionsObject



29
30
31
32
33
34
35
# File 'app/helpers/timelog_helper.rb', line 29

def activity_collection_for_select_options
  activities = Enumeration.activities
  collection = []
  collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default)
  activities.each { |a| collection << [a.name, a.id] }
  collection
end

#entries_to_csv(entries) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/helpers/timelog_helper.rb', line 63

def entries_to_csv(entries)
  ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')    
  decimal_separator = l(:general_csv_decimal_separator)
  custom_fields = TimeEntryCustomField.find(:all)
  export = StringIO.new
  CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
    # csv header fields
    headers = [l(:field_spent_on),
               l(:field_user),
               l(:field_activity),
               l(:field_project),
               l(:field_issue),
               l(:field_tracker),
               l(:field_subject),
               l(:field_hours),
               l(:field_comments)
               ]
    # Export custom fields
    headers += custom_fields.collect(&:name)
    
    csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
    # csv lines
    entries.each do |entry|
      fields = [format_date(entry.spent_on),
                entry.user,
                entry.activity,
                entry.project,
                (entry.issue ? entry.issue.id : nil),
                (entry.issue ? entry.issue.tracker : nil),
                (entry.issue ? entry.issue.subject : nil),
                entry.hours.to_s.gsub('.', decimal_separator),
                entry.comments
                ]
      fields += custom_fields.collect {|f| show_value(entry.custom_value_for(f)) }
                
      csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
    end
  end
  export.rewind
  export
end

#format_criteria_value(criteria, value) ⇒ Object



105
106
107
# File 'app/helpers/timelog_helper.rb', line 105

def format_criteria_value(criteria, value)
  value.blank? ? l(:label_none) : ((k = @available_criterias[criteria][:klass]) ? k.find_by_id(value.to_i) : format_value(value, @available_criterias[criteria][:format]))
end

#options_for_period_select(value) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/helpers/timelog_helper.rb', line 49

def options_for_period_select(value)
  options_for_select([[l(:label_all_time), 'all'],
                      [l(:label_today), 'today'],
                      [l(:label_yesterday), 'yesterday'],
                      [l(:label_this_week), 'current_week'],
                      [l(:label_last_week), 'last_week'],
                      [l(:label_last_n_days, 7), '7_days'],
                      [l(:label_this_month), 'current_month'],
                      [l(:label_last_month), 'last_month'],
                      [l(:label_last_n_days, 30), '30_days'],
                      [l(:label_this_year), 'current_year']],
                      value)
end

#render_timelog_breadcrumbObject



21
22
23
24
25
26
27
# File 'app/helpers/timelog_helper.rb', line 21

def render_timelog_breadcrumb
  links = []
  links << link_to(l(:label_project_all), {:project_id => nil, :issue_id => nil})
  links << link_to(h(@project), {:project_id => @project, :issue_id => nil}) if @project
  links << link_to_issue(@issue) if @issue
  breadcrumb links
end

#report_criteria_to_csv(csv, criterias, periods, hours, level = 0) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/helpers/timelog_helper.rb', line 134

def report_criteria_to_csv(csv, criterias, periods, hours, level=0)
  hours.collect {|h| h[criterias[level]].to_s}.uniq.each do |value|
    hours_for_value = select_hours(hours, criterias[level], value)
    next if hours_for_value.empty?
    row = [''] * level
    row << to_utf8(format_criteria_value(criterias[level], value))
    row += [''] * (criterias.length - level - 1)
    total = 0
    periods.each do |period|
      sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s))
      total += sum
      row << (sum > 0 ? "%.2f" % sum : '')
    end
    row << "%.2f" %total
    csv << row
    
    if criterias.length > level + 1
      report_criteria_to_csv(csv, criterias, periods, hours_for_value, level + 1)
    end
  end
end

#report_to_csv(criterias, periods, hours) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/helpers/timelog_helper.rb', line 109

def report_to_csv(criterias, periods, hours)
  export = StringIO.new
  CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
    # Column headers
    headers = criterias.collect {|criteria| l(@available_criterias[criteria][:label]) }
    headers += periods
    headers << l(:label_total)
    csv << headers.collect {|c| to_utf8(c) }
    # Content
    report_criteria_to_csv(csv, criterias, periods, hours)
    # Total row
    row = [ l(:label_total) ] + [''] * (criterias.size - 1)
    total = 0
    periods.each do |period|
      sum = sum_hours(select_hours(hours, @columns, period.to_s))
      total += sum
      row << (sum > 0 ? "%.2f" % sum : '')
    end
    row << "%.2f" %total
    csv << row
  end
  export.rewind
  export
end

#select_hours(data, criteria, value) ⇒ Object



37
38
39
# File 'app/helpers/timelog_helper.rb', line 37

def select_hours(data, criteria, value)
  data.select {|row| row[criteria] == value}
end

#sum_hours(data) ⇒ Object



41
42
43
44
45
46
47
# File 'app/helpers/timelog_helper.rb', line 41

def sum_hours(data)
  sum = 0
  data.each do |row|
    sum += row['hours'].to_f
  end
  sum
end

#to_utf8(s) ⇒ Object



156
157
158
159
# File 'app/helpers/timelog_helper.rb', line 156

def to_utf8(s)
  @ic ||= Iconv.new(l(:general_csv_encoding), 'UTF-8')
  begin; @ic.iconv(s.to_s); rescue; s.to_s; end
end