File: app/helpers/timelog_helper.rb

Overview
Module Structure
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: TimelogHelper#20
includes
  ApplicationHelper   
has properties
method: render_timelog_breadcrumb #23
method: activity_collection_for_select_options / 2 #40
method: select_hours / 3 #58
method: sum_hours / 1 #66
method: options_for_period_select / 1 #74
method: entries_to_csv / 1 #88
method: format_criteria_value / 2 #131
method: report_to_csv / 1 #146
method: report_criteria_to_csv / 7 #173

Code

   1  # encoding: utf-8
   2  #
   3  # Redmine - project management software
   4  # Copyright (C) 2006-2011  Jean-Philippe Lang
   5  #
   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  #
  11  # 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  #
  16  # 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 
  23    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 
  58    def select_hours(data, criteria, value)
  59          if value.to_s.empty?
  60                  data.select {|row| row[criteria].blank? }
  61      else
  62          data.select {|row| row[criteria].to_s == value.to_s}
  63      end
  64    end
  65 
  66    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 
  74    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                          [l(:label_last_n_days, 7), '7_days'],
  81                          [l(:label_this_month), 'current_month'],
  82                          [l(:label_last_month), 'last_month'],
  83                          [l(:label_last_n_days, 30), '30_days'],
  84                          [l(:label_this_year), 'current_year']],
  85                          value)
  86    end
  87 
  88    def entries_to_csv(entries)
  89      decimal_separator = l(:general_csv_decimal_separator)
  90      custom_fields = TimeEntryCustomField.find(:all)
  91      export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
  92        # csv header fields
  93        headers = [l(:field_spent_on),
  94                   l(:field_user),
  95                   l(:field_activity),
  96                   l(:field_project),
  97                   l(:field_issue),
  98                   l(:field_tracker),
  99                   l(:field_subject),
 100                   l(:field_hours),
 101                   l(:field_comments)
 102                   ]
 103        # Export custom fields
 104        headers += custom_fields.collect(&:name)
 105 
 106        csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8(
 107                                       c.to_s,
 108                                       l(:general_csv_encoding) )  }
 109        # csv lines
 110        entries.each do |entry|
 111          fields = [format_date(entry.spent_on),
 112                    entry.user,
 113                    entry.activity,
 114                    entry.project,
 115                    (entry.issue ? entry.issue.id : nil),
 116                    (entry.issue ? entry.issue.tracker : nil),
 117                    (entry.issue ? entry.issue.subject : nil),
 118                    entry.hours.to_s.gsub('.', decimal_separator),
 119                    entry.comments
 120                    ]
 121          fields += custom_fields.collect {|f| show_value(entry.custom_field_values.detect {|v| v.custom_field_id == f.id}) }
 122 
 123          csv << fields.collect {|c| Redmine::CodesetUtil.from_utf8(
 124                                       c.to_s,
 125                                       l(:general_csv_encoding) )  }
 126        end
 127      end
 128      export
 129    end
 130 
 131    def format_criteria_value(criteria_options, value)
 132      if value.blank?
 133        "[#{l(:label_none)}]"
 134      elsif k = criteria_options[:klass]
 135        obj = k.find_by_id(value.to_i)
 136        if obj.is_a?(Issue)
 137          obj.visible? ? "#{obj.tracker} ##{obj.id}: #{obj.subject}" : "##{obj.id}"
 138        else
 139          obj
 140        end
 141      else
 142        format_value(value, criteria_options[:format])
 143      end
 144    end
 145 
 146    def report_to_csv(report)
 147      decimal_separator = l(:general_csv_decimal_separator)
 148      export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
 149        # Column headers
 150        headers = report.criteria.collect {|criteria| l(report.available_criteria[criteria][:label]) }
 151        headers += report.periods
 152        headers << l(:label_total)
 153        csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8(
 154                                      c.to_s,
 155                                      l(:general_csv_encoding) ) }
 156        # Content
 157        report_criteria_to_csv(csv, report.available_criteria, report.columns, report.criteria, report.periods, report.hours)
 158        # Total row
 159        str_total = Redmine::CodesetUtil.from_utf8(l(:label_total), l(:general_csv_encoding))
 160        row = [ str_total ] + [''] * (report.criteria.size - 1)
 161        total = 0
 162        report.periods.each do |period|
 163          sum = sum_hours(select_hours(report.hours, report.columns, period.to_s))
 164          total += sum
 165          row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '')
 166        end
 167        row << ("%.2f" % total).gsub('.',decimal_separator)
 168        csv << row
 169      end
 170      export
 171    end
 172 
 173    def report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours, level=0)
 174      decimal_separator = l(:general_csv_decimal_separator)
 175      hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value|
 176        hours_for_value = select_hours(hours, criteria[level], value)
 177        next if hours_for_value.empty?
 178        row = [''] * level
 179        row << Redmine::CodesetUtil.from_utf8(
 180                          format_criteria_value(available_criteria[criteria[level]], value).to_s,
 181                          l(:general_csv_encoding) )
 182        row += [''] * (criteria.length - level - 1)
 183        total = 0
 184        periods.each do |period|
 185          sum = sum_hours(select_hours(hours_for_value, columns, period.to_s))
 186          total += sum
 187          row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '')
 188        end
 189        row << ("%.2f" % total).gsub('.',decimal_separator)
 190        csv << row
 191        if criteria.length > level + 1
 192          report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours_for_value, level + 1)
 193        end
 194      end
 195    end
 196  end