Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
duration.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset_attribute.inc';
19 
37 {
38 
39 
40  var $units = Array(
41  'days' => 86400,
42  'hours' => 3600,
43  'minutes' => 60,
44  'seconds' => 1,
45  );
46 
47 
55  function Asset_Attribute_Duration($attributeid=0, $value=NULL)
56  {
57  $this->Asset_Attribute($attributeid, (int) $value);
58 
59  // Default display.
60  $this->_edit_params['biggest_units'] = 86400;
61  $this->_edit_params['smallest_units'] = 60;
62 
63  }//end constructor
64 
65 
75  function setEditParams(&$node)
76  {// TODO TOF TO TEST
77  if (!parent::setEditParams($node)) return FALSE;
78  $biggest = $this->units[(string)$node->attributes()->biggest_units];
79  $smallest = $this->units[(string)$node->attributes()->smallest_units];
80  $this->_edit_params['biggest_units'] = max($biggest, $smallest);
81  $this->_edit_params['smallest_units'] = min($biggest, $smallest);
82  return TRUE;
83 
84  }//end setEditParams()
85 
86 
93  function getUnitValues()
94  {
95  $unit_values = Array();
96  $biggest_units = array_get_index($this->_edit_params, 'biggest_units', $this->units['days']);
97  $smallest_units = array_get_index($this->_edit_params, 'smallest_units', $this->units['minutes']);
98  $remaining_value = $this->value;
99  foreach ($this->units as $current_name => $current_units) {
100  if ($current_units >= $smallest_units && $current_units <= $biggest_units) {
101  $current_value = (int) ($remaining_value / $current_units);
102  $unit_values[$current_name] = $current_value;
103  $remaining_value -= $current_value * $current_units;
104  }
105  }
106  return $unit_values;
107 
108  }//end getUnitValues()
109 
110 
120  function paint($prefix, $read_only=FALSE)
121  {
122  $biggest_units = array_get_index($this->_edit_params, 'biggest_units', $this->units['days']);
123  $smallest_units = array_get_index($this->_edit_params, 'smallest_units', $this->units['minutes']);
124 
125  // moves smallest unit to the lowest unit above the smallest unit that has a value
126  if ($read_only) {
127  $all_values = array_reverse($this->getUnitValues(), TRUE);
128  $set = FALSE;
129  foreach ($all_values as $unit_name => $value) {
130  if ($set) $smallest_units = $this->units[$unit_name];
131  if ($this->units[$unit_name] == $smallest_units) {
132  if ($value == 0) {
133  $set = TRUE;
134  } else {
135  break;
136  }
137  }
138  }
139  }
140  require_once SQ_LIB_PATH.'/html_form/html_form.inc';
141  $remaining_value = $this->value;
142  $last_value = 0;
143  $printed_any = FALSE;
144  foreach ($this->units as $current_name => $current_units) {
145  if ($current_units >= $smallest_units && $current_units <= $biggest_units) {
146  $current_value = (int) ($remaining_value / $current_units);
147  $remaining_value -= $current_value * $current_units;
148 
149  // modified below to only print duration units that have a value other than 0 when read-only is set
150  if ($smallest_units != $biggest_units) {
151  if (!$read_only) {
152  if ($current_units == $smallest_units) {
153  echo ' and ';
154  } else if ($current_units != $biggest_units) {
155  echo ', ';
156  }
157  } else {
158  if ($current_value != 0) {
159  if ($current_units == $smallest_units && $printed_any) {
160  echo ' and ';
161  } else if ($current_units != $biggest_units && $printed_any) {
162  echo ', ';
163  }
164  }
165  }
166  }
167  if ($read_only) {
168  if ($current_value != 0) {
169  if ($current_value == 1) {
170  // If singular, strip the plural 's' off the name
171  $current_name = substr($current_name, 0, -1);
172  }
173  echo ' '.$current_value.' '.$current_name;
174  $printed_any = TRUE;
175  }
176  } else {
177  text_box($prefix.'_'.$current_name, $current_value, 2, 0, FALSE, ' onkeypress="return checkDurationValue(this, event)" onblur="updateDurationValues(this)" style="width:4ex"');
178  echo '<label for="'.$prefix.'_'.$current_name.'"> '.$current_name.'</label>';
179  $printed_any = TRUE;
180  }
181  }//end if
182  }//end foreach unit
183 
184  }//end paint()
185 
186 
195  function process($prefix)
196  {
197  $biggest_units = array_get_index($this->_edit_params, 'biggest_units', $this->units['days']);
198  $smallest_units = array_get_index($this->_edit_params, 'smallest_units', $this->units['minutes']);
199 
200  if (!isset($_REQUEST[$prefix.'_'.array_search($biggest_units, $this->units)])) {
201  return FALSE;
202  }
203 
204  $res = 0;
205  foreach ($this->units as $name => $number) {
206  $res += array_get_index($_REQUEST, $prefix.'_'.$name, 0) * $number;
207  }
208 
209  $value = (int) $res;
210  if ($this->value != $value && $this->setValue($value)) {
211  $this->processed = TRUE;
212  } else {
213  $this->processed = FALSE;
214  }
215 
216  }//end process()
217 
218 
227  function validateValue(&$value)
228  {
229  $value = trim($value);
230 
231 
232  if ($value < 0) $value = $this->_default_value;
233 
234  $new_value = (int)$value;
235 
236  // we are comparing by string here, reason is that passing in a string as $value
237  // becomes comparing $value with zero which is TRUE. So instead we would be comparing
238  // it with a string '0', which is FALSE and what we want for it to invalidate.
239  // Only genuine ints will pass through.
240  if ((string)$new_value != $value) return FALSE;
241  return TRUE;
242 
243  }//end validateValue()
244 
245 
253  {
254  $value_array = $this->getUnitValues();
255  $value_xml = '';
256  foreach ($value_array as $unit => $unit_value) {
257  $value_xml .= '<'.$unit.'><![CDATA['.$unit_value.']]></'.$unit.'>';
258  }
259  $xml = '<value>'.$value_xml.'</value>';
260 
261  return $xml;
262 
263  }//end exportContentsToXML()
264 
265 
266 }//end class
267 
268 ?>