You are here

function redhen_relation_connection_form_validate in RedHen CRM 7

Validation handler for redhen_relation_connection_form().

File

modules/redhen_relation/includes/redhen_relation.forms.inc, line 268
Form definition and handling for redhen relations.

Code

function redhen_relation_connection_form_validate($form, &$form_state) {
  $entity = $form_state['entity'];
  $relation = $form_state['relation'];

  // Is this editing an existing relation?
  $edit = !empty($relation->rid) ? TRUE : FALSE;

  // Check if user has edit/update permission on the entity.
  $edit_access = entity_access('edit', $entity
    ->entityType(), $entity);
  if (!$edit && (isset($form_state['input']['new_or_existing']) && $form_state['input']['new_or_existing'] == 0 && isset($form_state['values']['entity_to_relate']))) {

    // Load the relation information, to determine arity.
    $relation_type = relation_type_load($form_state['values']['relation_type']);

    // parse out the entity id from the autocomplete string
    preg_match('/(.+) \\((\\d+)\\)$/', $form_state['values']['entity_to_relate'], $matches);
    if (isset($matches[2]) && $matches[2] > 0) {
      list($src_relation_entity_type) = explode(":", $relation_type->source_bundles[0]);

      // ensure source/target order are correct
      if ($relation_type->directional && $form_state['entity_to_relate_type'] == $src_relation_entity_type) {
        $endpoints = array(
          array(
            'entity_type' => $form_state['entity_to_relate_type'],
            'entity_id' => $matches[2],
          ),
          array(
            'entity_type' => $entity
              ->entityType(),
            'entity_id' => $entity
              ->internalIdentifier(),
          ),
        );
      }
      else {
        $endpoints = array(
          array(
            'entity_type' => $entity
              ->entityType(),
            'entity_id' => $entity
              ->internalIdentifier(),
          ),
          array(
            'entity_type' => $form_state['entity_to_relate_type'],
            'entity_id' => $matches[2],
          ),
        );
      }
      $form_state['values']['endpoints'][LANGUAGE_NONE] = $endpoints;
    }
    else {
      if ($relation_type->min_arity == 1 && empty($form_state['values']['entity_to_relate'])) {
        $endpoints = array(
          array(
            'entity_type' => $entity
              ->entityType(),
            'entity_id' => $entity
              ->internalIdentifier(),
          ),
        );
        $form_state['values']['endpoints'][LANGUAGE_NONE] = $endpoints;
      }
      else {
        form_set_error('entity_to_relate', 'Invalid connection.');
      }
    }

    // Set the relation type based on selected value before validating.
    $relation->relation_type = $form_state['values']['relation_type'];
  }

  // On edit of existing or creation of new entity, validate fields.
  if ($edit && $edit_access || isset($form_state['input']['new_or_existing']) && $form_state['input']['new_or_existing'] == 1) {
    field_attach_form_validate($entity
      ->entityType(), $entity, $form, $form_state);
  }
  field_attach_form_validate('relation', $relation, $form, $form_state);
}