function tripal_core_chado_ws_api_object_format
2.x tripal_core.ws_hal.inc | tripal_core_chado_ws_api_object_format($table_name, $object, $schema, $api_url, $query) |
Parameters
$object:
$schema:
$api_url:
1 call to tripal_core_chado_ws_api_object_format()
- tripal_core_chado_hal_api in tripal_core/
includes/ tripal_core.ws_hal.inc
File
- tripal_core/
includes/ tripal_core.ws_hal.inc, line 242
Code
function tripal_core_chado_ws_api_object_format($table_name, $object, $schema, $api_url, $query) {
global $base_url;
$pkey = $schema['primary key'][0];
$id = $object->$pkey;
// Add the self link first
if ($id) {
$object->_links['self'] = array('href' => "$api_url/$table_name/$id");
$object->_links['show_expansion'] = array('href' => "$api_url/$table_name/$id?show_expansion=1");
}
else {
$object->_links['self'] = array('href' => "$api_url/$table_name");
$object->_links['show_expansion'] = array('href' => "$api_url/$table_name?show_expansion=1");
}
// Add the links for the table.
$object->_links["tables"] = array('href' => "$api_url");
$object->_links["schema"] = array('href' => "$api_url/$table_name/schema");
// Add links for editing, insert, delete but only if user has permission.
// TODO: how do we set permissions?
$object->_links["add"] = array('href' => "$api_url/$table_name/add");
// Add the links if an id is available.
if ($id) {
$object->_links["edit"] = array('href' => "$api_url/$table_name/$id/edit");
$object->_links["delete"] = array('href' => "$api_url/$table_name/$id/delete");
}
// 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('show_expansion', $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
// specified in PHP with explicit numerical indexes and sometimes not. But,
// 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.
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 (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);
}
}
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) {
// Replace the object with the actual value if it exists. If there is
// no key value then this is probably an expanded table so just unset
if (property_exists($value, $key)) {
$object->$key = $value->$key;
}
else {
unset($object->$key);
}
// Recursively format the object.
$value = tripal_core_chado_ws_api_object_format($table_name, $value, $schema, $api_url, $query);
// Add the object as an "_embedded" object of the JSON.
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])) {
$object->_embedded[$table_name][] = $value;
}
else {
$first = $object->_embedded[$table_name];
$object->_embedded[$table_name] = array();
$object->_embedded[$table_name] = $first;
$object->_embedded[$table_name][] = $value;
}
}
// This is the first time this embedded table has been seen
// there fore, add the value as a single element.
else {
$object->_embedded[$table_name] = $value;
}
}
else {
throw new Exception("Table, '$table_name', is not a valid table.");
}
}
}
if (array_key_exists('no_links', $query)) {
unset($object->_links);
}
return $object;
}