Framework  3.9
slider_field_renderer.inc
Go to the documentation of this file.
1 <?php
6 /**************************************************************
7 
8 Copyright (c) 2015 Sonjara, Inc
9 
10 Permission is hereby granted, free of charge, to any person
11 obtaining a copy of this software and associated documentation
12 files (the "Software"), to deal in the Software without
13 restriction, including without limitation the rights to use,
14 copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the
16 Software is furnished to do so, subject to the following
17 conditions:
18 
19 The above copyright notice and this permission notice shall be
20 included in all copies or substantial portions of the Software.
21 
22 Except as contained in this notice, the name(s) of the above
23 copyright holders shall not be used in advertising or otherwise
24 to promote the sale, use or other dealings in this Software
25 without prior written authorization.
26 
27 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
29 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
31 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
32 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34 OTHER DEALINGS IN THE SOFTWARE.
35 
36 *****************************************************************/
37 
38 require_once realpath(dirname(__FILE__))."/../field_renderers.inc";
39 
41 {
42  var $min = 0;
43  var $max = 10;
44  var $editable = true;
45  var $thresholds = null;
46  var $displayValue = true;
47 
48  function __construct(&$form, $field, $min = 0, $max = 10, $editable = true)
49  {
50  $this->min = $min;
51  $this->max = $max;
52  $this->editable = $editable;
53  $this->FieldRenderer($form);
54 
55  if ($field && !$form->data->hasField($field))
56  {
57  $form->add($this, $field);
58  }
59  else
60  {
61  $form->override($field, $label, $this);
62  }
63  }
64 
66  {
67  $this->thresholds = $thresholds;
68  }
69 
70  function renderScript($field)
71  {
72  if (isset($this->thresholds))
73  {
74  krsort($this->thresholds, SORT_NUMERIC);
75  $thresholds = json_encode($this->thresholds);
76  }
77  else
78  {
79  $thresholds = "null";
80  }
81 
82  $id = "{$this->parent->id}_{$field}";
83 
84  $script = <<<ENDSCRIPT
85 <script type='text/javascript'>
86 var {$id}_thresholds = {$thresholds};
87 window.addEvent('domready', function()
88 {
89  var range = document.id('{$id}');
90  var display = document.id('{$id}_display');
91 
92  if (display != null)
93  {
94  range.addEvent('input', function(elt)
95  {
96  if ({$id}_thresholds != null)
97  {
98  for(var score in {$id}_thresholds)
99  {
100  if (range.value == score)
101  {
102  display.set('html', ${id}_thresholds[score]);
103  }
104  }
105  }
106  else
107  {
108  display.set('text', range.value);
109  }
110  });
111  }
112 });
113 </script>
114 ENDSCRIPT;
115 
116  echo $script;
117  }
118 
119  function renderField($field)
120  {
121  $this->_startField($field);
122  $value = $this->parent->data->get($field);
123  if (!$value) $value = $this->min;
124 
125  echo "<input type='range' id='{$this->parent->id}_{$field}' name='{$field}' min='{$this->min}' max='{$this->max}' value='{$value}' data-displayField='{$this->parent->id}_{$field}_display'/>";
126 
127  if ($this->displayValue)
128  {
129  $threshold = ($this->thresholds) ? $this->thresholds[$value] : "";
130  echo "<span class='range_display' id='{$this->parent->id}_{$field}_display'>{$threshold}</span>";
131  }
132 
133  $this->_endField($field);
134  }
135 
136  function renderReadOnly($field)
137  {
138  }
139 }
140 
FieldRenderer is the abstract base class for all FieldRenderers.
FieldRenderer($parent)
Constructor.
renderScript($field)
FieldRenderers can override this method to provide any Javascript that their control requires for an ...
__construct(&$form, $field, $min=0, $max=10, $editable=true)
renderField($field)
FieldRenderers must override this method to provide the HTML implementation of the control used to ed...