annotate .svn/pristine/6e/6e19e1e338055e9065d37596fdaf97de3c4d84c3.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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