您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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