"Fossies" - the Fresh Open Source Software Archive

Member "redmine-4.1.1/app/helpers/timelog_helper.rb" (6 Apr 2020, 4685 Bytes) of package /linux/www/redmine-4.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "timelog_helper.rb": 4.1.0_vs_4.1.1.

    1 # frozen_string_literal: true
    2 
    3 # Redmine - project management software
    4 # Copyright (C) 2006-2019  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   # Returns a collection of activities for a select field.  time_entry
   24   # is optional and will be used to check if the selected TimeEntryActivity
   25   # is active.
   26   def activity_collection_for_select_options(time_entry=nil, project=nil)
   27     project ||= time_entry.try(:project)
   28     project ||= @project
   29     if project.nil?
   30       activities = TimeEntryActivity.shared.active
   31     else
   32       activities = project.activities
   33     end
   34 
   35     collection = []
   36     if time_entry && time_entry.activity && !time_entry.activity.active?
   37       collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ]
   38     else
   39       collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default)
   40     end
   41     activities.each { |a| collection << [a.name, a.id] }
   42     collection
   43   end
   44 
   45   def user_collection_for_select_options(time_entry)
   46     collection = time_entry.assignable_users
   47     collection << time_entry.user unless time_entry.user.nil? && !collection.include?(time_entry.user)
   48     principals_options_for_select(collection, time_entry.user_id.to_s)
   49   end
   50 
   51   def select_hours(data, criteria, value)
   52     if value.to_s.empty?
   53       data.select {|row| row[criteria].blank? }
   54     else
   55       data.select {|row| row[criteria].to_s == value.to_s}
   56     end
   57   end
   58 
   59   def sum_hours(data)
   60     sum = 0
   61     data.each do |row|
   62       sum += row['hours'].to_f
   63     end
   64     sum
   65   end
   66 
   67   def format_criteria_value(criteria_options, value, html=true)
   68     if value.blank?
   69       "[#{l(:label_none)}]"
   70     elsif k = criteria_options[:klass]
   71       obj = k.find_by_id(value.to_i)
   72       if obj.is_a?(Issue)
   73         if obj.visible?
   74           html ? link_to_issue(obj) : "#{obj.tracker} ##{obj.id}: #{obj.subject}"
   75         else
   76           "##{obj.id}"
   77         end
   78       else
   79         format_object(obj, html)
   80       end
   81     elsif cf = criteria_options[:custom_field]
   82       format_value(value, cf)
   83     else
   84       value.to_s
   85     end
   86   end
   87 
   88   def report_to_csv(report)
   89     Redmine::Export::CSV.generate(:encoding => params[:encoding]) do |csv|
   90       # Column headers
   91       headers = report.criteria.collect {|criteria| l_or_humanize(report.available_criteria[criteria][:label]) }
   92       headers += report.periods
   93       headers << l(:label_total_time)
   94       csv << headers
   95       # Content
   96       report_criteria_to_csv(csv, report.available_criteria, report.columns, report.criteria, report.periods, report.hours)
   97       # Total row
   98       str_total = l(:label_total_time)
   99       row = [ str_total ] + [''] * (report.criteria.size - 1)
  100       total = 0
  101       report.periods.each do |period|
  102         sum = sum_hours(select_hours(report.hours, report.columns, period.to_s))
  103         total += sum
  104         row << (sum > 0 ? sum : '')
  105       end
  106       row << total
  107       csv << row
  108     end
  109   end
  110 
  111   def report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours, level=0)
  112     hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value|
  113       hours_for_value = select_hours(hours, criteria[level], value)
  114       next if hours_for_value.empty?
  115       row = [''] * level
  116       row << format_criteria_value(available_criteria[criteria[level]], value, false).to_s
  117       row += [''] * (criteria.length - level - 1)
  118       total = 0
  119       periods.each do |period|
  120         sum = sum_hours(select_hours(hours_for_value, columns, period.to_s))
  121         total += sum
  122         row << (sum > 0 ? sum : '')
  123       end
  124       row << total
  125       csv << row
  126       if criteria.length > level + 1
  127         report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours_for_value, level + 1)
  128       end
  129     end
  130   end
  131 
  132   def cancel_button_tag_for_time_entry(project)
  133     fallback_path = project ? project_time_entries_path(project) : time_entries_path
  134     cancel_button_tag(fallback_path)
  135   end
  136 
  137 end