You are here

function node_convert_bulk in Node Convert 5

1 string reference to 'node_convert_bulk'
node_convert_menu in ./node_convert.module
Implementation of hook_menu().

File

./node_convert.module, line 178

Code

function node_convert_bulk($form_values = NULL) {
  $form = array();

  /* Setting the steps */
  if (!isset($form_values['step'])) {
    $op = "choose_source_dest_type";
  }
  elseif ($form_values['step'] == "choose_source_dest_type") {
    $op = "choose_nodes";
  }
  elseif ($form_values['step'] == "choose_nodes") {
    $op = "choose_fields";
  }
  $form['step'] = array(
    '#type' => 'hidden',
    '#value' => $op,
  );
  if ($op == "choose_source_dest_type") {
    $types = array_keys(content_types());

    // Get available content types
    foreach ($types as $value) {
      $options[$value] = $value;
    }
    $form['source_type'] = array(
      '#type' => 'select',
      '#title' => t("Source type"),
      '#options' => $options,
    );
    $form['dest_type'] = array(
      '#type' => 'select',
      '#title' => t("Destination type"),
      '#options' => $options,
    );
  }
  elseif ($op == "choose_nodes") {
    $info = array(
      'source_type' => $form_values['source_type'],
      'dest_type' => $form_values['dest_type'],
    );
    $result = db_query("SELECT n.*, u.name, u.uid FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE type = '%s'", $info['source_type']);
    while ($node = db_fetch_object($result)) {
      $nodes[$node->nid] = '';
      $form['title'][$node->nid] = array(
        '#value' => $node->title,
      );
      $form['type'][$node->nid] = array(
        '#value' => check_plain(node_get_types('name', $node)),
      );
      $form['author'][$node->nid] = array(
        '#value' => theme('username', $node),
      );
      $form['status'][$node->nid] = array(
        '#value' => $node->status ? t('published') : t('not published'),
      );
    }
    $form['nodes'] = array(
      '#type' => 'checkboxes',
      '#options' => $nodes,
    );
    $info = serialize($info);
    $form['info'] = array(
      '#type' => 'hidden',
      '#value' => $info,
    );
  }
  elseif ($op == "choose_fields") {
    $info = unserialize($form_values['info']);
    $nodes = $form_values['nodes'];
    $info['nodes'] = $nodes;
    $source_fields = content_types($info['source_type']);
    $source_fields = $source_fields['fields'];

    // Get the cck fields of the source type
    if (count($source_fields) == 0) {

      // In case there are no cck fields, just convert the node type
      $no_fields = true;
      $info['no_fields'] = $no_fields;
    }
    else {
      foreach ($source_fields as $field) {
        $i++;
        $options = array();
        $dest_fields = content_types($info['dest_type']);
        $dest_fields = $dest_fields['fields'];

        // Get the destination type fields
        $options['discard'] = 'discard';

        // Populate only the destination type fields into the select that are of the same type (cck type and multiple property)
        foreach ($dest_fields as $value) {
          if ($field['type'] == $value['type'] && $field['multiple'] == $value['multiple']) {
            $options[$value['field_name']] = $value['field_name'];
          }
        }
        $info['fields']['source'][] = $field['field_name'];

        // Remember the source fields to be converted
        // The select populated with possible destination cck fields for each source field
        $form['dest_field_' . $i] = array(
          '#type' => 'select',
          '#options' => $options,
          '#title' => $field['field_name'] . " " . t("should be inserted into"),
        );
      }
    }
    $info = serialize($info);
    $form['info'] = array(
      '#type' => 'hidden',
      '#value' => $info,
    );
  }
  $form['#multistep'] = TRUE;
  $form['#redirect'] = FALSE;
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t("Submit"),
  );
  return $form;
}