<?php
/**
 * Implementation of hook_enable().
 * Reset the calendar caches.
 */
function calendar_enable() {
  module_enable(array('date_api'));
  if (version_compare(PHP_VERSION, '5.2', '<')) {
    module_enable(array('date_php4'));
  }
  module_enable(array('date_timezone'));
  db_query("DELETE FROM {cache_views}");
}

/**
 * Implementation of hook_disable().
 * Empty the calendar caches.
 */
function calendar_disable() {
  db_query("DELETE FROM {cache_views}");
}

/**
 * Implementation of hook_uninstall().
 * Remove all traces of calendars.
 */
function calendar_uninstall() {
  $ret = array();
  variable_del('calendar_default_view_options');
  $displays = array(
    'calendar',
    'calendar_attachment',
    'calendar_year',
    'calendar_day',
    'calendar_month',
    'calendar_week',
    'calendar_block',
    'calendar_block_view',
    'calendar_ical',
    );
  $result = db_query("SELECT DISTINCT vid FROM {views_display} WHERE display_plugin IN ('". implode("','", $displays) ."')");
  while($row = db_fetch_array($result)) {
    db_query("DELETE FROM {views_view} WHERE vid = %d", $row['vid']);
    db_query("DELETE FROM {views_display} WHERE vid = %d", $row['vid']);
  }
  db_query("DELETE FROM {cache_views}");
  return $ret;
}

/**
 * Implementation of hook_install().
 */
function calendar_install() {
  $ret = array();
  module_enable(array('date_api'));
  if (version_compare(PHP_VERSION, '5.2', '<')) {
    module_enable(array('date_php4'));
  }
  module_enable(array('date_timezone'));
  // Make sure this module loads after date_api.
  db_query("UPDATE {system} SET weight = 1 WHERE name = 'calendar'");
  db_query("DELETE FROM {cache_views}");
  return $ret;
}

function calendar_update_last_removed() {
  return 5200;
}

// No longer track views info in variables now that
// Views 2 has settings we can use.
function calendar_update_6000() {
  $ret = array();
  // don't attempt to upgrade if views is not yet upgraded.
  if (drupal_get_installed_schema_version('views', TRUE) < 6000) {
    $ret = array();
    drupal_set_message(t('Calendar module cannot be updated until after Views has been updated. Please return to <a href="@update-php">update.php</a> and run the remaining updates.', array('@update-php' => base_path() .'update.php?op=selection')), 'warning', FALSE);
    $ret['#abort'] = array('success' => FALSE, 'query' => t('calendar.module has updates, but cannot be updated until views.module is updated first.'));

    return $ret;
  }

  variable_del('calendar_empty_arg');

  // Can't use variable_del because we don't have a reliable
  // way to find the old view names.
  db_query("DELETE FROM {variable} WHERE name LIKE 'calendar_%'");
  cache_clear_all('variables', 'cache');
  return $ret;
}

/**
 * Make sure handlers for disabled Calendar iCal module don't get saved in the view.
 */
function calendar_update_6001() {
  $ret = array();
  // don't attempt to upgrade if views is not yet upgraded.
  if (drupal_get_installed_schema_version('views', TRUE) < 6000) {
    $ret = array();
    drupal_set_message(t('Calendar module cannot be updated until after Views has been updated. Please return to <a href="@update-php">update.php</a> and run the remaining updates.', array('@update-php' => base_path() .'update.php?op=selection')), 'warning', FALSE);
    $ret['#abort'] = array('success' => FALSE, 'query' => t('calendar.module has updates, but cannot be updated until views.module is updated first.'));

    return $ret;
  }
  if (!module_exists('calendar_ical')) {
    $ret[] = update_sql("DELETE FROM {views_display} WHERE display_plugin = 'ical'");
  }
  return $ret;
}

function calendar_update_6002() {
  $ret = array();
  // don't attempt to upgrade if views is not yet upgraded.
  if (drupal_get_installed_schema_version('views', TRUE) < 6000) {
    $ret = array();
    drupal_set_message(t('Calendar module cannot be updated until after Views has been updated. Please return to <a href="@update-php">update.php</a> and run the remaining updates.', array('@update-php' => base_path() .'update.php?op=selection')), 'warning', FALSE);
    $ret['#abort'] = array('success' => FALSE, 'query' => t('calendar.module has updates, but cannot be updated until views.module is updated first.'));

    return $ret;
  }
  $periods = array(
    'calendar_month' => 'calendar_period_1',
    'calendar_year' => 'calendar_period_2',
    'calendar_day' => 'calendar_period_3',
    'calendar_week' => 'calendar_period_4',
    'calendar_block_view' => 'calendar_period_5',
    );
  $result = db_query("SELECT * FROM {views_display} d LEFT JOIN {views_view} v ON d.vid = v.vid");
  drupal_load('module', 'views');
  while ($row = db_fetch_array($result)) {
    if (in_array($row['display_plugin'], array_keys($periods))) {
      $id = $row['id'];
      $options = unserialize($row['display_options']);
      if ($row['display_plugin'] == 'calendar_block_view') {
        $options['calendar_type'] = 'month';
        $options['displays'] = array('calendar_1' => 0, 'default' => 0, 'calendar_block_1' => 'calendar_block_1');
      }
      else {
        $options['calendar_type'] = str_replace('calendar_', '', $row['display_plugin']);
        $options['displays'] = array('calendar_1' => 'calendar_1', 'default' => 0, 'calendar_block_1' => 0);
      }
      $row['id'] = $periods[$row['id']];
      $row['display_plugin'] = 'calendar_period';
      $row['display_options'] = serialize($options);
      db_query("UPDATE {views_display} SET id='%s', display_plugin='%s', display_options='%s' WHERE id='%s'", $row['id'], $row['display_plugin'], $row['display_options'], $id);
    }
    elseif ($row['display_plugin'] == 'calendar' || $row['display_plugin'] == 'calendar_block') {
      db_query("UPDATE {views_display} SET id='%s' WHERE id='%s'", $row['id'] .'_1', $row['id']);
    }
    db_query("DELETE FROM {views_object_cache} WHERE name = '%s'", $row['name']);
  }
  views_invalidate_cache();
  $ret[] = array('success' => TRUE, 'query' => 'Updated calendar displays to use new handlers.');
  return $ret;
}
