You are here

function node_gallery_update_7002 in Node Gallery 7

Migrate Node Gallery relationships to Drupal 7 relationship entities.

File

./node_gallery.install, line 275
Install, update and uninstall functions for the node_gallery module.

Code

function node_gallery_update_7002() {
  if (!db_table_exists('node_gallery_relationships_d6')) {
    return;
  }
  if (!module_exists('node_gallery_api')) {
    throw new DrupalUpdateException('This update requires Node Gallery API (node_gallery_api) module is enabled. Enable this module and run the update again.');
  }

  // Most sites only have 1 relationship, so assuming a small number of fields
  // is like going to be acceptable here.
  $result = db_query("SELECT * FROM {node_gallery_relationships_d6}");
  foreach ($result as $row) {
    $old_settings = unserialize($row->settings);
    $old_settings['view_gallery_full_image_display'] = unserialize($old_settings['view_gallery_full_image_display']);
    $old_settings['view_gallery_teaser_view_image_display'] = unserialize($old_settings['view_gallery_teaser_view_image_display']);
    $old_settings['view_navigator_image_display'] = unserialize($old_settings['view_navigator_image_display']);
    $settings = node_gallery_api_relationship_type_settings_defaults();
    $settings['relationship']['name'] = $old_settings['name'];
    $settings['relationship_type']['gallery_types'] = array(
      $row->gallery_type,
    );
    $settings['relationship_type']['item_types'] = array(
      $row->image_type,
    );
    $settings['item_view']['display_navigator'] = $old_settings['display_navigator'];
    $settings['item_view']['view_navigator_item_display'] = 'node_gallery_gallery_item_views:embed_5';
    $settings['view_modes']['full']['view'] = 'node_gallery_gallery_item_views:embed_1';
    $settings['view_modes']['teaser']['view'] = 'node_gallery_gallery_item_views:embed_6';
    $settings['manage_items']['items_fields'] = $old_settings['manage_images_fields'];
    $settings['manage_items']['items_per_page'] = (int) $old_settings['manage_images_per_page'];
    if ((bool) $old_settings['manage_images_show_gallery_list']) {
      $settings['manage_items']['item_fields']['node_gallery_ref_' . $row->rid] = 'node_gallery_ref_' . $row->rid;
    }

    // Check if exists to prevent primary key dupes. http://drupal.org/node/1872808
    $exists = db_query("SELECT COUNT(*) FROM {node_gallery_relationship_type} WHERE id = :id", array(
      ':id' => $row->rid,
    ))
      ->fetchField();
    if (!$exists) {
      $r = new NodeGalleryRelationshipType();
      $r->id = $row->rid;
      $r->is_new = TRUE;
      $r->settings = $settings;
      $r->label = $settings['relationship']['name'];
      $r->filefield_name = $row->imagefield_name;
      $r
        ->save();
    }

    // Enable the default display modes.
    node_gallery_setup_display_modes($row->image_type);

    // Reconfigure display settings to use the new view modes.
    node_gallery_create_thumbnail_node_view_mode($row->image_type, $row->imagefield_name);
  }
}