You are here

function draggableviews_update_7101 in DraggableViews 7.2

Upgrades the draggableviews_structure table from 1.x to 2.x (schema_version 7200). This will run if you have 7.x-1.x installed and are upgrading to 7.x-2.x. Otherwise this will be skipped. All of your views will need to be manually updated using the Views UI. Backup your data before you run this!

File

./draggableviews.install, line 85
Draggableviews defines a new database schema for saving the order.

Code

function draggableviews_update_7101() {
  if (!db_field_exists('draggableviews_structure', 'dvid')) {
    drupal_set_message(t("Draggableviews 1.x to 2.x upgrade script started."));

    // New schema for draggableviews 2.x (schema version 7200).
    $schema['draggableviews_structure_new'] = array(
      'description' => 'The table saves the order settings of an draggableview.',
      'fields' => array(
        'dvid' => array(
          'description' => 'The primary identifier.',
          'type' => 'serial',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ),
        'view_name' => array(
          'description' => 'Makes the order unique for each view.',
          'type' => 'varchar',
          'length' => 128,
          'not null' => TRUE,
          'default' => '',
        ),
        'view_display' => array(
          'description' => 'Makes the order unique for each view display.',
          'type' => 'varchar',
          'length' => 64,
          'not null' => TRUE,
          'default' => '',
        ),
        'args' => array(
          'description' => 'Makes the order unique for a given set of arguments',
          'type' => 'varchar',
          'length' => 255,
          'not null' => FALSE,
          'default' => '',
        ),
        'entity_id' => array(
          'description' => 'Id of the entity that we are sorting (node, user, etc.).',
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ),
        'weight' => array(
          'description' => 'The order weight.',
          'type' => 'int',
          'unsigned' => FALSE,
          'not null' => TRUE,
          'default' => 0,
        ),
        'parent' => array(
          'description' => 'The order parent.',
          'type' => 'int',
          'unsigned' => FALSE,
          'not null' => TRUE,
          'default' => 0,
        ),
      ),
      'unique keys' => array(
        'dvid' => array(
          'dvid',
        ),
      ),
      'primary key' => array(
        'dvid',
      ),
    );

    // Get the name of the views displays.
    $query_views = db_select('draggableviews_structure', 'dv')
      ->fields('dv', array(
      'view_name',
    ))
      ->distinct()
      ->execute()
      ->fetchCol();
    for ($i = 0; $i < count($query_views); $i++) {
      $view = views_get_view($query_views[$i]);
      foreach ($view->display as $view_display => $display) {

        // Condition: before upgrade to draggableviews 2.x and new views added.
        if (isset($display->display_options['style_plugin']) && $display->display_options['style_plugin'] == 'draggabletable') {

          // Can grab handler from $display->display_options['tabledrag_order'] (if its native or fieldAPI).
          $view_name = $query_views[$i];
          $draggableviews_info[$view_name][$view_display] = $view_display;
        }
        elseif (isset($display->display_options['fields']['draggableviews'])) {
          $view_name = $query_views[$i];
          $draggableviews_info[$view_name][$view_display] = $view_display;
        }
      }
    }

    // Get the old data out.
    $query_get_old = db_select('draggableviews_structure', 'dvs');
    $query_get_old
      ->join('draggableviews_structure', 'dvsd1', 'dvs.view_name = dvsd1.view_name and dvs.nid = dvsd1.nid');
    $query_get_old
      ->fields('dvs', array(
      'view_name',
      'nid',
      'args',
      'value',
    ))
      ->fields('dvsd1', array(
      'value',
    ));
    $query_get_old
      ->condition('dvs.delta', 0)
      ->condition('dvsd1.delta', 1);
    $query_get_old = $query_get_old
      ->execute();
    $query_set_new = db_insert('draggableviews_structure')
      ->fields(array(
      'view_name',
      'view_display',
      'args',
      'entity_id',
      'weight',
      'parent',
    ));
    foreach ($query_get_old
      ->fetchAll() as $record) {
      foreach ($draggableviews_info[$record->view_name] as $name => $display) {
        $new_record = array(
          'view_name' => $record->view_name,
          'view_display' => $display,
          'args' => '[]',
          'entity_id' => $record->nid,
          'weight' => $record->value,
          'parent' => $record->dvsd1_value,
        );
        $query_set_new
          ->values($new_record);
      }
    }

    // Drop the tables.
    db_drop_table('draggableviews_structure');
    db_drop_table('draggableviews_collapsed');

    // Create the table.
    db_create_table('draggableviews_structure', $schema['draggableviews_structure_new']);

    // Save the new formatted information.
    $query_set_new
      ->execute();
    drupal_set_message(t("Draggableviews 1.x to 2.x upgrade script finished."));
  }
  else {
    drupal_set_message(t("draggableviews_update_7101 skipped.  Database looks like its already been updated."));
  }
}