You are here

function document_insert in Document 7

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

Implementation of hook_insert().

1 call to document_insert()
document_update in ./document.module
Implementation of hook_update().

File

./document.module, line 309

Code

function document_insert($node) {
  try {
    $external_url = isset($node->document_external_url) ? $node->document_external_url : '';
    $external = strlen($external_url) > 0 ? DOCUMENT_EXTERNAL : DOCUMENT_INTERNAL;
    $author = $node->document_author;
    $year = $node->document_year;
    $keywords = $node->document_keywords;
    $abstract = $node->document_abstract;
    $type = NULL;
    $fid = 0;
    $file = NULL;
    $doc_url = NULL;
    if (isset($node->is_node_revision) && $node->is_node_revision) {
      $type = $node->document_type;
      $fid = $node->document_fid;
      $doc_url = $node->document_url;
      $external = $node->document_external;
    }
    else {
      $type = _document_get_node_terms($node);
      if ($external == DOCUMENT_INTERNAL) {

        //Save the Document uploaded.

        //TODO: Need a better approach here.
        $validators = array(
          'file_validate_extensions' => array(
            _document_get_allowed_extensions(),
          ),
        );
        $file = file_save_upload('document_filepath', $validators, 'public://' . variable_get('document_path', ''), FILE_EXISTS_RENAME);
        if ($file) {
          $fid = $file->fid;
          $doc_url = variable_get('document_path', '') . '/' . $file->filename;

          // $doc_url = file_create_url($file->uri);

          //Set the status of the uploaded file.
          $file->status = FILE_STATUS_PERMANENT;
          file_save($file);
        }
        else {
          drupal_set_message(t('Please select a document to upload.'), 'error');
        }
      }
      else {
        $doc_url = $external_url;
      }
    }
    db_insert('document')
      ->fields(array(
      'nid' => $node->nid,
      'vid' => $node->vid,
      'fid' => $fid,
      'type' => $type,
      'author' => $author,
      'publish_year' => $year,
      'keywords' => $keywords,
      'abstract' => $abstract,
      'url' => $doc_url,
      'external' => $external,
    ))
      ->execute();
    if ($node->status == 0) {
      drupal_set_message(t('The document has been submitted. Please wait until administrators review and publish it.'), 'status');
    }
  } catch (Exception $ex) {
    drupal_set_message(t($ex
      ->getMessage()), 'error');
  }
}