You are here

function linkit_update_7300 in Linkit 7.3

Migrate settings from v2 to v3 if needed.

File

./linkit.install, line 93
Install, update and uninstall functions for the Linkit module.

Code

function linkit_update_7300() {
  if (!db_field_exists('linkit_profiles', 'role_rids')) {

    // Already 3.x, no need for migration from 2.x.
    return;
  }

  // Get old profiles.
  $old_profiles = db_query("SELECT * FROM {linkit_profiles} ORDER BY weight DESC");

  //Drop redundant fields
  db_drop_field('linkit_profiles', 'role_rids');
  db_drop_field('linkit_profiles', 'weight');
  db_add_field('linkit_profiles', 'profile_type', array(
    'type' => 'varchar',
    'length' => 128,
    'not null' => TRUE,
    'description' => 'The profile type.',
    'default' => '',
  ));
  db_add_field('linkit_profiles', 'admin_description', array(
    'type' => 'text',
    'size' => 'medium',
    'description' => 'Administrative description for this profile.',
  ));

  // Make sure our schema changes are reflected in the cached schema or
  // subsequent update hooks making use of it might fail.
  drupal_get_schema('linkit_profiles', TRUE);
  foreach ($old_profiles as $profile) {
    $data = unserialize($profile->data);

    // Rename the plugins
    $data['search_plugins'] = $data['plugins'];
    unset($data['plugins']);
    $data['attribute_plugins'] = $data['attributes'];
    unset($data['plugins']);
    $data['attribute_plugins']['target'] = array(
      'enabled' => 0,
      'weight' => -10,
    );

    // Add new plugins
    $data['insert_plugin'] = array(
      'plugin' => 'raw_url',
      'url_method' => 1,
    );

    // Remove reverse_menu_trail
    foreach ($data as $key => $item) {
      if (strstr($key, 'entity:')) {
        unset($data[$key]['reverse_menu_trail']);
      }
    }
    $profile->data = serialize($data);

    // All old profiles are migrated as field profiles
    // Do the update.
    db_update('linkit_profiles')
      ->fields(array(
      'data' => $profile->data,
      'profile_type' => "2",
    ))
      ->condition('pid', $profile->pid)
      ->execute();

    // Store the weightest profile
    $weightest_profile = clone $profile;

    // Insert an editor profile for every field profile
    // Copy the prfoile for latter usage
    $profile_editor = clone $profile;
    $profile_editor->pid = NULL;
    $data = unserialize($profile_editor->data);
    $data['text_formats'] = array(
      'full_html' => 'full_html',
      'filtered_html' => 0,
      'plain_text' => 0,
    );
    $profile_editor->data = serialize($data);
    $profile_editor->name = $profile_editor->name . '_editor';
    $profile_editor->admin_title = $profile_editor->admin_title . ' [editor]';
    $profile_editor->profile_type = 1;
    $profile_editor->admin_description = '';
    unset($profile_editor->role_rids, $profile_editor->weight);
    db_insert('linkit_profiles')
      ->fields((array) $profile_editor)
      ->execute();
  }

  // Update the field instances with the weightest profile
  $instances_info = field_info_instances();
  foreach ($instances_info as $entity_type_name => $entity_type) {
    foreach ($entity_type as $bundle_name => $bundle) {
      foreach ($bundle as $field_name => $field) {
        if (isset($field['settings'], $field['settings']['linkit'])) {
          $settings =& $field['settings']['linkit'];
          $settings['button_text'] = 'Search';
          unset($settings['insert_plugin']);
          if ($settings['enable']) {
            $settings['profile'] = $weightest_profile->name;
          }
          else {
            $settings['profile'] = '';
          }
          field_update_instance($field);
        }
      }
    }
  }

  // Rebuild code registry so the LinkitProfile class is found.
  registry_rebuild();
}