View source
<?php
function responsive_preview_schema() {
$schema['responsive_preview'] = _responsive_preview_get_schema();
return $schema;
}
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'],
'@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;
}
function responsive_preview_install() {
$devices = _responsive_preview_get_devices();
$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;
}
}
function responsive_preview_update_7000() {
$devices = _responsive_preview_get_devices();
if (!db_table_exists('responsive_preview')) {
db_create_table('responsive_preview', _responsive_preview_get_schema());
}
$existing_devices = db_query("SELECT * FROM {responsive_preview}")
->fetchAllAssoc('name', PDO::FETCH_ASSOC);
$devices_old_format = variable_get('responsive_preview_devices', array());
foreach ($devices_old_format as $name => $data) {
if (array_key_exists($name, $existing_devices)) {
continue;
}
$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',
);
}
$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;
}
}
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',
),
),
);
}
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',
),
);
}