comparison app/helpers/timelog_helper.rb @ 909:cbb26bc654de redmine-1.3

Update to Redmine 1.3-stable branch (Redmine SVN rev 8964)
author Chris Cannam
date Fri, 24 Feb 2012 19:09:32 +0000
parents 513646585e45
children 433d4f72a19b
comparison
equal deleted inserted replaced
908:c6c2cbd0afee 909:cbb26bc654de
1 # redMine - project management software 1 # encoding: utf-8
2 # Copyright (C) 2006 Jean-Philippe Lang 2 #
3 # Redmine - project management software
4 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 # 5 #
4 # This program is free software; you can redistribute it and/or 6 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 7 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 8 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 9 # of the License, or (at your option) any later version.
8 # 10 #
9 # This program is distributed in the hope that it will be useful, 11 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details. 14 # GNU General Public License for more details.
13 # 15 #
14 # You should have received a copy of the GNU General Public License 16 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software 17 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 19
18 module TimelogHelper 20 module TimelogHelper
19 include ApplicationHelper 21 include ApplicationHelper
20 22
21 def render_timelog_breadcrumb 23 def render_timelog_breadcrumb
22 links = [] 24 links = []
23 links << link_to(l(:label_project_all), {:project_id => nil, :issue_id => nil}) 25 links << link_to(l(:label_project_all), {:project_id => nil, :issue_id => nil})
24 links << link_to(h(@project), {:project_id => @project, :issue_id => nil}) if @project 26 links << link_to(h(@project), {:project_id => @project, :issue_id => nil}) if @project
25 if @issue 27 if @issue
50 collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default) 52 collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default)
51 end 53 end
52 activities.each { |a| collection << [a.name, a.id] } 54 activities.each { |a| collection << [a.name, a.id] }
53 collection 55 collection
54 end 56 end
55 57
56 def select_hours(data, criteria, value) 58 def select_hours(data, criteria, value)
57 if value.to_s.empty? 59 if value.to_s.empty?
58 data.select {|row| row[criteria].blank? } 60 data.select {|row| row[criteria].blank? }
59 else 61 else
60 data.select {|row| row[criteria].to_s == value.to_s} 62 data.select {|row| row[criteria].to_s == value.to_s}
61 end 63 end
62 end 64 end
63 65
64 def sum_hours(data) 66 def sum_hours(data)
65 sum = 0 67 sum = 0
66 data.each do |row| 68 data.each do |row|
67 sum += row['hours'].to_f 69 sum += row['hours'].to_f
68 end 70 end
69 sum 71 sum
70 end 72 end
71 73
72 def options_for_period_select(value) 74 def options_for_period_select(value)
73 options_for_select([[l(:label_all_time), 'all'], 75 options_for_select([[l(:label_all_time), 'all'],
74 [l(:label_today), 'today'], 76 [l(:label_today), 'today'],
75 [l(:label_yesterday), 'yesterday'], 77 [l(:label_yesterday), 'yesterday'],
76 [l(:label_this_week), 'current_week'], 78 [l(:label_this_week), 'current_week'],
80 [l(:label_last_month), 'last_month'], 82 [l(:label_last_month), 'last_month'],
81 [l(:label_last_n_days, 30), '30_days'], 83 [l(:label_last_n_days, 30), '30_days'],
82 [l(:label_this_year), 'current_year']], 84 [l(:label_this_year), 'current_year']],
83 value) 85 value)
84 end 86 end
85 87
86 def entries_to_csv(entries) 88 def entries_to_csv(entries)
87 ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
88 decimal_separator = l(:general_csv_decimal_separator) 89 decimal_separator = l(:general_csv_decimal_separator)
89 custom_fields = TimeEntryCustomField.find(:all) 90 custom_fields = TimeEntryCustomField.find(:all)
90 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv| 91 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
91 # csv header fields 92 # csv header fields
92 headers = [l(:field_spent_on), 93 headers = [l(:field_spent_on),
99 l(:field_hours), 100 l(:field_hours),
100 l(:field_comments) 101 l(:field_comments)
101 ] 102 ]
102 # Export custom fields 103 # Export custom fields
103 headers += custom_fields.collect(&:name) 104 headers += custom_fields.collect(&:name)
104 105
105 csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end } 106 csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8(
107 c.to_s,
108 l(:general_csv_encoding) ) }
106 # csv lines 109 # csv lines
107 entries.each do |entry| 110 entries.each do |entry|
108 fields = [format_date(entry.spent_on), 111 fields = [format_date(entry.spent_on),
109 entry.user, 112 entry.user,
110 entry.activity, 113 entry.activity,
114 (entry.issue ? entry.issue.subject : nil), 117 (entry.issue ? entry.issue.subject : nil),
115 entry.hours.to_s.gsub('.', decimal_separator), 118 entry.hours.to_s.gsub('.', decimal_separator),
116 entry.comments 119 entry.comments
117 ] 120 ]
118 fields += custom_fields.collect {|f| show_value(entry.custom_value_for(f)) } 121 fields += custom_fields.collect {|f| show_value(entry.custom_value_for(f)) }
119 122
120 csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end } 123 csv << fields.collect {|c| Redmine::CodesetUtil.from_utf8(
124 c.to_s,
125 l(:general_csv_encoding) ) }
121 end 126 end
122 end 127 end
123 export 128 export
124 end 129 end
125 130
126 def format_criteria_value(criteria, value) 131 def format_criteria_value(criteria, value)
127 if value.blank? 132 if value.blank?
128 l(:label_none) 133 l(:label_none)
129 elsif k = @available_criterias[criteria][:klass] 134 elsif k = @available_criterias[criteria][:klass]
130 obj = k.find_by_id(value.to_i) 135 obj = k.find_by_id(value.to_i)
135 end 140 end
136 else 141 else
137 format_value(value, @available_criterias[criteria][:format]) 142 format_value(value, @available_criterias[criteria][:format])
138 end 143 end
139 end 144 end
140 145
141 def report_to_csv(criterias, periods, hours) 146 def report_to_csv(criterias, periods, hours)
147 decimal_separator = l(:general_csv_decimal_separator)
142 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv| 148 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
143 # Column headers 149 # Column headers
144 headers = criterias.collect {|criteria| l(@available_criterias[criteria][:label]) } 150 headers = criterias.collect {|criteria| l(@available_criterias[criteria][:label]) }
145 headers += periods 151 headers += periods
146 headers << l(:label_total) 152 headers << l(:label_total)
147 csv << headers.collect {|c| to_utf8(c) } 153 csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8(
154 c.to_s,
155 l(:general_csv_encoding) ) }
148 # Content 156 # Content
149 report_criteria_to_csv(csv, criterias, periods, hours) 157 report_criteria_to_csv(csv, criterias, periods, hours)
150 # Total row 158 # Total row
151 row = [ l(:label_total) ] + [''] * (criterias.size - 1) 159 str_total = Redmine::CodesetUtil.from_utf8(l(:label_total), l(:general_csv_encoding))
160 row = [ str_total ] + [''] * (criterias.size - 1)
152 total = 0 161 total = 0
153 periods.each do |period| 162 periods.each do |period|
154 sum = sum_hours(select_hours(hours, @columns, period.to_s)) 163 sum = sum_hours(select_hours(hours, @columns, period.to_s))
155 total += sum 164 total += sum
156 row << (sum > 0 ? "%.2f" % sum : '') 165 row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '')
157 end 166 end
158 row << "%.2f" %total 167 row << ("%.2f" % total).gsub('.',decimal_separator)
159 csv << row 168 csv << row
160 end 169 end
161 export 170 export
162 end 171 end
163 172
164 def report_criteria_to_csv(csv, criterias, periods, hours, level=0) 173 def report_criteria_to_csv(csv, criterias, periods, hours, level=0)
174 decimal_separator = l(:general_csv_decimal_separator)
165 hours.collect {|h| h[criterias[level]].to_s}.uniq.each do |value| 175 hours.collect {|h| h[criterias[level]].to_s}.uniq.each do |value|
166 hours_for_value = select_hours(hours, criterias[level], value) 176 hours_for_value = select_hours(hours, criterias[level], value)
167 next if hours_for_value.empty? 177 next if hours_for_value.empty?
168 row = [''] * level 178 row = [''] * level
169 row << to_utf8(format_criteria_value(criterias[level], value)) 179 row << Redmine::CodesetUtil.from_utf8(
180 format_criteria_value(criterias[level], value).to_s,
181 l(:general_csv_encoding) )
170 row += [''] * (criterias.length - level - 1) 182 row += [''] * (criterias.length - level - 1)
171 total = 0 183 total = 0
172 periods.each do |period| 184 periods.each do |period|
173 sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s)) 185 sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s))
174 total += sum 186 total += sum
175 row << (sum > 0 ? "%.2f" % sum : '') 187 row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '')
176 end 188 end
177 row << "%.2f" %total 189 row << ("%.2f" % total).gsub('.',decimal_separator)
178 csv << row 190 csv << row
179
180 if criterias.length > level + 1 191 if criterias.length > level + 1
181 report_criteria_to_csv(csv, criterias, periods, hours_for_value, level + 1) 192 report_criteria_to_csv(csv, criterias, periods, hours_for_value, level + 1)
182 end 193 end
183 end 194 end
184 end 195 end
185
186 def to_utf8(s)
187 @ic ||= Iconv.new(l(:general_csv_encoding), 'UTF-8')
188 begin; @ic.iconv(s.to_s); rescue; s.to_s; end
189 end
190 end 196 end