You are here

function farm_area_generate_form_submit in farmOS 7

Farm area generate form submit.

File

modules/farm/farm_area/farm_area_generate/farm_area_generate.module, line 323
Farm area generate module.

Code

function farm_area_generate_form_submit(&$form, &$form_state) {

  // If the area and polygon was not stored in validation, bail.
  if (empty($form_state['storage']['area']) || empty($form_state['storage']['polygon'])) {
    return;
  }

  // Get all the necessary variables from the form state.
  $area = $form_state['storage']['area'];
  $polygon = $form_state['storage']['polygon'];
  $area_type = $form_state['values']['area_type'];
  $count = $form_state['values']['count'];
  $orientation = $form_state['values']['orientation'];

  // Load the area type label and convert to lowercase.
  $area_types = farm_area_type_options();
  $area_type_label = strtolower($area_types[$area_type]);

  // Generate child geometries.
  $geometries = farm_area_generate_geometries($polygon, $count, $orientation);

  // Iterate through the geometries and save each one as a new area.
  foreach ($geometries as $key => $geometry) {

    // Assemble the new area name.
    $area_name = $area->name . ' ' . $area_type_label . ' ' . ($key + 1);

    // Create a new area term object.
    $new_area = farm_term($area_name, 'farm_areas', TRUE, FALSE);

    // Set the area type, geofield, parent, and weight.
    $new_area->field_farm_area_type = array(
      LANGUAGE_NONE => array(
        0 => array(
          'value' => $area_type,
        ),
      ),
    );
    $new_area->field_farm_geofield = array(
      LANGUAGE_NONE => array(
        0 => array(
          'geom' => $geometry
            ->asText(),
        ),
      ),
    );
    $new_area->parent = $area->tid;
    $new_area->weight = $key;
    taxonomy_term_save($new_area);
  }

  // Announce how many areas were generated.
  drupal_set_message(t('@count areas were generated.', array(
    '@count' => count($geometries),
  )));
}