tripal.variables.api.inc

Provides an application programming interface (API) for managing variables associated with Tripal managed content.

File

tripal/api/tripal.variables.api.inc
View source
  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. * @return
  30. * A record object containg the variable that was added if successful.
  31. *
  32. * @ingroup tripal_variables_api
  33. */
  34. function tripal_insert_variable($name, $description) {
  35. $name = trim($name);
  36. if (!$name) {
  37. tripal_report_error('tripal', TRIPAL_ERROR,
  38. 'Must have a variable name when adding a new Tripal Variable.', array());
  39. return NULL;
  40. }
  41. if (!$description) {
  42. tripal_report_error('tripal', TRIPAL_ERROR,
  43. 'Must have a description when adding a new Tripal Variable.', array());
  44. return NULL;
  45. }
  46. // Make sure the variable is not a duplicate. If so, then just select
  47. // it and return the variable_id
  48. $variable = tripal_get_variable($name);
  49. if ($variable) {
  50. return $variable;
  51. }
  52. else {
  53. db_insert('tripal_variables')
  54. ->fields(array(
  55. 'name' => $name,
  56. 'description' => $description,
  57. ))
  58. ->execute();
  59. return tripal_get_variable($name);
  60. }
  61. }
  62. /**
  63. * Retrieves the variable name record.
  64. *
  65. * @param $name
  66. * The name of the variable to retrieve
  67. * @return
  68. * A record object containg the variable.
  69. *
  70. * @ingroup tripal_variables_api
  71. */
  72. function tripal_get_variable($name) {
  73. return db_select('tripal_variables', 'v')
  74. ->fields('v')
  75. ->condition('name', $name)
  76. ->execute()
  77. ->fetchObject();
  78. }
  79. // TODO: add functions for getting/retrieving variables from/to entities.