You are here

css_injector.install in CSS Injector 7.2

Same filename and directory in other branches
  1. 6 css_injector.install
  2. 7 css_injector.install

css_injector.install

Install, update and uninstall functions for the css_injector module.

File

css_injector.install
View source
<?php

/**
 * @file css_injector.install
 *
 * Install, update and uninstall functions for the css_injector module.
 */

/**
 * Implements hook_schema().
 */
function css_injector_schema() {
  $schema['css_injector_rule'] = array(
    'description' => t('Table storing CSS Injector rule definitions.'),
    'export' => array(
      'key' => 'name',
      'primary key' => 'crid',
      'identifier' => 'rule',
      // Exports will be defined as $rule
      'default hook' => 'css_injector_rule',
      'save callback' => 'css_injector_rule_save',
      'delete callback' => 'css_injector_rule_delete',
      'api' => array(
        'owner' => 'css_injector',
        'api' => 'css_injector_rules',
        // Base name for api include files.
        'minimum_version' => 1,
        'current_version' => 1,
      ),
    ),
    'fields' => array(
      'name' => array(
        'type' => 'varchar',
        'length' => '255',
        'description' => 'Unique ID for injections. Used to identify them programmatically.',
      ),
      'crid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The primary identifier injection',
        'no export' => TRUE,
      ),
      'admin_description' => array(
        'type' => 'varchar',
        'length' => '255',
        'description' => 'A human readable name of a rule.',
      ),
      'css' => array(
        'type' => 'text',
        'size' => 'big',
        'description' => 'The actual CSS code.',
      ),
      'media' => array(
        'type' => 'varchar',
        'length' => '255',
        'description' => 'Media type for the stylesheet, e.g., all, print, screen.',
      ),
      'preprocess' => array(
        'description' => 'Boolean indicating whether the injection should be aggregated.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'inline' => array(
        'description' => 'Boolean indicating whether the rules should be inline (cannot be aggregated).',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'page_visibility' => array(
        'description' => 'Boolean indicating whether the rule has a white or black list for page visibility.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'page_visibility_pages' => array(
        'type' => 'text',
        'size' => 'big',
        'description' => 'A list of pages to either hide or show the injection.',
      ),
    ),
    'primary key' => array(
      'crid',
    ),
    'unique keys' => array(
      'name' => array(
        'name',
      ),
    ),
  );
  return $schema;
}

/*
 * Implements hook_install().
 *
 * This is required to create required files directory.
 */
function css_injector_install() {
  if (!css_injector_prepare_directory()) {
    drupal_set_message(t('The CSS injector cache directory could not be created. Please see the <a href="!url">status report</a> for more detail.', array(
      '!url' => 'admin/reports/status',
    )), 'error');
  }
}

/**
 * Implements hook_uninstall().
 *
 * This is required to cleanup the left over files.
 */
function css_injector_uninstall() {

  // Clean up the directory and all rules, we first need to include the module
  // file that defines the constant.
  module_load_include('module', 'css_injector');
  file_unmanaged_delete_recursive(drupal_realpath(CSS_INJECTOR_DIRECTORY_URI));
}

/**
 * Implements hook_requirements().
 *
 * We'll use this to prevent installation of the module if the file directory
 * is not available and writable.
 */
function css_injector_requirements($phase) {
  $requirements = array();
  if ($phase == 'runtime') {
    $requirements['css_injector_cache_dir'] = array(
      'title' => t('CSS injector cache dir'),
      'severity' => REQUIREMENT_OK,
      'value' => t('Exists'),
    );
    if (!css_injector_prepare_directory()) {
      $requirements['css_injector_cache_dir']['description'] = t('The CSS injector cache directory, %path could not be created due to a misconfigured files directory. Please ensure that the files directory is correctly configured and that the webserver has permission to create directories.', array(
        '%path' => file_uri_target(CSS_INJECTOR_DIRECTORY_URI),
      ));
      $requirements['css_injector_cache_dir']['severity'] = REQUIREMENT_ERROR;
      $requirements['css_injector_cache_dir']['value'] = t('Unable to create');
    }
  }
  return $requirements;
}

/**
 * Make sure that the css_injector files directory has been created.
 *
 * @return bool TRUE if the directory exists (or was created) and is writable.
 *              FALSE otherwise.
 */
function css_injector_prepare_directory() {
  $path = CSS_INJECTOR_DIRECTORY_URI;
  return file_prepare_directory($path, FILE_CREATE_DIRECTORY);
}

/**
 * Change the title field to name in css_injector_rule table
 */
function css_injector_update_7201(&$sandbox) {

  // Quick sanity check to ensure the column exists.
  if (db_field_exists('css_injector_rule', 'title')) {
    db_change_field('css_injector_rule', 'title', 'name', array(
      'type' => 'varchar',
      'length' => '255',
      'description' => 'Unique ID for rules. Used to identify them programmatically.',
    ));
  }
}

/**
 * Add the new fields to the css_injector_rule table
 */
function css_injector_update_7202(&$sandbox) {

  // add field so schema supports
  //   1.x
  //   crid
  //   title
  //   rule_type
  //   rule_conditions
  //   rule_themes
  //   media
  //   preprocess
  //   enabled
  //
  // 	2.x
  // 	crid
  // 	name
  // 	admin_description
  // 	css
  // 	media
  // 	preprocess
  // 	inline
  // 	page_visibility
  // 	page_visibility_pages
  //
  //   //changes
  //   title -> name (in previous hook)
  //   add css
  //   rule_conditions -> page_visibility and page_visibility_pages?
  //
  //   //what to do with
  //   rule_type?
  //   rule_themes?
  $fields = array(
    'admin_description' => array(
      'type' => 'varchar',
      'length' => '255',
      'description' => 'A human readable name of a rule.',
    ),
    'css' => array(
      'type' => 'text',
      'size' => 'big',
      'description' => 'The actual code.',
    ),
    'inline' => array(
      'description' => 'Boolean indicating whether the rules should be inline (cannot be aggregated).',
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
    ),
    'page_visibility' => array(
      'description' => 'Boolean indicating whether the rule has a white or black list for page visibility.',
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
    ),
    'page_visibility_pages' => array(
      'type' => 'text',
      'size' => 'big',
      'description' => 'A list of pages to either hide or show the CSS.',
    ),
  );
  foreach ($fields as $key => $field) {
    if (!db_field_exists('css_injector_rule', $key)) {
      db_add_field('css_injector_rule', $key, $field);
    }
  }
}

/**
 * Import the 1.x rules to the new 2.x structure
 */
function css_injector_update_7203(&$sandbox) {
  module_load_include('inc', 'ctools', 'includes/export');
  $results = db_query("SELECT * FROM {css_injector_rule}", array(), array(
    'fetch' => PDO::FETCH_ASSOC,
  ))
    ->fetchAllAssoc('crid');
  foreach ($results as $id => $rule) {
    $path = _css_injector_rule_uri($rule['crid']);
    if (file_exists($path)) {
      $css = file_get_contents($path);
    }
    else {
      $css = NULL;
      drupal_set_message(t('CSS Injector was unable to import CSS from file for @var
      during the update.  The structure of the rule was updated, but the CSS field will be empty.
      Because the expected .css file does not exist and the CSS was only stored in that file,
      there is no way to recover this data.  The 2.x version of CSS Injector now stores the
      CSS in the database in addition to writing it to a file.', array(
        '@var' => $rule['name'],
      )), 'warning');
    }
    $names = array(
      $rule['name'],
    );
    $newrule = ctools_export_load_object('css_injector_rule', 'names', $names);
    $newrule[$rule['name']]->css = $css;
    css_injector_rule_save($newrule[$rule['name']]);
  }
}

/**
 * remove NOT NULL requirement on rule_conditions field
 */
function css_injector_update_7205(&$sandbox) {
  if (db_field_exists('css_injector_rule', 'rule_conditions')) {
    db_change_field('css_injector_rule', 'rule_conditions', 'rule_conditions', array(
      'description' => 'LEGACY data to evaluate when determining if the CSS should be injected',
      'type' => 'text',
      'not null' => FALSE,
    ));
  }
}

Functions

Namesort descending Description
css_injector_install
css_injector_prepare_directory Make sure that the css_injector files directory has been created.
css_injector_requirements Implements hook_requirements().
css_injector_schema Implements hook_schema().
css_injector_uninstall Implements hook_uninstall().
css_injector_update_7201 Change the title field to name in css_injector_rule table
css_injector_update_7202 Add the new fields to the css_injector_rule table
css_injector_update_7203 Import the 1.x rules to the new 2.x structure
css_injector_update_7205 remove NOT NULL requirement on rule_conditions field