public function TripalFieldDownloader::writeDone

3.x TripalFieldDownloader.inc public TripalFieldDownloader::writeDone(TripalJob $job = NULL)

Closes the output file once writing of all entities is completed.

Parameters

TripalJob $job:

File

tripal/includes/TripalFieldDownloaders/TripalFieldDownloader.inc, line 275

Class

TripalFieldDownloader

Code

public function writeDone(TripalJob $job = NULL) {
  fclose($this->fh);

  $user = user_load($this->collection->uid);

  $file = new stdClass();
  $file->uri = $this->outfile;
  $file->filename = basename($this->outfile);
  $file->filemime = file_get_mimetype($this->outfile);
  $file->uid = $user->uid;
  $file->status = FILE_STATUS_PERMANENT;

  // Check if this file already exists. If it does then just update
  // the stats.
  $fid = db_select('file_managed', 'fm')
    ->fields('fm', array('fid'))
    ->condition('uri', $this->outfile)
    ->execute()
    ->fetchField();
  if ($fid) {
    $file->fid = $fid;
    $file = file_save($file);
  }
  else {
    $file = file_save($file);
    $fid = $file->fid;
    $file = file_load($fid);
  }

  // We use the fid for the last argument because these files
  // aren't really associated with any entity, but we need a value./
  // But, only add the usage if it doens't already exists.
  $usage = file_usage_list($file);
  if (array_key_exists('tripal', $usage)) {
    if (!array_key_exists('data-collection', $usage['tripal'])) {
      file_usage_add($file, 'tripal', 'data-collection', $fid);
    }
  }
}