Avail feature updated
[ninja.git] / application / helpers / option.php
blob576701b9b56d7fce5c1a2c9219bad9bd1bbb06fc
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
3 /**
4 * A helper for widgets to render their custom configuration options
5 */
6 class option
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 */
16 /**
17 * Constructor
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) {
27 $this->ns = $ns;
28 $this->name = $name;
29 $this->label = $label;
30 $this->type = $type;
31 $this->args = $args;
32 $this->default = $default;
35 /**
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;
42 /**
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;
54 /**
55 * Print the label tag
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>";
62 /**
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) {
68 $type = $this->type;
70 $args = $this->args;
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);
79 switch ($type) {
80 case 'checkbox':
81 case 'radio':
82 if (!isset($args['value']))
83 $args['value'] = 1;
84 $value_arg = 'checked';
85 break;
86 case 'dropdown':
87 $value_arg = 'selected';
88 break;
89 default:
90 $value_arg = 'value';
91 break;
93 if (!isset($args[$value_arg]))
94 $args[$value_arg] = $value;
96 $f = new form();
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)
106 return '';
107 return <<<EOSCRIPT
108 function(widget) {
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);
111 });}
112 EOSCRIPT;