You are here

fivestar.install in Fivestar 6.2

Installation file for fivestar module.

File

fivestar.install
View source
<?php

/**
 * @file
 * Installation file for fivestar module.
 */
function fivestar_schema() {
  return array();
}
function fivestar_install() {
  drupal_install_schema('fivestar');
  if (module_exists('content')) {
    content_notify('install', 'fivestar');
  }
}
function fivestar_uninstall() {
  drupal_uninstall_schema('fivestar');
  db_query("DELETE FROM {variable} WHERE name LIKE 'fivestar_%'");
  if (module_exists('content')) {
    content_notify('uninstall', 'fivestar');
  }
}

/**
 * Implementation of hook_enable().
 */
function fivestar_enable() {
  if (module_exists('content')) {
    module_load_include('inc', 'fivestar', 'fivestar_field');
    content_notify('enable', 'fivestar');
  }
}

/**
 * Implementation of hook_disable().
 */
function fivestar_disable() {
  if (module_exists('content')) {
    content_notify('disable', 'fivestar');
  }
}

/**
 * Function to retrieve the current version to prevent duplicate updates.
 */
function fivestar_update_version() {
  static $version;
  if (!isset($version)) {
    $version = db_result(db_query("SELECT schema_version FROM {system} WHERE type = 'module' AND name = 'fivestar'"));
  }
  return $version;
}

/**
 * Add the fivestar comment tables.
 */
function fivestar_update_1() {

  // No longer needed. The tables are added by fivestar_commment_install().
  return array();
}

/**
 * Convert previous fivestar widget settings to new format.
 */
function fivestar_update_2() {
  $types = node_get_types('names');
  foreach ($types as $key => $name) {
    $style = variable_get('fivestar_style_' . $key, 'default');
    $enabled = variable_get('fivestar_' . $key, FALSE);

    // Split the display style into two variables for stars and text.
    if ($enabled) {
      switch ($style) {
        case 'default':
          variable_set('fivestar_style_' . $key, 'user');
          variable_set('fivestar_text_' . $key, 'average');
          break;
        case 'compact':
          variable_set('fivestar_style_' . $key, 'user');
          variable_set('fivestar_text_' . $key, 'none');
          break;
        case 'dual':
          variable_set('fivestar_text_' . $key, 'none');
          break;
      }
    }
    else {
      variable_del('fivestar_unvote_' . $key);
      variable_del('fivestar_style_' . $key);
      variable_del('fivestar_position_' . $key);
      variable_del('fivestar_position_teaser_' . $key);
    }
  }
  return array();
}

/**
 * Upgrade to Drupal 6 and VotingAPI 2.
 *
 * Remove anonymous vote interval from Fivestar, now handled by VotingAPI.
 */
function fivestar_update_6100() {

  // If using CCK, make sure it has updated first.
  if (function_exists('content_check_update') && ($abort = content_check_update('fivestar'))) {
    return $abort;
  }
  $ret = array();
  variable_del('fivestar_anonymous_vote_interval');
  $ret[] = array(
    'success' => TRUE,
    'query' => "variable_del('fivestar_anonymous_vote_interval')",
  );
  return $ret;
}

/**
 * Add vote_id column to the fivestar_comment table.
 */
function fivestar_update_6101() {

  // Moved to fivestar_commment_update_6100().
  return array();
}

/**
 * Set Fivestar weight to -1 so that it can load before content.module.
 */
function fivestar_update_6102() {
  $ret = array();

  // This update will already be run as fivestar_update_5701 on Drupal 5.
  if (fivestar_update_version() >= 6100) {
    $ret[] = update_sql("UPDATE {system} SET weight = -1 WHERE type = 'module' AND name = 'fivestar'");
  }
  return $ret;
}

/**
 * Move comment support to a separate module.
 */
function fivestar_update_6103() {

  // Comment support was added in schema version 1.
  if (fivestar_update_version() >= 6100) {

    // Enable the module, but don't run the install hook (tables already exist).
    if (module_exists('comment')) {
      module_rebuild_cache();
      module_enable(array(
        'fivestar_comment',
      ));
      $version = array_pop(drupal_get_schema_versions('fivestar_comment'));
      drupal_set_installed_schema_version('fivestar_comment', $version);
      module_rebuild_cache();
    }

    // If the comment table needs an update, run fivestar_comment_update_5100().
    if (fivestar_update_version() < 6101) {
      module_load_install('fivestar_comment');
      fivestar_comment_update_6100();
    }
  }
  return array();
}

/**
 * Update CCK target logic to rename "target" to "php_target".
 */
function fivestar_update_6104() {

  // Previously ran as fivestar_update_5703 on Drupal 5.
  // Also, don't run this update if CCK is not enabled.
  if (fivestar_update_version() >= 6100 && module_exists('content')) {
    $result = db_query("SELECT field_name, global_settings FROM {content_node_field} WHERE type = 'fivestar'");
    while ($field = db_fetch_object($result)) {
      $settings = unserialize($field->global_settings);
      if (!empty($settings['target'])) {
        if (is_numeric($settings['target'])) {

          // If previously a straight integer, just add a "return" to the number.
          $settings['php_target'] = 'return ' . $settings['target'];
        }
        else {

          // If already PHP code, remove the PHP brackets.
          $php = trim($settings['target']);
          $php = preg_replace('/^<\\?(php)?/', '', $php);
          $php = preg_replace('/\\?>$/', '', $php);
          $settings['php_target'] = $php;
        }
      }
      unset($settings['target']);
      unset($settings['php']);
      db_query("UPDATE {content_node_field} SET global_settings = '%s' WHERE field_name = '%s'", serialize($settings), $field->field_name);
    }
  }
  return array();
}

/**
 * Belated update for Drupal 6: Re-enable Fivestar CCK fields.
 */
function fivestar_update_6105() {
  $ret = array();
  if (!module_exists('content')) {
    $ret[] = array(
      'success' => TRUE,
      'query' => t('CCK is not installed. No update ran.'),
    );
    return $ret;
  }
  if ($abort = content_check_update('fivestar')) {
    return $abort;
  }

  // All CCK fields are disabled by Content module during the D6 upgrade.
  // Re-enable the Fivestar fields.
  module_load_include('inc', 'fivestar', 'includes/fivestar.field');
  content_associate_fields('fivestar');
  $ret[] = array(
    'success' => TRUE,
    'query' => t('Re-enabled Fivestar CCK fields.'),
  );
  return $ret;
}

Functions

Namesort descending Description
fivestar_disable Implementation of hook_disable().
fivestar_enable Implementation of hook_enable().
fivestar_install
fivestar_schema @file Installation file for fivestar module.
fivestar_uninstall
fivestar_update_1 Add the fivestar comment tables.
fivestar_update_2 Convert previous fivestar widget settings to new format.
fivestar_update_6100 Upgrade to Drupal 6 and VotingAPI 2.
fivestar_update_6101 Add vote_id column to the fivestar_comment table.
fivestar_update_6102 Set Fivestar weight to -1 so that it can load before content.module.
fivestar_update_6103 Move comment support to a separate module.
fivestar_update_6104 Update CCK target logic to rename "target" to "php_target".
fivestar_update_6105 Belated update for Drupal 6: Re-enable Fivestar CCK fields.
fivestar_update_version Function to retrieve the current version to prevent duplicate updates.