home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (fivestar.install)

This example Drupal source code file (fivestar.install) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, database, fivestar, fivestar_update_version, function, if, key, module, name, php, return, sql, version, where

The fivestar.install Drupal example source code

<?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;
      }
    }
    // We no longer save any settings if Fivestar is disabled.
    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;
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal fivestar.install source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.