tripal_core.ws_hal.inc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. function tripal_core_chado_hal_api() {
  3. global $base_url;
  4. // Set some initial variables.
  5. $response = array();
  6. $result = array();
  7. $status = 'success';
  8. $version = '0.1';
  9. $message = '';
  10. $api_url = "$base_url/ws/chado/$version";
  11. try {
  12. $table_name = arg(3);
  13. $schema = chado_get_schema($table_name);
  14. if (!$schema) {
  15. throw new Exception("Table $table_name is not a valid table.");
  16. }
  17. $limit = 25;
  18. $pager_id = 0;
  19. $options = array(
  20. 'return_array' => 1,
  21. 'pager' => array(
  22. 'limit' => $limit,
  23. 'element' => $pager_id
  24. ),
  25. );
  26. $values = array();
  27. $var = chado_generate_var($table_name, $values, $options);
  28. // Links come first
  29. $result['_links'] = array('self' => array('href' => "$api_url/$table_name"));
  30. if (count($var) > 1) {
  31. $total = chado_pager_get_count($pager_id);
  32. // Add pager links
  33. $result['_links']['first'] = array('href' => "$api_url/$table_name");
  34. $result['_links']['prev'] = array('href' => "$api_url/$table_name?page=2");
  35. $result['_links']['next'] = array('href' => "$api_url/$table_name?page=4");
  36. $result['_links']['last'] = array('href' => "$api_url/$table_name?page=12");
  37. $result['count'] = count($var);
  38. $result['total'] = (integer) $total;
  39. // Collection names are always plural of the table name
  40. foreach ($var as $item) {
  41. $table_name = $item->tablename;
  42. $item = tripal_core_chado_ws_api_object_format($item, $schema, $api_url);
  43. $result['_embedded'][$table_name][] = $item;
  44. }
  45. }
  46. }
  47. catch (Exception $e) {
  48. watchdog('tripal_ws', $e->getMessage(), array(), WATCHDOG_ERROR);
  49. $message = $e->getMessage();
  50. $status = 'error';
  51. $result = array();
  52. }
  53. // The responses follow the same format as the AGAVE API with a
  54. // status, message, version and all data in the "result" object.
  55. $response['status'] = $status;
  56. $response['message'] = $message;
  57. $response['version'] = $version;
  58. $response['result'] = $result;
  59. print drupal_json_output($response);
  60. }
  61. /**
  62. *
  63. * @param $object
  64. * @param $schema
  65. * @param $api_url
  66. */
  67. function tripal_core_chado_ws_api_object_format($object, $schema, $api_url) {
  68. global $base_url;
  69. $table_name = $object->tablename;
  70. $pkey = $schema['primary key'][0];
  71. $id = $object->$pkey;
  72. // Add the self link.
  73. $object->_links = array('self' => array('href' => "$api_url/$table_name/$id"));
  74. // Add the schema link.
  75. $object->_links["schema"] = array('href' => "$api_url/$table_name/schema");
  76. // Add links for editing, insert, delete but only if user has permission.
  77. // TODO: how do we set permissions?
  78. $object->_links["add"] = array('href' => "$api_url/$table_name/add");
  79. $object->_links["edit"] = array('href' => "$api_url/$table_name/$id/edit");
  80. $object->_links["delete"] = array('href' => "$api_url/$table_name/$id/delete");
  81. // Add the link to the Drupal page.
  82. if ($object->nid) {
  83. $object->_links["view"] = array('href' => $base_url . url("node/$object->nid"));
  84. }
  85. // Deal with the expandable tables/fields/fkeys/nodes. Sometimes there are
  86. // specified in PHP with explicit numerical indexes and sometimes not. But,
  87. // the JSON converter will maintain the indexes if present which creates
  88. // an inconsistent look. So, we use the array_values function to just
  89. // get the list.
  90. $object->expandable_tables = array_values($object->expandable_tables);
  91. $object->expandable_fields = array_values($object->expandable_fields);
  92. $object->expandable_foreign_keys = array_values($object->expandable_foreign_keys);
  93. $object->expandable_nodes = array_values($object->expandable_nodes);
  94. // Add the links for the expandable fields, fkeys, nodes and tables
  95. if (count($object->expandable_fields) > 0) {
  96. $object->_links["expand_field"] = array('href' => "$api_url/$table_name/$id?expand_field={field}");
  97. }
  98. else {
  99. unset($object->expandable_fields);
  100. }
  101. if (count($object->expandable_foreign_keys) > 0) {
  102. $object->_links["expand_fkey"] = array('href' => "$api_url/$table_name/$id?expand_fkey={fkey}");
  103. }
  104. else {
  105. unset($object->expandable_foreign_keys);
  106. }
  107. if (count($object->expandable_nodes) > 0) {
  108. $object->_links["expand_node"] = array('href' => "$api_url/$table_name/$id?expand_node={node}");
  109. }
  110. else {
  111. unset($object->expandable_nodes);
  112. }
  113. if (count($object->expandable_tables) > 0) {
  114. $object->_links["expand_table"] = array('href' => "$api_url/$table_name/$id?expand_table={table}");
  115. }
  116. else {
  117. unset($object->expandable_tables);
  118. }
  119. // iterate through the items in the object and see if they in turn are
  120. // objects. If so, then recurse.
  121. foreach ($object as $key => $value) {
  122. if (is_object($value)) {
  123. $table_name = $value->tablename;
  124. $schema = chado_get_schema($table_name);
  125. if ($schema) {
  126. // Replace the object with the actual value.
  127. $object->$key = $value->$key;
  128. // Recursively format the object.
  129. $value = tripal_core_chado_ws_api_object_format($value, $schema, $api_url);
  130. // Add the object as an "_embedded" object of the JSON.
  131. if (array_key_exists($table_name, $object->_embedded)) {
  132. // If the element is already an array then add to it, otherwise
  133. // convert it into an array.
  134. if (is_array($object->_embedded[$table_name])) {
  135. $object->_embedded[$table_name][] = $value;
  136. }
  137. else {
  138. $first = $object->_embedded[$table_name];
  139. $object->_embedded[$table_name] = array();
  140. $object->_embedded[$table_name] = $first;
  141. $object->_embedded[$table_name][] = $value;
  142. }
  143. }
  144. // This is the first time this embedded table has been seen
  145. // there fore, add the value as a single element.
  146. else {
  147. $object->_embedded[$table_name] = $value;
  148. }
  149. }
  150. }
  151. }
  152. return $object;
  153. }