You are here

block_class.install in Block Class 6.2

Provides the (un)install and update logic for block_class.

File

block_class.install
View source
<?php

/**
 * @file
 * Provides the (un)install and update logic for block_class.
 */

/**
 * Implements hook_schema().
 */
function block_class_schema() {
  $schema['block_class'] = array(
    'fields' => array(
      'module' => array(
        'type' => 'varchar',
        'length' => '64',
        'not null' => TRUE,
        'description' => 'The module to which the block belongs.',
      ),
      'delta' => array(
        'type' => 'varchar',
        'length' => '32',
        'not null' => TRUE,
        'description' => "The ID of the module's blocks.",
      ),
      'css_class' => array(
        'type' => 'text',
        'size' => 'big',
        'description' => 'A serialized PHP array containing the classes for the block.',
      ),
    ),
    'primary key' => array(
      'module',
      'delta',
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 */
function block_class_install() {
  drupal_install_schema('block_class');
}

/**
 * Implements hook_uninstall().
 */
function block_class_uninstall() {
  drupal_uninstall_schema('block_class');
}

/**
 * Implements hook_update_N().
 *
 * Alters the structure of the block_class schema.
 */
function block_class_update_6100() {
  $status = array();

  // -- Update the schema.
  db_drop_primary_key($status, 'block_class');
  db_change_field($status, 'block_class', 'module', 'module', array(
    'type' => 'varchar',
    'length' => '64',
    'not null' => TRUE,
    'description' => t('The module to which the block belongs.'),
  ));
  db_change_field($status, 'block_class', 'css_class', 'css_class', array(
    'type' => 'text',
    'size' => 'big',
    'description' => t('A serialized PHP array containing the classes for the block.'),
  ));

  // -- Restore the primary key.
  db_add_primary_key($status, 'block_class', array(
    'module',
    'delta',
  ));

  // -- Convert the existing arbitrary CSS classes to the new format.
  $blocks = array();
  $result = db_query("SELECT module, delta, css_class FROM {block_class}");
  while ($row = db_fetch_array($result)) {
    $blocks[] = array(
      'module' => $row['module'],
      'delta' => $row['delta'],
      'css_class' => serialize(array(
        '_' => $row['css_class'],
      )),
    );
  }
  $key = array(
    'module',
    'delta',
    'theme',
  );
  foreach ($blocks as $block) {
    drupal_write_record('block_class', $block, $key);
  }
  return $status;
}