You are here

function farm_area_import_form in farmOS 7

Area import form.

1 string reference to 'farm_area_import_form'
farm_area_import_menu in modules/farm/farm_area/farm_area_import/farm_area_import.module
Implements hook_menu().

File

modules/farm/farm_area/farm_area_import/farm_area_import.module, line 56
Farm area import module.

Code

function farm_area_import_form($form, &$form_state) {

  // Set the page title.
  drupal_set_title('Area importer');

  // Create a fieldset for input.
  $form['input'] = array(
    '#type' => 'fieldset',
    '#title' => t('Input'),
  );

  // KML text area.
  $form['input']['kml'] = array(
    '#type' => 'textarea',
    '#title' => t('KML'),
    '#description' => t('Paste the contents of your KML file here and click "Parse".'),
    '#required' => TRUE,
  );

  // Default area type.
  // Default to 'field' if available. Otherwise use 'other' because it's the
  // only one we can depend on.
  $area_type_options = farm_area_type_options();
  $default_area_type = 'other';
  if (array_key_exists('field', $area_type_options)) {
    $default_area_type = 'field';
  }
  $form['input']['area_type'] = array(
    '#type' => 'select',
    '#title' => t('Default area type'),
    '#description' => t('Specify the default area type for the ares in this KML. This can be overridden below on a per-area basis before creating the areas.'),
    '#options' => $area_type_options,
    '#default_value' => $default_area_type,
  );

  // Parse button.
  $form['input']['parse'] = array(
    '#type' => 'submit',
    '#value' => t('Parse'),
    '#submit' => array(
      'farm_area_import_form_parse',
    ),
    '#ajax' => array(
      'callback' => 'farm_area_import_form_ajax',
      'wrapper' => 'output',
    ),
  );

  // Create a wrapper for the output.
  $form['output'] = array(
    '#tree' => TRUE,
    '#prefix' => '<div id="output">',
    '#suffix' => '</div>',
  );

  // If a file has not been uploaded, return the form.
  if (empty($form_state['values']['kml'])) {
    return $form;
  }

  // Parse the KML into an array of geometries.
  $geometries = farm_map_kml_parse_geometries($form_state['values']['kml']);

  // Make the output wrapper into a fieldset.
  $form['output']['#type'] = 'fieldset';
  $form['output']['#title'] = t('Output');
  $form['output']['#description'] = t('Total geometries: @number', array(
    '@number' => count($geometries),
  ));

  // Iterate through the geometries and add form fields for each.
  foreach ($geometries as $i => $geometry) {

    // Create a simple fieldset for the geometry.
    $form['output'][$i] = array(
      '#type' => 'fieldset',
      '#title' => t('Geometry') . ' ' . ($i + 1),
    );

    // Get the placemark name and put it in a text field.
    $form['output'][$i]['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Name'),
      '#default_value' => $geometry['name'],
    );

    // Area type select.
    $form['output'][$i]['type'] = array(
      '#type' => 'select',
      '#title' => t('Area type'),
      '#options' => $area_type_options,
      '#default_value' => $form_state['values']['area_type'],
    );

    // Get the placemark description and put it in a text field.
    $form['output'][$i]['description'] = array(
      '#type' => 'textarea',
      '#title' => t('Description'),
      '#default_value' => $geometry['description'],
    );

    // Add a text area for the geometry data.
    $form['output'][$i]['geometry'] = array(
      '#type' => 'textarea',
      '#title' => t('Geometry'),
      '#default_value' => $geometry['wkt'],
    );

    // Checkbox for creating the area.
    $form['output'][$i]['confirm'] = array(
      '#type' => 'checkbox',
      '#title' => t('Create this area'),
      '#description' => t('Uncheck this if you do not want to create this area in farmOS.'),
      '#default_value' => TRUE,
    );
  }

  // Parent area name (for optionally creating a new parent area for all the
  // newly created areas).
  $form['output']['parent'] = array(
    '#type' => 'textfield',
    '#title' => t('Optional parent area'),
    '#description' => t('If a name is entered here, it will be used to create a new parent area, and all geometries above will be added as child areas of it. This is helpful if you are importing a lot of areas, and want to keep them all organized upon import.'),
  );

  // Submit button.
  $form['output']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Create areas'),
    '#validate' => array(
      'farm_area_import_form_create_validate',
    ),
    '#submit' => array(
      'farm_area_import_form_create_submit',
    ),
  );
  return $form;
}