You are here

responsive_preview.install in Responsive Theme Preview 7

File

responsive_preview.install
View source
<?php

/**
 * @file
 * responsive_preview.install
 */

/**
 * Implements hook_schema().
 */
function responsive_preview_schema() {
  $schema['responsive_preview'] = _responsive_preview_get_schema();
  return $schema;
}

/**
 * Implements hook_requirements().
 */
function responsive_preview_requirements($phase) {
  $requirements = array();
  if ($phase == 'runtime') {
    $libraries = array(
      'underscore' => 'Underscore',
      'backbone' => 'Backbone',
    );
    foreach ($libraries as $lib => $label) {
      $requirements['responsive_preview_' . $lib] = array(
        'title' => t('Responsive preview: @library library', array(
          '@library' => $label,
        )),
        'value' => t('The @library library is not present', array(
          '@library' => $label,
        )),
        'severity' => REQUIREMENT_ERROR,
      );
      if (function_exists('libraries_detect')) {
        if (($library = libraries_detect($lib)) && !empty($library['installed'])) {
          $requirements['responsive_preview_' . $lib]['value'] = t('@version (@variant)', array(
            '@version' => $library['version'],
            // @todo This is problematic, because there is no way to enforce a
            //   certain variant.
            '@variant' => _responsive_preview_libraries_get_preferred_variant_name($library),
          ));
          $requirements['responsive_preview_' . $lib]['severity'] = REQUIREMENT_OK;
        }
        elseif (!empty($library['error'])) {
          $requirements['responsive_preview_' . $lib]['description'] = $library['error message'];
        }
      }
    }
  }
  return $requirements;
}

/**
 * Implements hook_install().
 */
function responsive_preview_install() {

  /* References:
   * - http://en.wikipedia.org/wiki/List_of_displays_by_pixel_density
   * - http://www.w3.org/blog/CSS/2012/06/14/unprefix-webkit-device-pixel-ratio/
   * - http://pieroxy.net/blog/2012/10/18/media_features_of_the_most_common_devices.html
   */
  $devices = _responsive_preview_get_devices();

  // Insert the preconfigured devices into the database.
  $query = db_insert('responsive_preview');
  $query
    ->fields(array(
    'name',
    'label',
    'width',
    'height',
    'dppx',
    'orientation',
    'weight',
    'status',
    'langcode',
  ));
  foreach ($devices as $device) {
    $query
      ->values($device);
  }
  $transaction = db_transaction();
  try {
    $query
      ->execute();
  } catch (Exception $e) {
    $transaction
      ->rollback();
    watchdog_exception('responsive_preview', $e);
    throw $e;
  }
}

/**
 * Update configuration to the DB schema.
 */
function responsive_preview_update_7000() {
  $devices = _responsive_preview_get_devices();

  // Create the responsive_preview db table.
  if (!db_table_exists('responsive_preview')) {
    db_create_table('responsive_preview', _responsive_preview_get_schema());
  }

  // Get a list of existing devices in the DB. This might have happened if
  // a user updated the code but didn't run update db and somehow "forced" the
  // module to work and added some devices.
  $existing_devices = db_query("SELECT * FROM {responsive_preview}")
    ->fetchAllAssoc('name', PDO::FETCH_ASSOC);

  // Insert devices.
  $devices_old_format = variable_get('responsive_preview_devices', array());
  foreach ($devices_old_format as $name => $data) {

    // Make sure the name of the device isn't already in the database. Skip it
    // if it is.
    if (array_key_exists($name, $existing_devices)) {
      continue;
    }

    // Store the converted device information. Prefer this data to the default
    // device definition because there exists the chance that a user altered
    // the device information. They can always edit the data later.
    $devices[$name] = array(
      'name' => $name,
      'label' => $data['label'],
      'width' => !empty($data['dimensions']['width']) ? $data['dimensions']['width'] : 0,
      'height' => !empty($data['dimensions']['height']) ? $data['dimensions']['height'] : 0,
      'dppx' => !empty($data['dimensions']['dppx']) ? $data['dimensions']['dppx'] : 0,
      'orientation' => $data['orientation'],
      'weight' => !empty($devices[$name]['weight']) ? $devices[$name]['weight'] : 0,
      'status' => !empty($devices[$name]['status']) ? $devices[$name]['status'] : 0,
      'langcode' => 'en',
    );
  }

  // Insert the preconfigured devices into the database.
  $query = db_insert('responsive_preview');
  $query
    ->fields(array(
    'name',
    'label',
    'width',
    'height',
    'dppx',
    'orientation',
    'weight',
    'status',
    'langcode',
  ));
  foreach ($devices as $device) {
    $query
      ->values($device);
  }
  $transaction = db_transaction();
  try {
    $query
      ->execute();
  } catch (Exception $e) {
    $transaction
      ->rollback();
    watchdog_exception('responsive_preview', $e);
    throw $e;
  }
}

/**
 * Returns the schema defintion for the responsive_preview table.
 */
function _responsive_preview_get_schema() {
  return array(
    'description' => '',
    'fields' => array(
      'name' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Device machine name.',
      ),
      'label' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => "Human readable device label.",
      ),
      'width' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The width of the device screen in pixels.',
      ),
      'height' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The height of the device screen in pixels.',
      ),
      'dppx' => array(
        'type' => 'float',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The number of dots per pixel unit.',
      ),
      'orientation' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
        'description' => "The default orientation of the device.",
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Weight, used in lists of devices.',
      ),
      'status' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'Whether a device is available to end users for previewing. (0 = no, 1 = yes)',
      ),
      'langcode' => array(
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
        'description' => "Language code, e.g. 'de' or 'en-US'.",
      ),
    ),
    'primary key' => array(
      'name',
    ),
    'indexes' => array(
      'list' => array(
        'weight',
        'name',
      ),
    ),
  );
}

/**
 * Returns the default devices available for preview.
 */
function _responsive_preview_get_devices() {
  return array(
    'small' => array(
      'name' => 'small',
      'label' => 'Smart phone',
      'width' => 768,
      'height' => 1280,
      'dppx' => 2,
      'orientation' => 'portrait',
      'weight' => 0,
      'status' => 0,
      'langcode' => 'en',
    ),
    'medium' => array(
      'name' => 'medium',
      'label' => 'Tablet',
      'width' => 800,
      'height' => 1280,
      'dppx' => 1.325,
      'orientation' => 'portrait',
      'weight' => 1,
      'status' => 0,
      'langcode' => 'en',
    ),
    'large' => array(
      'name' => 'large',
      'label' => 'Typical desktop',
      'width' => 1366,
      'height' => 768,
      'dppx' => 1,
      'orientation' => 'landscape',
      'weight' => 2,
      'status' => 0,
      'langcode' => 'en',
    ),
    'iphone6' => array(
      'name' => 'iphone6',
      'label' => 'iPhone 6',
      'width' => 750,
      'height' => 1334,
      'dppx' => 2,
      'orientation' => 'portrait',
      'weight' => 3,
      'status' => 1,
      'langcode' => 'en',
    ),
    'iphone6p' => array(
      'name' => 'iphone6p',
      'label' => 'iPhone 6+',
      'width' => 1080,
      'height' => 1980,
      'dppx' => 2.46,
      'orientation' => 'portrait',
      'weight' => 4,
      'status' => 1,
      'langcode' => 'en',
    ),
    'nexus5' => array(
      'name' => 'nexus5',
      'label' => 'Nexus 5',
      'width' => 1080,
      'height' => 1920,
      'dppx' => 3,
      'orientation' => 'portrait',
      'weight' => 5,
      'status' => 1,
      'langcode' => 'en',
    ),
    'nexus6' => array(
      'name' => 'nexus6',
      'label' => 'Nexus 6',
      'width' => 1440,
      'height' => 2560,
      'dppx' => 3.5,
      'orientation' => 'portrait',
      'weight' => 6,
      'status' => 1,
      'langcode' => 'en',
    ),
    'nexus9' => array(
      'name' => 'nexus9',
      'label' => 'Nexus 9',
      'width' => 1536,
      'height' => 2048,
      'dppx' => 2,
      'orientation' => 'portrait',
      'weight' => 7,
      'status' => 1,
      'langcode' => 'en',
    ),
  );
}

Functions

Namesort descending Description
responsive_preview_install Implements hook_install().
responsive_preview_requirements Implements hook_requirements().
responsive_preview_schema Implements hook_schema().
responsive_preview_update_7000 Update configuration to the DB schema.
_responsive_preview_get_devices Returns the default devices available for preview.
_responsive_preview_get_schema Returns the schema defintion for the responsive_preview table.