You are here

function bootstrap_tour_update_7200 in Bootstrap Tour 7.2

Upgrade old 1.x tours to the new 2.x Entity format, then delete the old DB table.

File

./bootstrap_tour.install, line 22

Code

function bootstrap_tour_update_7200() {
  module_load_include('inc', 'inline_entity_form', 'includes/entity.inline_entity_form');
  module_load_include('inc', 'bootstrap_tour', 'includes/bootstrap_tour.controller');
  if (!module_exists('inline_entity_form') && !module_enable(array(
    'inline_entity_form',
  ))) {

    // If you drop in 2.x and try to run updates, Drupal won't complain even though
    // 2.x has a hard dependency on Inline Entity Form. So, we check for that explicitly.
    throw new DrupalUpdateException(t('Bootstrap Tour 2.x requires the Inline Entity Form module which is not installed.'));
    return;
  }

  // Rename bootstrap_tour table so doesn't conflict
  // @see bootstrap_tour_schema().
  if (db_table_exists('bootstrap_tour')) {
    db_rename_table('bootstrap_tour', 'bootstrap_tour_old');
    drupal_get_complete_schema(TRUE);

    // Make sure relavent caches are clear
    cache_clear_all('features_api', 'cache');
    cache_clear_all('entity_info', 'cache', TRUE);
    drupal_static_reset('entity_get_info');
    drupal_static_reset('features_get_components');
    drupal_static_reset('features_get_components_by_key');
  }

  // First we need to create the two new tables.
  db_create_table('bootstrap_tour_tour', bootstrap_tour_db_tour_table());
  db_create_table('bootstrap_tour_step', bootstrap_tour_db_step_table());
  bootstrap_tour_setup_step_field();

  // Now we need to load all of the 1.x tours and re-save them as 2.x entities.
  $old_tours = bootstrap_tour_load_legacy_tours();
  foreach ($old_tours as $tour) {
    $tour = (array) $tour;
    $steps = $tour['steps'];
    unset($tour['steps']);

    // Steps are their own entities now so we need to save them as such.
    foreach ($steps as $index => $step) {
      $new_step = entity_create('bootstrap_tour_step', array(
        'path' => isset($step['path']) ? $step['path'] : '',
        'selector' => $step['selector'],
        'placement' => $step['placement'],
        'title' => $step['title'],
        'content' => $step['content'],
        'content_text_format' => $step['format'],
      ));
      $new_step
        ->save();
      if (!empty($new_step->bootstrap_tour_step_id)) {
        $tour['bootstrap_tour_step_reference'][LANGUAGE_NONE][$index]['target_id'] = $new_step->bootstrap_tour_step_id;
      }
    }
    $entity = entity_create('bootstrap_tour', $tour);
    $entity->old_tour = $tour;
    $entity
      ->save();
  }

  // Now we need to make sure the numbers of old vs. new match so that we can be
  // sure the upgrade went successfully. If so, we can delete the old DB table.
  $new_tours = entity_load('bootstrap_tour');
  if ($count = count($old_tours)) {
    if (count($new_tours) === $count) {
      db_drop_table('bootstrap_tour_old');
      return t('If you have any Tours in exported Features, you will have to recreate them now.');
    }
    else {
      throw new DrupalUpdateException(t('Bootstrap Tour failed to upgrade old tours to the new format.'));
    }
  }
}