1 # encoding: utf-8
2 #
3 # Redmine - project management software
4 # Copyright (C) 2006-2012 Jean-Philippe Lang
5 #
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 #
11 # 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 #
16 # 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 tabs = [{:name => 'IssueCustomField', :partial => 'custom_fields/index', :label => :label_issue_plural},
24 {:name => 'TimeEntryCustomField', :partial => 'custom_fields/index', :label => :label_spent_time},
25 {:name => 'ProjectCustomField', :partial => 'custom_fields/index', :label => :label_project_plural},
26 {:name => 'VersionCustomField', :partial => 'custom_fields/index', :label => :label_version_plural},
27 {:name => 'UserCustomField', :partial => 'custom_fields/index', :label => :label_user_plural},
28 {:name => 'GroupCustomField', :partial => 'custom_fields/index', :label => :label_group_plural},
29 {:name => 'TimeEntryActivityCustomField', :partial => 'custom_fields/index', :label => TimeEntryActivity::OptionName},
30 {:name => 'IssuePriorityCustomField', :partial => 'custom_fields/index', :label => IssuePriority::OptionName},
31 {:name => 'DocumentCategoryCustomField', :partial => 'custom_fields/index', :label => DocumentCategory::OptionName}
32 ]
33 end
34
35 # Return custom field html tag corresponding to its format
36 def custom_field_tag(name, custom_value)
37 custom_field = custom_value.custom_field
38 field_name = "#{name}[custom_field_values][#{custom_field.id}]"
39 field_name << "[]" if custom_field.multiple?
40 field_id = "#{name}_custom_field_values_#{custom_field.id}"
41
42 field_format = Redmine::CustomFieldFormat.find_by_name(custom_field.field_format)
43 case field_format.try(:edit_as)
44 when "date"
45 text_field_tag(field_name, custom_value.value, :id => field_id, :size => 10) +
46 calendar_for(field_id)
47 when "text"
48 text_area_tag(field_name, custom_value.value, :id => field_id, :rows => 3, :style => 'width:90%')
49 when "bool"
50 hidden_field_tag(field_name, '0') + check_box_tag(field_name, '1', custom_value.true?, :id => field_id)
51 when "list"
52 blank_option = ''
53 unless custom_field.multiple?
54 if custom_field.is_required?
55 unless custom_field.default_value.present?
56 blank_option = "<option value=\"\">--- #{l(:actionview_instancetag_blank_option)} ---</option>"
57 end
58 else
59 blank_option = '<option></option>'
60 end
61 end
62 s = select_tag(field_name, blank_option.html_safe + options_for_select(custom_field.possible_values_options(custom_value.customized), custom_value.value),
63 :id => field_id, :multiple => custom_field.multiple?)
64 if custom_field.multiple?
65 s << hidden_field_tag(field_name, '')
66 end
67 s
68 else
69 text_field_tag(field_name, custom_value.value, :id => field_id)
70 end
71 end
72
73 # Return custom field label tag
74 def custom_field_label_tag(name, custom_value)
75 content_tag "label", h(custom_value.custom_field.name) +
76 (custom_value.custom_field.is_required? ? " <span class=\"required\">*</span>".html_safe : ""),
77 :for => "#{name}_custom_field_values_#{custom_value.custom_field.id}"
78 end
79
80 # Return custom field tag with its label tag
81 def custom_field_tag_with_label(name, custom_value)
82 custom_field_label_tag(name, custom_value) + custom_field_tag(name, custom_value)
83 end
84
85 def custom_field_tag_for_bulk_edit(name, custom_field, projects=nil)
86 field_name = "#{name}[custom_field_values][#{custom_field.id}]"
87 field_name << "[]" if custom_field.multiple?
88 field_id = "#{name}_custom_field_values_#{custom_field.id}"
89 field_format = Redmine::CustomFieldFormat.find_by_name(custom_field.field_format)
90 case field_format.try(:edit_as)
91 when "date"
92 text_field_tag(field_name, '', :id => field_id, :size => 10) +
93 calendar_for(field_id)
94 when "text"
95 text_area_tag(field_name, '', :id => field_id, :rows => 3, :style => 'width:90%')
96 when "bool"
97 select_tag(field_name, options_for_select([[l(:label_no_change_option), ''],
98 [l(:general_text_yes), '1'],
99 [l(:general_text_no), '0']]), :id => field_id)
100 when "list"
101 options = []
102 options << [l(:label_no_change_option), ''] unless custom_field.multiple?
103 options << [l(:label_none), '__none__'] unless custom_field.is_required?
104 options += custom_field.possible_values_options(projects)
105 select_tag(field_name, options_for_select(options),
106 :id => field_id, :multiple => custom_field.multiple?)
107 else
108 text_field_tag(field_name, '', :id => field_id)
109 end
110 end
111
112 # Return a string used to display a custom value
113 def show_value(custom_value)
114 return "" unless custom_value
115 format_value(custom_value.value, custom_value.custom_field.field_format)
116 end
117
118 # Return a string used to display a custom value
119 def format_value(value, field_format)
120 if value.is_a?(Array)
121 value.collect {|v| format_value(v, field_format)}.compact.sort.join(', ')
122 else
123 Redmine::CustomFieldFormat.format_value(value, field_format)
124 end
125 end
126
127 # Return an array of custom field formats which can be used in select_tag
128 def custom_field_formats_for_select(custom_field)
129 Redmine::CustomFieldFormat.as_select(custom_field.class.customized_class.name)
130 end
131
132 # Renders the custom_values in api views
133 def render_api_custom_values(custom_values, api)
134 api.array :custom_fields do
135 custom_values.each do |custom_value|
136 attrs = {:id => custom_value.custom_field_id, :name => custom_value.custom_field.name}
137 attrs.merge!(:multiple => true) if custom_value.custom_field.multiple?
138 api.custom_field attrs do
139 if custom_value.value.is_a?(Array)
140 api.array :value do
141 custom_value.value.each do |value|
142 api.value value unless value.blank?
143 end
144 end
145 else
146 api.value custom_value.value
147 end
148 end
149 end
150 end unless custom_values.empty?
151 end
152 end