You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timelog_helper.rb 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module TimelogHelper
  19. include ApplicationHelper
  20. # Returns a collection of activities for a select field. time_entry
  21. # is optional and will be used to check if the selected TimeEntryActivity
  22. # is active.
  23. def activity_collection_for_select_options(time_entry=nil, project=nil)
  24. project ||= time_entry.try(:project)
  25. project ||= @project
  26. if project.nil?
  27. activities = TimeEntryActivity.shared.active
  28. else
  29. activities = project.activities
  30. end
  31. collection = []
  32. if time_entry && time_entry.activity && !time_entry.activity.active?
  33. collection << ["--- #{l(:actionview_instancetag_blank_option)} ---", '']
  34. else
  35. unless activities.detect(&:is_default)
  36. collection << ["--- #{l(:actionview_instancetag_blank_option)} ---", '']
  37. end
  38. end
  39. activities.each {|a| collection << [a.name, a.id]}
  40. collection
  41. end
  42. def user_collection_for_select_options(time_entry)
  43. collection = time_entry.assignable_users
  44. if time_entry.user && !collection.include?(time_entry.user)
  45. collection << time_entry.user
  46. end
  47. principals_options_for_select(collection, time_entry.user_id.to_s)
  48. end
  49. def select_hours(data, criteria, value)
  50. if value.to_s.empty?
  51. data.select {|row| row[criteria].blank?}
  52. else
  53. data.select {|row| row[criteria].to_s == value.to_s}
  54. end
  55. end
  56. def sum_hours(data)
  57. sum = 0
  58. data.each do |row|
  59. sum += row['hours'].to_f
  60. end
  61. sum
  62. end
  63. def format_criteria_value(criteria_options, value, html=true)
  64. if value.blank?
  65. "[#{l(:label_none)}]"
  66. elsif k = criteria_options[:klass]
  67. obj = k.find_by_id(value.to_i)
  68. if obj.is_a?(Issue)
  69. if obj.visible?
  70. html ? link_to_issue(obj) : "#{obj.tracker} ##{obj.id}: #{obj.subject}"
  71. else
  72. "##{obj.id}"
  73. end
  74. else
  75. format_object(obj, html)
  76. end
  77. elsif cf = criteria_options[:custom_field]
  78. format_value(value, cf)
  79. else
  80. value.to_s
  81. end
  82. end
  83. def report_to_csv(report)
  84. Redmine::Export::CSV.generate(:encoding => params[:encoding]) do |csv|
  85. # Column headers
  86. headers =
  87. report.criteria.collect do |criteria|
  88. l_or_humanize(report.available_criteria[criteria][:label])
  89. end
  90. headers += report.periods
  91. headers << l(:label_total_time)
  92. csv << headers
  93. # Content
  94. report_criteria_to_csv(csv, report.available_criteria, report.columns,
  95. report.criteria, report.periods, report.hours)
  96. # Total row
  97. str_total = l(:label_total_time)
  98. row = [str_total] + [''] * (report.criteria.size - 1)
  99. total = 0
  100. report.periods.each do |period|
  101. sum = sum_hours(select_hours(report.hours, report.columns, period.to_s))
  102. total += sum
  103. row << (sum > 0 ? sum : '')
  104. end
  105. row << total
  106. csv << row
  107. end
  108. end
  109. def report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours, level=0)
  110. hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value|
  111. hours_for_value = select_hours(hours, criteria[level], value)
  112. next if hours_for_value.empty?
  113. row = [''] * level
  114. row << format_criteria_value(available_criteria[criteria[level]], value, false).to_s
  115. row += [''] * (criteria.length - level - 1)
  116. total = 0
  117. periods.each do |period|
  118. sum = sum_hours(select_hours(hours_for_value, columns, period.to_s))
  119. total += sum
  120. row << (sum > 0 ? sum : '')
  121. end
  122. row << total
  123. csv << row
  124. if criteria.length > level + 1
  125. report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours_for_value, level + 1)
  126. end
  127. end
  128. end
  129. def cancel_button_tag_for_time_entry(project)
  130. fallback_path = project ? project_time_entries_path(project) : time_entries_path
  131. cancel_button_tag(fallback_path)
  132. end
  133. end