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.

custom_fields_helper.rb 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2020 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 CustomFieldsHelper
  19. CUSTOM_FIELDS_TABS = [
  20. {:name => 'IssueCustomField', :partial => 'custom_fields/index',
  21. :label => :label_issue_plural},
  22. {:name => 'TimeEntryCustomField', :partial => 'custom_fields/index',
  23. :label => :label_spent_time},
  24. {:name => 'ProjectCustomField', :partial => 'custom_fields/index',
  25. :label => :label_project_plural},
  26. {:name => 'VersionCustomField', :partial => 'custom_fields/index',
  27. :label => :label_version_plural},
  28. {:name => 'DocumentCustomField', :partial => 'custom_fields/index',
  29. :label => :label_document_plural},
  30. {:name => 'UserCustomField', :partial => 'custom_fields/index',
  31. :label => :label_user_plural},
  32. {:name => 'GroupCustomField', :partial => 'custom_fields/index',
  33. :label => :label_group_plural},
  34. {:name => 'TimeEntryActivityCustomField', :partial => 'custom_fields/index',
  35. :label => TimeEntryActivity::OptionName},
  36. {:name => 'IssuePriorityCustomField', :partial => 'custom_fields/index',
  37. :label => IssuePriority::OptionName},
  38. {:name => 'DocumentCategoryCustomField', :partial => 'custom_fields/index',
  39. :label => DocumentCategory::OptionName}
  40. ]
  41. def render_custom_fields_tabs(types)
  42. tabs = CUSTOM_FIELDS_TABS.select {|h| types.include?(h[:name])}
  43. render_tabs tabs
  44. end
  45. def custom_field_type_options
  46. CUSTOM_FIELDS_TABS.map {|h| [l(h[:label]), h[:name]]}
  47. end
  48. def custom_field_title(custom_field)
  49. items = []
  50. items << [l(:label_custom_field_plural), custom_fields_path]
  51. items << [l(custom_field.type_name), custom_fields_path(:tab => custom_field.class.name)] if custom_field
  52. items << (custom_field.nil? || custom_field.new_record? ? l(:label_custom_field_new) : custom_field.name)
  53. title(*items)
  54. end
  55. def render_custom_field_format_partial(form, custom_field)
  56. partial = custom_field.format.form_partial
  57. if partial
  58. render :partial => custom_field.format.form_partial, :locals => {:f => form, :custom_field => custom_field}
  59. end
  60. end
  61. def custom_field_tag_name(prefix, custom_field)
  62. name = "#{prefix}[custom_field_values][#{custom_field.id}]"
  63. name += "[]" if custom_field.multiple?
  64. name
  65. end
  66. def custom_field_tag_id(prefix, custom_field)
  67. "#{prefix}_custom_field_values_#{custom_field.id}"
  68. end
  69. # Return custom field html tag corresponding to its format
  70. def custom_field_tag(prefix, custom_value)
  71. css = custom_value.custom_field.css_classes
  72. data = nil
  73. if custom_value.custom_field.full_text_formatting?
  74. css += ' wiki-edit'
  75. data = {
  76. :auto_complete => true
  77. }
  78. end
  79. custom_value.custom_field.format.edit_tag(
  80. self,
  81. custom_field_tag_id(prefix, custom_value.custom_field),
  82. custom_field_tag_name(prefix, custom_value.custom_field),
  83. custom_value,
  84. :class => css,
  85. :data => data)
  86. end
  87. # Return custom field name tag
  88. def custom_field_name_tag(custom_field)
  89. title = custom_field.description.presence
  90. css = title ? "field-description" : nil
  91. content_tag 'span', custom_field.name, :title => title, :class => css
  92. end
  93. # Return custom field label tag
  94. def custom_field_label_tag(name, custom_value, options={})
  95. required = options[:required] || custom_value.custom_field.is_required?
  96. for_tag_id = options.fetch(:for_tag_id, "#{name}_custom_field_values_#{custom_value.custom_field.id}")
  97. content = custom_field_name_tag custom_value.custom_field
  98. content_tag(
  99. "label", content +
  100. (required ? " <span class=\"required\">*</span>".html_safe : ""),
  101. :for => for_tag_id)
  102. end
  103. # Return custom field tag with its label tag
  104. def custom_field_tag_with_label(name, custom_value, options={})
  105. tag = custom_field_tag(name, custom_value)
  106. tag_id = nil
  107. ids = tag.scan(/ id="(.+?)"/)
  108. if ids.size == 1
  109. tag_id = ids.first.first
  110. end
  111. custom_field_label_tag(name, custom_value, options.merge(:for_tag_id => tag_id)) + tag
  112. end
  113. # Returns the custom field tag for when bulk editing objects
  114. def custom_field_tag_for_bulk_edit(prefix, custom_field, objects=nil, value='')
  115. css = custom_field.css_classes
  116. data = nil
  117. if custom_field.full_text_formatting?
  118. css += ' wiki-edit'
  119. data = {
  120. :auto_complete => true
  121. }
  122. end
  123. custom_field.format.bulk_edit_tag(
  124. self,
  125. custom_field_tag_id(prefix, custom_field),
  126. custom_field_tag_name(prefix, custom_field),
  127. custom_field,
  128. objects,
  129. value,
  130. :class => css,
  131. :data => data)
  132. end
  133. # Returns custom field value tag
  134. def custom_field_value_tag(value)
  135. attr_value = show_value(value)
  136. if !attr_value.blank? && value.custom_field.full_text_formatting?
  137. content_tag('div', attr_value, :class => 'wiki')
  138. else
  139. attr_value
  140. end
  141. end
  142. # Return a string used to display a custom value
  143. def show_value(custom_value, html=true)
  144. format_object(custom_value, html)
  145. end
  146. # Return a string used to display a custom value
  147. def format_value(value, custom_field)
  148. format_object(custom_field.format.formatted_value(self, custom_field, value, false), false)
  149. end
  150. # Return an array of custom field formats which can be used in select_tag
  151. def custom_field_formats_for_select(custom_field)
  152. Redmine::FieldFormat.as_select(custom_field.class.customized_class.name)
  153. end
  154. # Yields the given block for each custom field value of object that should be
  155. # displayed, with the custom field and the formatted value as arguments
  156. def render_custom_field_values(object, &block)
  157. object.visible_custom_field_values.each do |custom_value|
  158. formatted = show_value(custom_value)
  159. if formatted.present?
  160. yield custom_value.custom_field, formatted
  161. end
  162. end
  163. end
  164. # Renders the custom_values in api views
  165. def render_api_custom_values(custom_values, api)
  166. api.array :custom_fields do
  167. custom_values.each do |custom_value|
  168. attrs = {:id => custom_value.custom_field_id, :name => custom_value.custom_field.name}
  169. attrs[:multiple] = true if custom_value.custom_field.multiple?
  170. api.custom_field attrs do
  171. if custom_value.value.is_a?(Array)
  172. api.array :value do
  173. custom_value.value.each do |value|
  174. api.value value unless value.blank?
  175. end
  176. end
  177. else
  178. api.value custom_value.value
  179. end
  180. end
  181. end
  182. end unless custom_values.empty?
  183. end
  184. def edit_tag_style_tag(form, options={})
  185. select_options = [[l(:label_drop_down_list), ''], [l(:label_checkboxes), 'check_box']]
  186. if options[:include_radio]
  187. select_options << [l(:label_radio_buttons), 'radio']
  188. end
  189. form.select :edit_tag_style, select_options, :label => :label_display
  190. end
  191. def select_type_radio_buttons(default_type)
  192. if CUSTOM_FIELDS_TABS.none? {|tab| tab[:name] == default_type}
  193. default_type = 'IssueCustomField'
  194. end
  195. custom_field_type_options.map do |name, type|
  196. content_tag(:label, :style => 'display:block;') do
  197. radio_button_tag('type', type, type == default_type) + name
  198. end
  199. end.join("\n").html_safe
  200. end
  201. end