You are here

migrate_webform.module in Migrate Webform 7

Migrate does not use this file. So global helper functions could go here.

File

migrate_webform.module
View source
<?php

/**
 * @file
 * Migrate does not use this file. So global helper functions could go here.
 *
 */
function migrate_webform_menu() {
  $items['admin/content/migrate/webform'] = array(
    'title' => 'Configure Webform Migration',
    'description' => 'Database configuration for Migrate Webform',
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'migrate_webform_admin_form',
    ),
    'access arguments' => array(
      'migration information',
    ),
    'weight' => 111,
  );
  return $items;
}

/**
 * Administrative form to set migration options.
 */
function migrate_webform_admin_form($form, &$form_state) {
  $form['migrate_webform_source_database'] = array(
    '#type' => 'textfield',
    '#title' => t('Source database key in settings.php'),
    '#description' => t('The source database key in settings.php that describes an external database from which the import should be done. If left as "default" the default database will be used (and it must have all the Webform tables in it.)'),
    '#default_value' => variable_get('migrate_webform_source_database', 'default'),
  );
  $form['migrate_webform_source_database_api_version'] = array(
    '#type' => 'select',
    '#title' => t('Source Database Drupal major version'),
    '#options' => array(
      'd6' => t('Drupal 6'),
    ),
    '#default_value' => variable_get('migrate_webform_source_database_api_version', 'd6'),
  );
  $filters = filter_formats();
  foreach ($filters as $machine_name => $info) {
    $options[$machine_name] = $info->name;
  }
  $form['migrate_webform_default_filter_format'] = array(
    '#type' => 'select',
    '#title' => t('Default filter format to be used if the source input format cannot be correlated to input formats in this Drupal system'),
    '#description' => t('The input filters in one Drupal installation may not map to this Drupal install. First, get all your filters set up to be the same. This filter will be used as the fallback. Note that this choice may have security implications, if you accidentally set the format to something you do not want, like "php".'),
    '#options' => $options,
    '#default_value' => variable_get('migrate_webform_default_filter_format', 'plain_text'),
  );
  $form['migrate_webform_user_map_ok'] = array(
    '#type' => 'checkbox',
    '#title' => t('User ownership should be mapped'),
    '#description' => t('Check this box if the users and user ids on this system are the same as in the source database. Uncheck it if user ownership of products and nodes should not be mapped, in which case they will be assigned to user 1.'),
    '#default_value' => variable_get('migrate_webform_user_map_ok', FALSE),
  );
  $form['migrate_webform_user_migration_class'] = array(
    '#type' => 'textfield',
    '#title' => t('Class name of migration to be used to map users'),
    '#description' => t('(optional) If mapping users to entities, a migration class may be specified, like "MigrateUser". Incoming uids will then be mapped using that migration; otherwise they will be used as is, which would work on an upgraded site. You must previously have migrated this migration, of course.'),
    '#default_value' => variable_get('migrate_webform_user_migration_class', ""),
    '#states' => array(
      'visible' => array(
        ':input[name=migrate_webform_user_map_ok]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['migrate_webform_file_map_ok'] = array(
    '#type' => 'checkbox',
    '#title' => t('Files should be mapped'),
    '#description' => t('Check this box if the files were attached to webform components. You will need to add the name of your Files migration here. If you do not have one, you can create one using migrate_d2d.'),
    '#default_value' => variable_get('migrate_webform_file_map_ok', FALSE),
  );
  $form['migrate_webform_file_migration_class'] = array(
    '#type' => 'textfield',
    '#title' => t('Class name of migration to be used to map files.'),
    '#description' => t('(optional) list all your Files migrations here, separated by spaces. Run the files migration(s) before importing for the files to be mapped correctly.'),
    '#default_value' => variable_get('migrate_webform_file_migration_class', ""),
    '#states' => array(
      'visible' => array(
        ':input[name=migrate_webform_file_map_ok]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['migrate_webform_node_map_ok'] = array(
    '#type' => 'checkbox',
    '#title' => t('Create a node migration by default'),
    '#description' => t('If you have already created a node migration which is handling your webform node content (for the title, body, path, etc) uncheck this box and give the name of the node migration you would like to use.'),
    '#default_value' => variable_get('migrate_webform_node_map_ok', TRUE),
  );
  $form['migrate_webform_node_migration_class'] = array(
    '#type' => 'textfield',
    '#title' => t('Class name of migration to be used to map nodes.'),
    '#description' => t('(optional) list all your nodes migrations that contain webform content types here, separated by spaces.'),
    '#default_value' => variable_get('migrate_webform_node_migration_class', ""),
    '#states' => array(
      'visible' => array(
        ':input[name=migrate_webform_node_map_ok]' => array(
          'checked' => FALSE,
        ),
      ),
    ),
  );
  $form['#submit'][] = 'migrate_webform_admin_form_submit';

  // custom callback name
  return system_settings_form($form);
}

/**
 * Make sure that we can actually access the database and directory provided
 */
function migrate_webform_admin_form_validate($form, &$form_state) {

  // Check to see whether we can access the database
  $migration_source_db = $form_state['values']['migrate_webform_source_database'];
  if (empty($GLOBALS['databases'][$migration_source_db])) {
    form_set_error('migrate_webform_source_database', t('The database key $databases["%key"] does not exist', array(
      '%key' => $migration_source_db,
    )));
  }
  else {
    try {
      $connection = Database::getConnection('default', $migration_source_db);
      $query = $connection
        ->select('webform', 'w')
        ->fields('w', array(
        'nid',
      ))
        ->countQuery()
        ->execute()
        ->fetchField();
    } catch (Exception $e) {
      form_set_error('migrate_webform_source_database', t('The database key $databases["%key"] is not functional (Exception message %msg', array(
        '%key' => $migration_source_db,
        '%msg' => $e
          ->getMessage(),
      )));
    }
    $x = 1;
  }
}
function migrate_webform_admin_form_submit($form, &$form_state) {
  migrate_static_registration();
  migrate_static_registration();
  drupal_set_message(t("If you do not see Webform Migrations on the dashboard try saving the settings a second time."), 'warning');
}

/**
 * Decide whether we have an external database and return the correct connection.
 *
 * If the migration source database exists, we will get a connection to it
 * instead of using the $databases['default'].
 */
function migrate_webform_get_source_connection() {
  $migration_source_db = variable_get('migrate_webform_source_database', 'default');
  if (empty($GLOBALS['databases'][$migration_source_db]['default'])) {
    $migration_source_db = NULL;
  }
  $connection = Database::getConnection('default', $migration_source_db);
  return $connection;
}
function migrate_webform_get_roles() {
  $query = Database::getConnection('default', variable_get('migrate_webform_source_database', 'default'))
    ->select('role', 'r')
    ->fields('r');
  foreach ($query
    ->execute() as $row) {
    $items[$row->rid]['value'] = $row->name;
  }
  $query = db_select('role', 'r')
    ->fields('r');
  foreach ($query
    ->execute() as $row) {
    foreach ($items as $old => $name) {
      if ($name['value'] == $row->name) {
        $items[$old]['new'] = $row->rid;
      }
    }
  }
  return $items;
}

Functions

Namesort descending Description
migrate_webform_admin_form Administrative form to set migration options.
migrate_webform_admin_form_submit
migrate_webform_admin_form_validate Make sure that we can actually access the database and directory provided
migrate_webform_get_roles
migrate_webform_get_source_connection Decide whether we have an external database and return the correct connection.
migrate_webform_menu @file Migrate does not use this file. So global helper functions could go here.