tripal.variables.api.inc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) for managing variables
  5. * associated with Tripal managed content.
  6. */
  7. /**
  8. * @defgroup tripal_variables_api Variables
  9. * @ingroup tripal_api
  10. * @{
  11. * Provides an application programming interface (API) for managing variables
  12. * associated with Tripal managed content. The Tripal Variables API
  13. * supports storing any type of variable such as a property or setting that
  14. * should be associated with a Tripal managed content. Variables are
  15. * meant to store non-biological information only because biological data
  16. * should be stored together in the primary data store (e.g. Chado). Be aware
  17. * that any data stored as a Tripal Variable will not be made visible through
  18. * services such as Tripal Web Services and therefore can be a good place to
  19. * hide application specific settings.
  20. * @}
  21. */
  22. /**
  23. * Adds a new variable name.
  24. *
  25. * @param $name
  26. * The name of the variable
  27. * @param $description
  28. * The description for the variable
  29. *
  30. * @return
  31. * A record object containg the variable that was added if successful.
  32. *
  33. * @ingroup tripal_variables_api
  34. */
  35. function tripal_insert_variable($name, $description) {
  36. $name = trim($name);
  37. if (!$name) {
  38. tripal_report_error('tripal', TRIPAL_ERROR,
  39. 'Must have a variable name when adding a new Tripal Variable.', []);
  40. return NULL;
  41. }
  42. if (!$description) {
  43. tripal_report_error('tripal', TRIPAL_ERROR,
  44. 'Must have a description when adding a new Tripal Variable.', []);
  45. return NULL;
  46. }
  47. // Make sure the variable is not a duplicate. If so, then just select
  48. // it and return the variable_id
  49. $variable = tripal_get_variable($name);
  50. if ($variable) {
  51. return $variable;
  52. }
  53. else {
  54. db_insert('tripal_variables')
  55. ->fields([
  56. 'name' => $name,
  57. 'description' => $description,
  58. ])
  59. ->execute();
  60. return tripal_get_variable($name);
  61. }
  62. }
  63. /**
  64. * Retrieves the variable name record.
  65. *
  66. * @param $name
  67. * The name of the variable to retrieve
  68. *
  69. * @return
  70. * A record object containg the variable.
  71. *
  72. * @ingroup tripal_variables_api
  73. */
  74. function tripal_get_variable($name) {
  75. return db_select('tripal_variables', 'v')
  76. ->fields('v')
  77. ->condition('name', $name)
  78. ->execute()
  79. ->fetchObject();
  80. }
  81. // TODO: add functions for getting/retrieving variables from/to entities.