public function sbo__relationship::validate

3.x sbo__relationship.inc public sbo__relationship::validate($entity_type, $entity, $langcode, $items, &$errors)

Overrides TripalField::validate

See also

TripalField::validate()

File

tripal_chado/includes/TripalFields/sbo__relationship/sbo__relationship.inc, line 805

Class

sbo__relationship

Code

public function validate($entity_type, $entity, $langcode, $items, &$errors) {

  // If we don't have an entity then we don't want to validate.  The case
  // where this could happen is when a user is editing the field settings
  // and trying to set a default value. In that case there's no entity and
  // we don't want to validate.  There will always be an entity for creation
  // and update operations of a content type.
  if (!$entity) {
    return;
  }

  $field_name = $this->field['field_name'];
  $field_type = $this->field['type'];
  $field_table = $this->instance['settings']['chado_table'];
  $field_column = $this->instance['settings']['chado_column'];
  $base_table = $this->instance['settings']['base_table'];

  $schema = chado_get_schema($field_table);
  $fkeys = $schema['foreign keys'];

  // 'nd_reagent_relationship' and 'project_relationship' have different column names from
  // subject_id/object_id. Do a pattern matching to get the column names.
  $subject_id_key = 'subject_id';
  $object_id_key = 'object_id';
  foreach ($schema['foreign keys'][$base_table]['columns'] as $lcolum => $rcolum) {
    if (preg_match('/^subject_.*id/', $lcolum)) {
      $subject_id_key = $lcolum;
    }
    else if (preg_match('/^object_.*id/', $lcolum)) {
      $object_id_key = $lcolum;
    }
  }

  foreach ($items as $delta => $item) {
    $subject_id = $item['chado-' . $field_table . '__' . $subject_id_key];
    $object_id = $item['chado-' . $field_table . '__' . $object_id_key];
    $type_id = $item['chado-' . $field_table . '__type_id'];
    $type_id = isset($item['type_id']) ? $item['chado-' . $field_table . '__type_id'] : $type_id;
    $type_name = isset($item['type_name']) ? $item['type_name'] : '';
    $subject_name = $item['subject_name'];
    $object_name = $item['object_name'];


    // If the row is empty then just continue, there's nothing to validate.
    if (!$type_id and !$type_name and !$subject_name and !$object_name) {
      continue;
    }

    // Make sure we have values for all of the fields.
    $form_error = FALSE;
    if (!$type_name && !$type_id) {
      $errors[$field_name][$delta]['und'][] = array(
        'error' => 'sbo__relationship',
        'message' => t("Please provide the type of relationship."),
      );
    }
    if ($entity and !$subject_name) {
      $errors[$field_name][$delta]['und'][] = array(
        'error' => 'sbo__relationship',
        'message' => t("Please provide the subject of the relationship."),
      );
    }
    if ($entity and !$object_name) {
      $errors[$field_name][$delta]['und'][] = array(
        'error' => 'sbo__relationship',
        'message' => t("Please provide the object of the relationship."),
      );
    }
    if ($form_error) {
      continue;
    }

    // Before submitting this form we need to make sure that our subject_id and
    // object_ids are real records.  There are two ways to get the record, either
    // just with the text value or with an [id: \d+] string embedded.  If the
    // later we will pull it out.
    $subject_id = '';
    $fkey_rcolumn = $fkeys[$base_table]['columns'][$subject_id_key];
    $matches = array();
    if ($entity) {
      if (preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
        $subject_id = $matches[1];
        $values = array($fkey_rcolumn => $subject_id);
        $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
        if (count($subject) == 0) {
          $errors[$field_name][$delta]['und'][] = array(
            'error' => 'sbo__relationship',
            'message' => t("The subject record cannot be found using the specified id (e.g. [id: xx])."),
          );
        }
      }
      else {
        $values = array('uniquename' => $subject_name);
        $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
        if (count($subject) == 0) {
          $errors[$field_name][$delta]['und'][] = array(
            'error' => 'sbo__relationship',
            'message' => t("The subject record cannot be found. Please check spelling."),
          );
        }
        elseif (count($subject) > 1) {
          $errors[$field_name][$delta]['und'][] = array(
            'error' => 'sbo__relationship',
            'message' => t("The subject is not unique and therefore the relationship cannot be made."),
          );
        }
      }
    }

    // Now check for a matching object.
    $object_id = '';
    $fkey_rcolumn = $fkeys[$base_table]['columns'][$object_id_key];
    $matches = array();
    if ($entity) {
      if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
        $object_id = $matches[1];
        $values = array($fkey_rcolumn => $object_id);
        $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
        if (count($subject) == 0) {
          $errors[$field_name][$delta]['und'][] = array(
            'error' => 'sbo__relationship',
            'message' => t("The object record cannot be found using the specified id (e.g. [id: xx])."),
          );
        }
      }
      else {
        $values = array('uniquename' => $object_name);
        $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
        if (count($object) == 0) {
          $errors[$field_name][$delta]['und'][] = array(
            'error' => 'sbo__relationship',
            'message' => t("The object record cannot be found. Please check spelling."),
          );
        }
        elseif (count($object) > 1) {
          $errors[$field_name][$delta]['und'][] = array(
            'error' => 'sbo__relationship',
            'message' => t("The object is not unique and therefore the relationship cannot be made."),
          );
        }
      }
    }

    // Make sure that either our object or our subject refers to the base record.
    if ($entity) {
      $chado_record_id = $entity->chado_record_id;
      if ($object_id != $chado_record_id and $subject_id != $chado_record_id) {
        $errors[$field_name][$delta]['und'][] = array(
          'error' => 'sbo__relationship',
          'message' => t("Either the subject or the object in the relationship must refer to this record."),
        );
      }

      // Make sure that the object and subject are not both the same thing.
      if ($object_id == $subject_id) {
        $errors[$field_name][$delta]['und'][] = array(
          'error' => 'sbo__relationship',
          'message' => t("The subject and the object in the relationship cannot both refer to the same record."),
        );
      }
    }
  }
}