You are here

uc_price_per_role.install in Ubercart Price Per Role 7

Same filename and directory in other branches
  1. 5 uc_price_per_role.install
  2. 6 uc_price_per_role.install

Install/deinstall hooks for uc_price_per_role.

File

uc_price_per_role.install
View source
<?php

/**
 * @file
 * Install/deinstall hooks for uc_price_per_role.
 */

/**
 * Implements hook_uninstall().
 */
function uc_price_per_role_uninstall() {
  variable_del('uc_price_per_role_enabled');
  variable_del('uc_price_per_role_weights');
}

/**
 * Implements hook_schema().
 */
function uc_price_per_role_schema() {
  $schema = array();
  $schema['uc_price_per_role_prices'] = array(
    'description' => 'Ubercart price per role',
    'fields' => array(
      'rpid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'vid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'rid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'price' => array(
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0.0,
      ),
    ),
    'primary key' => array(
      'rpid',
    ),
  );
  $schema['uc_price_per_role_option_prices'] = array(
    'description' => 'Ubercart price per role option prices',
    'fields' => array(
      'opid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'oid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'rid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'price' => array(
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0.0,
      ),
    ),
    'primary key' => array(
      'opid',
    ),
  );
  return $schema;
}

/**
 * Remove orphaned database entries and configuration for deleted roles.
 */
function uc_price_per_role_update_7100() {
  $roles = user_roles();
  $role_ids = array_keys($roles);

  // Remove deleted roles from uc_price_per_role_weights variable.
  $weights = variable_get('uc_price_per_role_weights', array());
  $weight_role_ids = array_keys($weights);
  foreach ($weight_role_ids as $weight_role_id) {
    if (!array_key_exists($weight_role_id, $roles)) {
      unset($weights[$weight_role_id]);
    }
  }
  variable_set('uc_price_per_role_weights', $weights);

  // Remove deleted roles from uc_price_per_role_enabled variable.
  // This should have the same indexes as uc_price_per_role_weights, but
  // repeat all the steps, just in case something is corrupted.
  $enabled = variable_get('uc_price_per_role_enabled', array());
  $enabled_role_ids = array_keys($enabled);
  foreach ($enabled_role_ids as $enabled_role_id) {
    if (!array_key_exists($enabled_role_id, $roles)) {
      unset($enabled[$enabled_role_id]);
    }
  }
  variable_set('uc_price_per_role_enabled', $enabled);

  // Remove data for deleted roles from the {uc_price_per_role_prices} table.
  db_delete('uc_price_per_role_prices')
    ->condition('rid', $role_ids, 'NOT IN')
    ->execute();

  // Remove deleted roles from the {uc_price_per_role_option_prices} table.
  db_delete('uc_price_per_role_option_prices')
    ->condition('rid', $role_ids, 'NOT IN')
    ->execute();
}

/**
 * Implements hook_update_last_removed().
 */
function uc_price_per_role_update_last_removed() {
  return 6000;
}

Functions

Namesort descending Description
uc_price_per_role_schema Implements hook_schema().
uc_price_per_role_uninstall Implements hook_uninstall().
uc_price_per_role_update_7100 Remove orphaned database entries and configuration for deleted roles.
uc_price_per_role_update_last_removed Implements hook_update_last_removed().