You are here

optimizely.install in Optimizely 7.2

Install, update and uninstall functions for the Optimizely module

File

optimizely.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the Optimizely module
 */

/**
 * Implements hook_schema().
 *
 * Called at both install and uninstall time, creates/deletes a custom table in
 * the database for the Optimizely module.
 */
function optimizely_schema() {
  $schema['optimizely'] = array(
    'description' => 'This table holds the Optimizely project / experiment
		  entries from the adminstration form.',
    'fields' => array(
      'oid' => array(
        'description' => 'The unique identifier of each Optimizely
				  project/experiment entry.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'project_title' => array(
        'description' => 'The title of each project.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
      ),
      'include' => array(
        'description' => 'Switch to include / exclude Optimizely snippet on
				  specific page paths.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 1,
      ),
      'enabled' => array(
        'description' => 'Switch to enabled / disabled Optimizely snippet entry.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'path' => array(
        'description' => 'Serialized array of paths where the Optimizely code
				  snippet appears',
        'type' => 'text',
        'size' => 'normal',
        'not null' => FALSE,
      ),
      'project_code' => array(
        'description' => 'Optimizely project code.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => FALSE,
        'default' => '',
      ),
    ),
    'primary key' => array(
      'oid',
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 *
 * Included in the process of adding a optimizely database table is the creation
 * of a default project entry in the table. The default entry is used to add an
 * initial javascript file (snippit) on a site wide basis. The Optimizely
 * account ID will need to be entered in the account setup page to complete the
 * default entry. The Optimizely site uses the account ID to generate the basic
 * javascipt file to be included on the site. Once additional projects /
 * experiments are created on the Optimizely site additional project entries can
 * be added to load the additional javascript files on specific site paths.
 * Selective loading of the Optimizely javascipt file helps in page load times
 * and the amount of custom Optimizely javascript in each Javascript include
 * file.
 */
function optimizely_install() {
  drupal_set_message(st('Optimizely database table has been created.'), 'status');

  // Add default entry - check to see if entry already exsists
  $default_entry_exists = (bool) db_query("\n\t  SELECT\n\t\t  project_title\n\t\tFROM\n\t\t  {optimizely}\n\t\tWHERE\n\t\t  oid = 1\n\t\t")
    ->fetchField();
  if ($default_entry_exists == TRUE) {
    drupal_set_message(st("A default entry found in the optimizely database table. Something funky is going on but it's not the end of the world."), 'warning');
  }
  else {

    // Create default entry
    $default_entry_created = (bool) db_insert('optimizely')
      ->fields(array(
      'project_title' => 'Default',
      'include' => 1,
      'enabled' => 0,
      'path' => serialize(array(
        '*',
      )),
      'project_code' => 0,
    ))
      ->execute();

    // Inform the administrator that a default snippet entry has been made.
    // Acount ID and access permisisons need to be configured
    if ($default_entry_created == TRUE) {
      drupal_set_message(st('A default project / experiment entry has been created. Next, enter your' . ' ' . l(st('Optimizely account ID'), 'admin/config/system/optimizely/settings') . '. There\'s also an ' . l(st('optimizely permission'), 'admin/people/permissions', array(
        'fragment' => 'module-optimizely',
      )) . ' ' . st('that can be set for specific roles to access the administration functionality.')), 'status');
    }
    else {
      drupal_set_message(st('An error was encountred added the default project entry for the Optimizely module.'), 'error');
    }
  }
}

/**
 * Implements hook_uninstall().
 *
 * Clean up / remove all data created by the module.
 */
function optimizely_uninstall() {

  // Remove all optimizely settings in the variable table
  $cleanup_status = (bool) db_delete('variable')
    ->condition('name', '%optimizely_%', 'LIKE')
    ->execute();
  if ($cleanup_status == TRUE) {
    drupal_set_message(st('Optimizely variables deleted.'), 'status');
  }
  else {
    drupal_set_message(st('No entries found when deleting Optimizely variables.'), 'warning');
  }

  // hook_uninstall removes schema automatically
  drupal_set_message(st('Optimizely database table dropped.'), 'status');
}

/**
 * User enabled module
 */
function optimizely_enable() {

  // Inform the user if the Optimizely account has been set
  $account_id = variable_get('optimizely_id', FALSE);
  if (!$account_id) {
    drupal_set_message(st('The Optimizely account ID has not been set, please added it to complete the default setup at') . ' ' . l(st('Account Settings'), 'admin/config/system/optimizely/settings') . '.', 'status');
  }
}

/**
 * Create database table for the Optimizely module.
 *
 * A default disbaled entry is made to support a base, site wide Optimizely
 * snippet / experiment.
 *
 * @parm
 *   &$sandbox
 */
function optimizely_update_7200(&$sandbox) {

  // Setup the new table
  drupal_install_schema('optimizely');

  // Create default entry
  // Load Optimizely account code - used for default project snippet ID for javascript file name
  $account_id = variable_get('optimizely_id', 0);
  $default_entry_created = (bool) db_insert('optimizely')
    ->fields(array(
    'project_title' => 'Default',
    'include' => 1,
    'enabled' => 0,
    'path' => serialize(array(
      '*',
    )),
    'project_code' => $account_id,
  ))
    ->execute();

  // Inform the administrator that a default snippet / project entry has been made. Acount ID and access permisisons need to be configured
  if ($default_entry_created) {
    drupal_set_message(st('A default project / experiment entry has been created. Next, enter your') . ' ' . l(st('Optimizely account ID'), 'admin/config/system/optimizely/settings') . ' ' . st('There\'s also an') . ' ' . l(st('optimizely permission'), 'admin/people/permissions#module-optimizely', array(
      'fragment' => 'module-optimizely',
    )) . ' ' . st('that can be set for specific roles to access the adminstration functionality.'), 'status');
  }
  else {
    drupal_set_message(st('An error was encountred adding the default project entry for the Optimizely module.'), 'error');
  }
}

/**
 * Disable all project entries to ensure that validatation tests are run on
 * each entry when enabled. Validation includes valid and unique paths.
 */
function optimizely_update_7210(&$sandbox) {

  // Update all database project entries to disabled
  $disabled_projects = (bool) db_update('optimizely')
    ->fields(array(
    'enabled' => 0,
  ))
    ->execute();
  drupal_set_message(st('All Optimizely project entries have been disabled. Reenable each project on the') . ' ' . l(st('project listing Adminstration page'), 'admin/config/system/optimizely') . ' ' . st('to ensure that the settings pass new validation functionality. Each entry must have valid paths that point to functional paths that are unique to the entry.'), 'status');
}

/**
 * Disable all project entries to ensure that validatation tests are run on
 * each entry when enabled. Validation includes valid and unique paths.
 *
 * Again...
 */
function optimizely_update_7211(&$sandbox) {

  // Update all database project entries to disabled
  $disabled_projects = (bool) db_update('optimizely')
    ->fields(array(
    'enabled' => 0,
  ))
    ->execute();
  drupal_set_message(st('All Optimizely project entries have been disabled. Reenable each project on the') . ' ' . l(st('project listing Adminstration page'), 'admin/config/system/optimizely') . ' ' . st('to ensure that the settings pass new validation functionality. Each entry must have valid paths that point to functional paths that are unique to the entry.'), 'status');
}

Functions

Namesort descending Description
optimizely_enable User enabled module
optimizely_install Implements hook_install().
optimizely_schema Implements hook_schema().
optimizely_uninstall Implements hook_uninstall().
optimizely_update_7200 Create database table for the Optimizely module.
optimizely_update_7210 Disable all project entries to ensure that validatation tests are run on each entry when enabled. Validation includes valid and unique paths.
optimizely_update_7211 Disable all project entries to ensure that validatation tests are run on each entry when enabled. Validation includes valid and unique paths.