To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / app / helpers / custom_fields_helper.rb @ 1298:4f746d8966dd
History | View | Annotate | Download (5.89 KB)
| 1 | 909:cbb26bc654de | Chris | # encoding: utf-8
|
|---|---|---|---|
| 2 | #
|
||
| 3 | 441:cbce1fd3b1b7 | Chris | # Redmine - project management software
|
| 4 | 1295:622f24f53b42 | Chris | # Copyright (C) 2006-2013 Jean-Philippe Lang
|
| 5 | 0:513646585e45 | Chris | #
|
| 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 | 909:cbb26bc654de | Chris | #
|
| 11 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | #
|
| 16 | 0:513646585e45 | Chris | # 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 | def custom_fields_tabs |
||
| 23 | 1115:433d4f72a19b | Chris | CustomField::CUSTOM_FIELDS_TABS |
| 24 | 0:513646585e45 | Chris | end
|
| 25 | 909:cbb26bc654de | Chris | |
| 26 | 0:513646585e45 | Chris | # Return custom field html tag corresponding to its format
|
| 27 | 1295:622f24f53b42 | Chris | def custom_field_tag(name, custom_value) |
| 28 | 0:513646585e45 | Chris | custom_field = custom_value.custom_field |
| 29 | field_name = "#{name}[custom_field_values][#{custom_field.id}]"
|
||
| 30 | 1115:433d4f72a19b | Chris | field_name << "[]" if custom_field.multiple? |
| 31 | 0:513646585e45 | Chris | field_id = "#{name}_custom_field_values_#{custom_field.id}"
|
| 32 | |||
| 33 | 1115:433d4f72a19b | Chris | tag_options = {:id => field_id, :class => "#{custom_field.field_format}_cf"}
|
| 34 | |||
| 35 | 0:513646585e45 | Chris | field_format = Redmine::CustomFieldFormat.find_by_name(custom_field.field_format) |
| 36 | 441:cbce1fd3b1b7 | Chris | case field_format.try(:edit_as) |
| 37 | 0:513646585e45 | Chris | when "date" |
| 38 | 1115:433d4f72a19b | Chris | text_field_tag(field_name, custom_value.value, tag_options.merge(:size => 10)) + |
| 39 | 0:513646585e45 | Chris | calendar_for(field_id) |
| 40 | when "text" |
||
| 41 | 1115:433d4f72a19b | Chris | text_area_tag(field_name, custom_value.value, tag_options.merge(:rows => 3)) |
| 42 | 0:513646585e45 | Chris | when "bool" |
| 43 | 1115:433d4f72a19b | Chris | hidden_field_tag(field_name, '0') + check_box_tag(field_name, '1', custom_value.true?, tag_options) |
| 44 | 0:513646585e45 | Chris | when "list" |
| 45 | 1115:433d4f72a19b | Chris | blank_option = ''.html_safe
|
| 46 | unless custom_field.multiple?
|
||
| 47 | if custom_field.is_required?
|
||
| 48 | unless custom_field.default_value.present?
|
||
| 49 | blank_option = content_tag('option', "--- #{l(:actionview_instancetag_blank_option)} ---", :value => '') |
||
| 50 | end
|
||
| 51 | else
|
||
| 52 | blank_option = content_tag('option')
|
||
| 53 | end
|
||
| 54 | end
|
||
| 55 | s = select_tag(field_name, blank_option + options_for_select(custom_field.possible_values_options(custom_value.customized), custom_value.value), |
||
| 56 | tag_options.merge(:multiple => custom_field.multiple?))
|
||
| 57 | if custom_field.multiple?
|
||
| 58 | s << hidden_field_tag(field_name, '')
|
||
| 59 | end
|
||
| 60 | s |
||
| 61 | 0:513646585e45 | Chris | else
|
| 62 | 1115:433d4f72a19b | Chris | text_field_tag(field_name, custom_value.value, tag_options) |
| 63 | 0:513646585e45 | Chris | end
|
| 64 | end
|
||
| 65 | 909:cbb26bc654de | Chris | |
| 66 | 0:513646585e45 | Chris | # Return custom field label tag
|
| 67 | 1115:433d4f72a19b | Chris | def custom_field_label_tag(name, custom_value, options={}) |
| 68 | required = options[:required] || custom_value.custom_field.is_required?
|
||
| 69 | |||
| 70 | 909:cbb26bc654de | Chris | content_tag "label", h(custom_value.custom_field.name) +
|
| 71 | 1115:433d4f72a19b | Chris | (required ? " <span class=\"required\">*</span>".html_safe : ""), |
| 72 | :for => "#{name}_custom_field_values_#{custom_value.custom_field.id}" |
||
| 73 | 0:513646585e45 | Chris | end
|
| 74 | 909:cbb26bc654de | Chris | |
| 75 | 0:513646585e45 | Chris | # Return custom field tag with its label tag
|
| 76 | 1115:433d4f72a19b | Chris | def custom_field_tag_with_label(name, custom_value, options={}) |
| 77 | custom_field_label_tag(name, custom_value, options) + custom_field_tag(name, custom_value) |
||
| 78 | 0:513646585e45 | Chris | end
|
| 79 | 909:cbb26bc654de | Chris | |
| 80 | 441:cbce1fd3b1b7 | Chris | def custom_field_tag_for_bulk_edit(name, custom_field, projects=nil) |
| 81 | 0:513646585e45 | Chris | field_name = "#{name}[custom_field_values][#{custom_field.id}]"
|
| 82 | 1115:433d4f72a19b | Chris | field_name << "[]" if custom_field.multiple? |
| 83 | 0:513646585e45 | Chris | field_id = "#{name}_custom_field_values_#{custom_field.id}"
|
| 84 | 1115:433d4f72a19b | Chris | |
| 85 | tag_options = {:id => field_id, :class => "#{custom_field.field_format}_cf"}
|
||
| 86 | |||
| 87 | 0:513646585e45 | Chris | field_format = Redmine::CustomFieldFormat.find_by_name(custom_field.field_format) |
| 88 | 441:cbce1fd3b1b7 | Chris | case field_format.try(:edit_as) |
| 89 | 0:513646585e45 | Chris | when "date" |
| 90 | 1115:433d4f72a19b | Chris | text_field_tag(field_name, '', tag_options.merge(:size => 10)) + |
| 91 | 0:513646585e45 | Chris | calendar_for(field_id) |
| 92 | when "text" |
||
| 93 | 1115:433d4f72a19b | Chris | text_area_tag(field_name, '', tag_options.merge(:rows => 3)) |
| 94 | 0:513646585e45 | Chris | when "bool" |
| 95 | select_tag(field_name, options_for_select([[l(:label_no_change_option), ''], |
||
| 96 | [l(:general_text_yes), '1'], |
||
| 97 | 1115:433d4f72a19b | Chris | [l(:general_text_no), '0']]), tag_options) |
| 98 | 0:513646585e45 | Chris | when "list" |
| 99 | 1115:433d4f72a19b | Chris | options = [] |
| 100 | options << [l(:label_no_change_option), ''] unless custom_field.multiple? |
||
| 101 | options << [l(:label_none), '__none__'] unless custom_field.is_required? |
||
| 102 | options += custom_field.possible_values_options(projects) |
||
| 103 | select_tag(field_name, options_for_select(options), tag_options.merge(:multiple => custom_field.multiple?))
|
||
| 104 | 0:513646585e45 | Chris | else
|
| 105 | 1115:433d4f72a19b | Chris | text_field_tag(field_name, '', tag_options)
|
| 106 | 0:513646585e45 | Chris | end
|
| 107 | end
|
||
| 108 | |||
| 109 | # Return a string used to display a custom value
|
||
| 110 | def show_value(custom_value) |
||
| 111 | return "" unless custom_value |
||
| 112 | format_value(custom_value.value, custom_value.custom_field.field_format) |
||
| 113 | end
|
||
| 114 | 909:cbb26bc654de | Chris | |
| 115 | 0:513646585e45 | Chris | # Return a string used to display a custom value
|
| 116 | def format_value(value, field_format) |
||
| 117 | 1115:433d4f72a19b | Chris | if value.is_a?(Array) |
| 118 | value.collect {|v| format_value(v, field_format)}.compact.sort.join(', ')
|
||
| 119 | else
|
||
| 120 | Redmine::CustomFieldFormat.format_value(value, field_format) |
||
| 121 | end
|
||
| 122 | 0:513646585e45 | Chris | end
|
| 123 | |||
| 124 | # Return an array of custom field formats which can be used in select_tag
|
||
| 125 | 441:cbce1fd3b1b7 | Chris | def custom_field_formats_for_select(custom_field) |
| 126 | Redmine::CustomFieldFormat.as_select(custom_field.class.customized_class.name) |
||
| 127 | 0:513646585e45 | Chris | end
|
| 128 | 909:cbb26bc654de | Chris | |
| 129 | 119:8661b858af72 | Chris | # Renders the custom_values in api views
|
| 130 | def render_api_custom_values(custom_values, api) |
||
| 131 | api.array :custom_fields do |
||
| 132 | custom_values.each do |custom_value|
|
||
| 133 | 1115:433d4f72a19b | Chris | attrs = {:id => custom_value.custom_field_id, :name => custom_value.custom_field.name}
|
| 134 | attrs.merge!(:multiple => true) if custom_value.custom_field.multiple? |
||
| 135 | api.custom_field attrs do
|
||
| 136 | if custom_value.value.is_a?(Array) |
||
| 137 | api.array :value do |
||
| 138 | custom_value.value.each do |value|
|
||
| 139 | api.value value unless value.blank?
|
||
| 140 | end
|
||
| 141 | end
|
||
| 142 | else
|
||
| 143 | api.value custom_value.value |
||
| 144 | end
|
||
| 145 | 119:8661b858af72 | Chris | end
|
| 146 | end
|
||
| 147 | end unless custom_values.empty? |
||
| 148 | end
|
||
| 149 | 0:513646585e45 | Chris | end |