tripal_bulk_loader.api.templates.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * @file
  4. * All functions in this file provide an API to administrative management of bulk loader templates
  5. */
  6. /**
  7. * Meant to be called from a form_validate function to ensure a newly added bulk loader record
  8. * name is unique and not empty.
  9. *
  10. * @param $new_record_name
  11. * The record name to check for uniqueness
  12. * @param $template_id
  13. * The template_id of the template to add the record to
  14. * @param $template_array
  15. * The array describing the template. Optional -will be loaded using template_id if not provided
  16. * @param $current_priority
  17. * The priority of the already existing record -checks that the name only occurs on this particular record
  18. *
  19. * @return
  20. * TRUE if the record name is not empty and not already in the template_array; FALSE otherwise
  21. */
  22. function tripal_bulk_loader_is_record_name_unique($new_record_name, $template_id, $template_array = NULL, $current_priority = NULL) {
  23. // get the template array if it's not supplied
  24. if (empty($template_array)) {
  25. $template = db_fetch_object(db_query("SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d", $template_id));
  26. $template_array = unserialize($template->template_array);
  27. if (!is_array($template_array)) {
  28. watchdog(
  29. 'tripal_bulk_loader',
  30. 'Unable to retrieve template array from database where template_id=%template_id',
  31. array('%template_id' => $template_id),
  32. WATCHDOG_WARNING
  33. );
  34. return FALSE;
  35. }
  36. }
  37. // Check that the new record name is not empty
  38. if (empty($new_record_name)) {
  39. return FALSE;
  40. }
  41. // Check the new record name is unique
  42. foreach ($template_array as $priority => $t) {
  43. if (strcmp($t['record_id'], $new_record_name) == 0) {
  44. if (($priority != $current_priority) AND ($current_priority !== NULL)) {
  45. return FALSE;
  46. }
  47. }
  48. }
  49. return TRUE;
  50. }
  51. /**
  52. * An API function to delete a record from a template array
  53. *
  54. * @param $delete_priority
  55. * The priority of the record to be deleted
  56. * @param $template_array
  57. * The array describing the template
  58. *
  59. * @return
  60. * The modified template array
  61. */
  62. function tripal_bulk_loader_delete_record($delete_priority, $template_array) {
  63. if (empty($template_array)) {
  64. drupal_set_message("Unable to delete record with a priority of $priority since the template was not supplied",'error');
  65. return FALSE;
  66. }
  67. $new_template_array = array();
  68. $i=0;
  69. foreach ($template_array as $priority => $record) {
  70. if ($priority != $delete_priority) {
  71. $new_template_array[$i] = $record;
  72. $i++;
  73. }
  74. }
  75. return $new_template_array;
  76. }
  77. /**
  78. * An API function to delete a field from a template array
  79. *
  80. * @param $priority
  81. * The priority of the record containing the field
  82. * @param $delete_field_index
  83. * The index of the field to be deleted
  84. * @param $template_array
  85. * The array describing the template
  86. *
  87. * @return
  88. * The modified template array
  89. */
  90. function tripal_bulk_loader_delete_field($priority, $delete_field_index, $template_array) {
  91. if (empty($template_array)) {
  92. drupal_set_message("Unable to delete record with a priority of $priority since the template was not supplied",'error');
  93. return FALSE;
  94. }
  95. // Re-order the remaining fields of the same record to ensure that the indicies are
  96. // 0 to size and. If this is not done, weird behaviour may result
  97. $new_template_array = $template_array;
  98. $new_template_array[$priority]['fields'] = array();
  99. $i=0;
  100. foreach ($template_array[$priority]['fields'] as $field_index => $field_details) {
  101. if ($field_index != $delete_field_index) {
  102. $new_template_array[$priority]['fields'][$i] = $field_details;
  103. $i++;
  104. }
  105. }
  106. // If this field was the only one in the current record, also delete the record
  107. if (empty($new_template_array[$priority]['fields'])) {
  108. $new_template_array = tripal_bulk_loader_delete_record($priority, $new_template_array);
  109. }
  110. return $new_template_array;
  111. }