Chris@909
|
1 # encoding: utf-8
|
Chris@909
|
2 #
|
Chris@441
|
3 # Redmine - project management software
|
Chris@441
|
4 # Copyright (C) 2006-2011 Jean-Philippe Lang
|
Chris@0
|
5 #
|
Chris@0
|
6 # This program is free software; you can redistribute it and/or
|
Chris@0
|
7 # modify it under the terms of the GNU General Public License
|
Chris@0
|
8 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
9 # of the License, or (at your option) any later version.
|
Chris@909
|
10 #
|
Chris@0
|
11 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
14 # GNU General Public License for more details.
|
Chris@909
|
15 #
|
Chris@0
|
16 # You should have received a copy of the GNU General Public License
|
Chris@0
|
17 # along with this program; if not, write to the Free Software
|
Chris@0
|
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
19
|
Chris@0
|
20 module CustomFieldsHelper
|
Chris@0
|
21
|
Chris@0
|
22 def custom_fields_tabs
|
Chris@0
|
23 tabs = [{:name => 'IssueCustomField', :partial => 'custom_fields/index', :label => :label_issue_plural},
|
Chris@0
|
24 {:name => 'TimeEntryCustomField', :partial => 'custom_fields/index', :label => :label_spent_time},
|
Chris@0
|
25 {:name => 'ProjectCustomField', :partial => 'custom_fields/index', :label => :label_project_plural},
|
Chris@0
|
26 {:name => 'VersionCustomField', :partial => 'custom_fields/index', :label => :label_version_plural},
|
Chris@0
|
27 {:name => 'UserCustomField', :partial => 'custom_fields/index', :label => :label_user_plural},
|
Chris@0
|
28 {:name => 'GroupCustomField', :partial => 'custom_fields/index', :label => :label_group_plural},
|
Chris@0
|
29 {:name => 'TimeEntryActivityCustomField', :partial => 'custom_fields/index', :label => TimeEntryActivity::OptionName},
|
Chris@0
|
30 {:name => 'IssuePriorityCustomField', :partial => 'custom_fields/index', :label => IssuePriority::OptionName},
|
Chris@0
|
31 {:name => 'DocumentCategoryCustomField', :partial => 'custom_fields/index', :label => DocumentCategory::OptionName}
|
Chris@0
|
32 ]
|
Chris@0
|
33 end
|
Chris@909
|
34
|
Chris@0
|
35 # Return custom field html tag corresponding to its format
|
Chris@0
|
36 def custom_field_tag(name, custom_value)
|
Chris@0
|
37 custom_field = custom_value.custom_field
|
Chris@0
|
38 field_name = "#{name}[custom_field_values][#{custom_field.id}]"
|
Chris@0
|
39 field_id = "#{name}_custom_field_values_#{custom_field.id}"
|
Chris@0
|
40
|
Chris@0
|
41 field_format = Redmine::CustomFieldFormat.find_by_name(custom_field.field_format)
|
Chris@441
|
42 case field_format.try(:edit_as)
|
Chris@0
|
43 when "date"
|
Chris@909
|
44 text_field_tag(field_name, custom_value.value, :id => field_id, :size => 10) +
|
Chris@0
|
45 calendar_for(field_id)
|
Chris@0
|
46 when "text"
|
Chris@0
|
47 text_area_tag(field_name, custom_value.value, :id => field_id, :rows => 3, :style => 'width:90%')
|
Chris@0
|
48 when "bool"
|
Chris@0
|
49 hidden_field_tag(field_name, '0') + check_box_tag(field_name, '1', custom_value.true?, :id => field_id)
|
Chris@0
|
50 when "list"
|
Chris@0
|
51 blank_option = custom_field.is_required? ?
|
Chris@909
|
52 (custom_field.default_value.blank? ? "<option value=\"\">--- #{l(:actionview_instancetag_blank_option)} ---</option>" : '') :
|
Chris@0
|
53 '<option></option>'
|
Chris@441
|
54 select_tag(field_name, blank_option + options_for_select(custom_field.possible_values_options(custom_value.customized), custom_value.value), :id => field_id)
|
Chris@0
|
55 else
|
Chris@0
|
56 text_field_tag(field_name, custom_value.value, :id => field_id)
|
Chris@0
|
57 end
|
Chris@0
|
58 end
|
Chris@909
|
59
|
Chris@0
|
60 # Return custom field label tag
|
Chris@0
|
61 def custom_field_label_tag(name, custom_value)
|
Chris@909
|
62 content_tag "label", h(custom_value.custom_field.name) +
|
Chris@909
|
63 (custom_value.custom_field.is_required? ? " <span class=\"required\">*</span>".html_safe : ""),
|
Chris@0
|
64 :for => "#{name}_custom_field_values_#{custom_value.custom_field.id}",
|
Chris@0
|
65 :class => (custom_value.errors.empty? ? nil : "error" )
|
Chris@0
|
66 end
|
Chris@909
|
67
|
Chris@0
|
68 # Return custom field tag with its label tag
|
Chris@0
|
69 def custom_field_tag_with_label(name, custom_value)
|
Chris@0
|
70 custom_field_label_tag(name, custom_value) + custom_field_tag(name, custom_value)
|
Chris@0
|
71 end
|
Chris@909
|
72
|
Chris@441
|
73 def custom_field_tag_for_bulk_edit(name, custom_field, projects=nil)
|
Chris@0
|
74 field_name = "#{name}[custom_field_values][#{custom_field.id}]"
|
Chris@0
|
75 field_id = "#{name}_custom_field_values_#{custom_field.id}"
|
Chris@0
|
76 field_format = Redmine::CustomFieldFormat.find_by_name(custom_field.field_format)
|
Chris@441
|
77 case field_format.try(:edit_as)
|
Chris@0
|
78 when "date"
|
Chris@909
|
79 text_field_tag(field_name, '', :id => field_id, :size => 10) +
|
Chris@0
|
80 calendar_for(field_id)
|
Chris@0
|
81 when "text"
|
Chris@0
|
82 text_area_tag(field_name, '', :id => field_id, :rows => 3, :style => 'width:90%')
|
Chris@0
|
83 when "bool"
|
Chris@0
|
84 select_tag(field_name, options_for_select([[l(:label_no_change_option), ''],
|
Chris@0
|
85 [l(:general_text_yes), '1'],
|
Chris@0
|
86 [l(:general_text_no), '0']]), :id => field_id)
|
Chris@0
|
87 when "list"
|
Chris@441
|
88 select_tag(field_name, options_for_select([[l(:label_no_change_option), '']] + custom_field.possible_values_options(projects)), :id => field_id)
|
Chris@0
|
89 else
|
Chris@0
|
90 text_field_tag(field_name, '', :id => field_id)
|
Chris@0
|
91 end
|
Chris@0
|
92 end
|
Chris@0
|
93
|
Chris@0
|
94 # Return a string used to display a custom value
|
Chris@0
|
95 def show_value(custom_value)
|
Chris@0
|
96 return "" unless custom_value
|
Chris@0
|
97 format_value(custom_value.value, custom_value.custom_field.field_format)
|
Chris@0
|
98 end
|
Chris@909
|
99
|
Chris@0
|
100 # Return a string used to display a custom value
|
Chris@0
|
101 def format_value(value, field_format)
|
Chris@0
|
102 Redmine::CustomFieldFormat.format_value(value, field_format) # Proxy
|
Chris@0
|
103 end
|
Chris@0
|
104
|
Chris@0
|
105 # Return an array of custom field formats which can be used in select_tag
|
Chris@441
|
106 def custom_field_formats_for_select(custom_field)
|
Chris@441
|
107 Redmine::CustomFieldFormat.as_select(custom_field.class.customized_class.name)
|
Chris@0
|
108 end
|
Chris@909
|
109
|
Chris@119
|
110 # Renders the custom_values in api views
|
Chris@119
|
111 def render_api_custom_values(custom_values, api)
|
Chris@119
|
112 api.array :custom_fields do
|
Chris@119
|
113 custom_values.each do |custom_value|
|
Chris@119
|
114 api.custom_field :id => custom_value.custom_field_id, :name => custom_value.custom_field.name do
|
Chris@119
|
115 api.value custom_value.value
|
Chris@119
|
116 end
|
Chris@119
|
117 end
|
Chris@119
|
118 end unless custom_values.empty?
|
Chris@119
|
119 end
|
Chris@0
|
120 end
|