tripal.variables.api.inc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 Tripal Variables API
  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 Drupal node. Variables are
  15. * meant to store non-biological information only. Be aware that any data
  16. * stored as a Tripal Variable will not be made visible through services such
  17. * as Tripal Web Services and therefore can be a good place to hide application
  18. * specific settings.
  19. *
  20. * @}
  21. *
  22. */
  23. /**
  24. * Adds a new variable name.
  25. *
  26. * @param $name
  27. * The name of the variable
  28. * @param $description
  29. * The description for the variable
  30. * @return
  31. * A record object containg the variable that was added if successful.
  32. */
  33. function tripal_insert_variable($name, $description) {
  34. $name = trim($name);
  35. if (!$name) {
  36. tripal_report_error('tripal', TRIPAL_ERROR,
  37. 'Must have a variable name when adding a new Tripal Variable.', array());
  38. return NULL;
  39. }
  40. if (!$description) {
  41. tripal_report_error('tripal', TRIPAL_ERROR,
  42. 'Must have a description when adding a new Tripal Variable.', array());
  43. return NULL;
  44. }
  45. // Make sure the variable is not a duplicate. If so, then just select
  46. // it and return the variable_id
  47. $variable = tripal_get_variable($name);
  48. if ($variable) {
  49. return $variable;
  50. }
  51. else {
  52. db_insert('tripal_variables')
  53. ->fields(array(
  54. 'name' => $name,
  55. 'description' => $description,
  56. ))
  57. ->execute();
  58. return tripal_get_variable($name);
  59. }
  60. }
  61. /**
  62. * Retrieves the variable name record.
  63. *
  64. * @param $name
  65. * The name of the variable to retrieve
  66. * @return
  67. * A record object containg the variable.
  68. */
  69. function tripal_get_variable($name) {
  70. return db_select('tripal_variables', 'v')
  71. ->fields('v')
  72. ->condition('name', $name)
  73. ->execute()
  74. ->fetchObject();
  75. }
  76. // TODO: add functions for getting/retrieving variables from/to entities.