1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
4 * A helper for widgets to render their custom configuration options
8 private $should_render_js = true; /**< True to auto-render javascript to handle changes */
9 public $ns; /**< A unique namespace, usually the widget name */
10 public $name; /**< A field name, should be "code-friendly" */
11 public $label; /**< The label text, should be translated */
12 private $type; /**< The widget type, ie "input", "checkbox", "textarea", etc - must exist in kohana's form helper */
13 private $args; /**< Arguments to send to the form helper to render it properly */
14 private $default; /**< A default value to fall back on */
19 * @param $ns A unique namespace for the options, usually the widget name
20 * @param $name A field name, should be "code-friendly" so lower-case ascii and no spaces
21 * @param $label A (translated) label to print
22 * @param $type The widget type, ie "input", "checkbox", "textarea", etc - must exist in kohana's form helper
23 * @param $args An attribute-value map that will be added to the widget
24 * @param $default Default value for the widget
26 public function __construct($ns, $name, $label, $type, $args=array(), $default=0) {
29 $this->label
= $label;
32 $this->default = $default;
36 * True to auto-render javascript to handle changes, false otherwise
38 public function should_render_js($render_js) {
39 $this->should_render_js
= $render_js;
43 * Get the value for the setting
45 * @param $settings Settings from a widget model object
46 * @return The setting if set, otherwise the default value
48 public function value($settings) {
49 if (isset($settings[$this->name
]))
50 return $settings[$this->name
];
51 return $this->default;
56 * @param $id Instance id for this widget
58 public function render_label($id) {
59 return "<label for=\"$this->ns-$this->name-$id\">"._($this->label
)."</label>";
63 * Print the widget itself
64 * @param $id Instance id for this widget
65 * @param $settings Settings from a widget model object
67 public function render_widget($id, $settings) {
71 if (!isset($args['id']))
72 $args['id'] = $this->ns
.'-'.$this->name
.'-'.$id;
73 if (!isset($args['class']))
74 $args['class'] = $this->name
;
75 if (!isset($args['name']))
76 $args['name'] = $this->name
;
78 $value = $this->value($settings);
82 if (!isset($args['value']))
84 $value_arg = 'checked';
87 $value_arg = 'selected';
93 if (!isset($args[$value_arg]))
94 $args[$value_arg] = $value;
97 return $f->$type($args);
101 * Returns the javascript to handle option changes
102 * Will return the empty string if should_render_js is false
104 public function render_js() {
105 if (!$this->should_render_js
)
109 \$('#'+ widget.widget_id + ' *[name=$this->name]').on('change', function() {
110 widget.save_custom_val(this.type == 'checkbox' ? this.checked + 0 : $(this).val(), '$this->name', widget.update_display);