<?php

/**
 * Implementation of hook_requirements().
 * Added to be sure the Date API version matches this code so invalid
 * functions are not called.
 */
function date_popup_requirements($phase) {
  $requirements = array();
  $t = get_t();
  switch ($phase) {
    case 'runtime':
      if (!module_exists('jquery_ui')) {
        $requirements['date_popup_jquery_ui'] = array(
          'title' => $t('Date Popup requirements'),
          'value' => $t('The Date Popup module needs code added by the <a href="http://drupal.org/project/jquery_ui">jQuery UI module.</a> Install that module as soon as possible.'),
          'severity' => REQUIREMENT_WARNING,
          );
      }
      break;
  }
  return $requirements;
}

/**
 * Implementation of hook_install().
 */
function date_popup_install() {
  if (module_exists('content')) {
    drupal_load('module', 'content');
    if (!db_table_exists(content_instance_tablename())) {
      return;
    }
    if (module_exists('date_repeat')) {
      db_query("UPDATE {". content_instance_tablename() ."} SET widget_active=1 WHERE widget_type='%s' OR widget_type='%s'", 'date_popup', 'date_popup_repeat');
    }
    else {
      db_query("UPDATE {". content_instance_tablename() ."} SET widget_active=1 WHERE widget_type='%s'", 'date_popup');
    }
    content_clear_type_cache(TRUE);
  }
}

/**
 * Implementation of hook_uninstall().
 */
function date_popup_uninstall() {
  if (module_exists('content')) {
    drupal_load('module', 'content');
    if (!db_table_exists(content_instance_tablename())) {
      return;
    }
    if (module_exists('date_repeat')) {
      db_query("UPDATE {". content_instance_tablename() ."} SET widget_active=0 WHERE widget_type='%s' OR widget_type='%s'", 'date_popup', 'date_popup_repeat');
    }
    else {
      db_query("UPDATE {". content_instance_tablename() ."} SET widget_active=0 WHERE widget_type='%s'", 'date_popup');
    }
    content_clear_type_cache(TRUE);
  }
}

/**
 * Implementation of hook_enable().
 */
function date_popup_enable() {
  if (module_exists('content')) {
    drupal_load('module', 'content');
    if (!db_table_exists(content_instance_tablename())) {
      return;
    }
    if (module_exists('date_repeat')) {
      db_query("UPDATE {". content_instance_tablename() ."} SET widget_active=1 WHERE widget_type='%s' OR widget_type='%s'", 'date_popup', 'date_popup_repeat');
    }
    else {
      db_query("UPDATE {". content_instance_tablename() ."} SET widget_active=1 WHERE widget_type='%s'", 'date_popup');
    }
    content_clear_type_cache(TRUE);
  }
}

/**
 * Implementation of hook_disable().
 */
function date_popup_disable() {
  if (module_exists('content')) {
    drupal_load('module', 'content');
    if (!db_table_exists(content_instance_tablename())) {
      return;
    }
    if (module_exists('date_repeat')) {
      db_query("UPDATE {". content_instance_tablename() ."} SET widget_active=0 WHERE widget_type='%s' OR widget_type='%s'", 'date_popup', 'date_popup_repeat');
    }
    else {
      db_query("UPDATE {". content_instance_tablename() ."} SET widget_active=0 WHERE widget_type='%s'", 'date_popup');
    }
    content_clear_type_cache(TRUE);
  }
}

/**
 * Update default path for date popup css
 */
function date_popup_update_6001() {
  $ret = array();
  $version = function_exists('jquery_ui_get_version') ? jquery_ui_get_version() : '1.6';
  switch ($version) {
    case '1.6':
      $path = drupal_get_path('module', 'date_popup') . '/themes/datepicker.css';
      break;
    default:
      $path = drupal_get_path('module', 'date_popup') . '/themes/datepicker.1.7.css';
      break;
  }
  variable_set('date_popup_css_file', $path);
  if (!module_exists('jquery_ui')) {
    drupal_set_message('The jQuery UI module is now required when using Date Popup. See the <a href="http://drupal.org/project/jquery_ui">jQuery UI</a> page for installation instructions.', 'error');
  }
  return $ret;
}
