You are here

function bundle_copy_export in Bundle Copy 7.2

Same name and namespace in other branches
  1. 7 bundle_copy.module \bundle_copy_export()

Menu callback: present the export page.

1 string reference to 'bundle_copy_export'
bundle_copy_menu in ./bundle_copy.module
Implements hook_menu().

File

./bundle_copy.module, line 130
Bundle copy.

Code

function bundle_copy_export($form, &$form_state, $entity_type = 'node') {
  if (isset($form_state['step'])) {
    $step = $form_state['step'];
  }
  else {
    $step = 1;
    $form_state['step'] = $step;
  }
  switch ($step) {

    // Select the bundles.
    case 1:
      $bundles = _bundle_copy_bundle_info($entity_type, TRUE);
      $form['bundle-info'] = array(
        '#markup' => t('Select bundles you want to export.'),
      );
      $form['bundles'] = array(
        '#type' => 'tableselect',
        '#header' => array(
          'label' => t('Bundle'),
        ),
        '#options' => $bundles,
        '#required' => TRUE,
        '#empty' => t('No bundles found.'),
      );
      $form['next'] = array(
        '#type' => 'submit',
        '#value' => t('Next'),
      );
      break;

    // List the fields / field groups.
    case 2:

      // Field group.
      $all_groups = function_exists('field_group_info_groups') ? field_group_info_groups() : array();

      // Fields.
      $field_options = $instances = array();
      $selected_bundles = $form_state['page_values'][1]['bundles'];
      foreach ($selected_bundles as $key => $bundle) {
        if ($key === $bundle) {
          $instances += field_info_instances($entity_type, $bundle);
        }
      }
      ksort($instances);
      foreach ($instances as $key => $info) {

        // Same as $key.
        $field_options[$key]['field'] = $info['field_name'];
        $field_options[$key]['label'] = $info['label'];
      }
      $form['fields-info'] = array(
        '#markup' => t('Select fields you want to export.'),
      );
      $form['fields'] = array(
        '#type' => 'tableselect',
        '#header' => array(
          'field' => t('Field name'),
          'label' => t('Label'),
        ),
        '#options' => $field_options,
        '#empty' => t('No fields found.'),
      );

      // Field group support.
      if (!empty($all_groups)) {
        $group_options = $fieldgroups = array();
        if (isset($all_groups[$entity_type])) {
          foreach ($selected_bundles as $key => $bundle) {
            if ($key === $bundle) {
              if (!isset($all_groups[$entity_type][$key])) {
                continue;
              }
              foreach ($all_groups[$entity_type][$key] as $view_mode => $groups) {
                foreach ($groups as $field_group) {
                  $group_options[$field_group->identifier]['fieldgroup'] = $field_group->label . ' (' . $field_group->bundle . ' - ' . $field_group->mode . ')';
                  $fieldgroups[$field_group->identifier] = $field_group;
                }
              }
            }
          }
        }
        if (!empty($group_options)) {
          $form['fieldgroups-info'] = array(
            '#markup' => t('Select field groups you want to export.'),
          );
          $form['fieldgroups'] = array(
            '#type' => 'tableselect',
            '#header' => array(
              'fieldgroup' => t('Field group name'),
            ),
            '#options' => $group_options,
          );
          $form['fieldgroups-full'] = array(
            '#type' => 'value',
            '#value' => $fieldgroups,
          );
        }
      }
      $form['actions'] = array(
        '#type' => 'actions',
      );
      $form['actions']['next'] = array(
        '#type' => 'submit',
        '#value' => t('Export'),
      );
      $bc_info = bundle_copy_get_info();
      $form['actions']['cancel'] = array(
        '#markup' => l(t('Cancel'), $bc_info[$entity_type]['export_menu']['path']),
      );
      break;

    // Export data.
    case 3:
      $data = _bundle_copy_export_data($entity_type, $form_state['page_values']);
      $form['export'] = array(
        '#title' => t('Export data'),
        '#type' => 'textarea',
        '#cols' => 60,
        '#value' => $data,
        '#rows' => 40,
        '#description' => t('Copy the export text and paste it into another bundle using the import function.'),
      );
      break;
  }
  return $form;
}