You are here

function node_convert_conversion_form in Node Convert 5

Same name and namespace in other branches
  1. 6 node_convert.module \node_convert_conversion_form()
  2. 7 node_convert.forms.inc \node_convert_conversion_form()
1 string reference to 'node_convert_conversion_form'
node_convert_form_page in ./node_convert.module

File

./node_convert.module, line 58

Code

function node_convert_conversion_form($nid, $form_values = NULL) {
  $form = array();
  $node = node_load($nid);

  /* Setting the steps */
  if (!isset($form_values['step'])) {
    $op = "choose_destination_type";
  }
  elseif ($form_values['step'] == "choose_destination_type") {
    $op = "choose_destination_fields";
  }
  $form['step'] = array(
    '#type' => 'hidden',
    '#value' => $op,
  );
  $form['nid'] = array(
    '#type' => 'hidden',
    '#value' => $nid,
  );

  /* Form steps */
  if ($op == "choose_destination_type") {
    $form['current_type'] = array(
      '#type' => 'markup',
      '#value' => $node->type,
    );

    // Remember current node type
    $types = array_keys(content_types());

    // Get available content types
    $key = array_search($form['current_type']['#value'], $types);
    if ($key !== false) {
      unset($types[$key]);
    }

    // Delete the current content type from the list
    foreach ($types as $value) {
      $options[$value] = $value;
    }

    // Populate the select with possible content types
    $form['destination_type'] = array(
      '#type' => 'select',
      '#options' => $options,
      '#title' => t("To what content type should this node be converted"),
    );
  }
  if ($op == "choose_destination_fields") {
    $form['destination_type'] = array(
      '#type' => 'hidden',
      '#value' => $form_values['destination_type'],
    );

    // Remember the destination type
    $source_fields = content_types($node->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
      $form['no_fields_flag'] = array(
        '#type' => 'hidden',
        '#value' => 'true',
      );
      $form['no_fields'] = array(
        '#type' => 'markup',
        '#value' => t("There are no cck fields to convert. Please press submit."),
      );
    }
    else {

      // Otherwise
      foreach ($source_fields as $field) {
        $i++;
        $options = array();
        $dest_fields = content_types($form_values['destination_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'];
          }
        }
        $form['source_field_' . $i] = array(
          '#type' => 'hidden',
          '#value' => $field['field_name'],
        );

        // Remember the source fields to be converted
        $form['dest_field_type_' . $i] = array(
          '#type' => 'hidden',
          '#value' => $field['type'],
        );

        // Remember the destination type fields
        // 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"),
        );

        // Print the current value of the source field
        if ($field['type'] == "image") {
          $temp_value = $node->{$field['field_name']}[0]['title'] . " ; " . $node->{$field['field_name']}[0]['filepath'];
        }
        elseif ($field['type'] == "link") {
          $temp_value = $node->{$field['field_name']}[0]['url'] . " ; " . $node->{$field['field_name']}[0]['title'];
        }
        elseif ($field['type'] == "email") {
          $temp_value = $node->{$field['field_name']}[0]['email'];
        }
        elseif ($field['type'] == "file_audio" || $field['type'] == "file_video") {
          $temp_value = $node->{$field['field_name']}[0]['filename'] . " " . $node->{$field['field_name']}[0]['filemime'] . " " . $node->{$field['field_name']}[0]['filesize'] . " " . t("bytes");
        }
        elseif ($field['type'] == "nodereference") {
          $temp_value = "nid: " . $node->{$field['field_name']}[0]['nid'];
        }
        elseif ($field['type'] == "userreference") {
          $temp_value = "uid: " . $node->{$field['field_name']}[0]['uid'];
        }
        else {
          $temp_value = $node->{$field['field_name']}[0]['value'];
        }
        if (empty($temp_value)) {
          $temp_value = "NULL";
        }
        $form['current_field_value_' . $i] = array(
          '#type' => 'markup',
          '#value' => '<div>' . t("Current value is:") . " <b>" . $temp_value . '</b></div>',
        );
      }
    }
  }
  $form['#multistep'] = TRUE;
  $form['#redirect'] = FALSE;
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t("Submit"),
  );
  return $form;
}