tripal_bulk_loader.api.templates.inc

  1. 2.x tripal_bulk_loader/api/tripal_bulk_loader.api.templates.inc
  2. 3.x tripal_bulk_loader/api/tripal_bulk_loader.api.templates.inc
  3. 1.x tripal_bulk_loader/api/tripal_bulk_loader.api.templates.inc

Provides functions for hooking into bulk loader functionality.

File

tripal_bulk_loader/api/tripal_bulk_loader.api.templates.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Provides functions for hooking into bulk loader functionality.
  5. *
  6. * @ingroup tripal_bulk_loader
  7. */
  8. /**
  9. * @defgroup tripal_bulk_loader_api Bulk Loader
  10. * @ingroup tripal_api
  11. * @{
  12. * All functions in this file provide an API to administrative management of
  13. * bulk loader templates.
  14. * @}
  15. */
  16. /**
  17. * Validates an $options array for insert or update of a bulk loader record.
  18. *
  19. * @param $val_type
  20. * The type of validation. Can be either 'insert' or 'update'.
  21. * @param $options
  22. * An array of key/value pairs containing the following keys:
  23. * template_name: The name of the template.
  24. * template_array: The JSON array representing the template.
  25. * Optional:
  26. * strict: If set then only JSON formatted templates are allowed.
  27. * @param $errors
  28. * An empty array where validation error messages will be set. The keys
  29. * of the array will be name of the field from the options array and the
  30. * value is the error message.
  31. * @param $warnings
  32. * An empty array where validation warning messagges will be set. The
  33. * warnings should not stop an insert or an update but should be provided
  34. * to the user as information by a drupal_set_message() if appropriate. The
  35. * keys of the array will be name of the field from the options array and the
  36. * value is the error message.
  37. *
  38. * @return
  39. * If validation failes then FALSE is returned. Any options that do not pass
  40. * validation checks will be added in the $errors array with the key being
  41. * the option and the value being the error message. If validation
  42. * is successful then TRUE is returned.
  43. *
  44. * @ingroup tripal_bulk_loader_api
  45. */
  46. function tripal_validate_bulk_loader_template($val_type, &$options, &$errors, &$warnings = array()) {
  47. $template_array = trim($options['template_array']);
  48. $template_name = trim($options['template_name']);
  49. $strict = array_key_exists('strict', $options) ? $options['strict'] : FALSE;
  50. // Make sure the template array is one of the supported types
  51. // DEPRECATED: A stringified version of the array (causes security issues)
  52. if (preg_match('/^array/', $template_array)) {
  53. if ($strict) {
  54. $errors['template_array'] = t('Invalid template array. Please provide
  55. a JSON formatted array');
  56. return FALSE;
  57. }
  58. else {
  59. $warnings['template_array'] = t('Please note that import of
  60. bulk loader templates as PHP arrays as a stringified array is deprecated
  61. and will be removed in future versions of Tripal. Export and import
  62. format will be JSON.');
  63. }
  64. }
  65. // DEPRECATED: A serialized PHP array
  66. elseif (preg_match('/^a:/', $template_array)) {
  67. if ($strict) {
  68. $errors['template_array'] = t('Invalid template array. Please provide
  69. a JSON formatted array');
  70. return FALSE;
  71. }
  72. else {
  73. $warnings['template_array'] = t('Please note that import of
  74. bulk loader templates as PHP serialized arrays is deprecated and will
  75. be removed in future versions of Tripal. Export and import format will
  76. be JSON.');
  77. }
  78. }
  79. // JSON FORMAT
  80. elseif (json_decode($template_array)) {
  81. // This is correct!
  82. }
  83. else {
  84. $errors['template_array'] = t('The template array must be in
  85. JSON format (although PHP strigified arrays and PHP serialized
  86. arrays are temporarily supported for backwards compatibility).');
  87. return FALSE;
  88. }
  89. // Make sure the template name is unique
  90. $name_exists = db_select('tripal_bulk_loader_template', 'tblt')
  91. ->fields('tblt', array('template_id'))
  92. ->condition('name', $template_name)
  93. ->execute()
  94. ->fetchField();
  95. if ($name_exists) {
  96. $errors['template_name'] = t('The template name already exists. Please
  97. choose another name.');
  98. return FALSE;
  99. }
  100. return TRUE;
  101. }
  102. /**
  103. * Inserts a bulk loader template record.
  104. *
  105. * This function validates the options passed prior to insertion of the record.
  106. *
  107. * @param $options
  108. * An array of key/value pairs containing the following keys:
  109. * 'template_name': The name of the template.
  110. * 'template_array': The JSON array representing the template.
  111. * Optional:
  112. * 'strict': If set then only JSON formatted templates are allowed.
  113. * @param $errors
  114. * An empty array where validation error messages will be set. The keys
  115. * of the array will be name of the field from the options array and the
  116. * value is the error message.
  117. * @param $warnings
  118. * An empty array where validation warning messagges will be set. The
  119. * warnings should not stop an insert or an update but should be provided
  120. * to the user as information by a drupal_set_message() if appropriate. The
  121. * keys of the array will be name of the field from the options array and the
  122. * value is the error message.
  123. * @return
  124. * TRUE for success and FALSE for failure.
  125. *
  126. * @ingroup tripal_bulk_loader_api
  127. */
  128. function tripal_insert_bulk_loader_template($options, &$errors, &$warnings) {
  129. $success = tripal_validate_bulk_loader_template('insert', $options, $errors, $warnings);
  130. if (!$success) {
  131. foreach ($errors as $field => $message) {
  132. tripal_report_error('tripal_bulkldr', TRIPAL_ERROR, $message);
  133. }
  134. return FALSE;
  135. }
  136. // Insert the bulk loader template.
  137. $template_array = trim($options['template_array']);
  138. $template_name = trim($options['template_name']);
  139. // Previous version of Tripal would export the template as a PHP array.
  140. // This has security implications and is deprecated. This support should
  141. // be reomved in future versions of Tripal, but to support transfers of
  142. // templates between v1.1 and v2.x sites we support it.
  143. if (preg_match('/^array/', $template_array)) {
  144. $tarray = array();
  145. eval("\$tarray = $template_array;");
  146. $template_array = serialize($tarray);
  147. }
  148. // For a brief period, the bulk loader templates were exported as a PHP
  149. // serialized array. We have moved to exporting in JSON as JSON is more
  150. // user friendly. But we must support the serialized PHP array for
  151. // backwards compatibility of v2.0-rc1 sites and v2.x sites.
  152. elseif (preg_match('/^a:/', $template_array)) {
  153. // do nothing it's in PHP serialized format
  154. }
  155. // The typical format is JSON
  156. elseif (json_decode($template_array)) {
  157. $template_array = serialize(json_decode($template_array, TRUE));
  158. }
  159. else {
  160. $errors['template_array'] = t('Unrecognized array type.');
  161. return FALSE;
  162. }
  163. $record = array(
  164. 'name' => $template_name,
  165. 'template_array' => $template_array,
  166. 'created' => time(),
  167. 'changed' => time()
  168. );
  169. if (!drupal_write_record('tripal_bulk_loader_template', $record)) {
  170. return FALSE;
  171. }
  172. return TRUE;
  173. }
  174. /**
  175. * Meant to be called from a form_validate function to ensure a newly added bulk
  176. * loader record name is unique and not empty.
  177. *
  178. * @param $new_record_name
  179. * The record name to check for uniqueness
  180. * @param $template_id
  181. * The template_id of the template to add the record to
  182. * @param $template_array
  183. * The array describing the template. Optional -will be loaded using
  184. * template_id if not provided
  185. * @param $current_priority
  186. * The priority of the already existing record -checks that the name only
  187. * occurs on this particular record
  188. *
  189. * @return
  190. * TRUE if the record name is not empty and not already in the template_array;
  191. * FALSE otherwise
  192. *
  193. * @ingroup tripal_bulk_loader_api
  194. */
  195. function tripal_is_bulk_loader_record_name_unique($new_record_name, $template_id, $template_array = NULL, $current_priority = NULL) {
  196. // get the template array if it's not supplied
  197. if (empty($template_array)) {
  198. $template = db_query("SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template", array(':template' => $template_id))->fetchObject();
  199. $template_array = unserialize($template->template_array);
  200. if (!is_array($template_array)) {
  201. return TRUE;
  202. }
  203. }
  204. // Check that the new record name is not empty
  205. if (empty($new_record_name)) {
  206. return FALSE;
  207. }
  208. // Check the new record name is unique
  209. foreach ($template_array as $priority => $t) {
  210. if (strcmp($t['record_id'], $new_record_name) == 0) {
  211. if (($priority != $current_priority) AND ($current_priority !== NULL)) {
  212. return FALSE;
  213. }
  214. }
  215. }
  216. return TRUE;
  217. }
  218. /**
  219. * An API function to delete a record from a template array
  220. *
  221. * @param $delete_priority
  222. * The priority of the record to be deleted
  223. * @param $template_array
  224. * The array describing the template
  225. *
  226. * @return
  227. * The modified template array
  228. *
  229. * @ingroup tripal_bulk_loader_api
  230. */
  231. function tripal_delete_bulk_loader_record($delete_priority, $template_array) {
  232. if (empty($template_array)) {
  233. drupal_set_message("Unable to delete record with a priority of $priority since the template was not supplied",'error');
  234. return FALSE;
  235. }
  236. $new_template_array = array();
  237. $i=0;
  238. foreach ($template_array as $priority => $record) {
  239. if ($priority != $delete_priority) {
  240. $new_template_array[$i] = $record;
  241. $i++;
  242. }
  243. }
  244. return $new_template_array;
  245. }
  246. /**
  247. * An API function to delete a field from a template array
  248. *
  249. * @param $priority
  250. * The priority of the record containing the field
  251. * @param $delete_field_index
  252. * The index of the field to be deleted
  253. * @param $template_array
  254. * The array describing the template
  255. *
  256. * @return
  257. * The modified template array
  258. *
  259. * @ingroup tripal_bulk_loader_api
  260. */
  261. function tripal_delete_bulk_loader_field($priority, $delete_field_index, $template_array) {
  262. if (empty($template_array)) {
  263. drupal_set_message("Unable to delete record with a priority of $priority since the template was not supplied",'error');
  264. return FALSE;
  265. }
  266. // Re-order the remaining fields of the same record to ensure that the indicies are
  267. // 0 to size and. If this is not done, weird behaviour may result
  268. $new_template_array = $template_array;
  269. $new_template_array[$priority]['fields'] = array();
  270. $i=0;
  271. foreach ($template_array[$priority]['fields'] as $field_index => $field_details) {
  272. if ($field_index != $delete_field_index) {
  273. $new_template_array[$priority]['fields'][$i] = $field_details;
  274. $i++;
  275. }
  276. }
  277. // If this field was the only one in the current record, also delete the record
  278. if (empty($new_template_array[$priority]['fields'])) {
  279. $new_template_array = tripal_delete_bulk_loader_record($priority, $new_template_array);
  280. }
  281. return $new_template_array;
  282. }

Related topics