Framework  3.9
currency_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 
45 {
46  var $allowNegative = false;
47  var $autocomplete = true;
48  var $disable = false;
49  var $onChange = "";
50  var $onKeyUp;
51  var $template = 2;
52  var $size = 20;
53  var $static = false;
54 
56  {
57  $this->FieldRenderer($parent);
58  }
59 
60  function renderScript($field)
61  {
62  //TODO: Add Numeric handling script from ICT-in-Education Toolkit
63  }
64 
65  function renderField($field)
66  {
67  $value = htmlspecialchars($this->parent->data->get($field), ENT_QUOTES, 'UTF-8');
68 
69  $value = floatval($value);
70  // display with commas between thousands
71  $value = $this->format($field);
72  $disable = ($this->disable) ? "disabled='disabled'" : "";
73  $readonly = ($this->static) ? " readonly='readonly'" : "";
74  $allowneg = ($this->allowNegative) ? 1 : 0;
75  $autocomplete = ($this->autocomplete) ? "" : " autocomplete='off' ";
76  $onchange = ($this->onChange) ? "onchange='$this->onChange(this)'" : "";
77  $onKeyUp = ($this->onKeyUp) ? " onkeyup='{$this->onKeyUp}(this)'" : "";
78 
79  $this->_startField($field);
80 
81  echo "$&nbsp;<input id='{$this->parent->id}_{$field}'
82  type='text' name='$field' value='{$value}' $autocomplete onkeypress='return maskInput(event, $allowneg);'
83  $onchange $onKeyUp $disable $readonly size='{$this->size}'/>";
84 
85  $this->_endField($field);
86  }
87 
88  function renderSearchField($field, $mode = "equal")
89  {
90  if ($mode == "range")
91  {
92  $from = htmlspecialchars($this->parent->params->get($field, "from"), ENT_QUOTES, 'UTF-8');
93  $to = htmlspecialchars($this->parent->params->get($field, "to"), ENT_QUOTES, 'UTF-8');
94 
95  echo "<tr>\n";
96  $this->_printLabel($field);
97  echo "<td>From <input type='text' name='$field:from' value='{$from}' size='20'> to <input type='text' name='$field:to' value='{$to}' size='20'></td>\n";
98  echo "</tr>\n";
99  }
100  else
101  {
102  $value = htmlspecialchars($this->parent->params->get($field, $mode), ENT_QUOTES, 'UTF-8');
103  echo "<tr>\n";
104  $this->_printLabel($field);
105  echo "<td><input type='text' name='$field:$mode' value='{$value}' size='20'></td>\n";
106  echo "</tr>\n";
107  }
108  }
109 
110  function renderReadOnly($field)
111  {
112  $this->_startField($field);
113 
114  echo "$". $this->format($field);
115 
116  $this->_endField($field);
117  }
118 
119  // allow user to enter commas if they wish and strip them out before saving
120  function preProcess($field = "")
121  {
122  if ($this->parent->readOnlyForm || $this->parent->isReadOnly($field) || array_key_exists($field, $this->parent->hidden)) return "";
123 
124  $value= $_POST[$field];
125  $value = str_replace(",", "", $value);
126  $this->parent->data->set($field, $value);
127  $_POST[$field] = $value;
128  }
129 
130  function addValidatorsToForm($field, $required = false)
131  {
132  $label = isset($this->label) ? $this->label : $this->parent->prettifyFieldName($field);
133 
134  if($required)
135  {
136  $this->parent->validator->add(new RequiredCurrencyValidator($field, $label));
137  }
138 
139  }
140 
141  function renderOnSubmitHandler($field)
142  {
143  // JDG 3/2/2011., no js for readonly field
144  if ($this->parent->isReadOnly($field)) return "";
145 
146  echo "\tdocument.id('{$this->parent->id}_{$field}').value = rawNumber(document.id('{$this->parent->id}_{$field}').value);\n";
147  }
148 
149  function format($field)
150  {
151  return CurrencyTypeRenderer::format($this->parent->data->get($field), $this->template);
152  }
153 
154 }
155 
162 {
164  {
166  $this->template = 3;
167  }
168 }
169 ?>
Use Currency3 if you want to have 3 decimal places displayed.
Field renderer for currency data fields.
$onKeyUp
optional javascript callback on key release within field
$allowNegative
Whether user can enter a negative number.
preProcess($field="")
FieldRenderers can override this method to provide behavior that occurs prior to the saving of the pa...
renderScript($field)
FieldRenderers can override this method to provide any Javascript that their control requires for an ...
renderSearchField($field, $mode="equal")
FieldRenderers must override this method to provide the HTML implementation of the control displayed ...
$autocomplete
Whether the browser is allowed to show autocomplete for this field.
$disable
alternative to readonly; field can be reenabled thru javascript
renderOnSubmitHandler($field)
FieldRenderers can override this method to provide any Javascript that must be executed when the form...
$static
Render the field as a static (readonly) value, that is visible, non-editable and the value is submitt...
$onChange
optional javascript callback on data input change
renderField($field)
FieldRenderers must override this method to provide the HTML implementation of the control used to ed...
addValidatorsToForm($field, $required=false)
This method is called by the AutoForm to add any default input validators that are required by the Fi...
$template
how to format readonly - 2 decimal places
$size
size of the text field to be displayed (in characters)
static format($value, $template="")
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.
_printLabel($field, $colspan=1, $styles="", $annotation="")
Internal method to generate the HTML for the field label.
FieldRenderer($parent)
Constructor.
RequiredCurrencyField Validator.