Fixed wrong format date in SuiteDesk date fields

This commit is contained in:
Manuel Cillero 2017-09-08 13:46:45 +02:00
parent 892f86fc25
commit 9b5e9d7df0
2 changed files with 32 additions and 16 deletions

View file

@ -13,15 +13,15 @@ function storm_install() {
variable_set('storm_report_header', ''); variable_set('storm_report_header', '');
variable_set('storm_icons_path', drupal_get_path('module', 'storm') .'/icons'); variable_set('storm_icons_path', drupal_get_path('module', 'storm') .'/icons');
variable_set('storm_organization_nid', 0); variable_set('storm_organization_nid', 0);
variable_set('storm_yearsrangebegin', 2001); variable_set('storm_yearsrangebegin', 2010);
variable_set('storm_yearsrangeend', 2015); variable_set('storm_yearsrangeend', 2025);
// Set the weight to 2 (to execute module hooks after pathauto): // Set the weight to 2 (to execute module hooks after pathauto):
# db_query("UPDATE {system} SET weight = 2 WHERE name = 'storm'"); # db_query("UPDATE {system} SET weight = 2 WHERE name = 'storm'");
// Clear the cache to get these to take effect: // Clear the cache to get these to take effect:
# cache_clear_all(); # cache_clear_all();
drupal_install_schema('storm'); drupal_install_schema('storm');
} }

View file

@ -1989,14 +1989,14 @@ function storm_admin_settings() {
'#type' => 'select', '#type' => 'select',
'#title' => t('Begin'), '#title' => t('Begin'),
'#options' => drupal_map_assoc(range(1970, 2037)), '#options' => drupal_map_assoc(range(1970, 2037)),
'#default_value' => variable_get('storm_yearsrangebegin', 2001), '#default_value' => variable_get('storm_yearsrangebegin', 2010),
); );
$form['yearsrange']['group0']['storm_yearsrangeend'] = array( $form['yearsrange']['group0']['storm_yearsrangeend'] = array(
'#type' => 'select', '#type' => 'select',
'#title' => t('End'), '#title' => t('End'),
'#options' => drupal_map_assoc(range(1970, 2037)), '#options' => drupal_map_assoc(range(1970, 2037)),
'#default_value' => variable_get('storm_yearsrangeend', 2015), '#default_value' => variable_get('storm_yearsrangeend', 2025),
); );
$form['taxation'] = array( $form['taxation'] = array(
@ -2246,7 +2246,7 @@ function storm_datetime_expand($element) {
foreach ($order as $type) { foreach ($order as $type) {
switch ($type) { switch ($type) {
case 'year': case 'year':
$options = drupal_map_assoc(range(variable_get('storm_yearsrangebegin', 2001), variable_get('storm_yearsrangeend', 2012))); $options = drupal_map_assoc(range(variable_get('storm_yearsrangebegin', 2010), variable_get('storm_yearsrangeend', 2025)));
break; break;
case 'month': case 'month':
$options = drupal_map_assoc(range(1, 12), 'map_month'); $options = drupal_map_assoc(range(1, 12), 'map_month');
@ -2319,21 +2319,25 @@ function storm_dateext_expand($element, $edit, $form_state, $form) {
} }
$current_year = date('Y'); $current_year = date('Y');
$begin = variable_get('storm_yearsrangebegin', 2001) - $current_year; $begin = variable_get('storm_yearsrangebegin', 2010) - $current_year;
if ($begin > 0) { if ($begin > 0) {
$begin = '+'. $begin; $begin = '+'. $begin;
} }
$end = variable_get('storm_yearsrangeend', 2015) - $current_year; $end = variable_get('storm_yearsrangeend', 2025) - $current_year;
if ($end > 0) { if ($end > 0) {
$end = '+'. $end; $end = '+'. $end;
} }
$format = variable_get('date_format_short', 'm/d/Y - H:i');
$onlydate = strpos($format, ' ');
$format = $onlydate > 0 ? substr($format, 0, $onlydate) : $format;
$parents = $element['#parents']; $parents = $element['#parents'];
$parents[] = 'popup'; $parents[] = 'popup';
$element['popup'] = array( $element['popup'] = array(
'#type' => 'date_popup', '#type' => 'date_popup',
'#date_timezone' => date_default_timezone_name(), '#date_timezone' => date_default_timezone_name(),
'#date_format' => "Y-m-d", '#date_format' => $format,
'#date_year_range' => $begin .':'. $end, '#date_year_range' => $begin .':'. $end,
'#default_value' => $value, '#default_value' => $value,
'#attributes' => $element['#attributes'], '#attributes' => $element['#attributes'],
@ -2361,7 +2365,7 @@ function storm_dateext_expand($element, $edit, $form_state, $form) {
foreach ($order as $type) { foreach ($order as $type) {
switch ($type) { switch ($type) {
case 'year': case 'year':
$options = drupal_map_assoc(range(variable_get('storm_yearsrangebegin', 2001), variable_get('storm_yearsrangeend', 2012))); $options = drupal_map_assoc(range(variable_get('storm_yearsrangebegin', 2010), variable_get('storm_yearsrangeend', 2025)));
break; break;
case 'month': case 'month':
$options = drupal_map_assoc(range(1, 12), 'map_month'); $options = drupal_map_assoc(range(1, 12), 'map_month');
@ -2397,16 +2401,28 @@ function storm_dateext_validate($element, &$form_state) {
if (!$element['#withnull'] && empty($element['#value']['popup']['date'])) { if (!$element['#withnull'] && empty($element['#value']['popup']['date'])) {
form_set_error($element, t('Field %field is required.', array('%field' => !empty($element['#title']) ? $element['#title'] : ''))); form_set_error($element, t('Field %field is required.', array('%field' => !empty($element['#title']) ? $element['#title'] : '')));
} }
# if (preg_match('/(\d{4})-(\d{2})-(\d{2})/', $element['#value']['popup']['date'], $matches)) {
if (isset($element['#value']['popup']['date']) && preg_match('/(\d{4})-(\d{2})-(\d{2})/', $element['#value']['popup']['date'], $matches)) { $format = variable_get('date_format_short', 'm/d/Y - H:i');
$onlydate = strpos($format, ' ');
$format = $onlydate > 0 ? substr($format, 0, $onlydate) : $format;
if (isset($element['#value']['popup']['date'])) {
$format_match = str_replace(array('d', 'j', 'm', 'M', 'Y'), '(\d+)', $format);
$format_match = str_replace('/', '\/', $format_match);
preg_match("/$format_match/", $element['#value']['popup']['date'], $matches);
$format = str_replace(array('/', '-'), '', $format);
$pos_d = max(strpos($format, 'd'), strpos($format, 'j')) + 1;
$pos_m = max(strpos($format, 'm'), strpos($format, 'M')) + 1;
$pos_y = strpos($format, 'Y') + 1;
$element['#value'] = array(); $element['#value'] = array();
$element['#value']['day'] = (int) $matches[3]; $element['#value']['day'] = (int) $matches[$pos_d];
$element['#value']['month'] = (int) $matches[2]; $element['#value']['month'] = (int) $matches[$pos_m];
$element['#value']['year'] = (int) $matches[1]; $element['#value']['year'] = (int) $matches[$pos_y];
} }
else { else {
if (!$element['#withnull'] && !empty($element['#value']['popup']['date'])) { if (!$element['#withnull'] && !empty($element['#value']['popup']['date'])) {
form_set_error($element, t('Wrong Format for Field %field. Format should be YYYY-MM-DD.', array('%field' => !empty($element['#title']) ? $element['#title'] : ''))); form_set_error($element, t('Wrong Format for Field %field. Format should be "!format".', array('%field' => !empty($element['#title']) ? $element['#title'] : '', '!format' => $format)));
} }
$element['#value'] = array('day' => -1, 'month' => -1, 'year' => -1); $element['#value'] = array('day' => -1, 'month' => -1, 'year' => -1);
} }