To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / app / helpers / timelog_helper.rb @ 1298:4f746d8966dd
History | View | Annotate | Download (5.64 KB)
| 1 | 909:cbb26bc654de | Chris | # encoding: utf-8
|
|---|---|---|---|
| 2 | #
|
||
| 3 | # Redmine - project management software
|
||
| 4 | 1295:622f24f53b42 | Chris | # Copyright (C) 2006-2013 Jean-Philippe Lang
|
| 5 | 0:513646585e45 | Chris | #
|
| 6 | # This program is free software; you can redistribute it and/or
|
||
| 7 | # modify it under the terms of the GNU General Public License
|
||
| 8 | # as published by the Free Software Foundation; either version 2
|
||
| 9 | # of the License, or (at your option) any later version.
|
||
| 10 | 909:cbb26bc654de | Chris | #
|
| 11 | 0:513646585e45 | Chris | # This program is distributed in the hope that it will be useful,
|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
| 14 | # GNU General Public License for more details.
|
||
| 15 | 909:cbb26bc654de | Chris | #
|
| 16 | 0:513646585e45 | Chris | # You should have received a copy of the GNU General Public License
|
| 17 | # along with this program; if not, write to the Free Software
|
||
| 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
| 19 | |||
| 20 | module TimelogHelper |
||
| 21 | include ApplicationHelper
|
||
| 22 | 909:cbb26bc654de | Chris | |
| 23 | 0:513646585e45 | Chris | def render_timelog_breadcrumb |
| 24 | links = [] |
||
| 25 | links << link_to(l(:label_project_all), {:project_id => nil, :issue_id => nil}) |
||
| 26 | links << link_to(h(@project), {:project_id => @project, :issue_id => nil}) if @project |
||
| 27 | if @issue |
||
| 28 | if @issue.visible? |
||
| 29 | links << link_to_issue(@issue, :subject => false) |
||
| 30 | else
|
||
| 31 | links << "##{@issue.id}"
|
||
| 32 | end
|
||
| 33 | end
|
||
| 34 | breadcrumb links |
||
| 35 | end
|
||
| 36 | |||
| 37 | # Returns a collection of activities for a select field. time_entry
|
||
| 38 | # is optional and will be used to check if the selected TimeEntryActivity
|
||
| 39 | # is active.
|
||
| 40 | def activity_collection_for_select_options(time_entry=nil, project=nil) |
||
| 41 | project ||= @project
|
||
| 42 | if project.nil?
|
||
| 43 | activities = TimeEntryActivity.shared.active
|
||
| 44 | else
|
||
| 45 | activities = project.activities |
||
| 46 | end
|
||
| 47 | |||
| 48 | collection = [] |
||
| 49 | if time_entry && time_entry.activity && !time_entry.activity.active?
|
||
| 50 | collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] |
||
| 51 | else
|
||
| 52 | collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default) |
||
| 53 | end
|
||
| 54 | activities.each { |a| collection << [a.name, a.id] }
|
||
| 55 | collection |
||
| 56 | end
|
||
| 57 | 909:cbb26bc654de | Chris | |
| 58 | 0:513646585e45 | Chris | def select_hours(data, criteria, value) |
| 59 | 1295:622f24f53b42 | Chris | if value.to_s.empty?
|
| 60 | data.select {|row| row[criteria].blank? }
|
||
| 61 | 909:cbb26bc654de | Chris | else
|
| 62 | 1295:622f24f53b42 | Chris | data.select {|row| row[criteria].to_s == value.to_s}
|
| 63 | 0:513646585e45 | Chris | end
|
| 64 | end
|
||
| 65 | 909:cbb26bc654de | Chris | |
| 66 | 0:513646585e45 | Chris | def sum_hours(data) |
| 67 | sum = 0
|
||
| 68 | data.each do |row|
|
||
| 69 | sum += row['hours'].to_f
|
||
| 70 | end
|
||
| 71 | sum |
||
| 72 | end
|
||
| 73 | 909:cbb26bc654de | Chris | |
| 74 | 0:513646585e45 | Chris | def options_for_period_select(value) |
| 75 | options_for_select([[l(:label_all_time), 'all'], |
||
| 76 | [l(:label_today), 'today'], |
||
| 77 | [l(:label_yesterday), 'yesterday'], |
||
| 78 | [l(:label_this_week), 'current_week'], |
||
| 79 | [l(:label_last_week), 'last_week'], |
||
| 80 | 1115:433d4f72a19b | Chris | [l(:label_last_n_weeks, 2), 'last_2_weeks'], |
| 81 | 0:513646585e45 | Chris | [l(:label_last_n_days, 7), '7_days'], |
| 82 | [l(:label_this_month), 'current_month'], |
||
| 83 | [l(:label_last_month), 'last_month'], |
||
| 84 | [l(:label_last_n_days, 30), '30_days'], |
||
| 85 | [l(:label_this_year), 'current_year']], |
||
| 86 | value) |
||
| 87 | end
|
||
| 88 | 909:cbb26bc654de | Chris | |
| 89 | 1115:433d4f72a19b | Chris | def format_criteria_value(criteria_options, value) |
| 90 | 0:513646585e45 | Chris | if value.blank?
|
| 91 | 1115:433d4f72a19b | Chris | "[#{l(:label_none)}]"
|
| 92 | elsif k = criteria_options[:klass] |
||
| 93 | 0:513646585e45 | Chris | obj = k.find_by_id(value.to_i) |
| 94 | if obj.is_a?(Issue) |
||
| 95 | obj.visible? ? "#{obj.tracker} ##{obj.id}: #{obj.subject}" : "##{obj.id}" |
||
| 96 | else
|
||
| 97 | obj |
||
| 98 | end
|
||
| 99 | else
|
||
| 100 | 1115:433d4f72a19b | Chris | format_value(value, criteria_options[:format])
|
| 101 | 0:513646585e45 | Chris | end
|
| 102 | end
|
||
| 103 | 909:cbb26bc654de | Chris | |
| 104 | 1115:433d4f72a19b | Chris | def report_to_csv(report) |
| 105 | 909:cbb26bc654de | Chris | decimal_separator = l(:general_csv_decimal_separator)
|
| 106 | 0:513646585e45 | Chris | export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv| |
| 107 | # Column headers
|
||
| 108 | 1115:433d4f72a19b | Chris | headers = report.criteria.collect {|criteria| l(report.available_criteria[criteria][:label]) }
|
| 109 | headers += report.periods |
||
| 110 | 1295:622f24f53b42 | Chris | headers << l(:label_total_time)
|
| 111 | 909:cbb26bc654de | Chris | csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8(
|
| 112 | c.to_s, |
||
| 113 | l(:general_csv_encoding) ) }
|
||
| 114 | 0:513646585e45 | Chris | # Content
|
| 115 | 1115:433d4f72a19b | Chris | report_criteria_to_csv(csv, report.available_criteria, report.columns, report.criteria, report.periods, report.hours) |
| 116 | 0:513646585e45 | Chris | # Total row
|
| 117 | 1295:622f24f53b42 | Chris | str_total = Redmine::CodesetUtil.from_utf8(l(:label_total_time), l(:general_csv_encoding)) |
| 118 | 1115:433d4f72a19b | Chris | row = [ str_total ] + [''] * (report.criteria.size - 1) |
| 119 | 0:513646585e45 | Chris | total = 0
|
| 120 | 1115:433d4f72a19b | Chris | report.periods.each do |period|
|
| 121 | sum = sum_hours(select_hours(report.hours, report.columns, period.to_s)) |
||
| 122 | 0:513646585e45 | Chris | total += sum |
| 123 | 909:cbb26bc654de | Chris | row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '') |
| 124 | 0:513646585e45 | Chris | end
|
| 125 | 909:cbb26bc654de | Chris | row << ("%.2f" % total).gsub('.',decimal_separator) |
| 126 | 0:513646585e45 | Chris | csv << row |
| 127 | end
|
||
| 128 | export |
||
| 129 | end
|
||
| 130 | 909:cbb26bc654de | Chris | |
| 131 | 1115:433d4f72a19b | Chris | def report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours, level=0) |
| 132 | 909:cbb26bc654de | Chris | decimal_separator = l(:general_csv_decimal_separator)
|
| 133 | 1115:433d4f72a19b | Chris | hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value|
|
| 134 | hours_for_value = select_hours(hours, criteria[level], value) |
||
| 135 | 0:513646585e45 | Chris | next if hours_for_value.empty? |
| 136 | row = [''] * level
|
||
| 137 | 909:cbb26bc654de | Chris | row << Redmine::CodesetUtil.from_utf8( |
| 138 | 1115:433d4f72a19b | Chris | format_criteria_value(available_criteria[criteria[level]], value).to_s, |
| 139 | 909:cbb26bc654de | Chris | l(:general_csv_encoding) )
|
| 140 | 1115:433d4f72a19b | Chris | row += [''] * (criteria.length - level - 1) |
| 141 | 0:513646585e45 | Chris | total = 0
|
| 142 | periods.each do |period|
|
||
| 143 | 1115:433d4f72a19b | Chris | sum = sum_hours(select_hours(hours_for_value, columns, period.to_s)) |
| 144 | 0:513646585e45 | Chris | total += sum |
| 145 | 909:cbb26bc654de | Chris | row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '') |
| 146 | 0:513646585e45 | Chris | end
|
| 147 | 909:cbb26bc654de | Chris | row << ("%.2f" % total).gsub('.',decimal_separator) |
| 148 | 0:513646585e45 | Chris | csv << row |
| 149 | 1115:433d4f72a19b | Chris | if criteria.length > level + 1 |
| 150 | report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours_for_value, level + 1)
|
||
| 151 | 0:513646585e45 | Chris | end
|
| 152 | end
|
||
| 153 | end
|
||
| 154 | end |