<?php
//$Id: calendar.install,v 1.13.2.11 2009/03/17 18:03:36 karens Exp $
/**
 * @file
 * Install File
 */
/**
 * Implementation of hook_requirements().
 * Added to be sure the Date API version matches this code so invalid
 * functions are not called.
 */
function calendar_requirements($phase) {
  $requirements = array();
  $t = get_t();

  // This is the minimum required version for the Date API so that it will work with this module.
  $required_version = 5.2;

  // Make sure the matching version of date_api is installed.
  // Use info instead of an error at install time since the problem may
  // just be that they were installed in the wrong order.
  switch ($phase) {
    case 'runtime':
      if (variable_get('date_api_version', 0) < $required_version) {
        $requirements['calendar_api_version'] = array(
          'title' => $t('Calendar requirements'),
          'value' => $t('The Calendar module requires a more current version of the Date API. Please check for a newer version.'),
          'severity' => REQUIREMENT_ERROR,
          );
      }
      break;
     case 'install':
      if (variable_get('date_api_version', 0) < $required_version) {
        $requirements['calendar_api_version'] = array(
          'title' => $t('Calendar requirements'),
          'value' => $t('The Calendar module requires the latest version of the Date API, be sure you are installing the latest versions of both modules.'),
          'severity' => REQUIREMENT_INFO,
          );
      }
      break;
  }
  return $requirements;
}

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

/**
 * Move these caches from 'cache' to 'cache_views' so they get cleared
 * automatically whenever views_invalidate_cache() is called.
 */
function calendar_update_5000() {
  $ret = array();
  cache_clear_all('calendar_fields', 'cache');
  cache_clear_all('calendar_views', 'cache');
  return $ret;
}

/**
 * Implementation of hook_update().
 */
function calendar_update_5001() {
  $ret = array();
  $ret[] = update_sql("UPDATE {system} SET weight = 1 WHERE name = 'calendar'");

  return $ret;
}

function calendar_update_5200() {
  $ret = array();
  module_enable(array('date_api'));
  if (version_compare(PHP_VERSION, '5.2', '<')) {
    module_enable(array('date_php4'));
  }
  module_enable(array('date_timezone'));
  return $ret;
}

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