annotate .svn/pristine/3d/3d32001255668e3c83c7a368c346637efc3f891b.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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