Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

timelog_helper.rb 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. activities = TimeEntryActivity.available_activities(project)
  27. collection = []
  28. if time_entry && time_entry.activity && !time_entry.activity.active?
  29. collection << ["--- #{l(:actionview_instancetag_blank_option)} ---", '']
  30. else
  31. unless activities.detect(&:is_default)
  32. collection << ["--- #{l(:actionview_instancetag_blank_option)} ---", '']
  33. end
  34. end
  35. activities.each {|a| collection << [a.name, a.id]}
  36. collection
  37. end
  38. def user_collection_for_select_options(time_entry)
  39. collection = time_entry.assignable_users
  40. if time_entry.user && !collection.include?(time_entry.user)
  41. collection << time_entry.user
  42. end
  43. principals_options_for_select(collection, time_entry.user_id.to_s)
  44. end
  45. def default_activity(time_entry)
  46. if @project
  47. time_entry.activity_id
  48. else
  49. TimeEntryActivity.default_activity_id(User.current, time_entry.project)
  50. end
  51. end
  52. def select_hours(data, criteria, value)
  53. if value.to_s.empty?
  54. data.select {|row| row[criteria].blank?}
  55. else
  56. data.select {|row| row[criteria].to_s == value.to_s}
  57. end
  58. end
  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. def format_criteria_value(criteria_options, value, html=true)
  67. if value.blank?
  68. "[#{l(:label_none)}]"
  69. elsif k = criteria_options[:klass]
  70. obj = k.find_by_id(value.to_i)
  71. if obj.is_a?(Issue)
  72. if obj.visible?
  73. html ? link_to_issue(obj) : "#{obj.tracker} ##{obj.id}: #{obj.subject}"
  74. else
  75. "##{obj.id}"
  76. end
  77. else
  78. format_object(obj, html)
  79. end
  80. elsif cf = criteria_options[:custom_field]
  81. format_value(value, cf)
  82. else
  83. value.to_s
  84. end
  85. end
  86. def report_to_csv(report)
  87. Redmine::Export::CSV.generate(:encoding => params[:encoding]) do |csv|
  88. # Column headers
  89. headers =
  90. report.criteria.collect do |criteria|
  91. l_or_humanize(report.available_criteria[criteria][:label])
  92. end
  93. headers += report.periods
  94. headers << l(:label_total_time)
  95. csv << headers
  96. # Content
  97. report_criteria_to_csv(csv, report.available_criteria, report.columns,
  98. report.criteria, report.periods, report.hours)
  99. # Total row
  100. str_total = l(:label_total_time)
  101. row = [str_total] + [''] * (report.criteria.size - 1)
  102. total = 0
  103. report.periods.each do |period|
  104. sum = sum_hours(select_hours(report.hours, report.columns, period.to_s))
  105. total += sum
  106. row << (sum > 0 ? sum : '')
  107. end
  108. row << total
  109. csv << row
  110. end
  111. end
  112. def report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours, level=0)
  113. hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value|
  114. hours_for_value = select_hours(hours, criteria[level], value)
  115. next if hours_for_value.empty?
  116. row = [''] * level
  117. row << format_criteria_value(available_criteria[criteria[level]], value, false).to_s
  118. row += [''] * (criteria.length - level - 1)
  119. total = 0
  120. periods.each do |period|
  121. sum = sum_hours(select_hours(hours_for_value, columns, period.to_s))
  122. total += sum
  123. row << (sum > 0 ? sum : '')
  124. end
  125. row << total
  126. csv << row
  127. if criteria.length > level + 1
  128. report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours_for_value, level + 1)
  129. end
  130. end
  131. end
  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. end