tripal_core.ws_hal.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 = 'v0.1';
  9. $message = '';
  10. $api_url = "$base_url/ws/chado/$version";
  11. $page_limit = 25;
  12. $pager_id = 0;
  13. try {
  14. $id = 0;
  15. $action = '';
  16. // The table name is always specifid as the 3rd argument in the
  17. // current Drupal path.
  18. $table_name = arg(3);
  19. if (!chado_table_exists($table_name)) {
  20. throw new Exception("Table, '$table_name', is not a valid table.");
  21. }
  22. $schema = chado_get_schema($table_name);
  23. $pkey = $schema['primary key'][0];
  24. // If the fourth argument is numeric then the user is requesting a
  25. // record from the table. Otherwise the users is specifying an
  26. // action to perform on the table.
  27. if (is_numeric(arg(4))) {
  28. $id = arg(4);
  29. }
  30. else {
  31. $action = arg(4);
  32. }
  33. // Get any URL query arguments
  34. $query = drupal_get_query_parameters();
  35. // Specify the options for retrieving data.
  36. $options = array(
  37. 'return_array' => 1,
  38. 'pager' => array(
  39. 'limit' => $page_limit,
  40. 'element' => $pager_id
  41. ),
  42. );
  43. // Specify the values for selecing records.
  44. $values = array();
  45. if ($id) {
  46. $values[$pkey] = $id;
  47. }
  48. // Generate the chado variable.
  49. $var = chado_generate_var($table_name, $values, $options);
  50. // Links come first
  51. $result['_links'] = array('self' => array('href' => "$api_url/$table_name"));
  52. // If we have more than one record returned then this is a collection and
  53. // we should create the appropriate JSON for a collection.
  54. if (count($var) > 1) {
  55. $total = chado_pager_get_count($pager_id);
  56. // Add pager links
  57. $result['_links']['first'] = array('href' => "$api_url/$table_name");
  58. $result['_links']['prev'] = array('href' => "$api_url/$table_name?page=2");
  59. $result['_links']['next'] = array('href' => "$api_url/$table_name?page=4");
  60. $result['_links']['last'] = array('href' => "$api_url/$table_name?page=12");
  61. $result['count'] = count($var);
  62. $result['total'] = (integer) $total;
  63. // Do any expansion requested.
  64. $var = tripal_core_chado_ws_api_expand_object($var, $query);
  65. // Collection names are always plural of the table name
  66. foreach ($var as $item) {
  67. $table_name = $item->tablename;
  68. $item = tripal_core_chado_ws_api_object_format($item, $schema, $api_url, $query);
  69. $result['_embedded'][$table_name][] = $item;
  70. }
  71. }
  72. // If we only have one record then add it as a single record to the JSON.
  73. else {
  74. // Do any expansion requested.
  75. $var = tripal_core_chado_ws_api_expand_object($var, $query);
  76. $table_name = $item->tablename;
  77. $item = tripal_core_chado_ws_api_object_format($var, $schema, $api_url, $query);
  78. $result = $item;
  79. }
  80. }
  81. catch (Exception $e) {
  82. watchdog('tripal_ws', $e->getMessage(), array(), WATCHDOG_ERROR);
  83. $message = $e->getMessage();
  84. $status = 'error';
  85. $result = array();
  86. }
  87. // The responses follow the same format as the AGAVE API with a
  88. // status, message, version and all data in the "result" object.
  89. $response['status'] = $status;
  90. $response['message'] = $message;
  91. $response['version'] = $version;
  92. $response['result'] = $result;
  93. print drupal_json_output($response);
  94. }
  95. /**
  96. *
  97. * @param unknown $object
  98. * @param unknown $query
  99. */
  100. function tripal_core_chado_ws_api_expand_object($var, $query) {
  101. $page_limit = 25;
  102. $pager_id = 0;
  103. $options = array(
  104. 'return_array' => 1,
  105. 'pager' => array(
  106. 'limit' => $page_limit,
  107. 'element' => $pager_id
  108. ),
  109. );
  110. // If the user has requested to expand any fields then do that
  111. if ($query['expand_table']) {
  112. $expand_tables = explode(',', $query['expand_table']);
  113. foreach($expand_tables as $table) {
  114. // Does the table exist?
  115. if(!chado_table_exists($table)) {
  116. throw new Exception("Table, '$table', is not a valid table and thus cannot be expanded.");
  117. }
  118. // Expand the variable.
  119. $var = chado_expand_var($var, 'table', $table, $options);
  120. // if the table could not be expanded then the chado_expand_var
  121. // function just returns an empty record but sets the table name
  122. // in the object. For the JSON, we still want to create an _embedded
  123. // record so we have to create a stdClass object and set the
  124. // table name.
  125. if(!isset($var[0]->$table)) {
  126. $var[0]->$table = new stdClass();
  127. $var[0]->$table->tablename = $table;
  128. }
  129. }
  130. }
  131. if ($query['expand_field']) {
  132. $expand_fields = explode(',', $query['expand_field']);
  133. foreach($expand_fields as $field) {
  134. // TODO: check to make sure the field exists
  135. $var = chado_expand_var($var, 'field', $field);
  136. }
  137. }
  138. if ($query['expand_fkey']) {
  139. $expand_fkeys = explode(',', $query['expand_fkey']);
  140. foreach($expand_fkeys as $fkey) {
  141. // TODO: check to make sure the fkey exists
  142. $var = chado_expand_var($var, 'foreign_key', $fkey);
  143. }
  144. }
  145. if ($query['expand_node']) {
  146. $node_types = explode(',', $query['expand_node']);
  147. foreach($node_types as $node_type) {
  148. $var = chado_expand_var($var, 'node', $node_type);
  149. }
  150. }
  151. return $var;
  152. }
  153. /**
  154. *
  155. * @param $object
  156. * @param $schema
  157. * @param $api_url
  158. */
  159. function tripal_core_chado_ws_api_object_format($object, $schema, $api_url, $query) {
  160. global $base_url;
  161. $table_name = $object->tablename;
  162. $pkey = $schema['primary key'][0];
  163. $id = $object->$pkey;
  164. // Add the self link first
  165. $object->_links = array('self' => array('href' => "$api_url/$table_name/$id"));
  166. // Add the links for the table.
  167. $object->_links["schema"] = array('href' => "$api_url/$table_name/schema");
  168. // Add links for editing, insert, delete but only if user has permission.
  169. // TODO: how do we set permissions?
  170. $object->_links["add"] = array('href' => "$api_url/$table_name/add");
  171. // Add the links if an id is available.
  172. if ($id) {
  173. $object->_links["edit"] = array('href' => "$api_url/$table_name/$id/edit");
  174. $object->_links["delete"] = array('href' => "$api_url/$table_name/$id/delete");
  175. }
  176. // Add the link to the Drupal page.
  177. if ($object->nid) {
  178. $object->_links["view"] = array('href' => $base_url . url("node/$object->nid"));
  179. }
  180. // Deal with the expandable tables/fields/fkeys/nodes. Sometimes there are
  181. // specified in PHP with explicit numerical indexes and sometimes not. But,
  182. // the JSON converter will maintain the indexes if present which creates
  183. // an inconsistent look. So, we use the array_values function to just
  184. // get the list.
  185. $object->expandable_tables = array_values($object->expandable_tables);
  186. $object->expandable_fields = array_values($object->expandable_fields);
  187. $object->expandable_foreign_keys = array_values($object->expandable_foreign_keys);
  188. $object->expandable_nodes = array_values($object->expandable_nodes);
  189. // Add the links for the expandable fields, fkeys, nodes and tables
  190. // TODO: making a single key be an array may not be correct for HAL. check this out.
  191. if (count($object->expandable_fields) > 0) {
  192. $object->_links["expand_field"][] = array('href' => "$api_url/$table_name?expand_field={field}[,{field}...]");
  193. $object->_links["expand_field"][] = array('href' => "$api_url/$table_name/$id?expand_field={field}[,{field}...]");
  194. }
  195. else {
  196. unset($object->expandable_fields);
  197. }
  198. if (count($object->expandable_foreign_keys) > 0) {
  199. $object->_links["expand_fkey"][] = array('href' => "$api_url/$table_name?expand_fkey={fkey}[,{fkey}...]");
  200. $object->_links["expand_fkey"][] = array('href' => "$api_url/$table_name/$id?expand_fkey={fkey}[,{fkey}...]");
  201. }
  202. else {
  203. unset($object->expandable_foreign_keys);
  204. }
  205. if (count($object->expandable_nodes) > 0) {
  206. $object->_links["expand_node"][] = array('href' => "$api_url/$table_name?expand_node={node}[,{node}...]");
  207. $object->_links["expand_node"][] = array('href' => "$api_url/$table_name/$id?expand_node={node}[,{node}...]");
  208. }
  209. else {
  210. unset($object->expandable_nodes);
  211. }
  212. if (count($object->expandable_tables) > 0) {
  213. $object->_links["expand_table"][] = array('href' => "$api_url/$table_name?expand_table={table}[,{table}...]");
  214. $object->_links["expand_table"][] = array('href' => "$api_url/$table_name/$id?expand_table={table}[,{table}...]");
  215. }
  216. else {
  217. unset($object->expandable_tables);
  218. }
  219. // iterate through the items in the object and see if they in turn are
  220. // objects. If so, then recurse.
  221. foreach ($object as $key => $value) {
  222. // If any values are objects then recurse and format them correctly.
  223. if (is_object($value)) {
  224. $table_name = $value->tablename;
  225. $schema = chado_get_schema($table_name);
  226. if ($schema) {
  227. // Replace the object with the actual value if it exists. If there is
  228. // no key value then this is probably an expanded table so just unset
  229. if (property_exists($value, $key)) {
  230. $object->$key = $value->$key;
  231. }
  232. else {
  233. unset($object->key);
  234. }
  235. // Recursively format the object.
  236. $value = tripal_core_chado_ws_api_object_format($value, $schema, $api_url, $query);
  237. // Add the object as an "_embedded" object of the JSON.
  238. if (array_key_exists($table_name, $object->_embedded)) {
  239. // If the element is already an array then add to it, otherwise
  240. // convert it into an array.
  241. if (is_array($object->_embedded[$table_name])) {
  242. $object->_embedded[$table_name][] = $value;
  243. }
  244. else {
  245. $first = $object->_embedded[$table_name];
  246. $object->_embedded[$table_name] = array();
  247. $object->_embedded[$table_name] = $first;
  248. $object->_embedded[$table_name][] = $value;
  249. }
  250. }
  251. // This is the first time this embedded table has been seen
  252. // there fore, add the value as a single element.
  253. else {
  254. $object->_embedded[$table_name] = $value;
  255. }
  256. }
  257. else {
  258. throw new Exception("Table, '$table_name', is not a valid table.");
  259. }
  260. }
  261. }
  262. return $object;
  263. }