You are here

function responsive_preview_update_7000 in Responsive Theme Preview 7

Update configuration to the DB schema.

File

./responsive_preview.install, line 94
responsive_preview.install

Code

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;
  }
}