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

Drupal example source code file (votingapi.install)

This example Drupal source code file (votingapi.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, content_id, database, default, function, length, name, not, null, php, ret, sql, true, varchar

The votingapi.install Drupal example source code

<?php
// $Id: votingapi.install,v 1.21.4.11 2009/06/29 14:05:01 eaton Exp $

/**
 * @file
 * Installation file for VotingAPI module.
 */

function votingapi_schema() {
  $schema['votingapi_vote'] = array(
    'fields' => array(
      'vote_id' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
      'content_type' => array('type' => 'varchar', 'length' => 64, 'not null' => TRUE, 'default' => 'node'),
      'content_id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'value' => array('type' => 'float', 'not null' => TRUE, 'default' => 0),
      'value_type' => array('type' => 'varchar', 'length' => 64, 'not null' => TRUE, 'default' => 'percent'),
      'tag' => array('type' => 'varchar', 'length' => 64, 'not null' => TRUE, 'default' => 'vote'),
      'uid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'timestamp' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'vote_source' => array('type' => 'varchar', 'length' => 255),
    ),
    'primary key' => array('vote_id'),
    'indexes' => array(
      'content_uid' => array('content_type', 'content_id', 'uid'),
      'content_uid_2' => array('content_type', 'uid'),
      'content_source' => array('content_type', 'content_id', 'vote_source'),
      'content_value_tag' => array('content_type', 'content_id', 'value_type', 'tag'),
    ),
  );
  $schema['votingapi_cache'] = array(
    'fields' => array(
      'vote_cache_id' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
      'content_type' => array('type' => 'varchar', 'length' => 64, 'not null' => TRUE, 'default' => 'node'),
      'content_id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'value' => array('type' => 'float', 'not null' => TRUE, 'default' => 0),
      'value_type' => array('type' => 'varchar', 'length' => 64, 'not null' => TRUE, 'default' => 'percent'),
      'tag' => array('type' => 'varchar', 'length' => 64, 'not null' => TRUE, 'default' => 'vote'),
      'function' => array('type' => 'varchar', 'length' => 64, 'not null' => TRUE, 'default' => ''),
      'timestamp' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
    ),
    'primary key' => array('vote_cache_id'),
    'indexes' => array(
      'content' => array('content_type', 'content_id'),
      'content_function' => array('content_type', 'content_id', 'function'),
      'content_tag_func' => array('content_type', 'content_id', 'tag', 'function'),
      'content_vtype_tag' => array('content_type', 'content_id', 'value_type', 'tag'),
      'content_vtype_tag_func' => array('content_type', 'content_id', 'value_type', 'tag', 'function'),
    ),
  );
  return $schema;
}

function votingapi_install() {
  drupal_install_schema('votingapi');
}

function votingapi_uninstall() {
  drupal_uninstall_schema('votingapi');
}

/**
 * UTF8 update
 */
function votingapi_update_1() {
  return _system_update_utf8(array('votingapi_vote', 'votingapi_cache'));
}

/**
 * Value fields changed to signed floats.
 */
function votingapi_update_2() {
  $ret = array();
  db_drop_primary_key($ret, 'votingapi_cache');
  db_drop_primary_key($ret, 'votingapi_vote');
  db_change_field($ret, votingapi_cache, value, value, array('type' => 'float', 'default' => NULL), array('primary_key' => array('vote_cache_id')));
  db_change_field($ret, votingapi_vote, value, value, array('type' => 'float', 'default' => NULL), array('primary_key' => array('vote_id')));
  return $ret;
}

/**
 * Value fields changed to signed floats.
 */
function votingapi_update_3() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("UPDATE {votingapi_cache} SET value_type = 'percent' WHERE value_type = '1'");
      $ret[] = update_sql("UPDATE {votingapi_cache} SET value_type = 'points' WHERE value_type = '2'");
      $ret[] = update_sql("UPDATE {votingapi_cache} SET value_type = 'option' WHERE value_type = '3'");

      $ret[] = update_sql("UPDATE {votingapi_vote} SET value_type = 'percent' WHERE value_type = '1'");
      $ret[] = update_sql("UPDATE {votingapi_vote} SET value_type = 'points' WHERE value_type = '2'");
      $ret[] = update_sql("UPDATE {votingapi_vote} SET value_type = 'option' WHERE value_type = '3'");
      break;
  }

  return $ret;
}

/**
 * Initial work to roll Voting Actions functionality into Voting API.
 */

function votingapi_update_4() {
  $ret = array();
  $name = 'votingapi_action_set';
  $table_action_step = array(
    'fields' => array(
      'vasid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE),
      'parent' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE),
      'required' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
      'mask' => array('type' => 'varchar', 'default' => 'AND'),
      'content_type' => array('type' => 'varchar', 'length' => 20, 'default' => NULL),
      'name' => array('type' => 'varchar', 'length' => 128, 'default' => NULL),
      'enabled' => array('type' => 'int', 'default' => 1),
      'source' => array('type' => 'varchar', 'length' => 65, 'default' => NULL),
      'weight' => array('type' => 'int', 'length' => 10, 'not null' => TRUE, 'default' => 0),
    ),
    'primary key' => array('vasid'),
  );
  db_create_table($ret, $name, $table_action_set);

  $name = 'votingapi_action_condition';
  $table_action_condition = array(
    'fields' => array(
      'vacid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE),
      'vasid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE),
      'weight' => array('type' => 'int', 'length' => 10, 'not null' => TRUE, 'default' => 0),
      'value' => array('type' => 'varchar', 'length' => 255),
      'handler' => array('type' => 'varchar', 'length' => 255),
    ),
    'primary key' => array('vacid'),
  );
  db_create_table($ret, $name, $table_action_condition);

  $name = 'votingapi_action';
  $table_votingapi_action = array(
    'fields' => array(
      'vasid' => array('type' => 'int', 'length' => 10, 'unsigned' => TRUE, 'not null' => TRUE),
      'aid' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => 0),
    ),
  );
  db_create_table($ret, $name, $table_votingapi_action);

  return $ret;
}

/**
 * Fixed index definition, corrected table prefixes.
 */

function votingapi_update_5() {
  $ret = array();
  db_add_index($ret, '{votingapi_vote}', 'content', array('content_type', 'content_id'));
  db_add_index($ret, '{votingapi_cache}', 'content', array('content_type', 'content_id'));

  db_drop_index($ret, '{votingapi_vote}', 'content_type');
  db_drop_index($ret, '{votingapi_vote}', 'content_id');
  db_drop_index($ret, '{votingapi_cache}', 'content_type');
  db_drop_index($ret, '{votingapi_cache}', 'content_id');

  db_rename_table($ret, 'votingapi_action_condition', '{votingapi_action_condition}');
  db_rename_table($ret, 'votingapi_action', '{votingapi_action}');

  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("UPDATE {sequences} SET name = '{votingapi_cache}' WHERE name = 'votingapi_cache';");
      $ret[] = update_sql("UPDATE {sequences} SET name = '{votingapi_vote}' WHERE name = 'votingapi_vote';");
      break;
  }

  return $ret;
}

function votingapi_update_6() {
  $ret = array();

  db_add_column($ret, 'votingapi_action_condition', 'name', 'varchar', array('length' => 128));
  db_add_column($ret, 'votingapi_action_set', 'name', 'description', array('length' => 255, 'not null' => TRUE));
  db_change_column($ret, 'votingapi_action_condition', 'value', 'data', 'varchar', array('length' => 255));

  return $ret;
}


function votingapi_update_7() {
  // There are quite a few changes. Let's just take the easy way and nuke this puppy.
  // Nothing has been using the tables up to this point, anyhow.

  db_drop_table($ret, 'votingapi_action_set');
  db_drop_table($ret, 'votingapi_action_condition');
  db_drop_table($ret, 'votingapi_action');

  $name = 'votingapi_action_set';
  $table_votingapi_action_set = array(
    'fields' => array(
      'name' => array('type' => 'varchar', 'length' => 64, 'not null' => TRUE),
      'parent_name' => array('type' => 'varchar', 'default' => NULL),
      'content_type' => array('type' => 'varchar', 'length' => 20, 'default' => NULL),
      'source' => array('type' => 'varchar', 'length' => 64, 'default' => NULL),
      'description' => array('type' => 'varchar', 'length' => 255, 'default' => NULL),
      'required' => array('type' => 'int', 'length' => 8, 'not null' => TRUE, 'default' => 0),
      'criteria_mask' => array('type' => 'varchar', 'length' => 8, 'default' => 'AND'),
      'weight' => array('type' => 'int', 'length' => 10, 'not null' => TRUE, 'default' => 0),
    ),
    'primary_key' => array('name'),
  );
  db_create_table($ret, $name, $table_votingapi_action_set);

  $name = 'votingapi_action_condition';
  $table_votingapi_action_condition = array(
    'fields' => array(
      'name' => array('type' => 'varchar', 'length' => 64, 'length' => 64),
      'parent_name' => array('type' => 'varchar', 'default' => NULL),
      'description' => array('type' => 'varchar', 'length' => 255, 'default' => NULL),
      'data' => array('type' => 'varchar', 'length' => 255),
      'handler' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
    ),
    'primary key' => array('name'),
  );
  db_create_table($ret, $name, $table_votingapi_action_condition);

  $name = 'votingapi_action';
  $table_votingapi_action = array(
    'fields' => array(
      'parent_name' => array('type' => 'varchar', 'length' => 64, 'length' => 64),
      'aid' => array('type' => 'varchar', 'length' => 255, 'not null' => 255),
    ),
  );
  db_create_table($ret, $name, $table_votingapi_action);

  return $ret;
}

function votingapi_update_8() {
  // whoops. mis-named column.
  $ret = array();
  db_change_column($ret, 'votingapi_action_set', 'criteria_mask', 'condition_mask', 'varchar', array('length' => 8, 'default' => 'AND'));
  return $ret;
}

function votingapi_update_9() {
  $ret = array();

  db_add_column($ret, 'votingapi_cache', 'timestamp', 'int', array('length' => 11, 'default' => NULL));

  $ret[] = update_sql("UPDATE {votingapi_cache} SET timestamp = " . time() . " WHERE timestamp IS NULL;");
  return $ret;
}

function votingapi_update_6100() {
  $ret = array();

  // Convert vote_id and vote_cache_id columns to auto increment.
  db_drop_primary_key($ret, 'votingapi_cache');
  db_change_field($ret, 'votingapi_cache', 'vote_cache_id', 'vote_cache_id', array('type' => 'serial', 'not null' => TRUE), array('primary key' => array('vote_cache_id')));

  db_drop_primary_key($ret, 'votingapi_vote');
  db_change_field($ret, 'votingapi_vote', 'vote_id', 'vote_id', array('type' => 'serial', 'not null' => TRUE), array('primary key' => array('vote_id')));

  // Rename the 'hostname' field to 'vote_source' for more flexibility.
  db_change_field($ret, 'votingapi_vote', 'hostname', 'vote_source', array('type' => 'varchar', 'length' => 255));

  $indexes = array(
    'content_uid' => array('content_type', 'content_id', 'uid'),
    'content_source' => array('content_type', 'content_id', 'vote_source'),
    'content_vtype' => array('content_type', 'content_id', 'value_type'),
    'content_value_tag' => array('content_type', 'content_id', 'value_type', 'tag'),
  );
  foreach ($indexes as $name => $fields) {
    db_add_index($ret, 'votingapi_vote', $name, $fields);
  }

  $indexes = array(
    'content_function' => array('content_type', 'content_id', 'function'),
    'content_tag_func' => array('content_type', 'content_id', 'tag', 'function'),
    'content_vtype_tag' => array('content_type', 'content_id', 'value_type', 'tag'),
    'content_vtype_tag_func' => array('content_type', 'content_id', 'value_type', 'tag', 'function'),
  );
  foreach ($indexes as $name => $fields) {
    db_add_index($ret, 'votingapi_cache', $name, $fields);
  }

  // Yes, we loves us the SchemaAPI.
  return $ret;
}

// More index tweaking for votingapi_vote table.
// It's not really kosher, but this update was *altered* after the release of version 2.0
// to correct a typo. Better to fix it in-place than leave an actual bug.
function votingapi_update_6101() {
  $ret = array();
  db_drop_index($ret, 'votingapi_vote', 'content');
  db_drop_index($ret, 'votingapi_vote', 'content_vtype');
  db_add_index($ret, 'votingapi_vote', 'content_uid_2', array('content_type', 'uid'));
  return $ret;
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal votingapi.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.