<?php
// $Id: user_badges.install,v 1.2.2.3 2009/08/07 16:44:39 likeless Exp $

/**
 * @file
 * @brief User Badges install file
 * 
 * This file contains all the installation functions of the schema, tables and variables 
 * used by the module.
 *
 * @author Jeff Robbins (jjeff), http://drupal.org/user/17190
 * @author Chad Phillips (hunmonk), http://drupal.org/user/22079
 * @author Heine Deelstra (Heine), http://drupal.org/user/17943
 * @author Nuno Veloso (nunoveloso18), http://drupal.org/user/80656
 * @author Richard Skinner (Likeless), http://drupal.org/user/310635
 *
 * @warning For more information on licensing, read the LICENCE.txt file.
 *
 */

/**
 * Implementation of hook_schema()
 */
function user_badges_schema() {
  $schema = array();
  $schema['user_badges_badges'] = array(
    'description' => 'Defines the user badges themselves (image, weight etc.)',
    'fields' => array(
      'bid' => array(
        'description' => t('Original badge ID'),
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => t('Badge name'),
        'type' => 'varchar',
        'length' => 50,
        'not null' => TRUE,
        'default' => '',
      ),
      'image' => array(
        'description' => t('Associated image'),
        'type' => 'varchar',
        'length' => 80,
        'not null' => TRUE,
        'default' => '',
      ),
      'weight' => array(
        'description' => t('Order in list'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0
      ),
      'href' => array(
        'description' => t('Badge description URL'),
        'type' => 'varchar',
        'length' => 80,
        'not null' => FALSE,
        'default' => '',
      ),
    ),
    'primary key' => array('bid'),
    'indexes' => array(
      'bid_weight_name' => array('bid', 'weight', 'name'),
      'weight_name' => array('weight', 'name'),
    )
  );
  $schema['user_badges_roles'] = array(
    'description' => 'Stores which roles grant which badges.',
    'fields' => array(
      'rid' => array(
        'description' => t('Role ID'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'bid' => array(
        'description' => t('Badge ID'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array('bid', 'rid'),
  );
  $schema['user_badges_user'] = array(
    'description' => 'Stores which users have which badges.',
    'fields' => array(
      'uid' => array(
        'description' => t('User ID'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'bid' => array(
        'description' => t('Badge ID'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'type' => array(
        'description' => t("Whether set as part of the role, or individually assigned ('user', 'role')"),
        'type' => 'varchar',
        'length' => 20,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array('bid', 'uid', 'type'),
    'indexes' => array(
      'uid_bid' => array('uid', 'bid'),
      'type' => array('type'),
    ),
  );
  
  return $schema;
}


/**
 * Implementation of hook_install()
 */
function user_badges_install() {
  drupal_install_schema('user_badges');
}


/**
 * Implementation of hook_uninstall()
 */
function user_badges_uninstall() {
  // Delete files.
  $dir = file_create_path('badges');
  if ($dir) {
    $files = file_scan_directory($dir, '.*\.(gif|jpg|jpeg|png)', array('.', '..', 'CVS'), 0, FALSE);
    foreach ($files as $file) {
      file_delete($file->filename);
    }
  }
  
  // Delete the badges directory.
  if (file_exists($dir)) {
    rmdir($dir);
  }
  drupal_uninstall_schema('user_badges');
  variable_del('user_badges_showone');
  variable_del('user_badges_showblocked');
}


/**
 * Implementation of hook_update_N()
 * new DB structure
 */
function user_badges_update_6001() {
  $ret = array();
  db_drop_primary_key($ret, 'user_badges_user');
  db_add_primary_key($ret, 'user_badges_user', array('bid', 'uid', 'type'));
  db_add_index($ret, 'user_badges_user', 'type', array('type'));
  return $ret;
}


function user_badges_update_6100() {
  $ret = array();
  db_add_field($ret, 'user_badges_badges', 'href', 
    array('type' => 'varchar', 'length' => 80, 'not null' => FALSE, 'default' => ''));
  return $ret;
}

function user_badges_update_6101() {
  $ret = array();
  db_drop_index($ret, 'user_badges_product', 'bid_nid');
  db_drop_index($ret, 'user_badges_product', 'nid_bid');
  db_drop_index($ret, 'user_badges_roles', 'bid_nid');
  db_drop_index($ret, 'user_badges_roles', 'nid_bid');
  db_drop_index($ret, 'user_badges_user', 'bid_uid_type');
  return $ret;
}
