|
@@ -14,10 +14,13 @@ function tripal_core_chado_hal_api() {
|
|
|
$page_limit = 25;
|
|
|
$pager_id = 0;
|
|
|
|
|
|
+ // Lump everything ito a try block so that if there is a problem we can
|
|
|
+ // throw an error and have that returned in the response.
|
|
|
try {
|
|
|
$id = 0;
|
|
|
$action = '';
|
|
|
|
|
|
+ // GET THE BASE TABLE TO QUERY
|
|
|
// The table name is always specifid as the 3rd argument in the
|
|
|
// current Drupal path.
|
|
|
$table_name = arg(3);
|
|
@@ -27,11 +30,15 @@ function tripal_core_chado_hal_api() {
|
|
|
$schema = chado_get_schema($table_name);
|
|
|
$pkey = $schema['primary key'][0];
|
|
|
|
|
|
+ // GET THE RECORD AND THE ACTION TO PERFORM
|
|
|
// If the fourth argument is numeric then the user is requesting a
|
|
|
// record from the table. Otherwise the users is specifying an
|
|
|
// action to perform on the table.
|
|
|
if (is_numeric(arg(4))) {
|
|
|
$id = arg(4);
|
|
|
+ if (arg(5)) {
|
|
|
+ $action = arg(5);
|
|
|
+ }
|
|
|
}
|
|
|
else {
|
|
|
$action = arg(4);
|
|
@@ -40,60 +47,93 @@ function tripal_core_chado_hal_api() {
|
|
|
// Get any URL query arguments
|
|
|
$query = drupal_get_query_parameters();
|
|
|
|
|
|
- // Specify the options for retrieving data.
|
|
|
- $options = array(
|
|
|
- 'return_array' => 1,
|
|
|
- 'pager' => array(
|
|
|
- 'limit' => $page_limit,
|
|
|
- 'element' => $pager_id
|
|
|
- ),
|
|
|
- );
|
|
|
-
|
|
|
- // Specify the values for selecing records.
|
|
|
- $values = array();
|
|
|
- if ($id) {
|
|
|
- $values[$pkey] = $id;
|
|
|
- }
|
|
|
-
|
|
|
- // Generate the chado variable.
|
|
|
- $var = chado_generate_var($table_name, $values, $options);
|
|
|
+ switch ($action) {
|
|
|
+ case 'schema':
|
|
|
+ $result = $schema;
|
|
|
+ break;
|
|
|
+ case 'add':
|
|
|
+ $message = "Action, '$action', is currently not supported.";
|
|
|
+ $result = array();
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ // Specify the values for selecing records.
|
|
|
+ $values = array();
|
|
|
+ if ($id) {
|
|
|
+ $values[$pkey] = $id;
|
|
|
+ }
|
|
|
|
|
|
- // Links come first
|
|
|
- $result['_links'] = array('self' => array('href' => "$api_url/$table_name"));
|
|
|
-
|
|
|
- // If we have more than one record returned then this is a collection and
|
|
|
- // we should create the appropriate JSON for a collection.
|
|
|
- if (count($var) > 1) {
|
|
|
- $total = chado_pager_get_count($pager_id);
|
|
|
- // Add pager links
|
|
|
- $result['_links']['first'] = array('href' => "$api_url/$table_name");
|
|
|
- $result['_links']['prev'] = array('href' => "$api_url/$table_name?page=2");
|
|
|
- $result['_links']['next'] = array('href' => "$api_url/$table_name?page=4");
|
|
|
- $result['_links']['last'] = array('href' => "$api_url/$table_name?page=12");
|
|
|
-
|
|
|
- $result['count'] = count($var);
|
|
|
- $result['total'] = (integer) $total;
|
|
|
-
|
|
|
- // Do any expansion requested.
|
|
|
- $var = tripal_core_chado_ws_api_expand_object($var, $query);
|
|
|
+ // Specify the options for retrieving data.
|
|
|
+ $options = array(
|
|
|
+ 'return_array' => 1,
|
|
|
+ 'pager' => array(
|
|
|
+ 'limit' => $page_limit,
|
|
|
+ 'element' => $pager_id
|
|
|
+ ),
|
|
|
+ );
|
|
|
+
|
|
|
+ // Generate the chado variable.
|
|
|
+ $var = chado_generate_var($table_name, $values, $options);
|
|
|
|
|
|
- // Collection names are always plural of the table name
|
|
|
- foreach ($var as $item) {
|
|
|
- $table_name = $item->tablename;
|
|
|
-
|
|
|
- $item = tripal_core_chado_ws_api_object_format($item, $schema, $api_url, $query);
|
|
|
- $result['_embedded'][$table_name][] = $item;
|
|
|
- }
|
|
|
- }
|
|
|
- // If we only have one record then add it as a single record to the JSON.
|
|
|
- else {
|
|
|
-
|
|
|
- // Do any expansion requested.
|
|
|
- $var = tripal_core_chado_ws_api_expand_object($var, $query);
|
|
|
+ // If we have more than one record returned then this is a collection and
|
|
|
+ // we should create the appropriate JSON for a collection.
|
|
|
+ if (count($var) > 1) {
|
|
|
+
|
|
|
+ // Get the total number of records
|
|
|
+ $total = chado_pager_get_count($pager_id);
|
|
|
+ $curr_page = array_key_exists('page', $query) ? $query['page'] : 0;
|
|
|
+
|
|
|
+ $first_page = '0';
|
|
|
+ $last_page = ceil($total / $page_limit) - 1;
|
|
|
+ $result['_links']['first'] = array('href' => "$api_url/$table_name");
|
|
|
+ if ($curr_page > 0) {
|
|
|
+ $prev_page = $curr_page - 1;
|
|
|
+ if ($prev_page != $first_page) {
|
|
|
+ $result['_links']['previous'] = array('href' => "$api_url/$table_name?page=$prev_page");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $result['_links']['previous'] = $result['_links']['first'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ($curr_page < $last_page) {
|
|
|
+ $next_page = $curr_page + 1;
|
|
|
+ $result['_links']['next'] = array('href' => "$api_url/$table_name?page=$next_page");
|
|
|
+ }
|
|
|
+ if ($last_page > $first_page) {
|
|
|
+ $result['_links']['last'] = array('href' => "$api_url/$table_name?page=$last_page");
|
|
|
+ }
|
|
|
|
|
|
- $table_name = $item->tablename;
|
|
|
- $item = tripal_core_chado_ws_api_object_format($var, $schema, $api_url, $query);
|
|
|
- $result = $item;
|
|
|
+ // Add the number of elements for this collection
|
|
|
+ $result['count'] = count($var);
|
|
|
+ $result['total'] = (integer) $total;
|
|
|
+ $result['current_page'] = (integer) $curr_page;
|
|
|
+ $result['items_per_page'] = $page_limit;
|
|
|
+
|
|
|
+ // Do any expansion requested.
|
|
|
+ if ($action == 'expand') {
|
|
|
+ $var = tripal_core_chado_ws_api_expand_object($var, $query);
|
|
|
+ }
|
|
|
+
|
|
|
+ // recursively reformat the expanded objects to match HAL requirements.
|
|
|
+ foreach ($var as $item) {
|
|
|
+ $table_name = $item->tablename;
|
|
|
+
|
|
|
+ $item = tripal_core_chado_ws_api_object_format($item, $schema, $api_url, $query);
|
|
|
+ $result['_embedded'][$table_name][] = $item;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // If we only have one record then add it as a single record to the JSON.
|
|
|
+ else {
|
|
|
+ $item = $var[0];
|
|
|
+
|
|
|
+ // Do any expansion requested.
|
|
|
+ if ($action == 'expand') {
|
|
|
+ $item = tripal_core_chado_ws_api_expand_object($item, $query);
|
|
|
+ }
|
|
|
+
|
|
|
+ // recursively reformat the expanded objects to match HAL requirements.
|
|
|
+ $item = tripal_core_chado_ws_api_object_format($item, $schema, $api_url, $query);
|
|
|
+ $result = $item;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
catch (Exception $e) {
|
|
@@ -118,6 +158,7 @@ function tripal_core_chado_hal_api() {
|
|
|
* @param unknown $query
|
|
|
*/
|
|
|
function tripal_core_chado_ws_api_expand_object($var, $query) {
|
|
|
+
|
|
|
$page_limit = 25;
|
|
|
$pager_id = 0;
|
|
|
$options = array(
|
|
@@ -127,10 +168,10 @@ function tripal_core_chado_ws_api_expand_object($var, $query) {
|
|
|
'element' => $pager_id
|
|
|
),
|
|
|
);
|
|
|
-
|
|
|
+
|
|
|
// If the user has requested to expand any fields then do that
|
|
|
- if ($query['expand_table']) {
|
|
|
- $expand_tables = explode(',', $query['expand_table']);
|
|
|
+ if ($query['table']) {
|
|
|
+ $expand_tables = explode(',', $query['table']);
|
|
|
foreach($expand_tables as $table) {
|
|
|
// Does the table exist?
|
|
|
if(!chado_table_exists($table)) {
|
|
@@ -151,26 +192,20 @@ function tripal_core_chado_ws_api_expand_object($var, $query) {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- if ($query['expand_field']) {
|
|
|
- $expand_fields = explode(',', $query['expand_field']);
|
|
|
+ if ($query['field']) {
|
|
|
+ $expand_fields = explode(',', $query['field']);
|
|
|
foreach($expand_fields as $field) {
|
|
|
// TODO: check to make sure the field exists
|
|
|
$var = chado_expand_var($var, 'field', $field);
|
|
|
}
|
|
|
}
|
|
|
- if ($query['expand_fkey']) {
|
|
|
- $expand_fkeys = explode(',', $query['expand_fkey']);
|
|
|
+ if ($query['fkey']) {
|
|
|
+ $expand_fkeys = explode(',', $query['fkey']);
|
|
|
foreach($expand_fkeys as $fkey) {
|
|
|
// TODO: check to make sure the fkey exists
|
|
|
$var = chado_expand_var($var, 'foreign_key', $fkey);
|
|
|
}
|
|
|
}
|
|
|
- if ($query['expand_node']) {
|
|
|
- $node_types = explode(',', $query['expand_node']);
|
|
|
- foreach($node_types as $node_type) {
|
|
|
- $var = chado_expand_var($var, 'node', $node_type);
|
|
|
- }
|
|
|
- }
|
|
|
return $var;
|
|
|
}
|
|
|
|
|
@@ -181,6 +216,7 @@ function tripal_core_chado_ws_api_expand_object($var, $query) {
|
|
|
* @param $api_url
|
|
|
*/
|
|
|
function tripal_core_chado_ws_api_object_format($object, $schema, $api_url, $query) {
|
|
|
+
|
|
|
global $base_url;
|
|
|
$table_name = $object->tablename;
|
|
|
$pkey = $schema['primary key'][0];
|
|
@@ -202,9 +238,24 @@ function tripal_core_chado_ws_api_object_format($object, $schema, $api_url, $que
|
|
|
$object->_links["delete"] = array('href' => "$api_url/$table_name/$id/delete");
|
|
|
}
|
|
|
|
|
|
- // Add the link to the Drupal page.
|
|
|
- if ($object->nid) {
|
|
|
+ // Add the link to the Drupal page if a node exists.
|
|
|
+ if (property_exists($object, 'nid')) {
|
|
|
$object->_links["view"] = array('href' => $base_url . url("node/$object->nid"));
|
|
|
+ // Unset the node ID because it's really only needed within the context
|
|
|
+ // of the local Drupal site.
|
|
|
+ unset($object->nid);
|
|
|
+ }
|
|
|
+
|
|
|
+ // It doesn't make sense to allow expansion of node information outside
|
|
|
+ // of the context of the local Drupal site so remove this object.
|
|
|
+ unset($object->expandable_nodes);
|
|
|
+
|
|
|
+ // Only include links for expanding if the option to exclude them has not
|
|
|
+ // been passed.
|
|
|
+ if (array_key_exists('no_expansion_details', $query)) {
|
|
|
+ unset($object->expandable_fields);
|
|
|
+ unset($object->expandable_foreign_keys);
|
|
|
+ unset($object->expandable_tables);
|
|
|
}
|
|
|
|
|
|
// Deal with the expandable tables/fields/fkeys/nodes. Sometimes there are
|
|
@@ -212,48 +263,44 @@ function tripal_core_chado_ws_api_object_format($object, $schema, $api_url, $que
|
|
|
// the JSON converter will maintain the indexes if present which creates
|
|
|
// an inconsistent look. So, we use the array_values function to just
|
|
|
// get the list.
|
|
|
- $object->expandable_tables = array_values($object->expandable_tables);
|
|
|
- $object->expandable_fields = array_values($object->expandable_fields);
|
|
|
- $object->expandable_foreign_keys = array_values($object->expandable_foreign_keys);
|
|
|
- $object->expandable_nodes = array_values($object->expandable_nodes);
|
|
|
-
|
|
|
-
|
|
|
- // Add the links for the expandable fields, fkeys, nodes and tables
|
|
|
- // TODO: making a single key be an array may not be correct for HAL. check this out.
|
|
|
- if (count($object->expandable_fields) > 0) {
|
|
|
- $object->_links["expand_field"][] = array('href' => "$api_url/$table_name?expand_field={field}[,{field}...]");
|
|
|
- $object->_links["expand_field"][] = array('href' => "$api_url/$table_name/$id?expand_field={field}[,{field}...]");
|
|
|
- }
|
|
|
- else {
|
|
|
- unset($object->expandable_fields);
|
|
|
- }
|
|
|
- if (count($object->expandable_foreign_keys) > 0) {
|
|
|
- $object->_links["expand_fkey"][] = array('href' => "$api_url/$table_name?expand_fkey={fkey}[,{fkey}...]");
|
|
|
- $object->_links["expand_fkey"][] = array('href' => "$api_url/$table_name/$id?expand_fkey={fkey}[,{fkey}...]");
|
|
|
- }
|
|
|
- else {
|
|
|
- unset($object->expandable_foreign_keys);
|
|
|
- }
|
|
|
- if (count($object->expandable_nodes) > 0) {
|
|
|
- $object->_links["expand_node"][] = array('href' => "$api_url/$table_name?expand_node={node}[,{node}...]");
|
|
|
- $object->_links["expand_node"][] = array('href' => "$api_url/$table_name/$id?expand_node={node}[,{node}...]");
|
|
|
- }
|
|
|
- else {
|
|
|
- unset($object->expandable_nodes);
|
|
|
+ if (array_key_exists('expandable_tables', $object)) {
|
|
|
+ $object->expandable_tables = array_values($object->expandable_tables);
|
|
|
+ if (count($object->expandable_tables) > 0) {
|
|
|
+ $object->_links["expand_table"][] = array('href' => "$api_url/$table_name/expand?table={table}[,{table}...]");
|
|
|
+ $object->_links["expand_table"][] = array('href' => "$api_url/$table_name/$id/expand?table={table}[,{table}...]");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ unset($object->expandable_tables);
|
|
|
+ }
|
|
|
}
|
|
|
- if (count($object->expandable_tables) > 0) {
|
|
|
- $object->_links["expand_table"][] = array('href' => "$api_url/$table_name?expand_table={table}[,{table}...]");
|
|
|
- $object->_links["expand_table"][] = array('href' => "$api_url/$table_name/$id?expand_table={table}[,{table}...]");
|
|
|
+ if (array_key_exists('expandable_fields', $object)) {
|
|
|
+ $object->expandable_fields = array_values($object->expandable_fields);
|
|
|
+ if (count($object->expandable_fields) > 0) {
|
|
|
+ $object->_links["expand_field"][] = array('href' => "$api_url/$table_name/expand?field={field}[,{field}...]");
|
|
|
+ $object->_links["expand_field"][] = array('href' => "$api_url/$table_name/$id/expand?field={field}[,{field}...]");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ unset($object->expandable_fields);
|
|
|
+ }
|
|
|
}
|
|
|
- else {
|
|
|
- unset($object->expandable_tables);
|
|
|
+ if (array_key_exists('expandable_foreign_keys', $object)) {
|
|
|
+ $object->expandable_foreign_keys = array_values($object->expandable_foreign_keys);
|
|
|
+ if (count($object->expandable_foreign_keys) > 0) {
|
|
|
+ $object->_links["expand_fkey"][] = array('href' => "$api_url/$table_name/expand?fkey={fkey}[,{fkey}...]");
|
|
|
+ $object->_links["expand_fkey"][] = array('href' => "$api_url/$table_name/$id/expand?fkey={fkey}[,{fkey}...]");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ unset($object->expandable_foreign_keys);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// iterate through the items in the object and see if they in turn are
|
|
|
// objects. If so, then recurse.
|
|
|
foreach ($object as $key => $value) {
|
|
|
+
|
|
|
// If any values are objects then recurse and format them correctly.
|
|
|
if (is_object($value)) {
|
|
|
+
|
|
|
$table_name = $value->tablename;
|
|
|
$schema = chado_get_schema($table_name);
|
|
|
if ($schema) {
|
|
@@ -263,12 +310,13 @@ function tripal_core_chado_ws_api_object_format($object, $schema, $api_url, $que
|
|
|
$object->$key = $value->$key;
|
|
|
}
|
|
|
else {
|
|
|
- unset($object->key);
|
|
|
+ unset($object->$key);
|
|
|
}
|
|
|
// Recursively format the object.
|
|
|
$value = tripal_core_chado_ws_api_object_format($value, $schema, $api_url, $query);
|
|
|
// Add the object as an "_embedded" object of the JSON.
|
|
|
- if (array_key_exists($table_name, $object->_embedded)) {
|
|
|
+ if (property_exists($object,'_embedded') and
|
|
|
+ array_key_exists($table_name, $object->_embedded)) {
|
|
|
// If the element is already an array then add to it, otherwise
|
|
|
// convert it into an array.
|
|
|
if (is_array($object->_embedded[$table_name])) {
|
|
@@ -293,5 +341,9 @@ function tripal_core_chado_ws_api_object_format($object, $schema, $api_url, $que
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ if (array_key_exists('no_links', $query)) {
|
|
|
+ unset($object->_links);
|
|
|
+ }
|
|
|
+
|
|
|
return $object;
|
|
|
}
|