function tripal_core_chado_hal_api
2.x tripal_core.ws_hal.inc | tripal_core_chado_hal_api() |
File
- tripal_core/
includes/ tripal_core.ws_hal.inc, line 4
Code
function tripal_core_chado_hal_api() {
global $base_url;
// Set some initial variables.
$response = array();
$result = array();
$status = 'success';
$version = 'v0.1';
$message = '';
$api_url = "$base_url/ws/chado/$version";
$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 = '';
// If we don't have a table name then return the list of all tables.
if (!arg(3)) {
$tables = chado_get_table_names(FALSE);
foreach ($tables as $table) {
$result['_links']['chado_tables'][] = array('href' => "$api_url/$table");
}
// $tables = chado_get_custom_table_names(FALSE);
// foreach ($tables as $table) {
// $result['_links']['custom_tables'][] = array('href' => "$api_url/$table");
// }
// $tables = chado_get_mview_table_names();
// foreach ($tables as $table) {
// $result['_links']['mviews'][] = array('href' => "$api_url/$table");
// }
}
else {
// 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);
if (!chado_table_exists($table_name)) {
throw new Exception("Table, '$table_name', is not a valid table.");
}
$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);
}
// Get any URL query arguments
$query = drupal_get_query_parameters();
switch ($action) {
case 'schema':
$result = $schema;
break;
case 'add':
case 'edit':
case 'delete':
throw new Exception("Action, '$action', is currently not supported.");
break;
default:
// Specify the values for selecing records.
$values = array();
if ($id) {
$values[$pkey] = $id;
}
// 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);
// 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");
}
// 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) {
$item = tripal_core_chado_ws_api_object_format($table_name, $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($table_name, $item, $schema, $api_url, $query);
$result = $item;
}
}
}
}
catch (Exception $e) {
watchdog('tripal_ws', $e->getMessage(), array(), WATCHDOG_ERROR);
$message = $e->getMessage();
$status = 'error';
$result = array();
}
// The responses follow the same format as the AGAVE API with a
// status, message, version and all data in the "result" object.
$response['status'] = $status;
$response['message'] = $message;
$response['version'] = $version;
$response['source'] = array(
'site_name' => variable_get('site_name', "Unspecified"),
'site_url' => $base_url,
'site_slogan' => variable_get('site_slogan', "Unspecified"),
'site_email' => variable_get('site_mail', "Unspecified"),
);
$response['result'] = $result;
print drupal_json_output($response);
}