You are here

function node_convert_conversion_form in Node Convert 6

Same name and namespace in other branches
  1. 5 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_menu in ./node_convert.module
Implementation of hook_menu().

File

./node_convert.module, line 166
The node_convert module converts nodes from one type to another.

Code

function node_convert_conversion_form($form_state, $node) {
  $form = array();

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

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

    // Remember current node type, used in theme_ function
    $types = node_convert_return_access_node_types('to');

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

      // Delete the current content type from the list
      $options = $types;

      // 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"),
      );
    }
    else {

      // Just used as a message, not sure if it's the best implementation
      $form['destination_type'] = array(
        '#type' => 'markup',
        '#value' => t("You don't have access to any node types."),
      );
    }
  }
  elseif ($op == 'choose_destination_fields') {
    $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'] = array(
        '#type' => 'value',
        '#value' => 'TRUE',
      );
    }
    else {

      // Otherwise
      $dest_fields = content_types($form_state['storage']['destination_type']);
      $dest_fields = $dest_fields['fields'];

      // Get the destination type fields
      $i = 0;
      foreach ($source_fields as $source_field) {
        $i++;
        $options = array();
        $options['discard'] = 'discard';
        $options[APPEND_TO_BODY] = t('Append to body');
        $options[REPLACE_BODY] = t('Replace body');

        // Populate only the destination type fields into the select that are of the same type (cck type and multiple property)
        foreach ($dest_fields as $dest_value) {
          if ($source_field['type'] == $dest_value['type'] && $source_field['multiple'] == $dest_value['multiple']) {
            $options[$dest_value['field_name']] = $dest_value['field_name'];
          }
        }
        $form['source_field_' . $i] = array(
          '#type' => 'value',
          '#value' => $source_field['field_name'],
        );

        // Remember the source fields to be converted
        // The select populated with possible destination cck fields for each source field
        // If the destination node type has the same field as the source node type, the default value is set to it.
        $form['dest_field_' . $i] = array(
          '#type' => 'select',
          '#options' => $options,
          '#default_value' => $source_field['field_name'],
          '#title' => $source_field['field_name'] . " " . t("should be inserted into"),
        );

        // Print the current value of the source field
        $temp_value = node_convert_get_field_value($node, $source_field);
        $form['current_field_value_' . $i] = array(
          '#type' => 'markup',
          '#value' => '<div>' . t("Current value is:") . " <b>" . $temp_value . '</b></div>',
        );
      }
      $form['number_of_fields'] = array(
        '#type' => 'value',
        '#value' => $i,
      );
    }
    $hook_options = module_invoke_all('node_convert_change', array(
      'dest_node_type' => $form_state['storage']['destination_type'],
    ), 'options');
    if (!empty($hook_options)) {
      $form['hook_options'] = $hook_options;
      array_unshift($form['hook_options'], array(
        '#value' => '<br /><strong>' . t("Also the following parameters are available:") . '</strong>',
      ));
      $form['hook_options']['#tree'] = TRUE;
    }
  }
  if ($op != 'choose_destination_fields' && isset($types) && $types != FALSE) {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t("Next"),
    );
  }
  elseif ($op == 'choose_destination_fields') {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t("Convert"),
      '#weight' => 100,
    );
  }
  return $form;
}