"Fossies" - the Fresh Open Source Software Archive

Member "redmine-4.1.1/app/helpers/custom_fields_helper.rb" (6 Apr 2020, 8025 Bytes) of package /linux/www/redmine-4.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "custom_fields_helper.rb": 4.1.0_vs_4.1.1.

    1 # frozen_string_literal: true
    2 
    3 # Redmine - project management software
    4 # Copyright (C) 2006-2019  Jean-Philippe Lang
    5 #
    6 # This program is free software; you can redistribute it and/or
    7 # modify it under the terms of the GNU General Public License
    8 # as published by the Free Software Foundation; either version 2
    9 # of the License, or (at your option) any later version.
   10 #
   11 # This program is distributed in the hope that it will be useful,
   12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 # GNU General Public License for more details.
   15 #
   16 # You should have received a copy of the GNU General Public License
   17 # along with this program; if not, write to the Free Software
   18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   19 
   20 module CustomFieldsHelper
   21 
   22   CUSTOM_FIELDS_TABS = [
   23     {:name => 'IssueCustomField', :partial => 'custom_fields/index',
   24      :label => :label_issue_plural},
   25     {:name => 'TimeEntryCustomField', :partial => 'custom_fields/index',
   26      :label => :label_spent_time},
   27     {:name => 'ProjectCustomField', :partial => 'custom_fields/index',
   28      :label => :label_project_plural},
   29     {:name => 'VersionCustomField', :partial => 'custom_fields/index',
   30      :label => :label_version_plural},
   31     {:name => 'DocumentCustomField', :partial => 'custom_fields/index',
   32      :label => :label_document_plural},
   33     {:name => 'UserCustomField', :partial => 'custom_fields/index',
   34      :label => :label_user_plural},
   35     {:name => 'GroupCustomField', :partial => 'custom_fields/index',
   36      :label => :label_group_plural},
   37     {:name => 'TimeEntryActivityCustomField', :partial => 'custom_fields/index',
   38      :label => TimeEntryActivity::OptionName},
   39     {:name => 'IssuePriorityCustomField', :partial => 'custom_fields/index',
   40      :label => IssuePriority::OptionName},
   41     {:name => 'DocumentCategoryCustomField', :partial => 'custom_fields/index',
   42      :label => DocumentCategory::OptionName}
   43   ]
   44 
   45   def render_custom_fields_tabs(types)
   46     tabs = CUSTOM_FIELDS_TABS.select {|h| types.include?(h[:name]) }
   47     render_tabs tabs
   48   end
   49 
   50   def custom_field_type_options
   51     CUSTOM_FIELDS_TABS.map {|h| [l(h[:label]), h[:name]]}
   52   end
   53 
   54   def custom_field_title(custom_field)
   55     items = []
   56     items << [l(:label_custom_field_plural), custom_fields_path]
   57     items << [l(custom_field.type_name), custom_fields_path(:tab => custom_field.class.name)] if custom_field
   58     items << (custom_field.nil? || custom_field.new_record? ? l(:label_custom_field_new) : custom_field.name)
   59 
   60     title(*items)
   61   end
   62 
   63   def render_custom_field_format_partial(form, custom_field)
   64     partial = custom_field.format.form_partial
   65     if partial
   66       render :partial => custom_field.format.form_partial, :locals => {:f => form, :custom_field => custom_field}
   67     end
   68   end
   69 
   70   def custom_field_tag_name(prefix, custom_field)
   71     name = "#{prefix}[custom_field_values][#{custom_field.id}]"
   72     name += "[]" if custom_field.multiple?
   73     name
   74   end
   75 
   76   def custom_field_tag_id(prefix, custom_field)
   77     "#{prefix}_custom_field_values_#{custom_field.id}"
   78   end
   79 
   80   # Return custom field html tag corresponding to its format
   81   def custom_field_tag(prefix, custom_value)
   82     css = "#{custom_value.custom_field.field_format}_cf"
   83     data = nil
   84     if custom_value.custom_field.full_text_formatting?
   85       css += ' wiki-edit'
   86       data = {
   87         :auto_complete => true,
   88         :issues_url => auto_complete_issues_path(:project_id => custom_value.customized.project, :q => '')
   89       } if custom_value.customized&.try(:project)
   90     end
   91     custom_value.custom_field.format.edit_tag(
   92       self,
   93       custom_field_tag_id(prefix, custom_value.custom_field),
   94       custom_field_tag_name(prefix, custom_value.custom_field),
   95       custom_value,
   96       :class => css,
   97       :data => data)
   98   end
   99 
  100   # Return custom field name tag
  101   def custom_field_name_tag(custom_field)
  102     title = custom_field.description.presence
  103     css = title ? "field-description" : nil
  104     content_tag 'span', custom_field.name, :title => title, :class => css
  105   end
  106 
  107   # Return custom field label tag
  108   def custom_field_label_tag(name, custom_value, options={})
  109     required = options[:required] || custom_value.custom_field.is_required?
  110     for_tag_id = options.fetch(:for_tag_id, "#{name}_custom_field_values_#{custom_value.custom_field.id}")
  111     content = custom_field_name_tag custom_value.custom_field
  112     content_tag(
  113       "label", content +
  114       (required ? " <span class=\"required\">*</span>".html_safe : ""),
  115       :for => for_tag_id)
  116   end
  117 
  118   # Return custom field tag with its label tag
  119   def custom_field_tag_with_label(name, custom_value, options={})
  120     tag = custom_field_tag(name, custom_value)
  121     tag_id = nil
  122     ids = tag.scan(/ id="(.+?)"/)
  123     if ids.size == 1
  124       tag_id = ids.first.first
  125     end
  126     custom_field_label_tag(name, custom_value, options.merge(:for_tag_id => tag_id)) + tag
  127   end
  128 
  129   # Returns the custom field tag for when bulk editing objects
  130   def custom_field_tag_for_bulk_edit(prefix, custom_field, objects=nil, value='')
  131     custom_field.format.bulk_edit_tag(
  132       self,
  133       custom_field_tag_id(prefix, custom_field),
  134       custom_field_tag_name(prefix, custom_field),
  135       custom_field,
  136       objects,
  137       value,
  138       :class => "#{custom_field.field_format}_cf")
  139   end
  140 
  141   # Returns custom field value tag
  142   def custom_field_value_tag(value)
  143     attr_value = show_value(value)
  144 
  145     if !attr_value.blank? && value.custom_field.full_text_formatting?
  146       content_tag('div', attr_value, :class => 'wiki')
  147     else
  148       attr_value
  149     end
  150   end
  151 
  152   # Return a string used to display a custom value
  153   def show_value(custom_value, html=true)
  154     format_object(custom_value, html)
  155   end
  156 
  157   # Return a string used to display a custom value
  158   def format_value(value, custom_field)
  159     format_object(custom_field.format.formatted_value(self, custom_field, value, false), false)
  160   end
  161 
  162   # Return an array of custom field formats which can be used in select_tag
  163   def custom_field_formats_for_select(custom_field)
  164     Redmine::FieldFormat.as_select(custom_field.class.customized_class.name)
  165   end
  166 
  167   # Yields the given block for each custom field value of object that should be
  168   # displayed, with the custom field and the formatted value as arguments
  169   def render_custom_field_values(object, &block)
  170     object.visible_custom_field_values.each do |custom_value|
  171       formatted = show_value(custom_value)
  172       if formatted.present?
  173         yield custom_value.custom_field, formatted
  174       end
  175     end
  176   end
  177 
  178   # Renders the custom_values in api views
  179   def render_api_custom_values(custom_values, api)
  180     api.array :custom_fields do
  181       custom_values.each do |custom_value|
  182         attrs = {:id => custom_value.custom_field_id, :name => custom_value.custom_field.name}
  183         attrs.merge!(:multiple => true) if custom_value.custom_field.multiple?
  184         api.custom_field attrs do
  185           if custom_value.value.is_a?(Array)
  186             api.array :value do
  187               custom_value.value.each do |value|
  188                 api.value value unless value.blank?
  189               end
  190             end
  191           else
  192             api.value custom_value.value
  193           end
  194         end
  195       end
  196     end unless custom_values.empty?
  197   end
  198 
  199   def edit_tag_style_tag(form, options={})
  200     select_options = [[l(:label_drop_down_list), ''], [l(:label_checkboxes), 'check_box']]
  201     if options[:include_radio]
  202       select_options << [l(:label_radio_buttons), 'radio']
  203     end
  204     form.select :edit_tag_style, select_options, :label => :label_display
  205   end
  206 
  207   def select_type_radio_buttons(default_type)
  208     if CUSTOM_FIELDS_TABS.none? {|tab| tab[:name] == default_type}
  209       default_type = 'IssueCustomField'
  210     end
  211     custom_field_type_options.map do |name, type|
  212       content_tag(:label, :style => 'display:block;') do
  213         radio_button_tag('type', type, type == default_type) + name
  214       end
  215     end.join("\n").html_safe
  216   end
  217 end