Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
datetime_field.inc
1 <?php
17 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
18 
36 {
37 
42  var $prefix='';
43 
48  var $value='';
49 
87  var $parameters='';
88 
89 
98  function DateTime_Field($prefix, &$value, &$parameters)
99  {
100  $this->prefix =& $prefix;
101  $this->value =& $value;
102  $this->parameters =& $parameters;
103 
104  }//end constructor
105 
106 
115  function setValue(&$value)
116  {
117  $this->value =& $value;
118 
119  }//end setValue()
120 
121 
130  function setParameters(&$parameters)
131  {
132  $this->parameters =& $parameters;
133 
134  }//end setParameters()
135 
136 
145  function &getParam($code='')
146  {
147  if (isset($this->parameters[$code])) {
148  return $this->parameters[$code];
149  }
150  $res = NULL;
151  switch ($code) {
152  // default values
153  case 'min':
154  $res = '0000-00-00 00:00:00';
155  break;
156  case 'max':
157  $res = '9999-12-31 23:59:59';
158  break;
159  }
160  return $res;
161 
162  }//end getParam()
163 
164 
171  function printField()
172  {
173  $show =& $this->getParam('show');
174  $null =& $this->getParam('null');
175  if (!is_array($show)) $show = Array();
176  if (!is_array($null)) $null = Array();
177  ?>
178 
179  <script type="text/javascript">
180  //<![CDATA[
181  if (typeof adjustDate != "function") {
182  // function to enable + and - keys on a field
183  adjustDate = function(input, evt)
184  {
185  var key = evt.keyCode;
186  var value = parseInt(input.value * 1);
187  if (key == 107 || key == 109) {
188  value += (key == 109)?-1:1;
189  if (value != 0) {
190  input.value = value;
191  }
192  event.returnValue = false;
193  event.cancelBubble = true;
194  return false;
195  }
196  }
197  }
198  //]]>
199  </script>
200 
201  <?php
202  echo '<input type="hidden" name="'.$this->prefix.'show" value="'.implode('', $show).'" />';
203  $this->prefix .= 'value';
204  if ($this->getParam('allow_circa')) {
205  combo_box($this->prefix.'[c]', Array('0'=>'','1'=>'circa.'), FALSE, $this->getUnit('c'));
206  }
207  foreach (Array('d','m','y','h','i','s','a') as $t) {
208  $u = $this->getUnit($t);
209 
210  if (in_array($t, $show)) {
211  $this->inputBox($t, $this->prefix, $u, in_array($t, $null));
212  } else if ($t != 'a') {
213  echo '<input type="hidden" name="'.$this->prefix.'['.$t.']" value="'.$u.'" />';
214  }
215  if ($t == 'y') echo ' ';
216  if ($t == 'h' && in_array('h', $show) && in_array('i', $show)) {
217  echo ':';
218  }
219  if ($t == 'i' && in_array('i', $show) && in_array('s', $show)) {
220  echo ':';
221  }
222  }
223 
224  }//end printField()
225 
226 
238  function inputBox($u, $prefix, &$value, $allow_null=FALSE)
239  {
240  $input_style =& $this->getParam('style');
241  $extras =& $this->getParam('extras');
242  $increments =& $this->getParam('increment');
243  $min_array =& $this->getParam('min');
244  $max_array =& $this->getParam('max');
245 
246  $min = (int) $this->getUnit($u, $min_array);
247  $max = (int) $this->getUnit($u, $max_array);
248 
249  $diff = Array();
250  foreach (Array('y','m','d','h','i','s') as $diff_unit) {
251  $diff[$diff_unit] = $this->getUnit($diff_unit, $max_array) - $this->getUnit($diff_unit, $min_array);
252  }
253 
254  if ($allow_null && (!strlen($value) || $value == '--' || $value == '----')) {
255  $value = '';
256  } else if ($u != 'a') {
257  $value = (int) $value;
258  }
259 
260  $increment = 1;
261 
262  if (!empty($increments)) $increment = $increments[$u];
263 
264  $options = Array();
265  if (($u == 'a') || ($input_style[$u] == 's')) { // select box
266  if ($allow_null) {
267  $options = Array('--' => '');
268  }
269  }
270 
271  // calculate min, max and our selectable options
272  switch ($u) {
273  case 'y':
274  for ($i = (int) $min; $i <= $max; $i += $increment) {
275  $options[$i] = $i;
276  }
277  break;
278  case 'm':
279  // more than 1 years' difference between min and max value
280  if (($diff['y']*12) + $diff['m'] >= 12) {
281  $min = 1;
282  $max = 12;
283  // not more than 1 years' difference but rolls over a year boundary
284  // (eg. we have a range such as Aug 04..Jan 05)
285  } else if ($diff['y'] == 1) {
286  // take care of (Jan...$max)
287  for ($i = 1; $i <= $max; $i += $increment) {
288  $options[$i] = $this->shortMonth($i);
289  }
290  // now make usual addition take care of ($min...Dec)
291  $max = 12;
292  }
293  for ($i = (int) $min; $i <= $max; $i += $increment) {
294  $options[$i] = $this->shortMonth($i);
295  }
296  break;
297  case 'd':
298  // more than 1 month difference between min and max value
299  if (($diff['y'] > 0) || ($diff['m'] > 1) || (($diff['m'] == 1) && ($diff['d'] == 0)) ) {
300  $min = 1;
301  $max = 31;
302  // not more than 1 day difference but rolls over a day boundary
303  // (eg. range such as 6d 21h to 7d 05h)
304  } else if ($diff['m'] == 1) {
305  // take care of (1...$max)
306  for ($i = 1; $i <= $max; $i += $increment) {
307  $options[$i] = $i;
308  }
309  // now make usual addition take care of ($min...$end)
310  // argh, we need to work out how many days in the rolled-over month!
311  $mth = (int) $this->getUnit('m', $max_array);
312  $yr = (int) $this->getUnit('y', $max_array);
313  $lastday = gmmktime(0, 0, 0, $mth, 0, $yr); // get the last day of the minimum month
314  $max = (int)date('d', $lastday);
315  }
316  for ($i = (int) $min; $i <= $max; $i += $increment) {
317  $options[$i] = $i;
318  }
319  break;
320  case 'h':
321  // more than 1 day difference between min and max value
322  if (($diff['y'] > 0) || ($diff['m'] > 0) || ($diff['d'] > 1) || (($diff['d'] == 1) && ($diff['h'] == 0)) ) {
323  if (in_array('a', $this->getParam('show'))) {
324  $min = 1;
325  $max = 12;
326  } else {
327  $min = 0;
328  $max = 23;
329  }
330  // not more than 1 day difference but rolls over a day boundary
331  // (eg. range such as 6d 21h to 7d 05h)
332  } else if ($diff['d'] == 1) {
333  // take care of (00...$max)
334  for ($i = 0; $i <= $max; $i += $increment) {
335  $options[$i] = sprintf('%02d', $i);
336  }
337  // now make usual addition take care of ($min...23)
338  $max = (in_array('a', $this->getParam('show'))) ? 12 : 23;
339  }
340  for ($i = (int) $min; $i <= $max; $i += $increment) {
341  $options[$i] = sprintf('%02d', $i);
342  }
343  break;
344  case 'i':
345  // more than 1 hours' difference between min and max value
346  if (($diff['y'] > 0) || ($diff['m'] > 0) || ($diff['d'] > 0) || ($diff['h'] > 1) || (($diff['h'] == 1) && ($diff['i'] == 0))) {
347  $min = 0;
348  $max = 59;
349  // not more than 1 hours' difference but rolls over a hour boundary
350  // (eg. range such as 03:40 to 04:08)
351  } else if ($diff['h'] == 1) {
352  // take care of (00...$max)
353  for ($i = 0; $i <= $max; $i += $increment) {
354  $options[$i] = sprintf('%02d', $i);
355  }
356  // now make usual addition take care of ($min...59)
357  $max = 59;
358  }
359  for ($i = (int) $min; $i <= $max; $i += $increment) {
360  $options[$i] = sprintf('%02d', $i);
361  }
362  break;
363  case 's':
364  // more than 1 minutes' difference between min and max value
365  if (($diff['y'] > 0) || ($diff['m'] > 0) || ($diff['d'] > 0) || ($diff['h'] > 0) || ($diff['i'] > 1) || (($diff['i'] == 1) && ($diff['s'] == 0))) {
366  $min = 0;
367  $max = 59;
368  // not more than 1 minutes' difference but rolls over a minute boundary
369  // (eg. range such as 03:10:50 to 03:11:20)
370  } else if ($diff['i'] == 1) {
371  // take care of (00...$max)
372  for ($i = 0; $i <= $max; $i += $increment) {
373  $options[$i] = sprintf('%02d', $i);
374  }
375  // now make usual addition take care of ($min...59)
376  $max = 59;
377  }
378  for ($i = (int) $min; $i <= $max; $i += $increment) {
379  $options[$i] = sprintf('%02d', $i);
380  }
381  break;
382  case 'a':
383  // more than 1 day difference, or less than 1 day but crossing a day boundary, or less than 1 day but crossing midday
384  if ((($diff['y'] > 0) || ($diff['m'] > 0) || ($diff['d'] > 1) || (($diff['d'] == 1) && ($diff['h'] == 0)) ) || ($diff['d'] == 1) || (($this->getUnit('h', $min_array) < 12) && ($this->getUnit('h', $max_array) >= 12))) {
385  $options = Array('am'=>'am', 'pm'=>'pm');
386  // just a morning or afternoon
387  } else {
388  $options = ($min < 12) ? Array('am'=>'am') : Array('pm'=>'pm');
389  }
390  break;
391  }//end switch
392 
393  // print which ever form of input is desired
394  if (($u == 'a') || ($input_style[$u] == 's')) {
395  if (!isset($extras[$u])) $extras[$u] = '';
396  if (!stristr($extras[$u], 'id=')) $extras[$u] .= ' id="'.$prefix.'_'.$u.'"';
397  // select box
398  combo_box($prefix.'['.$u.']', $options, FALSE, $value, 0, $extras[$u]);
399  return;
400  } else {
401  // plain text
402  $size = (($u=='y') ? 4 : 2);
403 
404  // minutes value should always be 2 digits
405  if (($value !== '') && ($u == 'i')) {
406  $value = sprintf('%02d', $value);
407  }
408  if (!isset($extras[$u])) $extras[$u] = '';
409  if (!stristr($extras[$u], 'id=')) {
410  $adjustExtra = 'onkeydown="adjustDate(this, event)" onfocus="this.select()" id="'.$prefix.'_'.$u.'"';
411  } else {
412  $adjustExtra = 'onkeydown="adjustDate(this, event)" onfocus="this.select()"';
413  }
414  int_text_box($prefix.'['.$u.']', $value, FALSE, $size, $min, $max, $size, FALSE, $allow_null, $extras[$u].' '.$adjustExtra);
415  return;
416  }
417  echo '<input type="hidden" name="'.$prefix.'['.$u.']" value="'.$value.'" '.$extras[$u].' />';
418 
419  }//end inputBox()
420 
421 
431  function getUnit($unit, $value='')
432  {
433  if (empty($value)) {
434  $value = $this->value;
435  $use_am_pm = in_array('a', $this->getParam('show'));
436  } else {
437  $use_am_pm = FALSE;
438  }
439 
440  // separate value into date and time
441  $ar_value = explode(' ', $value);
442  $date = (isset($ar_value[0])) ? $ar_value[0] : '';
443  $time = (isset($ar_value[1])) ? $ar_value[1] : '';
444  $c = (isset($ar_value[2])) ? $ar_value[2] : '';
445 
446  // separate date into year, month and day
447  if (strlen($date) == 10) {
448  $y = substr($date, 0, 4);
449  $m = substr($date, 5, 2);
450  $d = substr($date, 8, 2);
451  } else {
452  $ar_date = explode('-', $date);
453  $y = (isset($ar_date[0])) ? $ar_date[0] : '';
454  $m = (isset($ar_date[1])) ? $ar_date[1] : '';
455  $d = (isset($ar_date[2])) ? $ar_date[2] : '';
456  }
457 
458  // separate time into hour, minute, second
459  $ar_time = explode(':', $time);
460  $a = (isset($ar_time[0]) && ($ar_time[0] >= 12)) ? 'pm' : 'am';
461  $h = (isset($ar_time[0])) ? ($use_am_pm ? (((($ar_time[0] % 12) + 11) % 12) + 1) : $ar_time[0]) : '';
462  $i = (isset($ar_time[1])) ? $ar_time[1] : '';
463  $s = (isset($ar_time[2])) ? $ar_time[2] : '';
464 
465  return $$unit;
466 
467  }//end getUnit()
468 
469 
479  function shortMonth($m='', $invert=FALSE)
480  {
481  $months = Array('','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
482  if ($invert) {
483  $months = array_flip($months);
484  $m = ucfirst($m);
485  }
486  return $months[$m];
487 
488  }//end shortMonth()
489 
490 
500  function longMonth($m='', $invert=FALSE)
501  {
502  $months = Array('','January','February','March','April','May','June','July','August','September','October','November','December');
503  if ($invert) {
504  $months = array_flip($months);
505  $m = ucfirst($m);
506  }
507  return $months[$m];
508 
509  }//end longMonth()
510 
511 
518  function processField()
519  {
520  if (!isset($_REQUEST[$this->prefix.'value'])) {
521  return FALSE;
522  }
523  $input = $_REQUEST[$this->prefix.'value'];
524  $input = $this->unitsArrayToStr($input);
525  $validated_value = $this->validateValue($input);
526  $this->value = $validated_value;
527  return TRUE;
528 
529  }//end processField()
530 
531 
540  function validateValue($value)
541  {
542  if (!is_array($value)) {
543  $value = trim($value);
544  $l = strlen($value);
545  $value = preg_replace('/^c(irc[a]?)?\.?[\s]*/i','', $value);
546  $c = FALSE;
547  if (($new_l = strlen($value)) != $l) $c = TRUE;
548  if (!preg_match("/^\-?[0-9\-]{4}-[0-9\-]{2}-[0-9\-]{2} ([0-9\-]{2}\:?){3}( [01]?)?$/", $value)) {
549  // Not "Correct" format
550  if (preg_match("/^\-?[0-9]{4}$/", $value)) { // Just a year?
551  $value .= '------ --:--:--';
552  } else if (preg_match("/^[0-9]+$/", $value)) { // Timestamp?
553  $value = date('Y-m-d H:i:s', $value);
554  } else {
555  $show =& $this->getParam('show');
556  // Okay lets try to parse this thing.
557  $v = Array('y'=>-1,'m'=>-1,'d'=>-1,'h'=>-1,'i'=>-1,'s'=>-1);
558  // Dates
559  if (preg_match("/([0-9]{1,2})[\-\/]([0-9]{1,2})[\-\/]([0-9]{2,4})([^0-9].*|)$/", $value, $matches) && ($matches[0] == $value)) {
560  // Probably "dd/mm/yyyy" or "mm/dd/yyyy"
561  if ($this->getParam('standard') == 'us') {
562  $v['d'] = $matches[2]; $v['m'] = $matches[1];
563  } else {
564  $v['d'] = $matches[1]; $v['m'] = $matches[2];
565  }
566  $v['y'] = $matches[3];
567  if (strlen($v['y']) <= 2) {
568  $v['y'] += (($v['y'] < 20) ? 2000 : 1900);
569  }
570  } else if (preg_match("/\-?([0-9]{2,4})[\-\/]([0-9]{1,2})[\-\/]([0-9]{1,2})([^0-9].*|)$/", $value, $matches) && ($matches[0] == $value)) {
571  // Probably a ISO 8601 / ISO 9075 [SQL92/99] date ("yyyy-mm-dd")
572  $v['d'] = $matches[3];
573  $v['m'] = $matches[2];
574  $v['y'] = $matches[1];
575  if (strlen($v['y']) <= 2) {
576  $v['y'] += (($v['y'] < 20) ? 2000 : 1900);
577  }
578  } else if (preg_match("/([0-9]{1,2})[\-\/]([0-9]{1,2})([^0-9].*|)$/", $value, $matches)) {
579  // Probably dd/mm
580  if ($this->getParam('standard') == 'us') {
581  $v['d'] = $matches[2]; $v['m'] = $matches[1];
582  } else {
583  $v['d'] = $matches[1]; $v['m'] = $matches[2];
584  }
585  } else if (preg_match("/([0-9]{1,2})[\-\/]([0-9]{4})([^0-9].*|)$/", $value, $matches)) {
586  // Probably mm/yy
587  $v['m'] = $matches[1];
588  $v['y'] = $matches[2];
589  } else {
590  // Maybe a more verbal date.
591  if (preg_match("/([0-9]{1,2})?([\s\,]*)(jan(uary)?|feb(ruary)?|mar(ch)?|apr(il)?|may|jun(e)?|jul(y)?|aug(ust)?|sep(tember)?|oct(ober)?|nov(ember)?|dec(ember)?)([\s\,]*)([0-9]{1,2})?([^0-9].*|)$/i", $value, $matches)) {
592  // textural month?
593  if (strlen($tmp = $matches[3]) == 3) {
594  $v['m'] = $this->shortMonth($tmp,1);
595  } else {
596  $v['m'] = $this->longMonth($tmp,1);
597  }
598  // maybe the day?
599  if ($matches[1]) {
600  $v['d'] = $matches[1];
601  } else if ($matches[16]) {
602  $v['d'] = $matches[16];
603  }
604  }
605  if (preg_match('/([0-9]{4})([^0-9].*|)$/', $value, $matches)) {
606  // four digit year?
607  $v['y'] = $matches[1];
608  }
609  if ($v['d'] == -1 && preg_match('/([0-9]{1,2})(st|nd|rd|th)/i', $value, $matches)) {
610  // 1st, 2nd, 3rd, 4th etc
611  $v['d'] = $matches[1];
612  }
613 
614  // Times !
615  if (preg_match("/([0-9]{1,2})[\:\.]([0-9]{1,2})[\:\.]([0-9]{1,2})([^0-9].*|)$/", $value, $matches)) {
616  $v['h'] = $matches[1];
617  $v['i'] = $matches[2];
618  $v['s'] = $matches[3];
619  } else if (preg_match('/([0-9]{1,2})[\:\.]([0-9]{1,2})([^0-9].*|)$/', $value, $matches)) {
620  if (in_array('s', $show) && !in_array('h', $show)) {
621  $v['i'] = $matches[1];
622  $v['s'] = $matches[2];
623  } else {
624  $v['h'] = $matches[1];
625  $v['i'] = $matches[2];
626  }
627  }
628  if (preg_match('/([0-9]{1,2})([\s]*)(p\.?m\.?|a\.?m\.?)/i', $value, $matches)) {
629  if ($v['h'] == -1) $v['h'] = $matches[1];
630  if (strtolower($matches[3][0]) == 'p') {
631  if ($v['h'] <= 12) $v['h'] += 12;
632  } else {
633  if ($v['h'] == 12) $v['h'] -= 12;
634  }
635  }
636  }//end else a more verbal form
637 
638  $value = $this->unitsArrayToStr($v);
639 
640  }//end else not timestamp
641 
642  }//end if format matches
643 
644  $value = $this->strToUnitsArray($value);
645  if ($c) $value['c'] = 1;
646 
647  }//end if value not array
648 
649  // Now we can assume we have an array of units values to verify
650  $min =& $this->getParam('min');
651  $max =& $this->getParam('max');
652  $value_str = $this->unitsArraytoStr($value);
653  if ($value_str != '---------- --:--:--') {
654  $value_substr = Array(
655  'y' => 0,
656  'm' => 5,
657  'd' => 8,
658  'h' => 11,
659  'i' => 14,
660  's' => 17,
661  );
662  foreach (array_keys($value_substr) as $value_part) {
663  $substr_length = ($value_part == 'y') ? 4 : 2;
664  if ($value[$value_part] == -1) {
665  $min = substr_replace($min, str_repeat('-', $substr_length), $value_substr[$value_part], $substr_length);
666  $max = substr_replace($max, str_repeat('-', $substr_length), $value_substr[$value_part], $substr_length);
667  }
668  }
669 
670  if (strcmp($value_str, $min) < 0) {
671  $value = $this->strToUnitsArray($min);
672  } else if (strcmp($value_str, $max) > 0) {
673  $value = $this->strToUnitsArray($max);
674  }
675  }
676 
677  // DO CIRCA STUFF HERE ON DOWN
678  $validate_day_month = ($value['y'] >= 1970) && ($value['y'] < 2038) && ($value['m'] >= 0) && ($value['d'] >= 0); // PHP limit
679  $value = $this->unitsArrayToStr($value);
680 
681  // ensures valid
682  if ($validate_day_month) {
683  $timestamp = mktime(
684  (int) substr($value,11,2),
685  (int) substr($value,14,2),
686  (int) substr($value,17,2),
687  (int) substr($value,5,2),
688  (int) substr($value,8,2),
689  (int) substr($value,0,4)
690  );
691 
692  $old_value = $value;
693 
694  if ((strlen($old_value) == 19) && false === strpos($old_value, '--')) {
695  // compare the whole lot
696  $value = date('Y-m-d H:i:s', $timestamp);
697  if (strcmp($value, $old_value) != 0) {
698  trigger_localised_error('SYS0311', E_USER_NOTICE, $old_value);
699  }
700  } else if (false === strpos($old_value, '----------')) {
701  // only compare the date bit
702  $value = date('Y-m-d ', $timestamp).substr($old_value, 11);
703  if (strcmp(substr($value, 0, 10), substr($old_value, 0, 10)) != 0) {
704  trigger_localised_error('SYS0312', E_USER_NOTICE, substr($old_value, 0, 10));
705  }
706  }
707 
708  }
709 
710  if (strlen($value) > 20 && !$this->getParam('allow_circa')) {
711  $value = substr($value, 20);
712  }
713 
714  return $value;
715 
716  }//end validateValue()
717 
718 
727  function strToUnitsArray($value)
728  {
729  $r = Array();
730  $r['y'] = ((($i = (int) ($s = substr($value,0,4))) || ($s{0} != '-' && strlen($s))) ? $i : -1);
731  $r['m'] = ((($i = (int) ($s = substr($value,5,2))) || ($s{0} != '-' && strlen($s))) ? $i : -1);
732  $r['d'] = ((($i = (int) ($s = substr($value,8,2))) || ($s{0} != '-' && strlen($s))) ? $i : -1);
733  $r['h'] = ((($i = (int) ($s = substr($value,11,2))) || ($s{0} != '-' && strlen($s))) ? $i : -1);
734  $r['i'] = ((($i = (int) ($s = substr($value,14,2))) || ($s{0} != '-' && strlen($s))) ? $i : -1);
735  $r['s'] = ((($i = (int) ($s = substr($value,17,2))) || ($s{0} != '-' && strlen($s))) ? $i : -1);
736  if ($this->getParam('allow_circa')) {
737  $r['c'] = (substr($value, 20, 1) ? 1 : 0);
738  }
739  return $r;
740 
741  }//end strToUnitsArray()
742 
743 
752  function unitsArrayToStr($v)
753  {
754  // convert from 12hr to 24hr if necessary
755  if (isset($v['a']) && !empty($v['a'])) {
756  $v['h'] = ($v['h'] % 12) + (($v['a'] == 'pm') ? 12 : 0);
757  }
758  $r = (!isset($v['y']) || ($v['y'] < 0 || !strlen($v['y']) || $v['y']{0} == '-') ? '----' : sprintf('%04d', $v['y'])).'-'
759  .(!isset($v['m']) || ($v['m'] < 0 || !strlen($v['m']) || $v['m']{0} == '-') ? '--' : sprintf('%02d', $v['m'])).'-'
760  .(!isset($v['d']) || ($v['d'] < 0 || !strlen($v['d']) || $v['d']{0} == '-') ? '--' : sprintf('%02d', $v['d'])).' '
761  .(!isset($v['h']) || ($v['h'] < 0 || !strlen($v['h']) || $v['h']{0} == '-') ? '--' : sprintf('%02d', $v['h'])).':'
762  .(!isset($v['i']) || ($v['i'] < 0 || !strlen($v['i']) || $v['i']{0} == '-') ? '--' : sprintf('%02d', $v['i'])).':'
763  .(!isset($v['s']) || ($v['s'] < 0 || !strlen($v['s']) || $v['s']{0} == '-') ? '--' : sprintf('%02d', $v['s']));
764  if ($this->getParam('allow_circa')) {
765  $r .= ' '.(isset($v['c']) && $v['c'] ? '1' : '0');
766  }
767  return $r;
768 
769  }//end unitsArrayToStr()
770 
771 
780  function format($format=NULL)
781  {
782  if (is_null($format)) {
783  $print_format =& $this->getParam('print_format');
784  $format = $print_format;
785  }
786 
787  // We need to strip certain elements out of the format if we don't have enough
788  // information about our datetime.
789  // A mapping of which format elements are dependant on which time elements.
790  $dep=Array('h' => 'aABcgGhHrU', 'd' => 'cdDIjlOrSUwWzZ', 'm' => 'cDFIlmMnOrtUwWzZ', 'y' => 'cDlLrtUwWYyz', 'i' => 'cirU', 's' => 'crsU');
791  $es = '';
792  foreach ($v = $this->strToUnitsArray($this->value) as $u => $n) {
793  if ($n < 0) {
794  if ($u == 'm' || $u == 'd') {
795  $$u = 1;
796  } else {
797  $$u = 0;
798  }
799  $es .= $dep[$u];
800  } else {
801  $$u = $n;
802  }
803  }
804 
805  // Certain things we can't work out if the year is before 1970
806  $real_y = 0;
807  if (!isset($c)) {
808  $c = $this->getParam('allow_circa');
809 
810  }//end if
811  if ($y < 1970 && !stristr($es,'y')) {
812  $es .= 'DIlLrUwWzZ';
813  $real_y = $y;
814  $y = 1980;
815  $format = str_replace('Y', '||||', $format);
816  $format = str_replace('y', '~~', $format);
817  }
818  if ($es) {
819  $regexp = "/[\\s]*[^a-zA-Z\\s]*[$es][^a-zA-Z\\|\{4\}\\~\{2\}\\s]*[\\s]*/";
820  $format = preg_replace($regexp, ' ', $format);
821  if ($real_y) {
822  $format = str_replace('||||', $real_y, $format);
823  $format = str_replace('~~', $real_y%100, $format);
824  }
825  }
826 
827  // Circa
828  $format = str_replace('C', ($c ? '~' : ''), $format);
829 
830  $format = date($format,mktime($h, $i, $s, $m, $d, $y));
831 
832  // Circa
833  $format = str_replace('~', 'circa.', $format);
834 
835  return $format;
836 
837  }//end format()
838 
839 
846  function printMinField()
847  {
848  $p = Array(
849  'min' => '0000-01-01 00:00:00',
850  'max' => '9999-12-31 23:59:59',
851  'show' => Array('y','m','d','h','i','s'),
852  'increment' => Array(),
853  'style' => $this->getParam('style'),
854  );
855  $p['style']['y'] = 't';
856  $min_val =& $this->getParam('min');
857  $d = new DateTime_Field($this->prefix.'_min', $min_val, $p);
858  $d->printField();
859 
860  }//end printMinField()
861 
862 
869  function processMinField()
870  {
871  $p = Array(
872  'min' => '0000-01-01 00:00:00',
873  'max' => '9999-12-31 23:59:59',
874  'show' => Array('y','m','d','h','i','s'),
875  'increment' => Array(),
876  'style' => $this->getParam('style'),
877  );
878  $min =& $this->getParam('min');
879  $d = new DateTime_Field($this->prefix.'_min', $min, $p);
880  $d->processField();
881 
882  }//end processMinField()
883 
884 
891  function printMaxField()
892  {
893  $p = Array(
894  'min' => '0000-01-01 00:00:00',
895  'max' => '9999-12-31 23:59:59',
896  'show' => Array('y','m','d','h','i','s'),
897  'increment' => Array(),
898  'style' => $this->getParam('style'),
899  );
900  $p['style']['y'] = 't';
901  $max_val =& $this->getParam('max');
902  $d = new DateTime_Field($this->prefix.'_max', $max_val, $p);
903  $d->printField();
904 
905  }//end printMaxField()
906 
907 
914  function processMaxField()
915  {
916  $p = Array(
917  'min' => '0000-01-01 00:00:00',
918  'max' => '9999-12-31 23:59:59',
919  'show' => Array('y','m','d','h','i','s'),
920  'increment' => Array(),
921  'style' => $this->getParam('style'),
922  );
923  $max =& $this->getParam('max');
924  $d = new DateTime_Field($this->prefix.'_max', $max, $p);
925  $d->processField();
926 
927  }//end processMaxField()
928 
929 
938  function printEditOptions($show=Array())
939  {
940  if (empty($show)) $show = Array('s','n','c','t'); // default set of fields to show
941 
942  $us = Array('d'=>'Day','m'=>'Month','y'=>'Year','h'=>'Hour','i'=>'Min.','s'=>'Sec.');
943  $s =& $this->getParam('show');
944  $n =& $this->getParam('null');
945  $y =& $this->getParam('style');
946  $c =& $this->getParam('allow_circa');
947  $t =& $this->getParam('standard');
948  ?>
949 
950  <table cellspacing="0" cellpadding="2" border="1">
951  <tr><td>
952  <?php
953  if (in_array('c', $show)) {
954  combo_box($this->prefix.'_allow_circa', Array(''=>'','1'=>'Allow "circa."'), FALSE, $c, 'class="smallprint"');
955  echo '<br />';
956  }
957  if (in_array('t', $show)) {
958  combo_box($this->prefix.'_standard', Array('int'=>'Intl. (dd/mm/yyyy)','us'=>'US (mm/dd/yyyy)'), $t, 'class="fineprint"');
959  }
960  ?>
961  </td>
962  <?php
963  foreach ($us as $k=>$v) {
964  ?>
965  <td align=center><b><?php echo $v; ?></b></td>
966  <?php }//end foreach
967  ?>
968  </tr>
969 
970  <?php
971  // checking if we should show field selection checkboxes
972  if (in_array('s', $show)) {
973  ?>
974  <tr>
975  <td><b>Show</b></td>
976  <?php
977  foreach ($us as $k => $v) {
978  ?>
979  <td align="center"><input type="checkbox" name="<?php echo $this->prefix; ?>_show[]" value=<?php echo $k; ?> <?php echo ((in_array($k, $s)) ? 'checked="checked"' : ''); ?>></b>
980  </td>
981  <?php }//end foreach
982  ?>
983  </tr>
984  <?php }//end if in_array
985  ?>
986 
987  <?php
988  // checking if we should show blank-allowed checkboxes
989  if (in_array('n', $show)) {
990  ?>
991  <tr>
992  <td><b>Allow blank</b></td>
993  <?php
994  foreach ($us as $k=>$v) {
995  ?><td align="center"><input type="checkbox" name="<?php echo $this->prefix; ?>_null[]" value=<?php echo $k; ?> <?php echo ((in_array($k, $n)) ? 'checked="checked"' : ''); ?>></b>
996  </td>
997  <?php }//end foreach
998  ?>
999  </tr>
1000  <?php }//end if in_array
1001  ?>
1002 
1003  <tr>
1004  <td><b>Select box</b></td>
1005  <?php
1006  foreach ($us as $k=>$v) {
1007  ?>
1008  <td align="center">
1009  <input type="radio" name="<?php echo $this->prefix; ?>_style[<?php echo $k; ?>]" value="s" <?php echo (($y[$k]=='s') ? 'checked="checked"' : ''); ?>></b>
1010  </td>
1011  <?php
1012  }
1013  ?>
1014  </tr>
1015 
1016  <tr>
1017  <td><b>Text box</b></td>
1018  <?php
1019  foreach ($us as $k=>$v) {
1020  ?>
1021  <td align="center">
1022  <input type="radio" name="<?php echo $this->prefix; ?>_style[<?php echo $k; ?>]" value="t" <?php echo (($y[$k]=='t') ? 'checked="checked"' : ''); ?>></b>
1023  </td>
1024  <?php
1025  }
1026  ?>
1027  </tr>
1028 
1029  <tr>
1030  <td><b>Min</b></td>
1031  <td colspan="6"><?php $this->printMinField(); ?></td>
1032  </tr>
1033 
1034  <tr>
1035  <td><b>Max</b></td>
1036  <td colspan="6"><?php $this->printMaxField(); ?></td>
1037  </tr>
1038 
1039  </table>
1040 
1041  <?php
1042 
1043  }//end printEditOptions()
1044 
1045 
1053  {
1054  $s = isset($_REQUEST[$this->prefix.'_show']) ? $_REQUEST[$this->prefix.'_show'] : Array();
1055  $n = isset($_REQUEST[$this->prefix.'_null']) ? $_REQUEST[$this->prefix.'_null'] : Array();
1056  $y = $_REQUEST[$this->prefix.'_style'];
1057  $c = $_REQUEST[$this->prefix.'_allow_circa'];
1058  $t = isset($_REQUEST[$this->prefix.'_standard']) ? $_REQUEST[$this->prefix.'_standard'] : Array();
1059 
1060  $this->parameters['show'] = $s;
1061  $this->parameters['null'] = $n;
1062  $this->parameters['style'] = $y;
1063  $this->parameters['allow_circa'] = $c;
1064  $this->parameters['standard'] = $t;
1065 
1066  $this->processMinField();
1067  $this->processMaxField();
1068 
1069  return TRUE;
1070 
1071  }//end processEditOptions()
1072 
1073 
1080  function isEmpty()
1081  {
1082  return ($this->value == '---------- --:--:--');
1083 
1084  }//end isEmpty()
1085 
1086 
1087 }//end class
1088 
1089 ?>