Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
float.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset_attribute.inc';
19 
38 {
39 
40 
48  function Asset_Attribute_Float($attributeid=0, $value=null)
49  {
50  $this->Asset_Attribute($attributeid, (float) $value, Array('decimal_places' => '2'));
51 
52  // set default edit parameters
53  $this->_edit_params['width'] = 5;
54 
55  }//end constructor
56 
57 
67  function setEditParams(&$node)
68  {
69  if (!parent::setEditParams($node)) return false;
70  $this->_edit_params['width'] = (int) $node->attributes()->width;
71 
72  return true;
73 
74  }//end setEditParams()
75 
76 
85  function paint($prefix, $read_only=false)
86  {
87  $v = number_format($this->value, $this->_params['decimal_places'], '.', '');
88 
89  if ($read_only) {
90  echo $v;
91  return;
92  }
93 
94  $js_validate = '';
95  if (isset($this->_params['range_upper'])){
96  $js_validate = ' else if (parseFloat(this.value) > parseFloat('.(float)$this->_params['range_upper'].')) { this.value = this.__sq_init_value;}';
97  }
98  if (isset($this->_params['range_lower'])){
99  $js_validate .= ' else if (parseFloat(this.value) < parseFloat('.(float)$this->_params['range_lower'].')) { this.value = this.__sq_init_value;}';
100  }
101 
102  require_once SQ_LIB_PATH.'/html_form/html_form.inc';
103  text_box($prefix, $v, $this->_edit_params['width'], 0, false, 'onFocus="javascript: this.__sq_init_value = this.value;" onChange="javascript: if (isNaN(parseFloat(this.value))) { this.value = this.__sq_init_value; alert(js_translate(\'cannot_accept_non_digit_characters\')); } '.$js_validate.' else {this.value = number_format(this.value, '.$this->_params['decimal_places'].', \'.\', \'\'); } "');
104 
105  }//end paint()
106 
107 
116  function process($prefix)
117  {
118  if (!isset($_REQUEST[$prefix])) return false;
119  $value = (float) $_REQUEST[$prefix];
120  if ($this->value != $value && $this->setValue($value)) {
121  $this->processed = true;
122  } else {
123  $this->processed = false;
124  }
125 
126  }//end process()
127 
128 
137  function validateValue(&$value)
138  {
139  $value = trim($value);
140  $new_value = (float)$value;
141 
142  // we are comparing by string here, reason is that passing in a string as $value
143  // becomes comparing $value with zero which is TRUE. So instead we would be comparing
144  // it with a string '0', which is FALSE and what we want for it to invalidate.
145  // Only genuine ints will pass through.
146  if ((string)$new_value != $value) return false;
147 
148 
149  if(isset($this->_params['range_upper'])) {
150  (float)$range_upper = $this->_params['range_upper'];
151  if ($new_value > $range_upper) {
152  return FALSE;
153  }
154  }
155 
156 
157  if(isset($this->_params['range_lower'])) {
158  (float)$range_lower = $this->_params['range_lower'];
159  if ($new_value < $range_lower) {
160  return FALSE;
161  }
162  }
163 
164  return TRUE;
165 
166  }//end validateValue()
167 
168 
169 }//end class
170 
171 ?>