You are here

fivestar.install in Fivestar 5

Installation file for fivestar module.

File

fivestar.install
View source
<?php

/**
 * @file
 * Installation file for fivestar module.
 */
function fivestar_install() {

  // No install needed for Fivestar. Uses the VotingAPI tables.
}
function fivestar_uninstall() {
  db_query("DELETE FROM {variable} WHERE name LIKE '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();
}

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

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

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

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

    // 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() < 5701) {
      module_load_install('fivestar_comment');
      fivestar_comment_update_5100();
    }
  }
  return array();
}

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

  // Only run if CCK exists.
  if (module_exists('content')) {
    $result = db_query("SELECT field_name, global_settings FROM {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 {node_field} SET global_settings = '%s' WHERE field_name = '%s'", serialize($settings), $field->field_name);
    }
  }
  return array();
}

Functions

Namesort descending Description
fivestar_install @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_5701 Add vote_id column to the fivestar_comment table.
fivestar_update_5702 Move comment support to a separate module.
fivestar_update_5703 Update CCK target logic to rename "target" to "php_target".
fivestar_update_version Function to retrieve the current version to prevent duplicate updates.