Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
int.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset_attribute.inc';
19 
40 {
41 
42 
50  function Asset_Attribute_Int($attributeid=0, $value=NULL)
51  {
52  $this->Asset_Attribute($attributeid, $value);
53 
54  // apply default params where necessary
55  if (!isset($this->_params['allow_negative'])) {
56  $this->_params['allow_negative'] = TRUE;
57  }
58  if (!isset($this->_params['allow_empty'])) {
59  $this->_params['allow_empty'] = FALSE;
60  }
61  if (!isset($this->_params['range_lower'])) {
62  $this->_params['range_lower'] = FALSE;
63  }
64  if (!isset($this->_params['range_upper'])) {
65  $this->_params['range_upper'] = FALSE;
66  }
67 
68  // set default edit parameters
69  $this->_edit_params['width'] = 0;
70 
71  }//end constructor
72 
73 
83  function setEditParams(&$node)
84  {
85  if (!parent::setEditParams($node)) return FALSE;
86  $this->_edit_params['width'] = (int) $node->attributes()->width;
87 
88  return TRUE;
89 
90  }//end setEditParams()
91 
92 
102  function paint($prefix, $read_only=FALSE)
103  {
104  // we want to retain empty values retrieved from the DB
105  // Postgres will return an empty string and Oracle will return a NULL value
106  if (($this->value === '') || is_null($this->value)) {
107  $this->value = '';
108  } else {
109  $this->value = (int)$this->value;
110  }
111 
112  if ($read_only) {
113  echo $this->value;
114  return;
115  }
116 
117  $range_lower = $this->_params['range_lower'];
118  $range_upper = $this->_params['range_upper'];
119 
120  // We don't want to inforce a range on the integer field.
121  if (!is_int($range_lower)) $range_lower = NULL;
122  if (!is_int($range_upper)) $range_upper = NULL;
123 
124  $allow_negative = array_get_index($this->_params, 'allow_negative', TRUE);
125 
126  require_once SQ_LIB_PATH.'/html_form/html_form.inc';
127  int_text_box($prefix, $this->value, $allow_negative, $this->_edit_params['width'], $range_lower, $range_upper, 0, FALSE, '');
128 
129  }//end paint()
130 
131 
140  function process($prefix)
141  {
142  if (!isset($_REQUEST[$prefix])) return FALSE;
143  $value = $_REQUEST[$prefix];
144  if ($value !== '') $value = (int)$value;
145  if ($this->value !== $value && $this->setValue($value)) {
146  $this->processed = TRUE;
147  } else {
148  $this->processed = FALSE;
149  }
150 
151  }//end process()
152 
153 
162  function validateValue(&$value)
163  {
164  // empty string is OK if enabled
165  if (($value === '') || is_null($value)) {
166  $allow_empty = array_get_index($this->_params, 'allow_empty', TRUE);
167  if ($allow_empty && (($value === '') || is_null($value))) {
168  return TRUE;
169  } else {
170  // we'll be nice and turn this into int zero rather than erroring
171  $value = (int)$value;
172  }
173  }
174 
175  $range_lower = array_get_index($this->_params, 'range_lower', FALSE);
176  if (!empty($range_lower) && ($value < $range_lower)) {
177  return FALSE;
178  }
179 
180  $range_upper = array_get_index($this->_params, 'range_upper', FALSE);
181  if (!empty($range_upper) && ($value > $range_upper)) {
182  return FALSE;
183  }
184 
185  // unless one of the range checks failed, zero is OK
186  if ($value === 0) return TRUE;
187  if ($value === '0') return TRUE;
188 
189  // anything else that equates to int zero is not OK
190  if ($value == 0) return FALSE;
191 
192  $allow_negative = array_get_index($this->_params, 'allow_negative', TRUE);
193  if (!$allow_negative && $value < 0) {
194  return FALSE;
195  }
196 
197  return TRUE;
198 
199  }//end validateValue()
200 
201 
202 }//end class
203 
204 ?>