annotate .svn/pristine/d3/d3a6378c9406ba10d03e5d3c6c1f099aa9094db0.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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