tripal_bulk_loader.api.templates.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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_query("SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template", array(':template' => $template_id))->fetchObject();
  31. $template_array = unserialize($template->template_array);
  32. if (!is_array($template_array)) {
  33. return TRUE;
  34. }
  35. }
  36. // Check that the new record name is not empty
  37. if (empty($new_record_name)) {
  38. return FALSE;
  39. }
  40. // Check the new record name is unique
  41. foreach ($template_array as $priority => $t) {
  42. if (strcmp($t['record_id'], $new_record_name) == 0) {
  43. if (($priority != $current_priority) AND ($current_priority !== NULL)) {
  44. return FALSE;
  45. }
  46. }
  47. }
  48. return TRUE;
  49. }
  50. /**
  51. * An API function to delete a record from a template array
  52. *
  53. * @param $delete_priority
  54. * The priority of the record to be deleted
  55. * @param $template_array
  56. * The array describing the template
  57. *
  58. * @return
  59. * The modified template array
  60. *
  61. * @ingroup tripal_bulk_loader_api
  62. */
  63. function tripal_bulk_loader_delete_record($delete_priority, $template_array) {
  64. if (empty($template_array)) {
  65. drupal_set_message("Unable to delete record with a priority of $priority since the template was not supplied",'error');
  66. return FALSE;
  67. }
  68. $new_template_array = array();
  69. $i=0;
  70. foreach ($template_array as $priority => $record) {
  71. if ($priority != $delete_priority) {
  72. $new_template_array[$i] = $record;
  73. $i++;
  74. }
  75. }
  76. return $new_template_array;
  77. }
  78. /**
  79. * An API function to delete a field from a template array
  80. *
  81. * @param $priority
  82. * The priority of the record containing the field
  83. * @param $delete_field_index
  84. * The index of the field to be deleted
  85. * @param $template_array
  86. * The array describing the template
  87. *
  88. * @return
  89. * The modified template array
  90. *
  91. * @ingroup tripal_bulk_loader_api
  92. */
  93. function tripal_bulk_loader_delete_field($priority, $delete_field_index, $template_array) {
  94. if (empty($template_array)) {
  95. drupal_set_message("Unable to delete record with a priority of $priority since the template was not supplied",'error');
  96. return FALSE;
  97. }
  98. // Re-order the remaining fields of the same record to ensure that the indicies are
  99. // 0 to size and. If this is not done, weird behaviour may result
  100. $new_template_array = $template_array;
  101. $new_template_array[$priority]['fields'] = array();
  102. $i=0;
  103. foreach ($template_array[$priority]['fields'] as $field_index => $field_details) {
  104. if ($field_index != $delete_field_index) {
  105. $new_template_array[$priority]['fields'][$i] = $field_details;
  106. $i++;
  107. }
  108. }
  109. // If this field was the only one in the current record, also delete the record
  110. if (empty($new_template_array[$priority]['fields'])) {
  111. $new_template_array = tripal_bulk_loader_delete_record($priority, $new_template_array);
  112. }
  113. return $new_template_array;
  114. }