function tripal_validate_bulk_loader_template

2.x tripal_bulk_loader.api.templates.inc tripal_validate_bulk_loader_template($val_type, &$options, &$errors, &$warnings = array())
3.x tripal_bulk_loader.api.templates.inc tripal_validate_bulk_loader_template($val_type, &$options, &$errors, &$warnings = array())

Validates an $options array for insert or update of a bulk loader record.

Parameters

$val_type: The type of validation. Can be either 'insert' or 'update'.

$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

If validation failes then FALSE is returned. Any options that do not pass validation checks will be added in the $errors array with the key being the option and the value being the error message. If validation is successful then TRUE is returned.

2 calls to tripal_validate_bulk_loader_template()
tripal_bulk_loader_import_template_form_validate in tripal_bulk_loader/includes/tripal_bulk_loader.admin.templates.inc
Validates the import template form
tripal_insert_bulk_loader_template in tripal_bulk_loader/api/tripal_bulk_loader.api.templates.inc
Inserts a bulk loader template record.

File

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

Code

function tripal_validate_bulk_loader_template($val_type, &$options, &$errors, &$warnings = array()) {
  $template_array = trim($options['template_array']);
  $template_name = trim($options['template_name']);
  $strict = array_key_exists('strict', $options) ? $options['strict'] : FALSE;

  // Make sure the template array is one of the supported types
  // DEPRECATED: A stringified version of the array (causes security issues)
  if (preg_match('/^array/', $template_array)) {
    if ($strict) {
      $errors['template_array'] = t('Invalid template array. Please provide
        a JSON formatted array');
      return FALSE;
    }
    else {
      $warnings['template_array'] = t('Please note that import of
        bulk loader templates as PHP arrays as a stringified array is deprecated
        and will be removed in future versions of Tripal. Export and import
        format will be JSON.');
    }
  }
  // DEPRECATED: A serialized PHP array
  elseif (preg_match('/^a:/', $template_array)) {
    if ($strict) {
      $errors['template_array'] = t('Invalid template array. Please provide
        a JSON formatted array');
      return FALSE;
    }
    else {
      $warnings['template_array'] = t('Please note that import of
        bulk loader templates as PHP serialized arrays is deprecated and will
        be removed in future versions of Tripal. Export and import format will
        be JSON.');
    }
  }
  // JSON FORMAT
  elseif (json_decode($template_array)) {
    // This is correct!
  }
  else {
    $errors['template_array'] = t('The template array must be in
      JSON format (although PHP strigified arrays and PHP serialized
      arrays are temporarily supported for backwards compatibility).');
    return FALSE;
  }

  // Make sure the template name is unique
  $name_exists = db_select('tripal_bulk_loader_template', 'tblt')
    ->fields('tblt', array('template_id'))
    ->condition('name', $template_name)
    ->execute()
    ->fetchField();
  if ($name_exists) {
    $errors['template_name'] = t('The template name already exists. Please
      choose another name.');
    return FALSE;
  }
  return TRUE;
}