From 9b5e9d7df0bd9169c269761e46d18e60064d30ae Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Fri, 8 Sep 2017 13:46:45 +0200 Subject: [PATCH] Fixed wrong format date in SuiteDesk date fields --- modules/storm/storm.install | 6 +++--- modules/storm/storm.module | 42 +++++++++++++++++++++++++------------ 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/modules/storm/storm.install b/modules/storm/storm.install index 07a69ff..5d57051 100644 --- a/modules/storm/storm.install +++ b/modules/storm/storm.install @@ -13,15 +13,15 @@ function storm_install() { variable_set('storm_report_header', ''); variable_set('storm_icons_path', drupal_get_path('module', 'storm') .'/icons'); variable_set('storm_organization_nid', 0); - variable_set('storm_yearsrangebegin', 2001); - variable_set('storm_yearsrangeend', 2015); + variable_set('storm_yearsrangebegin', 2010); + variable_set('storm_yearsrangeend', 2025); // Set the weight to 2 (to execute module hooks after pathauto): # db_query("UPDATE {system} SET weight = 2 WHERE name = 'storm'"); // Clear the cache to get these to take effect: # cache_clear_all(); - + drupal_install_schema('storm'); } diff --git a/modules/storm/storm.module b/modules/storm/storm.module index 5093105..23bcc81 100644 --- a/modules/storm/storm.module +++ b/modules/storm/storm.module @@ -1989,14 +1989,14 @@ function storm_admin_settings() { '#type' => 'select', '#title' => t('Begin'), '#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( '#type' => 'select', '#title' => t('End'), '#options' => drupal_map_assoc(range(1970, 2037)), - '#default_value' => variable_get('storm_yearsrangeend', 2015), + '#default_value' => variable_get('storm_yearsrangeend', 2025), ); $form['taxation'] = array( @@ -2246,7 +2246,7 @@ function storm_datetime_expand($element) { foreach ($order as $type) { switch ($type) { 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; case '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'); - $begin = variable_get('storm_yearsrangebegin', 2001) - $current_year; + $begin = variable_get('storm_yearsrangebegin', 2010) - $current_year; if ($begin > 0) { $begin = '+'. $begin; } - $end = variable_get('storm_yearsrangeend', 2015) - $current_year; + $end = variable_get('storm_yearsrangeend', 2025) - $current_year; if ($end > 0) { $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[] = 'popup'; $element['popup'] = array( '#type' => 'date_popup', '#date_timezone' => date_default_timezone_name(), - '#date_format' => "Y-m-d", + '#date_format' => $format, '#date_year_range' => $begin .':'. $end, '#default_value' => $value, '#attributes' => $element['#attributes'], @@ -2361,7 +2365,7 @@ function storm_dateext_expand($element, $edit, $form_state, $form) { foreach ($order as $type) { switch ($type) { 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; case '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'])) { 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']['day'] = (int) $matches[3]; - $element['#value']['month'] = (int) $matches[2]; - $element['#value']['year'] = (int) $matches[1]; + $element['#value']['day'] = (int) $matches[$pos_d]; + $element['#value']['month'] = (int) $matches[$pos_m]; + $element['#value']['year'] = (int) $matches[$pos_y]; } else { 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); }