##// END OF EJS Templates
replace tabs to spaces at app/helpers/timelog_helper.rb...
Toshi MARUYAMA -
r11176:5778bdd4f7c3
parent child
Show More
@@ -1,197 +1,197
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2013 Jean-Philippe Lang
5 5 #
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; either version 2
9 9 # of the License, or (at your option) any later version.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 19
20 20 module TimelogHelper
21 21 include ApplicationHelper
22 22
23 23 def render_timelog_breadcrumb
24 24 links = []
25 25 links << link_to(l(:label_project_all), {:project_id => nil, :issue_id => nil})
26 26 links << link_to(h(@project), {:project_id => @project, :issue_id => nil}) if @project
27 27 if @issue
28 28 if @issue.visible?
29 29 links << link_to_issue(@issue, :subject => false)
30 30 else
31 31 links << "##{@issue.id}"
32 32 end
33 33 end
34 34 breadcrumb links
35 35 end
36 36
37 37 # Returns a collection of activities for a select field. time_entry
38 38 # is optional and will be used to check if the selected TimeEntryActivity
39 39 # is active.
40 40 def activity_collection_for_select_options(time_entry=nil, project=nil)
41 41 project ||= @project
42 42 if project.nil?
43 43 activities = TimeEntryActivity.shared.active
44 44 else
45 45 activities = project.activities
46 46 end
47 47
48 48 collection = []
49 49 if time_entry && time_entry.activity && !time_entry.activity.active?
50 50 collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ]
51 51 else
52 52 collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default)
53 53 end
54 54 activities.each { |a| collection << [a.name, a.id] }
55 55 collection
56 56 end
57 57
58 58 def select_hours(data, criteria, value)
59 if value.to_s.empty?
60 data.select {|row| row[criteria].blank? }
59 if value.to_s.empty?
60 data.select {|row| row[criteria].blank? }
61 61 else
62 data.select {|row| row[criteria].to_s == value.to_s}
62 data.select {|row| row[criteria].to_s == value.to_s}
63 63 end
64 64 end
65 65
66 66 def sum_hours(data)
67 67 sum = 0
68 68 data.each do |row|
69 69 sum += row['hours'].to_f
70 70 end
71 71 sum
72 72 end
73 73
74 74 def options_for_period_select(value)
75 75 options_for_select([[l(:label_all_time), 'all'],
76 76 [l(:label_today), 'today'],
77 77 [l(:label_yesterday), 'yesterday'],
78 78 [l(:label_this_week), 'current_week'],
79 79 [l(:label_last_week), 'last_week'],
80 80 [l(:label_last_n_weeks, 2), 'last_2_weeks'],
81 81 [l(:label_last_n_days, 7), '7_days'],
82 82 [l(:label_this_month), 'current_month'],
83 83 [l(:label_last_month), 'last_month'],
84 84 [l(:label_last_n_days, 30), '30_days'],
85 85 [l(:label_this_year), 'current_year']],
86 86 value)
87 87 end
88 88
89 89 def entries_to_csv(entries)
90 90 decimal_separator = l(:general_csv_decimal_separator)
91 91 custom_fields = TimeEntryCustomField.all
92 92 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
93 93 # csv header fields
94 94 headers = [l(:field_spent_on),
95 95 l(:field_user),
96 96 l(:field_activity),
97 97 l(:field_project),
98 98 l(:field_issue),
99 99 l(:field_tracker),
100 100 l(:field_subject),
101 101 l(:field_hours),
102 102 l(:field_comments)
103 103 ]
104 104 # Export custom fields
105 105 headers += custom_fields.collect(&:name)
106 106
107 107 csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8(
108 108 c.to_s,
109 109 l(:general_csv_encoding) ) }
110 110 # csv lines
111 111 entries.each do |entry|
112 112 fields = [format_date(entry.spent_on),
113 113 entry.user,
114 114 entry.activity,
115 115 entry.project,
116 116 (entry.issue ? entry.issue.id : nil),
117 117 (entry.issue ? entry.issue.tracker : nil),
118 118 (entry.issue ? entry.issue.subject : nil),
119 119 entry.hours.to_s.gsub('.', decimal_separator),
120 120 entry.comments
121 121 ]
122 122 fields += custom_fields.collect {|f| show_value(entry.custom_field_values.detect {|v| v.custom_field_id == f.id}) }
123 123
124 124 csv << fields.collect {|c| Redmine::CodesetUtil.from_utf8(
125 125 c.to_s,
126 126 l(:general_csv_encoding) ) }
127 127 end
128 128 end
129 129 export
130 130 end
131 131
132 132 def format_criteria_value(criteria_options, value)
133 133 if value.blank?
134 134 "[#{l(:label_none)}]"
135 135 elsif k = criteria_options[:klass]
136 136 obj = k.find_by_id(value.to_i)
137 137 if obj.is_a?(Issue)
138 138 obj.visible? ? "#{obj.tracker} ##{obj.id}: #{obj.subject}" : "##{obj.id}"
139 139 else
140 140 obj
141 141 end
142 142 else
143 143 format_value(value, criteria_options[:format])
144 144 end
145 145 end
146 146
147 147 def report_to_csv(report)
148 148 decimal_separator = l(:general_csv_decimal_separator)
149 149 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
150 150 # Column headers
151 151 headers = report.criteria.collect {|criteria| l(report.available_criteria[criteria][:label]) }
152 152 headers += report.periods
153 153 headers << l(:label_total)
154 154 csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8(
155 155 c.to_s,
156 156 l(:general_csv_encoding) ) }
157 157 # Content
158 158 report_criteria_to_csv(csv, report.available_criteria, report.columns, report.criteria, report.periods, report.hours)
159 159 # Total row
160 160 str_total = Redmine::CodesetUtil.from_utf8(l(:label_total), l(:general_csv_encoding))
161 161 row = [ str_total ] + [''] * (report.criteria.size - 1)
162 162 total = 0
163 163 report.periods.each do |period|
164 164 sum = sum_hours(select_hours(report.hours, report.columns, period.to_s))
165 165 total += sum
166 166 row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '')
167 167 end
168 168 row << ("%.2f" % total).gsub('.',decimal_separator)
169 169 csv << row
170 170 end
171 171 export
172 172 end
173 173
174 174 def report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours, level=0)
175 175 decimal_separator = l(:general_csv_decimal_separator)
176 176 hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value|
177 177 hours_for_value = select_hours(hours, criteria[level], value)
178 178 next if hours_for_value.empty?
179 179 row = [''] * level
180 180 row << Redmine::CodesetUtil.from_utf8(
181 181 format_criteria_value(available_criteria[criteria[level]], value).to_s,
182 182 l(:general_csv_encoding) )
183 183 row += [''] * (criteria.length - level - 1)
184 184 total = 0
185 185 periods.each do |period|
186 186 sum = sum_hours(select_hours(hours_for_value, columns, period.to_s))
187 187 total += sum
188 188 row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '')
189 189 end
190 190 row << ("%.2f" % total).gsub('.',decimal_separator)
191 191 csv << row
192 192 if criteria.length > level + 1
193 193 report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours_for_value, level + 1)
194 194 end
195 195 end
196 196 end
197 197 end
General Comments 0
You need to be logged in to leave comments. Login now