tripal_bulk_loader.api.templates.inc 4.0 KB

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