You are here

function migrate_content_set_mappings in Migrate 6

Menu callback function for content set edit page.

1 string reference to 'migrate_content_set_mappings'
migrate_menu in ./migrate.module
Implementation of hook_menu().

File

./migrate_pages.inc, line 466

Code

function migrate_content_set_mappings($form_state, $mcsid) {
  $row = db_fetch_object(db_query("SELECT * FROM {migrate_content_sets}\n                                   WHERE mcsid=%d", $mcsid));
  $desttype = $row->desttype;
  $view_name = $row->view_name;
  $view_args = $row->view_args;
  $sourcekey = $row->sourcekey;
  $machine_name = $row->machine_name;
  $description = $row->description;
  $contenttype = $row->contenttype;
  $desttype = $row->desttype;
  $weight = $row->weight;
  $multiple_separator = $row->multiple_separator;
  drupal_set_title(check_plain($description));

  // Fetch information on available destinations
  $desttypes = migrate_invoke_all('types');
  $destfields = migrate_invoke_all("fields_{$contenttype}", $desttype);
  $form['mcsid'] = array(
    '#type' => 'value',
    '#value' => $mcsid,
  );
  $form['machine_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Content set name'),
    '#size' => 24,
    '#maxlength' => 50,
    '#default_value' => $machine_name,
    '#description' => t('This is the unique name of the content set. It must contain
      only alphanumeric characters and underscores; it is used to identify the
      content set internally and to generate map and message tables related to
      the content set.'),
  );
  $form['description'] = array(
    '#type' => 'textfield',
    '#title' => t('Description of the content set'),
    '#default_value' => $description,
  );
  $form['show_view_name'] = array(
    '#prefix' => '<div>',
    '#value' => t('<strong>Source view:</strong> ') . l($view_name, 'admin/build/views/edit/' . $view_name),
    '#suffix' => '</div>',
  );
  $form['view_args'] = array(
    '#type' => 'textfield',
    '#title' => t('View arguments'),
    '#description' => t('Arguments to apply to the view when processing,
      separated with a / as though they were a URL path.'),
    '#default_value' => $view_args,
  );
  $form['view_name'] = array(
    '#type' => 'value',
    '#value' => $view_name,
  );
  if ($desttype) {
    $destination = $desttypes["{$contenttype}/{$desttype}"];
  }
  else {
    $destination = $desttypes[$contenttype];
  }
  $form['show_contenttype'] = array(
    '#prefix' => '<div>',
    '#value' => t('<strong>Destination:</strong> ') . $destination,
    '#suffix' => '</div>',
  );
  $form['contenttype'] = array(
    '#type' => 'value',
    '#value' => $contenttype,
  );
  $form['desttype'] = array(
    '#type' => 'value',
    '#value' => $desttype,
  );
  $form['weight'] = array(
    '#type' => 'textfield',
    '#title' => t('Weight'),
    '#description' => t('The order in which content sets will be processed and displayed.'),
    '#default_value' => $weight,
  );
  $form['multiple_separator'] = array(
    '#type' => 'textfield',
    '#title' => t('Separator'),
    '#description' => t('Separator for fields potentially holding multiple values, e.g. taxonomy terms.'),
    '#size' => 2,
    '#maxlength' => 8,
    '#default_value' => $multiple_separator,
  );
  $form['header'] = array(
    '#type' => 'value',
    '#value' => array(
      array(
        'data' => t('Source field'),
      ),
      array(
        'data' => t('Default value'),
      ),
      array(
        'data' => t('Destination field'),
      ),
    ),
  );
  $view = views_get_view($view_name);
  if (!$view) {
    drupal_set_message(t('View !view does not exist - either (re)create a view with
      this name, or delete this content set.', array(
      '!view' => $view_name,
    )));
  }
  else {

    // Need to fill in the query, to find out the aliases that will be returned by the
    // query
    if ($view_args) {
      $view
        ->set_arguments(explode('/', $view_args));
    }
    $view
      ->build();
    $fields = $view
      ->get_items('field');
    $srcoptions = array();
    foreach ($view->query->fields as $fieldalias => $field) {
      $fieldname = $field['field'];
      $fieldtable = $field['table'];

      // The field name can be ambiguous (e.g., two map tables in the view), so
      // we can't just do $fields[$fieldname] - we need to iterate and match the
      // table as well
      foreach ($fields as $viewfieldname => $viewfield) {
        if ($viewfield['field'] == $fieldname && $viewfield['table'] == $fieldtable) {
          $srcoptions[$fieldalias] = $viewfield['label'];
          break;
        }
      }
      if (!isset($srcoptions[$fieldalias])) {
        $srcoptions[$fieldalias] = $fieldtable . '.' . $fieldname;
      }
    }
    $form['sourcekey'] = array(
      '#type' => 'select',
      '#options' => $srcoptions,
      '#default_value' => $sourcekey,
      '#title' => t('Primary key of source view'),
    );
    $mappings = array();
    $defaults = array();
    $srcoptions = array_merge(array(
      '' => t('<none>'),
    ), $srcoptions);
    foreach ($destfields as $destfield => $destname) {
      $matches = array();
      if (preg_match('/^\\[([^\\]]*)\\]$/', $destfield, $matches)) {
        $primary_key = $matches[1];
        $destfield = $primary_key;
        $destfields[$primary_key] = $destname;
        unset($destfields[$destfield]);
      }
      else {
        $primary_key = '';
      }
      $sql = "SELECT *\n              FROM {migrate_content_mappings}\n              WHERE mcsid=%d AND destfield='%s'";
      $result = db_query($sql, $mcsid, $destfield);
      $row = db_fetch_object($result);
      $cols[] = $destfield;
      $form['srcfield'][$destfield] = array(
        '#type' => 'select',
        '#options' => $srcoptions,
        '#default_value' => isset($row->srcfield) ? $row->srcfield : '',
      );
      $form['default_value'][$destfield] = array(
        '#type' => 'textfield',
        '#default_value' => isset($row->default_value) ? $row->default_value : '',
        '#size' => 25,
        '#maxlength' => 255,
      );
      $form['destfield'][$destfield] = array(
        '#value' => $destname,
      );
    }
    $form['cols'] = array(
      '#type' => 'value',
      '#value' => $cols,
    );
    $form['primary_key'] = array(
      '#type' => 'value',
      '#value' => $primary_key,
    );
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Submit changes'),
    );
  }
  $form['delete'] = array(
    '#type' => 'submit',
    '#value' => t('Delete'),
  );
  $form['#tree'] = TRUE;
  return $form;
}