public function TripalFieldDownloader::__construct

3.x TripalFieldDownloader.inc public TripalFieldDownloader::__construct($collection_id, $outfile_name)

Constructs a new instance of the TripalFieldDownloader class.

Parameters

$collection_id: The ID for the collection.

$outfile_name: The name of the output file to create. The name should not include a path.

File

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

Class

TripalFieldDownloader

Code

public function __construct($collection_id, $outfile_name) {

  if (!$outfile_name) {
    throw new Exception("Please provide an outputfilename");
  }

  // Get the collection record and store it.
  $collection = db_select('tripal_collection', 'tc')
    ->fields('tc')
    ->condition('collection_id', $collection_id, '=')
    ->execute()
    ->fetchObject();

  if (!$collection) {
    throw new Exception(t("Cannot find a collection that matches the provided id: !id", array('!id' => $collection_id)));
  }
  $this->collection = $collection;

  // Make sure the user directory exists
  $user = user_load($this->collection->uid);
  $user_dir = 'public://tripal/users/' . $user->uid;

  // Set the collection ID of the collection that this downloader will use.
  $this->collection_id = $collection_id;
  $this->outfile = $user_dir . '/' . $outfile_name;

  // A data collection may have multiple bundles.  We'll need to get
  // them all and store them.
  $collection_bundles = db_select('tripal_collection_bundle')
    ->fields('tripal_collection_bundle')
    ->condition('collection_id', $collection_id, '=')
    ->execute();
  while ($collection_bundle = $collection_bundles->fetchObject()) {
    $collection_bundle->ids = unserialize($collection_bundle->ids);
    $collection_bundle->fields = unserialize($collection_bundle->fields);
    $this->collection_bundles[] = $collection_bundle;
  }

  if (!file_prepare_directory($user_dir, FILE_CREATE_DIRECTORY)) {
    $message = 'Could not access the directory on the server for storing this file.';
    watchdog('tripal', $message, array(), WATCHDOG_ERROR);
    drupal_json_output(array(
      'status' => 'failed',
      'message' => $message,
      'file_id' => '',
    ));
    return;
  }

  // Map the fields to their term accessions.
  $this->setFields();
  $this->setFields2Terms();
  $this->setPrintableFields();
}