You are here

cpn.install in Code per Node 6

Same filename and directory in other branches
  1. 7 cpn.install

Installation, schema and update hook implementations.

File

cpn.install
View source
<?php

/**
 * @file
 * Installation, schema and update hook implementations.
 */

/**
 * Implementation of hook_schema().
 */
function cpn_schema() {
  $schema['cpn'] = array(
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'css' => array(
        'type' => 'text',
        'not null' => FALSE,
        'size' => 'big',
      ),
      'js' => array(
        'type' => 'text',
        'not null' => FALSE,
        'size' => 'big',
      ),
    ),
    'primary key' => array(
      'nid',
    ),
  );
  return $schema;
}

/**
 * Implementation of hook_schema_alter().
 */
function cpn_schema_alter(&$schema) {
  $schema['blocks']['fields']['css'] = array(
    'type' => 'text',
    'not null' => FALSE,
  );
  $schema['blocks']['fields']['js'] = array(
    'type' => 'text',
    'not null' => FALSE,
  );
}

/**
 * Implementation of hook_install().
 */
function cpn_install() {
  drupal_install_schema('cpn');

  // Alter blocks table, as defined in hook_schema_alter().
  $ret = array();
  $schema = array();
  cpn_schema_alter($schema);
  foreach ($schema['blocks']['fields'] as $name => $spec) {
    db_add_field($ret, 'blocks', $name, $spec);
  }
}

/**
 * Implementation of hook_uninstall().
 */
function cpn_uninstall() {
  drupal_uninstall_schema('cpn');

  // Drop fields from blocks table, as defined in hook_schema_alter().
  $ret = array();
  $schema = array();
  cpn_schema_alter($schema);
  foreach ($schema['blocks']['fields'] as $name => $spec) {
    db_drop_field($ret, 'blocks', $name);
  }

  // Delete CSS & JS files.
  cpn_recursive_delete(file_create_path(variable_get('cpn_path', 'cpn')));

  // Delete the variables.
  variable_del('cpn_aggregation_css');
  variable_del('cpn_aggregation_js');
  variable_del('cpn_global_css');
  variable_del('cpn_global_css_admin');
  variable_del('cpn_global_css_agree');
  variable_del('cpn_global_js');
  variable_del('cpn_global_js_admin');
  variable_del('cpn_global_js_agree');
  variable_del('cpn_path');
  variable_del('cpn_syntax_highlighting');
  variable_del('cpn_wrapper_block_css');
  variable_del('cpn_wrapper_block_js');
  variable_del('cpn_wrapper_node_css');
  variable_del('cpn_wrapper_node_js');

  // Delete the content type variables.
  foreach (array_keys(node_get_types('names')) as $node_type) {
    foreach (array(
      'css',
      'js',
    ) as $type) {
      variable_del('cpn_' . $type . '_enabled_' . $node_type);
      variable_del('cpn_' . $type . '_' . $node_type);
    }
  }
}

/**
 * Recursive delete function.
 */
function cpn_recursive_delete($path) {
  if (is_file($path) or is_link($path)) {
    unlink($path);
  }
  elseif (is_dir($path)) {
    $d = dir($path);
    while (($entry = $d
      ->read()) !== FALSE) {
      if ($entry == '.' or $entry == '..') {
        continue;
      }
      $entry_path = $path . '/' . $entry;
      cpn_recursive_delete($entry_path);
    }
    $d
      ->close();
    rmdir($path);
  }
  else {
    watchdog('cpn', 'Unknown file type(%path) stat: %stat ', array(
      '%path' => $path,
      '%stat' => print_r(stat($path), 1),
    ), WATCHDOG_ERROR);
  }
}

/**
 * Adds fields in blocks table.
 */
function cpn_update_6000() {
  $ret = array();
  db_add_field($ret, 'blocks', 'css', array(
    'type' => 'text',
    'not null' => FALSE,
  ));
  db_add_field($ret, 'blocks', 'js', array(
    'type' => 'text',
    'not null' => FALSE,
  ));
  return $ret;
}

/**
 * Make the CSS and JS fields long text fields.
 */
function cpn_update_6100() {
  $ret = array();
  $table = 'cpn';
  $field = 'css';
  $spec = array(
    'type' => 'text',
    'not null' => FALSE,
    'size' => 'big',
  );
  db_change_field($ret, $table, $field, $field, $spec);
  $field = 'js';
  $spec = array(
    'type' => 'text',
    'not null' => FALSE,
    'size' => 'big',
  );
  db_change_field($ret, $table, $field, $field, $spec);
  drupal_set_message(t("Expanded the CPN fields to have more space for custom code. That said, you really shouldn't insert that much code."));
  return $ret;
}

/**
 * Update variables to new format.
 */
function cpn_update_6101() {
  $ret = array();
  foreach (array_keys(node_get_types('names')) as $node_type) {
    foreach (array(
      'css',
      'js',
    ) as $type) {
      if (variable_get('cpn_' . $type . '_' . $node_type, FALSE)) {
        variable_set('cpn_' . $type . '_enabled_' . $node_type, TRUE);
        variable_del('cpn_' . $type . '_' . $node_type);
      }
    }
  }
  return $ret;
}

Functions

Namesort descending Description
cpn_install Implementation of hook_install().
cpn_recursive_delete Recursive delete function.
cpn_schema Implementation of hook_schema().
cpn_schema_alter Implementation of hook_schema_alter().
cpn_uninstall Implementation of hook_uninstall().
cpn_update_6000 Adds fields in blocks table.
cpn_update_6100 Make the CSS and JS fields long text fields.
cpn_update_6101 Update variables to new format.