18 require_once dirname(__FILE__).
'/../../form_question/form_question.inc';
19 require_once SQ_FUDGE_PATH.
'/datetime_field/datetime_field.inc';
20 require_once SQ_FUDGE_PATH.
'/general/datetime.inc';
74 $name =
'q'.$this->id;
78 $elements = Array(
'day' =>
'd',
'month' =>
'm',
'year' =>
'y',
'hour' =>
'h',
'min' =>
'i',
'sec' =>
's');
80 $parameters = Array();
81 $parameters[
'show'] = Array();
82 $parameters[
'null'] = Array();
83 $parameters[
'style'] = Array();
84 $parameters[
'min'] = $this->
attr(
'minimum');
86 if (($parameters[
'min'] ==
'---------- --:--:--') || ($parameters[
'min'] ==
'0001------ --:--:--')) {
87 $parameters[
'min'] =
'1900-01-01 00:00:00';
89 $parameters[
'max'] = $this->
attr(
'maximum');
91 if (($parameters[
'max'] ==
'---------- --:--:--') || ($parameters[
'max'] ==
'0001------ --:--:--')) {
92 $parameters[
'max'] =
'2099-12-31 23:59:59';
94 $parameters[
'extras'] = $this->
attr(
'extras');
96 if ($this->
attr(
'tabindex')) {
97 $tabindex =
' tabindex="'.$this->attr(
'tabindex').
'"';
98 foreach ($parameters[
'extras'] as $key => $extra) {
99 $parameters[
'extras'][$key] .= $tabindex;
103 $show_params = $this->
attr(
'show');
104 $text_params = $this->
attr(
'text');
105 $null_params = $this->
attr(
'null');
107 $parameters[
'allow_circa'] = $show_params[
'c'];
108 if (!empty($show_params[
'a'])) $parameters[
'show'][] =
'a';
110 foreach ($elements as $attr => $t) {
111 $parameters[
'style'][$t] = ($text_params[$t] ?
't' :
's');
112 if ($show_params[$t]) $parameters[
'show'][] = $t;
114 $parameters[
'null'][] = $t;
117 $dt_field =
new Datetime_Field($name, $value, $parameters);
120 $dt_field->printField();
121 $html .= ob_get_contents();
138 $elements = Array(
'day' =>
'd',
'month' =>
'm',
'year' =>
'y',
'hour' =>
'h',
'min' =>
'i',
'sec' =>
's');
140 $parameters = Array();
141 $parameters[
'show'] = Array();
142 $parameters[
'null'] = Array();
143 $parameters[
'style'] = Array();
144 $parameters[
'min'] =
'01-00-1970 00:00:00';
145 $parameters[
'max'] =
'31-12-2035 23:59:59';
146 $parameters[
'show'][
'c'] = 0;
147 $parameters[
'show'][
'a'] = 0;
149 foreach ($elements as $attr => $t) {
150 $parameters[
'style'][$t] =
's';
151 $parameters[
'show'][] = $t;
152 $parameters[
'null'][$t] = 1;
192 if (is_null($answer)) $answer = $this->
getValue();
194 $required_fields = $this->
attr(
'null');
195 $show_params = $this->
attr(
'show');
197 $custom_error = $this->
attr(
'cust_required_error');
198 if (empty($custom_error)) {
199 $custom_error = translate(
'core_form_datetime_invalid_date_time');
202 $this->failed_rules[] = $custom_error;
204 if (!is_iso8601($answer))
return FALSE;
207 foreach ($required_fields as $field => $required) {
208 if ($required !=
'1' && $show_params[$field] ==
'1') {
209 if ($field ==
'y' && (substr($answer, 0, 2) ==
'--')) {
212 }
else if ($field ==
'm' && (substr($answer, 5, 2) ==
'--')) {
215 }
else if ($field ==
'd' && (substr($answer, 8, 2) ==
'--')) {
218 }
else if ($field ==
'h' && (substr($answer, 11, 2) ==
'--')) {
221 }
else if ($field ==
'i' && (substr($answer, 14, 2) ==
'--')) {
224 }
else if ($field ==
's' && (substr($answer, 17, 2) ==
'--')) {
233 list($date[
'y'], $date[
'm'], $date[
'd']) = sscanf($answer,
'%04s-%02s-%02s');
236 foreach ($required_fields as $field => $required) {
237 if ($required ==
'1' || $show_params[$field] !=
'1') {
238 if ((($field ==
'y') || ($field ==
'm') || ($field ==
'd')) && (substr($date[$field], 0, 2) ==
'--')) {
247 $is_valid_date = checkdate($date[
'm'], $date[
'd'], $date[
'y']);
249 if (!$is_valid_date)
return FALSE;
251 return parent::hasValidValue($answer, $mute_errors);
266 $allow_circa = FALSE;
267 $show_params = $this->
attr(
'show');
268 if ($show_params[
'c']) $allow_circa = TRUE;
271 list($date, $time, $circa) = explode(
' ', $this->
getValue());
273 list($date, $time) = explode(
' ', $this->
getValue());
279 list($val[
'y'], $val[
'm'], $val[
'd']) = sscanf($date,
'%04s-%02s-%02s');
280 list($val[
'h'], $val[
'i'], $val[
's']) = sscanf($time,
'%02s:%02s:%02s');
283 $new_format = $this->
attr(
'print_format');
285 $required_fields = $this->
attr(
'null');
289 $non_empty_date = TRUE;
291 foreach ($val as $val_key => $val_time) {
292 $preg_match_result = preg_match(
'|^-*$|', $val[$val_key]);
293 if ($required_fields[$val_key] !=
'1' || !$preg_match_result) {
294 $non_empty_date = FALSE;
296 if ($preg_match_result) {
300 if ($required_fields[$val_key] == 1) {
303 $val[$val_key] = 2000;
310 if ($required_fields[$val_key] == 1) {
319 if ($required_fields[$val_key] == 1) {
329 if ($non_empty_date === TRUE)
return '';
332 $invalid_year = FALSE;
333 $temp_date = mktime($val[
'h'], $val[
'i'], $val[
's'], $val[
'm'], $val[
'd'], $val[
'y']);
336 if ($temp_date === FALSE || $val[
'y'] < 1970 || $val[
'y'] > 2038) {
337 $temp_date = mktime($val[
'h'], $val[
'i'], $val[
's'], $val[
'm'], $val[
'd'], 2000);
338 $invalid_year = TRUE;
344 preg_match_all(
'/y|Y/', $new_format, $matches, PREG_OFFSET_CAPTURE);
345 if (!empty($matches) && $invalid_year) {
346 $matches = array_reverse(array_pop($matches));
347 foreach ($matches as $match) {
349 $year_str = ($match[0] ==
'Y') ? $val[
'y'] : substr($val[
'y'], -2);
351 $new_format = $year_str.substr($new_format, 1);
352 }
else if (($pos > 0) && (substr($new_format, ($pos - 1), 1) !=
'\\')) {
353 $new_format = substr($new_format, 0, $pos).$year_str.substr($new_format, $pos + 1);
358 return ($circa ?
'circa ' :
'').date($new_format, $temp_date);
372 $replacement_mapping = Array (
373 'y' => Array (
'y',
'Y'),
374 'm' => Array (
'm',
'n',
'F',
'M'),
375 'd' => Array (
'd',
'j',
'S',
'D',
'l'),
376 'h' => Array (
'h',
'H',
'g',
'G'),
381 if (isset($replacement_mapping[$element])) {
382 foreach ($replacement_mapping[$element] as $replace_string) {
383 $original_format = str_replace($replace_string,
'', $original_format);
387 return $original_format;
404 $allow_circa = FALSE;
406 $show_params = $this->
attr(
'show');
407 if ($show_params[
'c']) $allow_circa = TRUE;
410 list($date, $time, $circa) = explode(
' ', $answer);
412 list($date, $time) = explode(
' ', $answer);
416 echo
'<datetime_q id="'.addslashes($this->
id).
'" name="'.htmlspecialchars($this->
attr(
'name')).
'"'.($circa ?
' circa="1"' :
'').
'>';
418 $iso = $date.
'T'.$time;
421 echo
'</datetime_q>';
423 $contents = ob_get_contents();
440 $name =
'q'.$this->id;
441 $value = array_get_index($_POST, $name.
'value',
'');
442 $show = array_get_index($_POST, $name.
'show',
'');
444 if (strpos($show,
'y') === FALSE) {
447 $val .= str_pad($_POST[$name.
'value'][
'y'], 4,
'-', STR_PAD_LEFT);
452 if ((isset($_POST[$name.
'value'][
'm']) && empty($_POST[$name.
'value'][
'm'])) || strpos($show,
'm') === FALSE) {
455 $val .= str_pad($_POST[$name.
'value'][
'm'], 2,
'0', STR_PAD_LEFT);
460 if ((isset($_POST[$name.
'value'][
'd']) && empty($_POST[$name.
'value'][
'd'])) || strpos($show,
'd') === FALSE) {
463 $val .= str_pad($_POST[$name.
'value'][
'd'], 2,
'0', STR_PAD_LEFT);
468 if ((isset($_POST[$name.
'value'][
'h']) && empty($_POST[$name.
'value'][
'h'])) || strpos($show,
'h') === FALSE) {
469 if (isset($_POST[$name.
'value'][
'h']) && (
string) $_POST[$name.
'value'][
'h'] ===
'0') {
475 $hour = str_pad($_POST[$name.
'value'][
'h'], 2,
'0', STR_PAD_LEFT);
476 if (!empty($_POST[$name.
'value'][
'a']) && strpos($show,
'a') !== FALSE && $hour !=
'--'){
477 $hour = date(
'H', strtotime($hour.
' '.$_POST[$name.
'value'][
'a']));
484 if ((isset($_POST[$name.
'value'][
'i']) && empty($_POST[$name.
'value'][
'i'])) || strpos($show,
'i') === FALSE) {
485 if (isset($_POST[$name.
'value'][
'i']) && (
string) $_POST[$name.
'value'][
'i'] ===
'0') {
491 $val .= str_pad($_POST[$name.
'value'][
'i'], 2,
'0', STR_PAD_LEFT);
496 if ((isset($_POST[$name.
'value'][
's']) && empty($_POST[$name.
'value'][
's'])) || strpos($show,
's') === FALSE) {
497 if (isset($_POST[$name.
'value'][
's']) && (
string) $_POST[$name.
'value'][
's'] ===
'0') {
503 $val .= str_pad($_POST[$name.
'value'][
's'], 2,
'0', STR_PAD_LEFT);