<?php

/**
 * Implementation of hook_install().
 * Reset the date caches.
 */
function date_install() {
  drupal_load('module', 'content');
  content_notify('install', 'date');
}

/**
 * Implementation of hook_uninstall().
 */
function date_uninstall() {
  drupal_load('module', 'content');
  content_notify('uninstall', 'date');
}

/**
 * Implementation of hook_enable().
 * Reset the calendar caches.
 */
function date_enable() {
  drupal_load('module', 'content');
  content_notify('enable', 'date');
}

/**
 * Implementation of hook_disable().
 * Empty the date caches.
 */
function date_disable() {
  drupal_load('module', 'content');
  content_notify('disable', 'date');
}

function date_update_last_removed() {
  return 5210;
}

/**
 * Granularity options were not saved correctly by CCK if created using checkboxes.
 * Checkboxes have been changed back to a select and we need to fix the values
 * stored in the settings.
 *
 * The bad values would have been stored in the form
 *   array(
 *     'year' => 'year'
 *     'month' => 'month',
 *     'day' => 0,
 *     'hour' => 0,
 *     'minute' => 0,
 *     'second' => 0,
 *   );
 *
 * Good values would have been stored in the form
 *   array(
 *     'year' => 'year',
 *     'month' => 'month',
 *   );
 *
 * This might or might not have gotten updated in D5.2,
 * we need to force it again in D6 just in case.
 *
 * @return unknown
 */
function date_update_6000() {
  include_once(drupal_get_path('module', 'content') .'/content.install');
  if ($abort = content_check_update('date')) {
    return $abort;
  }
  drupal_load('module', 'content');
  $ret = array();
  $result = db_query("SELECT field_name, global_settings from {". content_field_tablename() ."} where type LIKE 'date_%'");
  while ($field = db_fetch_array($result)) {
    // Change the format to one date_popup can use.
    $field_settings = unserialize($field['global_settings']);
    $granularity = array_filter($field_settings['granularity']);
    $field_settings['granularity'] = $granularity;
    db_query("UPDATE {". content_field_tablename() ."}  SET global_settings = '%s' WHERE field_name = '%s'", serialize($field_settings), $field['field_name']);
  }
  content_clear_type_cache();
  return $ret;
}

/**
 * Get rid of jscalendar popup widget.
 * Originally update_5201, but that was broken.
 */
function date_update_6001() {
  include_once(drupal_get_path('module', 'content') .'/content.install');
  if ($abort = content_check_update('date')) {
    return $abort;
  }
  drupal_load('module', 'content');
  $ret = array();
  if (db_result(db_query("SELECT COUNT(*) FROM {". content_instance_tablename() ."} WHERE widget_type = 'date_js'"))) {
    $replace = module_exists('date_popup') ? 'date_popup' : 'date_text';
    $result = db_query("SELECT field_name, type_name, widget_type from {". content_instance_tablename() ."} where widget_type = 'date_js'");
    while ($widget = db_fetch_array($result)) {
      db_query("UPDATE {". content_instance_tablename() ."} SET widget_type = '$replace' WHERE field_name = '%s' AND type_name = '%s'", $widget['field_name'], $widget['type_name']);
    }
    drupal_set_message(t('All date fields using the jscalendar widget have been changed to use the text widget instead, since the jscalendar widget is no longer supported. Enable the Date Popup module to make a jQuery popup calendar available and edit the field settings to select it.'));
    content_clear_type_cache();
  }
  return $ret;
}

/**
 * Switch to using different widgets for repeating dates so non-repeats
 * can use standard Content module multiple values handling.
 */
function date_update_6002() {
  include_once(drupal_get_path('module', 'content') .'/content.install');
  if ($abort = content_check_update('date')) {
    return $abort;
  }
  drupal_load('module', 'content');
  $ret = array();
  $types = content_types();
  $repeating_fields = array();
  foreach ($types as $type) {
    foreach ($type['fields'] as $field_name => $field) {
      if (!empty($field['repeat'])) {
        $repeating_fields[] = $field_name;
      }
    }
  }
  if (count($repeating_fields)) {
    $replace = array(
      'date_select' => 'date_select_repeat',
      'date_text' => 'date_text_repeat',
      'date_popup' => 'date_popup_repeat',
      );
    $result = db_query("SELECT * from {". content_instance_tablename() ."} WHERE widget_type IN('date_select', 'date_text', 'date_popup') AND field_name IN('". implode("','", $repeating_fields) ."')");
    while ($widget = db_fetch_array($result)) {
      db_query("UPDATE {". content_field_tablename() ."} SET multiple = 1 WHERE field_name = '%s'", $widget['field_name']);
      db_query("UPDATE {". content_instance_tablename() ."} SET widget_type = '". $replace[$widget['widget_type']] ."' WHERE field_name = '%s' AND type_name = '%s'", $widget['field_name'], $widget['type_name']);
    }
    drupal_set_message(t('All repeating date fields have been updated to use the new repeating widgets.'));
    content_clear_type_cache();
  }
  return $ret;
}

/**
 * Update all repeating date delta values with the RRULE
 * so we can use that info in Views when we pull individual items
 * out in a query.
 */
function date_update_6003() {
  $ret = array();
  drupal_load('module', 'content');
  $fields = content_fields();
  foreach ($fields as $field) {
    if (strstr($field['type'], 'date') && !empty($field['repeat'])) {
      $db_info = content_database_info($field);
      $table = $db_info['table'];
      $column = $field['field_name'] .'_rrule';
      $result = db_query("SELECT DISTINCT nid, vid, $column FROM {". $table ."} WHERE $column <> ''");
      while ($row = db_fetch_array($result)) {
        $ret[] = update_sql("UPDATE {". $table ."} SET $column='". $row[$column] ."' WHERE nid=". $row['nid'] ." AND vid=". $row['vid']);
      }
    }
  }
  return $ret;
}

/**
 * Empty the stored db for timezones that use timezone 'none'.
 */
function date_update_6004() {
  $ret = array();
  drupal_load('module', 'content');
  $fields = content_fields();
  foreach ($fields as $field) {
    if (strstr($field['type'], 'date') && !empty($field['tz_handling']) && $field['tz_handling'] == 'none') {
      $row = db_result(db_query("SELECT global_settings FROM {". content_field_tablename() ."} WHERE field_name='". $field['field_name'] ."'"));
      $settings = unserialize($row);
      $settings['timezone_db'] = '';
      $settings = serialize($settings);
      db_query("UPDATE {". content_field_tablename() ."} SET global_settings='%s' WHERE field_name='%s'", $settings, $field['field_name']);
    }
  }
  drupal_set_message(t("The database has been updated to correct the stored timezone for fields using timezone 'none'."));
  content_clear_type_cache();
  return $ret;
}

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;
}

function date_install_create_format($field, $title, $name, $format) {
  if (empty($name) || empty($format)) {
    return;
  }
  $fields = content_fields();
  if (empty($title)) {
    $type = str_replace($field['field_name'] .'_', '', $name);
    $field = $fields[$field['field_name']];
    $title = $field['widget']['label'];
  }
  variable_set('date_format_'. $name, $format);
  db_query("INSERT INTO {date_format_types} (type, title, locked) VALUES('%s', '%s', 0)", $name, $title);
  if (!db_result(db_query("SELECT dfid FROM {date_formats} WHERE format='%s' AND type='%s'", $format, $type))) {
    db_query("INSERT INTO {date_formats} (format, type, locked) VALUES('%s', '%s', 0)", $format, 'custom');
  }
}

function date_db_integrity($name) {
  $ret = array();
  if (!module_exists('content') || !module_exists('date_api')) {
    return $ret;
  }
  drupal_load('module', 'content');
  require_once('./'. drupal_get_path('module', 'content') .'/includes/content.admin.inc');
  $ret = array();
  $fields = content_fields();
  foreach ($fields as $field) {
    $db_info = content_database_info($field);
    if ($field['type'] == 'date' || $field['type'] == 'datestamp') {
      $table = $db_info['table'];

      // start with the new column patterns.
      $columns_start = $db_info['columns'];
      $columns_end = $db_info['columns'];

      // alter the start column values to invalid or empty
      // values to force the new columns to be reset.
      $columns_start['value']['length'] = 90;
      if (!empty($field['todate'])) {
        if (!db_column_exists($table, $columns_start['value2']['column'])) {
          unset($columns_start['value2']);
        }
        else {
          $columns_start['value2']['length'] = 80;
        }
      }
      if ($field['tz_handling'] == 'date') {
        if (!db_column_exists($table, $columns_start['timezone']['column'])) {
          unset($columns_start['timezone']);
        }
        else {
          $columns_start['timezone']['length'] = 80;
        }
        if (!db_column_exists($table, $columns_start['offset']['column'])) {
          unset($columns_start['offset']);
        }
        else {
          $columns_start['offset']['length'] = 80;
        }
        if ($field['todate']) {
          if (!db_column_exists($table, $columns_start['offset2']['column'])) {
            unset($columns_start['offset2']);
          }
          else {
            $columns_start['offset2']['length'] = 80;
          }
        }
      }
      if (!empty($field['repeat'])) {
        if (!db_column_exists($table, $columns_start['rrule']['column'])) {
          unset($columns_start['rrule']);
        }
        else {
          $columns_start['rrule']['length'] = 80;
        }
      }
      $start_field = $field;
      $start_field['columns'] = $columns_start;
      $end_field = $field;
      $end_field['columns'] = $columns_end;
      content_alter_db($start_field, $end_field);
      $message = 'Date database integrity check. Updated table '. $table .' to set all columns to accept NULL values.';
      $ret[] = array('success' => TRUE, 'query' => $message);
    }
  }
  content_clear_type_cache();
  return $ret;
}

