123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- <?php
- function tripal_validate_bulk_loader_template($val_type, &$options, &$errors, &$warnings = []) {
- $template_array = trim($options['template_array']);
- $template_name = trim($options['template_name']);
- $strict = array_key_exists('strict', $options) ? $options['strict'] : FALSE;
-
-
- 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.');
- }
- }
-
- 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.');
- }
- }
-
- elseif (json_decode($template_array)) {
-
- }
- 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;
- }
-
- $name_exists = db_select('tripal_bulk_loader_template', 'tblt')
- ->fields('tblt', ['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;
- }
- 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;
- }
-
- $template_array = trim($options['template_array']);
- $template_name = trim($options['template_name']);
-
-
-
-
- if (preg_match('/^array/', $template_array)) {
- $tarray = [];
- eval("\$tarray = $template_array;");
- $template_array = serialize($tarray);
- }
-
-
-
-
- elseif (preg_match('/^a:/', $template_array)) {
-
- }
-
- elseif (json_decode($template_array)) {
- $template_array = serialize(json_decode($template_array, TRUE));
- }
- else {
- $errors['template_array'] = t('Unrecognized array type.');
- return FALSE;
- }
- $record = [
- 'name' => $template_name,
- 'template_array' => $template_array,
- 'created' => time(),
- 'changed' => time(),
- ];
- if (!drupal_write_record('tripal_bulk_loader_template', $record)) {
- return FALSE;
- }
- return TRUE;
- }
- function tripal_is_bulk_loader_record_name_unique($new_record_name, $template_id, $template_array = NULL, $current_priority = NULL) {
-
- if (empty($template_array)) {
- $template = db_query("SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template", [':template' => $template_id])->fetchObject();
- $template_array = unserialize($template->template_array);
- if (!is_array($template_array)) {
- return TRUE;
- }
- }
-
- if (empty($new_record_name)) {
- return FALSE;
- }
-
- foreach ($template_array as $priority => $t) {
- if (strcmp($t['record_id'], $new_record_name) == 0) {
- if (($priority != $current_priority) AND ($current_priority !== NULL)) {
- return FALSE;
- }
- }
- }
- return TRUE;
- }
- function tripal_delete_bulk_loader_record($delete_priority, $template_array) {
- if (empty($template_array)) {
- drupal_set_message("Unable to delete record with a priority of $priority since the template was not supplied", 'error');
- return FALSE;
- }
- $new_template_array = [];
- $i = 0;
- foreach ($template_array as $priority => $record) {
- if ($priority != $delete_priority) {
- $new_template_array[$i] = $record;
- $i++;
- }
- }
- return $new_template_array;
- }
- function tripal_delete_bulk_loader_field($priority, $delete_field_index, $template_array) {
- if (empty($template_array)) {
- drupal_set_message("Unable to delete record with a priority of $priority since the template was not supplied", 'error');
- return FALSE;
- }
-
-
- $new_template_array = $template_array;
- $new_template_array[$priority]['fields'] = [];
- $i = 0;
- foreach ($template_array[$priority]['fields'] as $field_index => $field_details) {
- if ($field_index != $delete_field_index) {
- $new_template_array[$priority]['fields'][$i] = $field_details;
- $i++;
- }
- }
-
- if (empty($new_template_array[$priority]['fields'])) {
- $new_template_array = tripal_delete_bulk_loader_record($priority, $new_template_array);
- }
- return $new_template_array;
- }
|