<?php
/**
 * @file
 * Installation and uninstallation functions.
 */

/**
 * Implements hook_requirements().
 */
function chosen_requirements($phase) {
  $requirements = array();
  module_load_include('module', 'chosen');
  $t = get_t();
  $chosen_path = chosen_get_chosen_path();
  switch ($phase) {
    case 'runtime':
      if (!$chosen_path) {
        $requirements['chosen_js'] = array(
          'title' => $t('Chosen JavaScript file'),
          'severity' => REQUIREMENT_ERROR,
          'description' => $t('You need to download the !chosen and extract the entire contents of the archive into the %path directory on your server.', array(
            '!chosen' => l($t('Chosen JavaScript file'), CHOSEN_WEBSITE_URL),
            '%path' => 'sites/all/libraries')),
          'value' => $t('Not Installed'),
        );
      }
      else {
        $requirements['chosen_js'] = array(
          'title' => $t('Chosen JavaScript file'),
          'severity' => REQUIREMENT_OK,
          'value' => $t('Installed'),
        );
      }
      break;
  }
  return $requirements;
}

/**
 * Implements hook_uninstall().
 */
function chosen_uninstall() {
  // Delete created variables.
  variable_del('chosen_minimum_single');
  variable_del('chosen_minimum_multiple');
  variable_del('chosen_minimum_width');
  variable_del('chosen_search_contains');
  variable_del('chosen_jquery_selector');
  variable_del('chosen_placeholder_text_multiple');
  variable_del('chosen_placeholder_text_single');
  variable_del('chosen_no_results_text');
  variable_del('chosen_disabled_themes');
  variable_del('chosen_disable_search');
  variable_del('chosen_disable_search_threshold');
  variable_del('chosen_allow_single_deselect');
  variable_del('chosen_include');
}

/**
 * Implements hook_update_N().
 *
 * Transfer the old chosen minimum value to the new chosen minimum single and
 * chosen minimum multiple variables.
 */
function chosen_update_7201() {
  $chosen_minimum = variable_get('chosen_minimum', 20);
  variable_set('chosen_minimum_single', $chosen_minimum);
  variable_set('chosen_minimum_multiple', $chosen_minimum);
  variable_del('chosen_minimum');
}

/**
 * Fix variables that should be integers and not strings.
 */
function chosen_update_7202() {
  $variables = array(
    'chosen_minimum_single',
    'chosen_minimum_multiple',
    'chosen_disable_search_threshold',
  );
  foreach ($variables as $variable) {
    $value = variable_get($variable, 0);
    if (!is_numeric($value)) {
      variable_set($variable, 0);
    }
  }
}

/**
 * Implements hook_update_N().
 *
 * Update any option_select widgets with an empty Chosen setting
 * to the 'No preference' Chosen setting.
 */
function chosen_update_7203() {
  $field_names = db_query("SELECT field_name FROM {field_config_instance} WHERE data LIKE :widget", array(':widget' => '%' . db_like('options_select') . '%' . db_like('apply_chosen') . '%'))->fetchCol();
  if (empty($field_names)) {
    return;
  }

  $instances = field_read_instances(array('field_name' => $field_names));
  foreach ($instances as $instance) {
    if ($instance['widget']['type'] == 'options_select' && empty($instance['widget']['settings']['apply_chosen'])) {
      $instance['widget']['settings']['apply_chosen'] = '';
      field_update_instance($instance);
    }
  }
}

/**
 * Implements hook_update_N().
 *
 * Set the 'Disable the default Chosen theme' Chosen setting to the opposite
 * of the current general'Use the default chosen theme'
 * Chosen setting for all enabled themes.
 */
function chosen_update_7204() {
  module_load_include('inc', 'chosen', 'chosen.admin');

  $chosen_use_theme_setting = variable_get('chosen_use_theme', TRUE);

  // We now chose to disable the default Chosen CSS instead of deselecting
  // the 'Use the default chosen theme' option box.
  if (!$chosen_use_theme_setting) {
    $themes = system_list('theme');

    foreach ($themes as $theme) {
      // Only create options for enabled themes.
      if ($theme->status) {
        $options[$theme->name] = $theme->info['name'];
      }
    }

    variable_set('chosen_disabled_themes', array_keys($options));
  }

  // Delete the old variable.
  variable_del('chosen_use_theme');
}
