tripal_core.tripal_variables.api.inc

  1. 2.x tripal_core/api/tripal_core.tripal_variables.api.inc
  2. 3.x legacy/tripal_core/api/tripal_core.tripal_variables.api.inc

Provides an application programming interface (API) for managing variables associated with Tripal managed Drupal nodes/

File

tripal_core/api/tripal_core.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 Drupal nodes/
  6. */
  7. /**
  8. * @defgroup tripal_variables_api Tripal Variables API
  9. * @ingroup tripal_core_api
  10. * @{
  11. * Provides an application programming interface (API) for managing variables
  12. * associated with Tripal managed Drupal nodes. 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. All biological data should be
  16. * housed in the Chado tables. Be aware that any data stored as a Tripal
  17. * Variable will not be made visible through services such as Tripal Web
  18. * Services and therefore can be a good place to hide application specific
  19. * settings
  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_core', 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_core', 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. /**
  77. * Associates a variable and it's value to a node.
  78. *
  79. * If a variable is already associated more with a node then the rank value
  80. * is used to differentiate them. By default the rank is set to 0 and can
  81. * be manually set by using the $rank argument. But, if left unspecified
  82. * the next available rank will automatically be used.
  83. *
  84. * If the variable does not exist then it will be added automatically to
  85. * prevent errors. However, modules developers should always add their
  86. * variables first before using. If the variable already exists then it
  87. * will simply be re-used.
  88. *
  89. * @param $nid
  90. * The node ID.
  91. * @param $name
  92. * The name of the variable to associated with the node.
  93. * @param $value
  94. * The value of the variable.
  95. * @param $rank
  96. * The rank of the value. Default to zero.
  97. *
  98. * @return TRUE|FALSE
  99. * Returns TRUE if the variable was associated with the node and false if
  100. * a failure occured.
  101. */
  102. function tripal_add_node_variable($nid, $name, $value, $rank = 0) {
  103. // Make sure the variable exists. To prevent unwanted errors from
  104. // umet dependencies (e.g. modules using other modules' variables) the variable
  105. // will be created automatically if it is missing.
  106. $variable = tripal_get_variable($name);
  107. if (!$variable) {
  108. $variable = tripal_insert_variable($name, "Added automatically. Please describe this variable.");
  109. }
  110. // Is the variable already associated with this node? If so, then find the
  111. // next avilable rank. If the $update_existing variable is true then just
  112. // update the record.
  113. $values = tripal_get_node_variables($nid, $name);
  114. if (count($values) > 0) {
  115. // Get the max rank and increment the rank to the next highest value.
  116. $max_rank = 0;
  117. foreach ($values as $value) {
  118. // If the user has specified a rank then we want ot honor that. If the
  119. // rank is already used then don't continue.
  120. if ($rank > 0 and $rank == $value->rank) {
  121. tripal_report_error('tripal_core', TRIPAL_ERROR,
  122. "The rank for the term, '$term', is already used for node $nid. " .
  123. "Cannot add the variable.", array());
  124. return FALSE;
  125. }
  126. if ($value->rank > $max_rank) {
  127. $max_rank = $value->rank;
  128. }
  129. }
  130. $rank = $max_rank++;
  131. }
  132. // Add the new variable.
  133. $node_variable_id = db_insert('tripal_node_variables')
  134. ->fields(array(
  135. 'variable_id' => $variable->variable_id,
  136. 'nid' => $nid,
  137. 'value' => $value,
  138. 'rank' => $rank
  139. ))
  140. ->execute();
  141. return $node_variable_id;
  142. }
  143. /**
  144. * Returns one or more variables assigned to a node.
  145. *
  146. * An array is returned containing an object for each variable assigned
  147. * to a term that matches the criteria. If a name and rank are provided then
  148. * the variables returned must match.
  149. *
  150. * @param $nid
  151. * The node ID
  152. * @param $name
  153. * Optional. The name of the variable.
  154. * @param $rank
  155. * Optional. The rank of the variable to retreive.
  156. * @return
  157. * An array of variable objects.
  158. */
  159. function tripal_get_node_variables($nid, $name = '', $rank = '') {
  160. $variables = array();
  161. if (!$nid) {
  162. return $variables;
  163. }
  164. $query = db_select('tripal_node_variables', 'tnv')
  165. ->fields('tnv')
  166. ->condition('nid', $nid, '=');
  167. if ($name) {
  168. $variable = tripal_get_variable($name);
  169. $query->condition('variable_id', $variable->variable_id, '=');
  170. }
  171. if ($rank) {
  172. $query->condition('rank', $rank, '=');
  173. }
  174. $results = $query->execute();
  175. // Build the variables array and return it.
  176. while($variable = $results->fetchObject()) {
  177. $variables[] = $variable;
  178. }
  179. return $variables;
  180. }
  181. /**
  182. * Removes variables assigned to a node.
  183. *
  184. * @param $nid
  185. * The node ID
  186. * @param $name
  187. * Optional. The name of the variable.
  188. * @param $rank
  189. * Optional. The rank of the variable to retreive.
  190. *
  191. * @return
  192. * Return TRUE if deletion is successful, FALSE otherwise.
  193. */
  194. function tripal_delete_node_variables($nid, $name = '', $rank = '') {
  195. if (!$nid) {
  196. return FALSE;
  197. }
  198. $query = db_delete('tripal_node_variables')
  199. ->condition('nid', $nid, '=');
  200. if ($name) {
  201. $variable = tripal_get_variable($name);
  202. $query->condition('variable_id', $variable->variable_id, '=');
  203. }
  204. if ($rank) {
  205. $query->condition('rank', $rank, '=');
  206. }
  207. return $query->execute();
  208. }