You are here

function document_validate in Document 6

Same name and namespace in other branches
  1. 7 document.module \document_validate()
  2. 8.x document.module \document_validate()

Implementation of hook_validate().

File

./document.module, line 220

Code

function document_validate($node) {
  if (isset($node->is_node_revision) && $node->is_node_revision) {

    //No validation should be performed when an earlier Revision is being reverted.
    return;
  }

  //A new document must be uploaded for a new node, or a new Revision for an existing node.
  $hasDoc = _document_is_uploaded();
  $external_url = isset($node->document_external_url) ? $node->document_external_url : '';
  $is_external = strlen($external_url) > 0;
  $existing = NULL;
  if ($node->nid && !$node->revision) {

    //A switch from external to internal url or vice versa is only allowed while creating a revision.
    if ($existing == NULL) {
      $existing = node_load($node->nid);
    }
    $existing_external = $existing->document_external == DOCUMENT_EXTERNAL;
    if ($is_external != $existing_external) {
      form_set_error('document_filepath', t('A switch from an external document url to an internal file or vice versa is only allowed while creating a revision.'));
      return;
    }
  }
  if ($is_external) {
    if ($hasDoc) {
      form_set_error('document_external_url', t('You cannot specify an external Url and upload a document simultaneously.'));
    }
    if (!valid_url($external_url, TRUE)) {
      form_set_error('document_external_url', t('Invalid external url for document. You must specify a valid absolute url.'));
    }
    if ($node->nid && !$node->revision) {

      //A change in external url is not allowed without creating a revision.
      if ($existing == NULL) {
        $existing = node_load($node->nid);
      }
      if ($external_url != $existing->document_url) {
        form_set_error('document_external_url', t('A change in external url is only allowed while creating a revision.'));
      }
    }
  }
  else {

    //No external url specified. A document must have been uploaded while creating a node/new revision.
    if (!$node->nid || $node->revision) {
      if (!$hasDoc) {
        form_set_error('document_filepath', t('The document could not be uploaded. Neither did you specify an external url. Please try again. If the error persists, contact the administrator.'));
      }
    }
  }
  if ($hasDoc) {
    if ($node->nid && !$node->revision) {

      //A new document cannot be uploaded without creating a revision.
      form_set_error('document_filepath', t('A new document cannot be specified without creating a new Revision for an existing node.'));
    }
  }
}