annotate .svn/pristine/b5/b5d71b1233be49fcf1babc0bab7d8a2c992deddb.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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