annotate app/helpers/timelog_helper.rb @ 1295:622f24f53b42 redmine-2.3

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