You are here

function farm_area_generate_form_validate in farmOS 7

Area generator form validate.

File

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

Code

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

  // If GEOS is not installed, prevent submission.
  if (!farm_area_generate_geos_exists()) {
    form_set_error('', 'This area generator tool requires the GEOS libary: ' . l('http://trac.osgeo.org/geos', 'http://trac.osgeo.org/geos'));
    return;
  }

  // Load the area and store it in the form state.
  $area = farm_term($form_state['values']['area'], 'farm_areas', FALSE);
  $form_state['storage']['area'] = $area;

  // Ensure the area exists.
  if (empty($area)) {
    form_set_error('area', 'The area does not exist.');
    return;
  }

  // Ensure that the area doesn't already have child areas.
  $children = taxonomy_get_children($area->tid);
  $children_exist = FALSE;
  foreach ($children as $child) {
    if (!empty($child->field_farm_area_type[LANGUAGE_NONE][0]['value'])) {
      $children_exist = TRUE;
      break;
    }
  }
  if ($children_exist) {
    form_set_error('area', 'The selected area already has child areas.');
  }

  // Ensure that the area has a geometry.
  if (empty($area->field_farm_geofield[LANGUAGE_NONE][0]['geom'])) {
    form_set_error('area', 'The selected area does not have any geometry defined.');
    return;
  }

  // Ensure that the area's geometry is a single polygon.
  geophp_load();
  $geom = $area->field_farm_geofield[LANGUAGE_NONE][0]['geom'];
  $polygon = geoPHP::load($geom, 'wkt');
  $polygon = geoPHP::geometryReduce($polygon);
  $form_state['storage']['polygon'] = $polygon;
  if ($polygon
    ->geometryType() != 'Polygon') {
    form_set_error('area', 'The selected area is not a single polygon. Areas cannot be generated within points, lines, or complex geometries.');
    return;
  }

  // Put an upper limit on the number of child that can be created per area.
  if ($form_state['values']['count'] > 150) {
    form_set_error('count', 'The area generator tool has a limit of 150 areas. If you need to add more, consider breaking your area up into sub-areas first.');
  }

  // Ensure that the orientation is between 0 and 360.
  if ($form_state['values']['orientation'] < 0 || $form_state['values']['orientation'] > 360) {
    form_set_error('orientation', 'The orientation must be a number between 0 and 360.');
  }
}