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

File

legacy/tripal_core/api/tripal_core.tripal_variables.api.inc
View source
  1. <?php
  2. /**
  3. * Associates a variable and it's value to a node.
  4. *
  5. * If a variable is already associated more with a node then the rank value
  6. * is used to differentiate them. By default the rank is set to 0 and can
  7. * be manually set by using the $rank argument. But, if left unspecified
  8. * the next available rank will automatically be used.
  9. *
  10. * If the variable does not exist then it will be added automatically to
  11. * prevent errors. However, modules developers should always add their
  12. * variables first before using. If the variable already exists then it
  13. * will simply be re-used.
  14. *
  15. * @param $nid
  16. * The node ID.
  17. * @param $name
  18. * The name of the variable to associated with the node.
  19. * @param $value
  20. * The value of the variable.
  21. * @param $rank
  22. * The rank of the value. Default to zero.
  23. *
  24. * @return TRUE|FALSE
  25. * Returns TRUE if the variable was associated with the node and false if
  26. * a failure occured.
  27. */
  28. function tripal_add_node_variable($nid, $name, $value, $rank = 0) {
  29. // Make sure the variable exists. To prevent unwanted errors from
  30. // umet dependencies (e.g. modules using other modules' variables) the variable
  31. // will be created automatically if it is missing.
  32. $variable = tripal_get_variable($name);
  33. if (!$variable) {
  34. $variable = tripal_insert_variable($name, "Added automatically. Please describe this variable.");
  35. }
  36. // Is the variable already associated with this node? If so, then find the
  37. // next avilable rank. If the $update_existing variable is true then just
  38. // update the record.
  39. $values = tripal_get_node_variables($nid, $name);
  40. if (count($values) > 0) {
  41. // Get the max rank and increment the rank to the next highest value.
  42. $max_rank = 0;
  43. foreach ($values as $value) {
  44. // If the user has specified a rank then we want ot honor that. If the
  45. // rank is already used then don't continue.
  46. if ($rank > 0 and $rank == $value->rank) {
  47. tripal_report_error('tripal_core', TRIPAL_ERROR,
  48. "The rank for the term, '$term', is already used for node $nid. " .
  49. "Cannot add the variable.", array());
  50. return FALSE;
  51. }
  52. if ($value->rank > $max_rank) {
  53. $max_rank = $value->rank;
  54. }
  55. }
  56. $rank = $max_rank++;
  57. }
  58. // Add the new variable.
  59. $node_variable_id = db_insert('tripal_node_variables')
  60. ->fields(array(
  61. 'variable_id' => $variable->variable_id,
  62. 'nid' => $nid,
  63. 'value' => $value,
  64. 'rank' => $rank
  65. ))
  66. ->execute();
  67. return $node_variable_id;
  68. }
  69. /**
  70. * Returns one or more variables assigned to a node.
  71. *
  72. * An array is returned containing an object for each variable assigned
  73. * to a term that matches the criteria. If a name and rank are provided then
  74. * the variables returned must match.
  75. *
  76. * @param $nid
  77. * The node ID
  78. * @param $name
  79. * Optional. The name of the variable.
  80. * @param $rank
  81. * Optional. The rank of the variable to retreive.
  82. * @return
  83. * An array of variable objects.
  84. */
  85. function tripal_get_node_variables($nid, $name = '', $rank = '') {
  86. $variables = array();
  87. if (!$nid) {
  88. return $variables;
  89. }
  90. $query = db_select('tripal_node_variables', 'tnv')
  91. ->fields('tnv')
  92. ->condition('nid', $nid, '=');
  93. if ($name) {
  94. $variable = tripal_get_variable($name);
  95. $query->condition('variable_id', $variable->variable_id, '=');
  96. }
  97. if ($rank) {
  98. $query->condition('rank', $rank, '=');
  99. }
  100. $results = $query->execute();
  101. // Build the variables array and return it.
  102. while($variable = $results->fetchObject()) {
  103. $variables[] = $variable;
  104. }
  105. return $variables;
  106. }
  107. /**
  108. * Removes variables assigned to a node.
  109. *
  110. * @param $nid
  111. * The node ID
  112. * @param $name
  113. * Optional. The name of the variable.
  114. * @param $rank
  115. * Optional. The rank of the variable to retreive.
  116. *
  117. * @return
  118. * Return TRUE if deletion is successful, FALSE otherwise.
  119. */
  120. function tripal_delete_node_variables($nid, $name = '', $rank = '') {
  121. if (!$nid) {
  122. return FALSE;
  123. }
  124. $query = db_delete('tripal_node_variables')
  125. ->condition('nid', $nid, '=');
  126. if ($name) {
  127. $variable = tripal_get_variable($name);
  128. $query->condition('variable_id', $variable->variable_id, '=');
  129. }
  130. if ($rank) {
  131. $query->condition('rank', $rank, '=');
  132. }
  133. return $query->execute();
  134. }