function tripal_insert_bulk_loader_template

2.x tripal_bulk_loader.api.templates.inc tripal_insert_bulk_loader_template($options, &$errors, &$warnings)

Inserts a bulk loader template record.

This function validates the options passed prior to insertion of the record,

Parameters

$options: An array of key/value pairs containing the following keys: 'template_name': The name of the template. 'template_array': The JSON array representing the template. Optional: 'strict': If set then only JSON formatted templates are allowed.

$errors: An empty array where validation error messages will be set. The keys of the array will be name of the field from the options array and the value is the error message.

$warnings: An empty array where validation warning messagges will be set. The warnings should not stop an insert or an update but should be provided to the user as information by a drupal_set_message() if appropriate. The keys of the array will be name of the field from the options array and the value is the error message.

Return value

TRUE for success and FALSE for failure.

2 calls to tripal_insert_bulk_loader_template()
tripal_bulk_loader_import_template_form_submit in tripal_bulk_loader/includes/tripal_bulk_loader.admin.templates.inc
Import Template Form Submit
tripal_core_extensions_form_submit in tripal_core/includes/tripal_core.extensions.inc
Process the import buttons.

File

tripal_bulk_loader/api/tripal_bulk_loader.api.templates.inc, line 129
Provides functions for hooking into bulk loader functionality.

Code

function tripal_insert_bulk_loader_template($options, &$errors, &$warnings) {

  $success = tripal_validate_bulk_loader_template('insert', $options, $errors, $warnings);
  if (!$success) {
    foreach ($errors as $field => $message) {
      tripal_report_error('tripal_bulkldr', TRIPAL_ERROR, $message);
    }
    return FALSE;
  }

  // Insert the bulk loader template.
  $template_array = trim($options['template_array']);
  $template_name = trim($options['template_name']);

  // Previous version of Tripal would export the template as a PHP array.
  // This has security implications and is deprecated. This support should
  // be reomved in future versions of Tripal, but to support transfers of
  // templates between v1.1 and v2.x sites we support it.
  if (preg_match('/^array/', $template_array)) {
    $tarray = array();
    eval("\$tarray = $template_array;");
    $template_array = serialize($tarray);
  }
  // For a brief period, the bulk loader templates were exported as a PHP
  // serialized array. We have moved to exporting in JSON as JSON is more
  // user friendly. But we must support the serialized PHP array for
  // backwards compatibility of v2.0-rc1 sites and v2.x sites.
  elseif (preg_match('/^a:/', $template_array)) {
    // do nothing it's in PHP serialized format
  }
  // The typical format is JSON
  elseif (json_decode($template_array)) {
    $template_array = serialize(json_decode($template_array, TRUE));
  }
  else {
    $errors['template_array'] = t('Unrecognized array type.');
    return FALSE;
  }

  $record = array(
    'name' => $template_name,
    'template_array' => $template_array,
    'created' => time(),
    'changed' => time()
  );
  if (!drupal_write_record('tripal_bulk_loader_template', $record)) {
    return FALSE;
  }
  return TRUE;
}