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.

utils.rb 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 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. require 'fileutils'
  19. module Redmine
  20. module Utils
  21. class << self
  22. # Returns the relative root url of the application
  23. def relative_url_root
  24. if ActionController::Base.respond_to?('relative_url_root')
  25. ActionController::Base.relative_url_root.to_s
  26. else
  27. ActionController::Base.config.relative_url_root.to_s
  28. end
  29. end
  30. # Sets the relative root url of the application
  31. def relative_url_root=(arg)
  32. if ActionController::Base.respond_to?('relative_url_root=')
  33. ActionController::Base.relative_url_root=arg
  34. else
  35. ActionController::Base.config.relative_url_root = arg
  36. end
  37. end
  38. # Generates a n bytes random hex string
  39. # Example:
  40. # random_hex(4) # => "89b8c729"
  41. def random_hex(n)
  42. SecureRandom.hex(n)
  43. end
  44. def save_upload(upload, path)
  45. directory = File.dirname(path)
  46. unless File.exists?(directory)
  47. FileUtils.mkdir_p directory
  48. end
  49. File.open(path, "wb") do |f|
  50. if upload.respond_to?(:read)
  51. buffer = ""
  52. while (buffer = upload.read(8192))
  53. f.write(buffer)
  54. yield buffer if block_given?
  55. end
  56. else
  57. f.write(upload)
  58. yield upload if block_given?
  59. end
  60. end
  61. end
  62. end
  63. module Shell
  64. module_function
  65. def shell_quote(str)
  66. if Redmine::Platform.mswin?
  67. '"' + str.gsub(/"/, '\\"') + '"'
  68. else
  69. "'" + str.gsub(/'/, "'\"'\"'") + "'"
  70. end
  71. end
  72. def shell_quote_command(command)
  73. if Redmine::Platform.mswin? && RUBY_PLATFORM == 'java'
  74. command
  75. else
  76. shell_quote(command)
  77. end
  78. end
  79. end
  80. module DateCalculation
  81. # Returns the number of working days between from and to
  82. def working_days(from, to)
  83. days = (to - from).to_i
  84. if days > 0
  85. weeks = days / 7
  86. result = weeks * (7 - non_working_week_days.size)
  87. days_left = days - weeks * 7
  88. start_cwday = from.cwday
  89. days_left.times do |i|
  90. unless non_working_week_days.include?(((start_cwday + i - 1) % 7) + 1)
  91. result += 1
  92. end
  93. end
  94. result
  95. else
  96. 0
  97. end
  98. end
  99. # Adds working days to the given date
  100. def add_working_days(date, working_days)
  101. if working_days > 0
  102. weeks = working_days / (7 - non_working_week_days.size)
  103. result = weeks * 7
  104. days_left = working_days - weeks * (7 - non_working_week_days.size)
  105. cwday = date.cwday
  106. while days_left > 0
  107. cwday += 1
  108. unless non_working_week_days.include?(((cwday - 1) % 7) + 1)
  109. days_left -= 1
  110. end
  111. result += 1
  112. end
  113. next_working_date(date + result)
  114. else
  115. date
  116. end
  117. end
  118. # Returns the date of the first day on or after the given date that is a working day
  119. def next_working_date(date)
  120. cwday = date.cwday
  121. days = 0
  122. days += 1 while non_working_week_days.include?(((cwday + days - 1) % 7) + 1)
  123. date + days
  124. end
  125. # Returns the index of non working week days (1=monday, 7=sunday)
  126. def non_working_week_days
  127. @non_working_week_days ||= begin
  128. days = Setting.non_working_week_days
  129. if days.is_a?(Array) && days.size < 7
  130. days.map(&:to_i)
  131. else
  132. []
  133. end
  134. end
  135. end
  136. end
  137. end
  138. end