Framework  3.9
number_field_renderer.inc
Go to the documentation of this file.
1 <?php
6 /**************************************************************
7 
8  Copyright (c) 2007-2010 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 
49 {
50 
51  var $allowNegative = false;
52  var $autocomplete = true;
53  var $size = 15;
54  var $onKeyUp;
55  var $disable = false;
56  var $onChange = "";
57  var $units = "";
58  var $unitsPosition = "";
59  var $blankZero = false;
60  var $control = "text";
61  var $static = false;
62 
63  function NumberFieldRenderer(&$parent, $field = null)
64  {
65  $this->FieldRenderer($parent);
66  if ($field && !$parent->data->hasField($field))
67  {
68  $parent->add($this, $field);
69  }
70  }
71 
72  function addValidatorsToForm($field, $required = false)
73  {
74  parent::addValidatorsToForm($field);
75 
76  if ($this->control == "number")
77  {
78  $label = isset($this->label) ? $this->label : $this->parent->prettifyFieldName($field);
79 
80  $this->parent->validator->add(new RangeValidator($field, $label, $this->min, $this->max));
81  }
82  }
83 
89  function setUnits($units, $position)
90  {
91  $this->units = $units;
92  $this->unitsPosition = $position;
93  return $this;
94  }
95 
103  function setRange($min, $max, $step = 1)
104  {
105  $this->min = min($max, $min);
106  $this->max = max($max, $min);
107  $this->step = $step;
108  $this->control = "number";
109 
110  return $this;
111  }
112 
113  function renderField($field)
114  {
115  $this->_startField($field);
116 
117  $readonly = ($this->static) ? " readonly='readonly'" : "";
118  $disable = ($this->disable) ? "disabled='disabled'" : "";
119  $onKeyUp = ($this->onKeyUp) ? " onkeyup='{$this->onKeyUp}(this)'" : "";
120  // JDG - maskInput function doesn't recognize boolean var value
121  // translate to 0 or 1
122  $allowneg = ($this->allowNegative) ? 1 : 0;
123  $autocomplete = ($this->autocomplete) ? "" : " autocomplete='off' ";
124  $onchange = ($this->onChange) ? "onchange='{$this->onChange}(this)'" : "";
125 
126  if ($this->unitsPosition == "before") echo $this->units." ";
127 
128  $value = $this->parent->data->get($field);
129  if ($value == 0 && $this->blankZero)
130  {
131  $value = "";
132  }
133 
134  if ($this->control == "number")
135  {
136  $control = "number";
137  $rangeAttrs = " min='{$this->min}' max='{$this->max}' step='{$this->step}'";
138  }
139  else
140  {
141  $control = "text";
142  $rangeAttrs = "";
143  }
144  echo "<input id='{$this->parent->id}_$field' type='{$control}' style='width: auto' name='$field'
145  value='".$value."' $rangeAttrs $autocomplete
146  size='{$this->size}' onkeypress='return maskInput(event, $allowneg);' $disable $static $onchange $onKeyUp/>";
147 
148  if ($this->unitsPosition == "after") echo " ".$this->units;
149 
150  $this->_endField($field);
151  }
152 
153  function renderReadOnly($field)
154  {
155  $this->_startField($field);
156 
157  if ($this->unitsPosition == "before") echo $this->units." ";
158 
159  echo $this->parent->data->get($field);
160 
161  if ($this->unitsPosition == "after") echo " ".$this->units;
162 
163  $this->_endField($field);
164  }
165 
166  // if users enter negatives where they shouldn't (any position other than first,
167  // strip them out
168  function preProcess($field = "")
169  {
170  $current = $this->parent->data->get($field);
171 
172  $value = str_replace(",", "", $current);
173 
174  if($this->allowNegative)
175  {
176  if(substr($value, 0,1) == '-')
177  $value = '-' . str_replace("-", "", $value);
178  else
179  $value = str_replace("-", "", $value);
180  }
181  $this->parent->data->$field = $value;
182  }
183 }
184 ?>
FieldRenderer is the abstract base class for all FieldRenderers.
_startField($field, $styles="")
Internal method to generate the starting HTML for the field (including the label)
_endField($field)
Internal method to generate the closing HTML for the field.
FieldRenderer($parent)
Constructor.
Field renderer for numeric data fields.
renderField($field)
FieldRenderers must override this method to provide the HTML implementation of the control used to ed...
$control
The HTML control type to use when rendering. This gets set to "number" if a range is specified.
$allowNegative
Whether user can enter a negative number.
$onChange
optional javascript callback on data input change
$units
units text to display
preProcess($field="")
FieldRenderers can override this method to provide behavior that occurs prior to the saving of the pa...
$size
Size of the displayed control in characters.
$unitsPosition
whether to display units before or after the control
addValidatorsToForm($field, $required=false)
This method is called by the AutoForm to add any default input validators that are required by the Fi...
$static
Render the field as a static (readonly) value, that is visible, non-editable and the value is submitt...
$onKeyUp
optional javascript callback function for key up events
$autocomplete
Whether the browser is allowed to show autocomplete for this field.
$blankZero
Leave field blank if value is zero.
NumberFieldRenderer(&$parent, $field=null)
setUnits($units, $position)
Sets the displayed units and their position.
$disable
alternative to readonly; field can be reenabled or set thru javascript
setRange($min, $max, $step=1)
Specifies that the field should be rendered with a "number" HTML control with the supplied range and ...
RangeValidator class.