public function TripalImporter::create

3.x TripalImporter.inc public TripalImporter::create($run_args, $file_details = array())

Creates a new importer record.

Parameters

$run_args: An associative array of the arguments needed to run the importer. Each importer will have its own defined set of arguments.

$file_details: An associative array with one of the following keys: -fid: provides the Drupal managed File ID for the file. -file_local: provides the full path to the file on the server. -file_remote: provides the remote URL for the file. This argument is optional if the loader does not use the built-in file loader.

Throws

Exception

File

tripal/includes/TripalImporter.inc, line 263

Class

TripalImporter

Code

public function create($run_args, $file_details = array()) {
  global $user;
  $class = get_called_class();

  try {
    // Build the values for the tripal_importer table insert.
    $values = array(
      'uid' => $user->uid,
      'class' => $class,
      'submit_date' => time(),
    );

    // Build the arguments array, which consists of the run arguments
    // and the file.
    $arguments = array(
      'run_args' => $run_args,
      'files' => array(),
    );

    // Get the file argument.
    $has_file = 0;
    if (array_key_exists('file_local', $file_details)) {
      $arguments['files'][] = array(
        'file_local' => $file_details['file_local'],
        'file_path' => $file_details['file_local']
      );
      $has_file++;
    }
    if (array_key_exists('file_remote', $file_details)) {
      $arguments['files'][] = array(
        'file_remote' => $file_details['file_remote']
      );
      $has_file++;
    }
    if (array_key_exists('fid', $file_details)) {
      $values['fid'] = $file_details['fid'];
      // Handle multiple file uploads.
      if (preg_match('/\|/', $file_details['fid'])) {
        $fids = explode('|', $file_details['fid']);
        foreach ($fids as $fid) {
          $file = file_load($fid);
          $arguments['files'][] = array(
            'file_path' => base_path() . drupal_realpath($file->uri),
            'fid' => $fid
          );
          $has_file++;
        }
      }
      // Handle a single file.
      else {
        $fid = $file_details['fid'];
        $file = file_load($fid);
        $arguments['files'][] = array(
          'file_path' => base_path() . drupal_realpath($file->uri),
          'fid' => $fid
        );
        $has_file++;

        // For backwards compatibility add the old 'file' element.
        $arguments['file'] = array(
          'file_path' => base_path() . drupal_realpath($file->uri),
          'fid' => $fid
        );
      }
    }

    // Validate the $file_details argument.
    if ($has_file == 0 and $class::$file_required == TRUE) {
      throw new Exception("Must provide a proper file identifier for the \$file_details argument.");
    }

    // Store the arguments in the class and serialize for table insertion.
    $this->arguments = $arguments;
    $values['arguments'] = serialize($arguments);

    // Insert the importer record.
    $import_id = db_insert('tripal_import')
      ->fields($values)
      ->execute();

    $this->import_id = $import_id;
  }
  catch (Exception $e) {
    throw new Exception('Cannot create importer: ' . $e->getMessage());
  }
}