1 module Admin::ConfigurationHelper
2 # Defines helper methods for use in the admin interface when displaying or editing configuration.
3
4 # Renders the setting as label and value:
5 #
6 # show_config("admin.title")
7 # => <label for="admin_title">Admin title<label><span id="admin_title">Radiant CMS</span>
8 #
9 def show_config(key, options={})
10 setting = setting_for(key)
11 setting.valid?
12 domkey = key.gsub(/\W/, '_')
13 html = ""
14 html << content_tag(:label, t("config.#{key}").titlecase, :for => domkey)
15 if setting.boolean?
16 value = setting.checked? ? t('yes') : t('no')
17 html << content_tag(:span, value, :id => domkey, :class => "#{value} #{options[:class]}")
18 else
19 value = setting.selected_value || setting.value
20 html << content_tag(:span, value, :id => domkey, :class => options[:class])
21 end
22 html << content_tag(:span, " #{t("units.#{setting.units}")}", :class => 'units') if setting.units
23 html << content_tag(:span, " #{t('warning')}: #{[setting.errors.on(:value)].flatten.first}", :class => 'warning') if setting.errors.on(:value)
24 html
25 end
26
27 # Renders the setting as label and appropriate input field:
28 #
29 # edit_setting("admin.title")
30 # => <label for="admin_title">Admin title<label><input type="text" name="config['admin.title']" id="admin_title" value="Radiant CMS" />
31 #
32 # edit_config("defaults.page.status")
33 # =>
34 # <label for="defaults_page_status">Default page status<label>
35 # <select type="text" name="config['defaults.page.status']" id="defaults_page_status">
36 # <option value="Draft">Draft</option>
37 # ...
38 # </select>
39 #
40 # edit_setting("user.allow_password_reset?")
41 # => <label for="user_allow_password_reset_">Admin title<label><input type="checkbox" name="config['user.allow_password_reset?']" id="user_allow_password_reset_" value="1" checked="checked" />
42 #
43 def edit_config(key, options={})
44 setting = setting_for(key)
45 domkey = key.gsub(/\W/, '_')
46 name = "config[#{key}]"
47 title = t("config.#{key}").titlecase
48 title << content_tag(:span, " (#{t("units.#{setting.units}")})", :class => 'units') if setting.units
49 value = params[key.to_sym].nil? ? setting.value : params[key.to_sym]
50 html = ""
51 if setting.boolean?
52 html << hidden_field_tag(name, 0)
53 html << check_box_tag(name, 1, value, :class => 'setting', :id => domkey)
54 html << content_tag(:label, title, :class => 'checkbox', :for => domkey)
55 elsif setting.selector?
56 html << content_tag(:label, title, :for => domkey)
57 html << select_tag(name, options_for_select(setting.definition.selection, value), :class => 'setting', :id => domkey)
58 else
59 html << content_tag(:label, title, :for => domkey)
60 html << text_field_tag(name, value, :class => 'textbox', :id => domkey)
61 end
62 if setting.errors.on(:value)
63 html << content_tag(:span, [setting.errors.on(:value)].flatten.first, :class => 'error')
64 html = content_tag(:span, html, :class => "error-with-field")
65 end
66 html
67 end
68
69 def setting_for(key)
70 @config ||= {} # normally initialized in Admin::ConfigurationController
71 @config[key] ||= Radiant.config.find_or_create_by_key(key)
72 end
73
74 def definition_for(key)
75 if setting = setting_for(key)
76 setting.definition
77 end
78 end
79
80 en