annotate app/helpers/custom_fields_helper.rb @ 1516:b450a9d58aed redmine-2.4

Update to Redmine SVN revision 13356 on 2.4-stable branch
author Chris Cannam
date Tue, 09 Sep 2014 09:28:31 +0100
parents e248c7af89ec
children dffacf8a6908
rev   line source
Chris@909 1 # encoding: utf-8
Chris@909 2 #
Chris@441 3 # Redmine - project management software
Chris@1494 4 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@0 5 #
Chris@0 6 # This program is free software; you can redistribute it and/or
Chris@0 7 # modify it under the terms of the GNU General Public License
Chris@0 8 # as published by the Free Software Foundation; either version 2
Chris@0 9 # of the License, or (at your option) any later version.
Chris@909 10 #
Chris@0 11 # This program is distributed in the hope that it will be useful,
Chris@0 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 14 # GNU General Public License for more details.
Chris@909 15 #
Chris@0 16 # You should have received a copy of the GNU General Public License
Chris@0 17 # along with this program; if not, write to the Free Software
Chris@0 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 19
Chris@0 20 module CustomFieldsHelper
Chris@0 21
Chris@1464 22 CUSTOM_FIELDS_TABS = [
Chris@1464 23 {:name => 'IssueCustomField', :partial => 'custom_fields/index',
Chris@1464 24 :label => :label_issue_plural},
Chris@1464 25 {:name => 'TimeEntryCustomField', :partial => 'custom_fields/index',
Chris@1464 26 :label => :label_spent_time},
Chris@1464 27 {:name => 'ProjectCustomField', :partial => 'custom_fields/index',
Chris@1464 28 :label => :label_project_plural},
Chris@1464 29 {:name => 'VersionCustomField', :partial => 'custom_fields/index',
Chris@1464 30 :label => :label_version_plural},
Chris@1464 31 {:name => 'UserCustomField', :partial => 'custom_fields/index',
Chris@1464 32 :label => :label_user_plural},
Chris@1464 33 {:name => 'GroupCustomField', :partial => 'custom_fields/index',
Chris@1464 34 :label => :label_group_plural},
Chris@1464 35 {:name => 'TimeEntryActivityCustomField', :partial => 'custom_fields/index',
Chris@1464 36 :label => TimeEntryActivity::OptionName},
Chris@1464 37 {:name => 'IssuePriorityCustomField', :partial => 'custom_fields/index',
Chris@1464 38 :label => IssuePriority::OptionName},
Chris@1464 39 {:name => 'DocumentCategoryCustomField', :partial => 'custom_fields/index',
Chris@1464 40 :label => DocumentCategory::OptionName}
Chris@1464 41 ]
Chris@1464 42
Chris@0 43 def custom_fields_tabs
Chris@1464 44 CUSTOM_FIELDS_TABS
Chris@0 45 end
Chris@909 46
Chris@0 47 # Return custom field html tag corresponding to its format
Chris@1464 48 def custom_field_tag(name, custom_value)
Chris@0 49 custom_field = custom_value.custom_field
Chris@0 50 field_name = "#{name}[custom_field_values][#{custom_field.id}]"
Chris@1115 51 field_name << "[]" if custom_field.multiple?
Chris@0 52 field_id = "#{name}_custom_field_values_#{custom_field.id}"
Chris@0 53
Chris@1115 54 tag_options = {:id => field_id, :class => "#{custom_field.field_format}_cf"}
Chris@1115 55
Chris@0 56 field_format = Redmine::CustomFieldFormat.find_by_name(custom_field.field_format)
Chris@441 57 case field_format.try(:edit_as)
Chris@0 58 when "date"
Chris@1115 59 text_field_tag(field_name, custom_value.value, tag_options.merge(:size => 10)) +
Chris@0 60 calendar_for(field_id)
Chris@0 61 when "text"
Chris@1115 62 text_area_tag(field_name, custom_value.value, tag_options.merge(:rows => 3))
Chris@0 63 when "bool"
Chris@1115 64 hidden_field_tag(field_name, '0') + check_box_tag(field_name, '1', custom_value.true?, tag_options)
Chris@0 65 when "list"
Chris@1115 66 blank_option = ''.html_safe
Chris@1115 67 unless custom_field.multiple?
Chris@1115 68 if custom_field.is_required?
Chris@1115 69 unless custom_field.default_value.present?
Chris@1115 70 blank_option = content_tag('option', "--- #{l(:actionview_instancetag_blank_option)} ---", :value => '')
Chris@1115 71 end
Chris@1115 72 else
Chris@1115 73 blank_option = content_tag('option')
Chris@1115 74 end
Chris@1115 75 end
Chris@1115 76 s = select_tag(field_name, blank_option + options_for_select(custom_field.possible_values_options(custom_value.customized), custom_value.value),
Chris@1115 77 tag_options.merge(:multiple => custom_field.multiple?))
Chris@1115 78 if custom_field.multiple?
Chris@1115 79 s << hidden_field_tag(field_name, '')
Chris@1115 80 end
Chris@1115 81 s
Chris@0 82 else
Chris@1115 83 text_field_tag(field_name, custom_value.value, tag_options)
Chris@0 84 end
Chris@0 85 end
Chris@909 86
Chris@0 87 # Return custom field label tag
Chris@1115 88 def custom_field_label_tag(name, custom_value, options={})
Chris@1115 89 required = options[:required] || custom_value.custom_field.is_required?
Chris@1115 90
Chris@909 91 content_tag "label", h(custom_value.custom_field.name) +
Chris@1115 92 (required ? " <span class=\"required\">*</span>".html_safe : ""),
Chris@1115 93 :for => "#{name}_custom_field_values_#{custom_value.custom_field.id}"
Chris@0 94 end
Chris@909 95
Chris@0 96 # Return custom field tag with its label tag
Chris@1115 97 def custom_field_tag_with_label(name, custom_value, options={})
Chris@1115 98 custom_field_label_tag(name, custom_value, options) + custom_field_tag(name, custom_value)
Chris@0 99 end
Chris@909 100
Chris@1464 101 def custom_field_tag_for_bulk_edit(name, custom_field, projects=nil, value='')
Chris@0 102 field_name = "#{name}[custom_field_values][#{custom_field.id}]"
Chris@1115 103 field_name << "[]" if custom_field.multiple?
Chris@0 104 field_id = "#{name}_custom_field_values_#{custom_field.id}"
Chris@1115 105
Chris@1115 106 tag_options = {:id => field_id, :class => "#{custom_field.field_format}_cf"}
Chris@1115 107
Chris@1464 108 unset_tag = ''
Chris@1464 109 unless custom_field.is_required?
Chris@1464 110 unset_tag = content_tag('label',
Chris@1464 111 check_box_tag(field_name, '__none__', (value == '__none__'), :id => nil, :data => {:disables => "##{field_id}"}) + l(:button_clear),
Chris@1464 112 :class => 'inline'
Chris@1464 113 )
Chris@1464 114 end
Chris@1464 115
Chris@0 116 field_format = Redmine::CustomFieldFormat.find_by_name(custom_field.field_format)
Chris@441 117 case field_format.try(:edit_as)
Chris@0 118 when "date"
Chris@1464 119 text_field_tag(field_name, value, tag_options.merge(:size => 10)) +
Chris@1464 120 calendar_for(field_id) +
Chris@1464 121 unset_tag
Chris@0 122 when "text"
Chris@1464 123 text_area_tag(field_name, value, tag_options.merge(:rows => 3)) +
Chris@1464 124 '<br />'.html_safe +
Chris@1464 125 unset_tag
Chris@0 126 when "bool"
Chris@0 127 select_tag(field_name, options_for_select([[l(:label_no_change_option), ''],
Chris@0 128 [l(:general_text_yes), '1'],
Chris@1464 129 [l(:general_text_no), '0']], value), tag_options)
Chris@0 130 when "list"
Chris@1115 131 options = []
Chris@1115 132 options << [l(:label_no_change_option), ''] unless custom_field.multiple?
Chris@1115 133 options << [l(:label_none), '__none__'] unless custom_field.is_required?
Chris@1115 134 options += custom_field.possible_values_options(projects)
Chris@1464 135 select_tag(field_name, options_for_select(options, value), tag_options.merge(:multiple => custom_field.multiple?))
Chris@0 136 else
Chris@1464 137 text_field_tag(field_name, value, tag_options) +
Chris@1464 138 unset_tag
Chris@0 139 end
Chris@0 140 end
Chris@0 141
Chris@0 142 # Return a string used to display a custom value
Chris@0 143 def show_value(custom_value)
Chris@0 144 return "" unless custom_value
Chris@0 145 format_value(custom_value.value, custom_value.custom_field.field_format)
Chris@0 146 end
Chris@909 147
Chris@0 148 # Return a string used to display a custom value
Chris@0 149 def format_value(value, field_format)
Chris@1115 150 if value.is_a?(Array)
Chris@1115 151 value.collect {|v| format_value(v, field_format)}.compact.sort.join(', ')
Chris@1115 152 else
Chris@1115 153 Redmine::CustomFieldFormat.format_value(value, field_format)
Chris@1115 154 end
Chris@0 155 end
Chris@0 156
Chris@0 157 # Return an array of custom field formats which can be used in select_tag
Chris@441 158 def custom_field_formats_for_select(custom_field)
Chris@441 159 Redmine::CustomFieldFormat.as_select(custom_field.class.customized_class.name)
Chris@0 160 end
Chris@909 161
Chris@119 162 # Renders the custom_values in api views
Chris@119 163 def render_api_custom_values(custom_values, api)
Chris@119 164 api.array :custom_fields do
Chris@119 165 custom_values.each do |custom_value|
Chris@1115 166 attrs = {:id => custom_value.custom_field_id, :name => custom_value.custom_field.name}
Chris@1115 167 attrs.merge!(:multiple => true) if custom_value.custom_field.multiple?
Chris@1115 168 api.custom_field attrs do
Chris@1115 169 if custom_value.value.is_a?(Array)
Chris@1115 170 api.array :value do
Chris@1115 171 custom_value.value.each do |value|
Chris@1115 172 api.value value unless value.blank?
Chris@1115 173 end
Chris@1115 174 end
Chris@1115 175 else
Chris@1115 176 api.value custom_value.value
Chris@1115 177 end
Chris@119 178 end
Chris@119 179 end
Chris@119 180 end unless custom_values.empty?
Chris@119 181 end
Chris@0 182 end