You are here

function date_update_6005 in Date 6.2

File

date/date.install, line 191

Code

function date_update_6005() {
  include_once drupal_get_path('module', 'content') . '/content.install';
  if ($abort = content_check_update('date')) {
    return $abort;
  }

  // The new format table won't get built before the system tries to run this update.
  // We need to abort and tell the user to re-run it.
  // We need a custom abort process because the content_check_update could be OK
  // but we still need to update these values.
  if (!db_table_exists('date_format') && !db_table_exists('date_formats') || !db_table_exists('date_format_types')) {
    drupal_set_message(t('Some updates are still pending. Please return to <a href="@update-php">update.php</a> and run the remaining updates.', array(
      '@update-php' => base_path() . 'update.php?op=selection',
    )), 'error', FALSE);
    $ret['#abort'] = array(
      'success' => FALSE,
      'query' => t('Some updates are still pending.<br/>Please re-run the update script.'),
    );
    return $ret;
  }
  drupal_load('module', 'content');
  $ret = array();
  $new_map = array(
    'short' => 'short',
    'medium' => 'medium',
    'long' => 'long',
    'time' => 'time',
    'time_timezone' => 'time_timezone',
    'iso' => 'iso',
    'timestamp' => 'timestamp',
    'ical' => 'ical',
    'feed' => 'feed',
  );

  // Create new custom formats for each of these:
  date_install_create_format(NULL, t('Time'), 'time', date_limit_format(variable_get('date_format_short', 'm/d/Y - H:i'), array(
    'hour',
    'minute',
    'second',
  )));
  date_install_create_format(NULL, t('Time with timezone'), 'time_timezone', date_limit_format(variable_get('date_format_short', 'm/d/Y - H:i') . ' e', array(
    'hour',
    'minute',
    'second',
    'timezone',
  )));
  date_install_create_format(NULL, t('iCal'), 'ical', 'Ymd\\THis');
  date_install_create_format(NULL, t('Timestamp'), 'timestamp', 'U');
  date_install_create_format(NULL, t('Feed'), 'feed', 'D, j M Y H:i:s O');
  date_install_create_format(NULL, t('ISO'), 'iso', DATE_FORMAT_ISO);
  $result = db_query("SELECT field_name, global_settings from {" . content_field_tablename() . "} where type='date' OR type='datestamp' OR type='datetime'");
  while ($field = db_fetch_array($result)) {
    $field_settings = unserialize($field['global_settings']);
    $field_name = $field['field_name'];
    $default = !empty($field_settings['output_format_custom']) ? $field_settings['output_format_custom'] : $field_settings['output_format_date'];
    $short = !empty($field_settings['output_format_custom_short']) ? $field_settings['output_format_custom_short'] : $field_settings['output_format_date_short'];
    $med = !empty($field_settings['output_format_custom_medium']) ? $field_settings['output_format_custom_medium'] : $field_settings['output_format_date_medium'];
    $long = !empty($field_settings['output_format_custom_long']) ? $field_settings['output_format_custom_long'] : $field_settings['output_format_date_long'];
    $system_short = variable_get('date_format_short', 'm/d/Y - H:i');
    $system_med = variable_get('date_format_medium', 'D, m/d/Y - H:i');
    $system_long = variable_get('date_format_long', 'l, F j, Y - H:i');

    // The only thing we'll keep is the name of a default format type.
    $new_setting = 'default_format';

    // Create a map of the old and new formats.
    $map = $new_map;

    // If the field's long, medium, or short formats don't match the system
    // values, create custom format types for them.
    // If the default value matches a custom format, set that new format
    // type as the default format type.
    if ($system_short != $short) {
      $name = $field_name . '_short';
      date_install_create_format($field, NULL, $name, $short);
      $map['short'] = $name;
      if ($default == $short) {
        $field_settings[$new_setting] = $name;
        $map['default'] = $name;
      }
    }
    if ($system_med != $med) {
      $name = $field_name . '_medium';
      date_install_create_format($field, NULL, $name, $med);
      $map['medium'] = $name;
      if ($default == $med) {
        $field_settings[$new_setting] = $name;
        $map['default'] = $name;
      }
    }
    if ($system_long != $long) {
      $name = $field_name . '_long';
      date_install_create_format($field, NULL, $name, $long);
      $map['long'] = $name;
      if ($default == $long) {
        $field_settings[$new_setting] = $name;
        $map['default'] = $name;
      }
    }

    // If we haven't found a format type for the default format yet,
    // see if it matches any of the system formats.
    if (empty($field_settings[$new_setting])) {
      if ($default == $system_med) {
        $field_settings[$new_setting] = 'medium';
        $map['default'] = 'medium';
      }
      elseif ($default == $system_long) {
        $field_settings[$new_setting] = 'long';
        $map['default'] = 'long';
      }
      elseif ($default == $system_short) {
        $field_settings[$new_setting] = 'short';
        $map['default'] = 'short';
      }
      else {

        // If all else fails, create a new format type
        // for the default format.
        $name = $field_name . '_default';
        date_install_create_format($field, NULL, $name, $default);
        $field_settings[$new_setting] = $name;
        $map['default'] = $name;
      }
    }

    // Store the map of old and new formats in a variable.
    variable_set('date_format_map_' . $field['field_name'], $map);
    $ret[] = array(
      'success' => TRUE,
      'query' => t('Field %field formats were converted to custom formats.', array(
        '%field' => $field_name,
      )),
    );
    foreach ($map as $key => $value) {
      if (in_array($key, array(
        'default',
        'short',
        'medium',
        'long',
      ))) {
        $ret[] = array(
          'success' => TRUE,
          'query' => t("The old format type %from_format for field %field was converted to the new format type %to_format.", array(
            '%field' => $field_name,
            '%from_format' => $key,
            '%to_format' => $value,
          )),
        );
      }
    }

    // Unset all the old values, we won't use them any more.
    unset($field_settings['output_format_date']);
    unset($field_settings['output_format_custom']);
    unset($field_settings['output_format_date_short']);
    unset($field_settings['output_format_custom_short']);
    unset($field_settings['output_format_date_medium']);
    unset($field_settings['output_format_custom_medium']);
    unset($field_settings['output_format_date_long']);
    unset($field_settings['output_format_custom_long']);
    db_query("UPDATE {" . content_field_tablename() . "}  SET global_settings = '%s' WHERE field_name = '%s'", serialize($field_settings), $field_name);

    // Update the display settings to point to the new format types.
    $result2 = db_query("SELECT * FROM {" . content_instance_tablename() . "} WHERE field_name = '{$field_name}'");
    while ($instance = db_fetch_array($result2)) {
      $display_settings = unserialize($instance['display_settings']);
      foreach ($display_settings as $key => $setting) {
        if ($key != 'label' && array_key_exists($display_settings[$key]['format'], $map)) {
          $display_settings[$key]['format'] = $map[$display_settings[$key]['format']];
        }
      }
      db_query("UPDATE {" . content_instance_tablename() . "}  SET display_settings = '%s' WHERE field_name = '%s' AND type_name = '%s'", serialize($display_settings), $field_name, $instance['type_name']);
      $ret[] = array(
        'success' => TRUE,
        'query' => t("The display settings for field %field in content type %type_name were updated.", array(
          '%field' => $field_name,
          '%type_name' => $instance['type_name'],
        )),
      );
    }

    // See if any views stored in the database need date formats updated.

    /*
    $result2 = db_query("SELECT * FROM {views_display}");
    while ($row = db_fetch_array($result2)) {
      $updated = FALSE;
      $display_options = unserialize($row['display_options']);
      if (array_key_exists('fields', $display_options)) {
        if (array_key_exists($field_name .'_value', $display_options['fields'])) {
          $display_options['fields'][$field_name .'_value']['format'] = $map[$display_options['fields'][$field_name .'_value']['format']];
          $updated = TRUE;
        }
        if (array_key_exists($field_name .'_value2', $display_options['fields'])) {
          $display_options['fields'][$field_name .'_value2']['format'] = $map[$display_options['fields'][$field_name .'_value2']['format']];
          $updated = TRUE;
        }
        if ($updated) {
          db_query("UPDATE {views_display} SET display_options='%s'", serialize($display_options));
          $ret[] = array('success' => TRUE, 'query' => t("The format used for field %field in view %vid %display display was updated to the new format name.", array('%field' => $field_name, '%display' => $row['display_title'], '%vid' => $row['vid'])));
        }
      }
    }
    */
  }

  // Clear any caches that may have old formats in them.
  content_clear_type_cache();
  drupal_set_message(t('Date display formats are now included with the system date and time settings. Please visit <a href="@date-time-page">the Date and time format page</a> to see the new format types.', array(
    '@date-time-page' => url('admin/settings/date-time/formats'),
  )));
  return $ret;
}