You are here

function node_convert_add_template in Node Convert 6

Same name and namespace in other branches
  1. 7 node_convert.admin.inc \node_convert_add_template()
1 string reference to 'node_convert_add_template'
node_convert_menu in ./node_convert.module
Implementation of hook_menu().

File

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

Code

function node_convert_add_template($form_state) {
  $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,
  );
  if ($op == 'choose_destination_type') {

    // Get available content types
    $to_types = node_convert_return_access_node_types('to');
    $from_types = node_convert_return_access_node_types('from');
    if ($to_types != FALSE && $from_types != FALSE) {
      $form['template_name'] = array(
        '#type' => 'textfield',
        '#title' => t("Template name"),
        '#required' => TRUE,
      );
      $form['source_type'] = array(
        '#type' => 'select',
        '#title' => t("Source type"),
        '#options' => $from_types,
      );
      $form['dest_type'] = array(
        '#type' => 'select',
        '#title' => t("Destination type"),
        '#options' => $to_types,
      );
      $form['create_action'] = array(
        '#type' => 'checkbox',
        '#title' => t("Create action?"),
        '#description' => t("If the option is checked, an action named Convert *Template name* will be created."),
      );
    }
    else {
      $form['no_types'] = array(
        '#type' => 'markup',
        '#value' => t("You don't have access to any node types."),
      );
    }
  }
  elseif ($op == 'choose_destination_fields') {
    $source_fields = content_types($form_state['storage']['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
      $form['no_fields'] = array(
        '#type' => 'value',
        '#value' => 'TRUE',
      );
    }
    else {
      $dest_fields = content_types($form_state['storage']['dest_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_field) {
          if ($source_field['type'] == $dest_field['type'] && $source_field['multiple'] == $dest_field['multiple']) {
            $options[$dest_field['field_name']] = $dest_field['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
        $form['dest_field_' . $i] = array(
          '#type' => 'select',
          '#options' => $options,
          '#title' => $source_field['field_name'] . " " . t("should be inserted into"),
        );
      }
      $form['number_of_fields'] = array(
        '#type' => 'value',
        '#value' => $i,
      );
    }

    // All node specific form options needed for types like book, forum, etc. are done here
    $hook_options = module_invoke_all('node_convert_change', array(
      'dest_node_type' => $form_state['storage']['dest_type'],
    ), 'options');
    if (!empty($hook_options)) {
      $form['hook_options'] = $hook_options;
      array_unshift($form['hook_options'], array(
        '#value' => '<strong>' . t("Also the following parameters are available:") . '</strong>',
      ));
      $form['hook_options']['#tree'] = TRUE;
    }
  }
  if ($op == 'choose_destination_type' && $to_types != FALSE && $from_types != FALSE) {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t("Next"),
    );
  }
  elseif ($op == "choose_destination_fields") {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t("Create"),
    );
  }
  return $form;
}