custom_fields_helper.rb 6.83 KB
Newer Older
1 2
# encoding: utf-8
#
3
# Redmine - project management software
jplang's avatar
jplang committed
4
# Copyright (C) 2006-2017  Jean-Philippe Lang
5 6 7 8 9
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
10
#
11 12 13 14
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
15
#
16 17 18 19 20 21
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

module CustomFieldsHelper

22 23 24 25 26 27 28 29 30
  CUSTOM_FIELDS_TABS = [
    {:name => 'IssueCustomField', :partial => 'custom_fields/index',
     :label => :label_issue_plural},
    {:name => 'TimeEntryCustomField', :partial => 'custom_fields/index',
     :label => :label_spent_time},
    {:name => 'ProjectCustomField', :partial => 'custom_fields/index',
     :label => :label_project_plural},
    {:name => 'VersionCustomField', :partial => 'custom_fields/index',
     :label => :label_version_plural},
31 32
    {:name => 'DocumentCustomField', :partial => 'custom_fields/index',
     :label => :label_document_plural},
33 34 35 36 37 38 39 40 41 42 43 44
    {:name => 'UserCustomField', :partial => 'custom_fields/index',
     :label => :label_user_plural},
    {:name => 'GroupCustomField', :partial => 'custom_fields/index',
     :label => :label_group_plural},
    {:name => 'TimeEntryActivityCustomField', :partial => 'custom_fields/index',
     :label => TimeEntryActivity::OptionName},
    {:name => 'IssuePriorityCustomField', :partial => 'custom_fields/index',
     :label => IssuePriority::OptionName},
    {:name => 'DocumentCategoryCustomField', :partial => 'custom_fields/index',
     :label => DocumentCategory::OptionName}
  ]

45 46 47 48 49 50 51
  def render_custom_fields_tabs(types)
    tabs = CUSTOM_FIELDS_TABS.select {|h| types.include?(h[:name]) }
    render_tabs tabs
  end

  def custom_field_type_options
    CUSTOM_FIELDS_TABS.map {|h| [l(h[:label]), h[:name]]}
52
  end
53

54 55 56 57 58 59 60 61 62
  def custom_field_title(custom_field)
    items = []
    items << [l(:label_custom_field_plural), custom_fields_path]
    items << [l(custom_field.type_name), custom_fields_path(:tab => custom_field.class.name)] if custom_field
    items << (custom_field.nil? || custom_field.new_record? ? l(:label_custom_field_new) : custom_field.name) 

    title(*items)
  end

63 64 65 66
  def render_custom_field_format_partial(form, custom_field)
    partial = custom_field.format.form_partial
    if partial
      render :partial => custom_field.format.form_partial, :locals => {:f => form, :custom_field => custom_field}
67 68
    end
  end
69

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  def custom_field_tag_name(prefix, custom_field)
    name = "#{prefix}[custom_field_values][#{custom_field.id}]"
    name << "[]" if custom_field.multiple?
    name
  end

  def custom_field_tag_id(prefix, custom_field)
    "#{prefix}_custom_field_values_#{custom_field.id}"
  end

  # Return custom field html tag corresponding to its format
  def custom_field_tag(prefix, custom_value)
    custom_value.custom_field.format.edit_tag self,
      custom_field_tag_id(prefix, custom_value.custom_field),
      custom_field_tag_name(prefix, custom_value.custom_field),
      custom_value,
      :class => "#{custom_value.custom_field.field_format}_cf"
  end

89 90 91
  # Return custom field name tag
  def custom_field_name_tag(custom_field)
    title = custom_field.description.presence
92 93
    css = title ? "field-description" : nil
    content_tag 'span', custom_field.name, :title => title, :class => css
94 95
  end
  
96
  # Return custom field label tag
97 98
  def custom_field_label_tag(name, custom_value, options={})
    required = options[:required] || custom_value.custom_field.is_required?
99
    for_tag_id = options.fetch(:for_tag_id, "#{name}_custom_field_values_#{custom_value.custom_field.id}")
100
    content = custom_field_name_tag custom_value.custom_field
101

102
    content_tag "label", content +
103
      (required ? " <span class=\"required\">*</span>".html_safe : ""),
104
      :for => for_tag_id
105
  end
106

107
  # Return custom field tag with its label tag
108
  def custom_field_tag_with_label(name, custom_value, options={})
109 110 111 112 113 114 115
    tag = custom_field_tag(name, custom_value)
    tag_id = nil
    ids = tag.scan(/ id="(.+?)"/)
    if ids.size == 1
      tag_id = ids.first.first
    end
    custom_field_label_tag(name, custom_value, options.merge(:for_tag_id => tag_id)) + tag
116
  end
117

118 119 120 121 122 123 124 125 126
  # Returns the custom field tag for when bulk editing objects
  def custom_field_tag_for_bulk_edit(prefix, custom_field, objects=nil, value='')
    custom_field.format.bulk_edit_tag self,
      custom_field_tag_id(prefix, custom_field),
      custom_field_tag_name(prefix, custom_field),
      custom_field,
      objects,
      value,
      :class => "#{custom_field.field_format}_cf"
127
  end
128 129

  # Return a string used to display a custom value
130 131
  def show_value(custom_value, html=true)
    format_object(custom_value, html)
132
  end
133

134
  # Return a string used to display a custom value
135 136
  def format_value(value, custom_field)
    format_object(custom_field.format.formatted_value(self, custom_field, value, false), false)
137 138 139
  end

  # Return an array of custom field formats which can be used in select_tag
140
  def custom_field_formats_for_select(custom_field)
141
    Redmine::FieldFormat.as_select(custom_field.class.customized_class.name)
jplang's avatar
jplang committed
142
  end
143

144 145 146 147 148 149 150 151 152 153 154
  # Yields the given block for each custom field value of object that should be
  # displayed, with the custom field and the formatted value as arguments
  def render_custom_field_values(object, &block)
    object.visible_custom_field_values.each do |custom_value|
      formatted = show_value(custom_value)
      if formatted.present?
        yield custom_value.custom_field, formatted
      end
    end
  end

155 156 157 158
  # Renders the custom_values in api views
  def render_api_custom_values(custom_values, api)
    api.array :custom_fields do
      custom_values.each do |custom_value|
159 160 161 162 163 164 165 166 167 168 169 170
        attrs = {:id => custom_value.custom_field_id, :name => custom_value.custom_field.name}
        attrs.merge!(:multiple => true) if custom_value.custom_field.multiple?
        api.custom_field attrs do
          if custom_value.value.is_a?(Array)
            api.array :value do
              custom_value.value.each do |value|
                api.value value unless value.blank?
              end
            end
          else
            api.value custom_value.value
          end
171 172 173 174
        end
      end
    end unless custom_values.empty?
  end
175

176 177 178 179 180 181
  def edit_tag_style_tag(form, options={})
    select_options = [[l(:label_drop_down_list), ''], [l(:label_checkboxes), 'check_box']]
    if options[:include_radio]
      select_options << [l(:label_radio_buttons), 'radio']
    end
    form.select :edit_tag_style, select_options, :label => :label_display
182
  end
jplang's avatar
jplang committed
183
end