1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135 |
- <?php
- class TripalEntityService_v0_1 extends TripalWebService {
-
- public static $label = 'Content Types';
-
- public static $description = 'Provides acesss to the biological and ' .
- 'ancilliary data available on this site. Each content type represents ' .
- 'biological data that is defined in a controlled vocabulary (e.g. ' .
- 'Sequence Ontology term: gene (SO:0000704)).';
-
- public static $type = 'content';
-
- public function __construct($base_path) {
- parent::__construct($base_path);
-
- $this->addDocBundleClasses();
- $this->addDocContentCollectionClass();
- }
-
- public function handleRequest() {
-
- $ctype = (count($this->path) > 0) ? $this->path[0] : '';
- $entity_id = (count($this->path) > 1) ? $this->path[1] : '';
- $expfield = (count($this->path) > 2) ? $this->path[2] : '';
-
- if ($ctype) {
- $bundle = tripal_load_bundle_entity(array('label' => $ctype));
- if (!$bundle) {
- throw new Exception('Invalid content type: ' . $ctype);
- }
- }
-
-
- if ($ctype and !$entity_id and !$expfield) {
- $this->doContentTypeList($ctype);
- }
-
- else if ($ctype and $entity_id and !$expfield) {
- $this->doEntity($ctype, $entity_id);
- }
- else if ($ctype and $entity_id and $expfield) {
- $this->doExpandedField($ctype, $entity_id, $expfield);
- }
-
- else {
- $this->doAllTypesList();
- }
- }
-
- private function doExpandedField($ctype, $entity_id, $expfield) {
- $service_path = $this->getServicePath() . '/' . urlencode($ctype) . '/' . $entity_id;
- $this->resource = new TripalWebServiceResource($service_path);
-
- $bundle = tripal_load_bundle_entity(array('label' => $ctype));
-
- list($field, $instance, $term) = $this->findField($bundle, $expfield);
- if (!$field) {
- throw new Exception("Could not find a matching field for the name: $expfield");
- }
-
- $entity = tripal_load_entity('TripalEntity', array('id' => $entity_id), FALSE, array($field['id']));
- $entity = reset($entity);
-
- if (!$entity) {
- throw new Exception("Cannot find the record with id $entity_id.");
- }
-
-
- $this->checkAccess($entity);
-
- $this->setResourceType($this->resource, $term);
- $this->resource->setID(urlencode($term['name']));
- if (!property_exists($entity, $field['field_name'])) {
-
- field_attach_load($entity->type, array($entity->id => $entity),
- FIELD_LOAD_CURRENT, array('field_id' => $field['id']));
- }
- $this->addEntityField($term, $entity, $bundle, $field, $instance, $service_path, $expfield);
- }
-
- private function findField($bundle, $expfield) {
- $value = array();
- $instances = field_info_instances('TripalEntity', $bundle->name);
- foreach ($instances as $instance) {
- $field_name = $instance['field_name'];
- $field = field_info_field($field_name);
- $field_type = $field['type'];
-
- if ($field_type == 'remote__data') {
- continue;
- }
- $vocabulary = $instance['settings']['term_vocabulary'];
- $accession = $instance['settings']['term_accession'];
- $temp_term = tripal_get_term_details($vocabulary, $accession);
- if ($temp_term['name'] == $expfield) {
- return array($field, $instance, $temp_term);
- }
- }
- }
-
- private function doEntity($ctype, $entity_id) {
- $service_path = $this->getServicePath() . '/' . urlencode($ctype);
- $this->resource = new TripalWebServiceResource($service_path);
-
- $bundle = tripal_load_bundle_entity(array('label' => $ctype));
- $term = entity_load('TripalTerm', array('id' => $bundle->term_id));
- $term = reset($term);
-
- $term = tripal_get_term_details($term->vocab->vocabulary, $term->accession);
-
- $this->resource->addContextItem($term['name'], $term['url']);
-
- $entity = tripal_load_entity('TripalEntity', array('id' => $entity_id));
- $entity = reset($entity);
-
- if (!$entity) {
- throw new Exception("Cannot find this record.");
- }
-
-
- $this->checkAccess($entity);
- $itemPage = tripal_get_term_details('schema', 'ItemPage');
- $label = tripal_get_term_details('rdfs', 'label');
- $this->resource->setID($entity_id);
- $this->setResourceType($this->resource, $term);
- $this->addResourceProperty($this->resource, $label, $entity->title);
- $this->addResourceProperty($this->resource, $itemPage, url('/bio_data/' . $entity->id, array('absolute' => TRUE)));
-
- $this->addEntityFields($entity, $bundle, $term, $service_path);
- }
-
- private function checkAccess($entity) {
- global $user;
- if (!tripal_entity_access('view', $entity, $user, 'TripalEntity')) {
- throw new Exception("Permission Denied.");
- }
-
- if ($entity->status == 0) {
- throw new Exception("This record is currently unavailable.");
- }
- }
-
- private function addEntityFields($entity, $bundle, $term, $service_path) {
-
-
- $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id, 'hide');
-
-
- $instances = field_info_instances('TripalEntity', $bundle->name);
-
- uasort($instances, function($a, $b) {
- $a_weight = (is_array($a) && isset($a['widget']['weight'])) ? $a['widget']['weight'] : 0;
- $b_weight = (is_array($b) && isset($b['widget']['weight'])) ? $b['widget']['weight'] : 0;
- if ($a_weight == $b_weight) {
- return 0;
- }
- return ($a_weight < $b_weight) ? -1 : 1;
- });
-
-
- foreach ($instances as $field_name => $instance) {
-
- if ($instance['display']['default']['type'] == 'hidden') {
- continue;
- }
-
- $field = field_info_field($field_name);
-
-
- if ($field['type'] == 'remote__data') {
- continue;
- }
-
-
-
- $field_name = $instance['field_name'];
- $vocabulary = $instance['settings']['term_vocabulary'];
- $accession = $instance['settings']['term_accession'];
- $term = tripal_get_term_details($vocabulary, $accession);
- if (!$term) {
- continue;
- }
-
-
- $instance_settings = $instance['settings'];
- if (array_key_exists('auto_attach', $instance['settings']) and
- $instance_settings['auto_attach'] == FALSE) {
-
-
-
- $items = field_get_items('TripalEntity', $entity, $field_name);
- if ($items and count($items) > 0 and $items[0]['value']) {
- $this->addResourceProperty($this->resource, $term, $service_path . '/' . $entity->id . '/' . urlencode($term['name']), array('lowercase', 'spacing'));
- }
- else {
- if ($hide_fields == 'show') {
- $this->addResourceProperty($this->resource, $term, NULL, array('lowercase', 'spacing'));
- }
- }
- continue;
- }
-
- $this->addEntityField($term, $entity, $bundle, $field, $instance, $service_path);
- }
- }
-
- private function addEntityField($term, $entity, $bundle, $field, $instance,
- $service_path, $expfield = NULL) {
-
-
- $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id, 'hide');
-
- $field_name = $field['field_name'];
- $field_settings = $field['settings'];
- $items = field_get_items('TripalEntity', $entity, $field_name);
- if (!$items) {
- return;
- }
-
-
-
-
- drupal_alter('tripal_ws_value', $items, $field, $instance);
- $values = array();
- for ($i = 0; $i < count($items); $i++) {
- $values[$i] = $this->sanitizeFieldKeys($this->resource, $items[$i]['value'], $bundle, $service_path);
- }
- if ($hide_fields == 'hide' and empty($values[0])) {
- return;
- }
-
- if ($field['cardinality'] == 1) {
-
-
- if (is_array($values[0])) {
- if ($expfield) {
- foreach ($values[0] as $k => $v) {
- $this->resource->addProperty($k, $v);
- }
- }
- else {
- $this->addResourceProperty($this->resource, $term, $values[0], array('lowercase', 'spacing'));
- }
- }
-
-
- else {
- $this->addResourceProperty($this->resource, $term, $values[0], array('lowercase', 'spacing'));
- }
- }
-
- if ($field['cardinality'] != 1) {
-
-
- $response = new TripalWebServiceCollection($service_path . '/' . urlencode($expfield), $this->params);
- $label = tripal_get_term_details('rdfs', 'label');
- $this->addResourceProperty($response, $label, $instance['label']);
- $i = 0;
- foreach ($values as $delta => $element) {
- $member = new TripalWebServiceResource($service_path . '/' . urlencode($expfield));
- $member->setID($i);
-
-
- $member->setContext($this->resource);
- $this->setResourceType($member, $term);
- foreach ($element as $key => $value) {
- $member->addProperty($key, $value);
- }
- $response->addMember($member);
- $i++;
- }
- if ($expfield) {
- $this->resource = $response;
- }
- else {
-
- $this->addResourceProperty($this->resource, $term, $response, array('lowercase', 'spacing'));
- }
- }
- }
-
- private function sanitizeFieldKeys($resource, $value, $bundle, $service_path) {
-
-
- $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id, 'hide');
- $new_value = '';
-
-
- if (is_array($value)) {
- $temp = array();
- foreach ($value as $k => $v) {
-
- if (!isset($v) and $hide_fields == 'hide') {
- continue;
- }
- $matches = array();
- if (preg_match('/^(.+):(.+)$/', $k, $matches)) {
- $vocabulary = $matches[1];
- $accession = $matches[2];
- $term = tripal_get_term_details($vocabulary, $accession);
- $key = $this->addContextTerm($resource, $term, array('lowercase', 'spacing'));
- if (is_array($v)) {
- $temp[$key] = $this->sanitizeFieldKeys($resource, $v, $bundle, $service_path);
- }
- else {
- $temp[$key] = $v;
- }
- $term['name'] = $key;
- }
- else {
-
-
- }
- }
- $new_value = $temp;
-
-
- $this->sanitizeFieldEntity($new_value, $service_path);
- }
- else {
- $new_value = $value;
- }
- return $new_value;
- }
-
- private function sanitizeFieldEntity(&$items, $service_path) {
- if (!$items) {
- return;
- }
- foreach ($items as $key => $value) {
- if (is_array($value)) {
- $this->sanitizeFieldEntity($items[$key], $service_path);
- continue;
- }
- if ($key == 'entity') {
- list($item_etype, $item_eid) = explode(':', $items['entity']);
- if ($item_eid) {
- $item_entity = tripal_load_entity($item_etype, array($item_eid));
- $item_entity = reset($item_entity);
- $bundle = tripal_load_bundle_entity(array('name' => $item_entity->bundle));
- $items['@id'] = $this->getServicePath() . '/' . urlencode($bundle->label) . '/' . $item_eid;
- }
- unset($items['entity']);
- }
- }
- }
-
- private function getFieldMapping($bundle) {
-
-
-
-
- $field_mapping = array();
- $fields = field_info_fields();
- foreach ($fields as $field) {
- if (array_key_exists('TripalEntity', $field['bundles'])) {
- foreach ($field['bundles']['TripalEntity'] as $bundle_name) {
- if ($bundle_name == $bundle->name) {
- $instance = field_info_instance('TripalEntity', $field['field_name'], $bundle_name);
- if (array_key_exists('term_accession', $instance['settings'])){
- $vocabulary = $instance['settings']['term_vocabulary'];
- $accession = $instance['settings']['term_accession'];
- $fterm = tripal_get_term_details($vocabulary, $accession);
- $key = $fterm['name'];
- $key = strtolower(preg_replace('/ /', '_', $key));
- $field_mapping[$key] = $field['field_name'];
- $field_mapping[$field['field_name']] = $field['field_name'];
- }
- }
- }
- }
- }
- return $field_mapping;
- }
-
- private function getOrderBy($field_mapping, $bundle) {
- $order_by = array();
-
- if (array_key_exists('order', $this->params)) {
- $order_params = $this->params['order'];
- $dir = 'ASC';
-
-
- $items = explode(';', $order_params);
- foreach ($items as $key) {
-
-
- $matches = array();
- if (preg_match('/^(.*)\|(.*)$/', $key, $matches)) {
- $key = $matches[1];
- if ($matches[2] == 'ASC' or $matches[2] == 'DESC') {
- $dir = $matches[2];
- }
- else {
- throw new Exception('Please provide "ASC" or "DESC" for the ordering direction');
- }
- }
-
-
- $subkeys = explode(',', $key);
- if (count($subkeys) > 0) {
- $key = $subkeys[0];
- }
- if (array_key_exists($key, $field_mapping)) {
- $key_field_name = $field_mapping[$key];
- $key_field = field_info_field($key_field_name);
- $key_instance = field_info_instance('TripalEntity', $key_field_name, $bundle->name);
-
-
-
- $field_class = $key_field['type'];
- if (tripal_load_include_field_class($field_class)) {
-
-
- $key_field = new $field_class($key_field, $key_instance);
- $ws_data = $key_field->webServicesData();
- $sortable_keys = $ws_data['sortable'];
- $criteria = implode('.', $subkeys);
- if (array_key_exists($criteria, $sortable_keys)) {
- $order_by[$key_field_name][] = array(
- 'column' => $sortable_keys[$criteria],
- 'dir' => $dir,
- );
- }
- else {
- throw new Exception("The value, '$criteria', is not available for sorting.");
- }
- }
-
-
- else {
- $key_field_id = $key_instance['settings']['term_vocabulary'] . ':' . $key_instance['settings']['term_accession'];
- $order_by[$key_field_name][] = array(
- 'column' => $key_field_id,
- 'dir' => $dir,
- );
- }
- }
- else {
- throw new Exception("The value, '$key', is not available for sorting.");
- }
- }
- }
-
- if (count(array_keys($order_by)) == 0) {
- $key_field_names = array();
- if (in_array('data__identifier', $field_mapping)) {
- $key_field_names['data__identifier'][] = 'identifier';
- }
- else if (in_array('schema__name', $field_mapping)) {
- $key_field_names['schema__name'][] = 'name';
- }
- else if (in_array('rdfs_label', $field_mapping)) {
- $key_field_names['rdfs_label'][] = 'label';
- }
- else if (in_array('taxrank__genus', $field_mapping)) {
- $key_field_names['taxrank__genus'][] = 'genus';
- $key_field_names['taxrank__species'][] = 'species';
- }
- foreach ($key_field_names as $key_field_name => $criteria) {
- $key_field = field_info_field($key_field_name);
- $key_instance = field_info_instance('TripalEntity', $key_field_name, $bundle->name);
- $key_field_id = $key_instance['settings']['term_vocabulary'] . ':' . $key_instance['settings']['term_accession'];
- $field_class = $key_field['type'];
- if (tripal_load_include_field_class($field_class)) {
-
-
- $key_field = new $field_class($key_field, $key_instance);
- $ws_data = $key_field->webServicesData();
- $sortable_keys = $ws_data['sortable'];
- if (array_key_exists($criteria, $sortable_keys)) {
- $order_by[$key_field_name][] = array(
- 'column' => $sortable_keys[$criteria],
- 'dir' => $dir,
- );
- }
- }
-
-
- else {
- $order_by[$key_field_name][] = array(
- 'column' => $key_field_id,
- 'dir' => 'ASC',
- );
- }
- }
- }
- return $order_by;
- }
-
- private function getFilters($field_mapping, $bundle) {
- $filters = array();
-
- foreach ($this->params as $param => $value) {
-
- if ($param == 'page' or $param == 'limit') {
- continue;
- }
-
-
- if ($param == 'order') {
- continue;
- }
-
- $key = $param;
- $op = '=';
- $matches = array();
- if (preg_match('/^(.+);(.+)$/', $key, $matches)) {
- $key = $matches[1];
- $op = $matches[2];
- }
-
-
- $subkeys = explode(',', $key);
- if (count($subkeys) > 0) {
- $key = $subkeys[0];
- }
-
- if (array_key_exists($key, $field_mapping)) {
- $key_field_name = $field_mapping[$key];
- $key_field = field_info_field($key_field_name);
- $key_instance = field_info_instance('TripalEntity', $key_field_name, $bundle->name);
-
-
-
- $field_class = $key_field['type'];
- if (tripal_load_include_field_class($field_class)) {
-
-
- $key_field = new $field_class($key_field, $key_instance);
- $ws_data = $key_field->webServicesData();
- $searchable_keys = $ws_data['searchable'];
- $criteria = implode('.', $subkeys);
- if (array_key_exists($criteria, $searchable_keys)) {
- $filters[$key_field_name][] = array(
- 'value' => $value,
- 'op' => $op,
- 'column' => $searchable_keys[$criteria]
- );
- }
- else {
- throw new Exception("The filter term, '$criteria', is not available for use.");
- }
- }
-
-
- else {
- $key_field_id = $key_instance['settings']['term_vocabulary'] . ':' . $key_instance['settings']['term_accession'];
- $filters[$key_field_name][] = array(
- 'value' => $value,
- 'op' => $op,
- 'column' => $key_field_id,
- );
- }
- }
- else {
- throw new Exception("The filter term, '$key', is not available for use.");
- }
- }
-
-
- foreach ($filters as $key_field_name => $key_filters) {
- foreach ($key_filters as $i => $filter) {
- $op = '=';
- switch ($filters[$key_field_name][$i]['op']) {
- case 'eq':
- $op = '=';
- break;
- case 'gt':
- $op = '>';
- break;
- case 'gte':
- $op = '>=';
- break;
- case 'lt':
- $op = '<';
- break;
- case 'lte':
- $op = '<=';
- break;
- case 'ne':
- $op = '<>';
- break;
- case 'contains':
- $op = 'CONTAINS';
- break;
- case 'starts':
- $op = 'STARTS WITH';
- break;
- default:
- $op = '=';
- }
- $filters[$key_field_name][$i]['op'] = $op;
- }
- }
- return $filters;
- }
-
- private function doContentTypeList($ctype) {
- $service_path = $this->getServicePath() . '/' . urlencode($ctype);
- $this->resource = new TripalWebServiceCollection($service_path, $this->params);
-
- $bundle = tripal_load_bundle_entity(array('label' => $ctype));
- $term = entity_load('TripalTerm', array('id' => $bundle->term_id));
- $term = reset($term);
-
- $vocab_service = new TripalVocabService_v0_1($this->base_path);
- $this->resource->addContextItem('vocab', $vocab_service->getServicePath() . '#');
- $this->resource->setType('vocab:' . urlencode($bundle->label) . 'Collection');
-
- $term = tripal_get_term_details($term->vocab->vocabulary, $term->accession);
-
- $label = tripal_get_term_details('rdfs', 'label');
- $this->addResourceProperty($this->resource, $label, $bundle->label . " collection");
-
- $field_mapping = $this->getFieldMapping($bundle);
-
- $filters = $this->getFilters($field_mapping, $bundle);
- $order_by = $this->getOrderBy($field_mapping, $bundle);
-
-
- $query = new TripalFieldQuery();
- $query->entityCondition('entity_type', 'TripalEntity');
- $query->entityCondition('bundle', $bundle->name);
- $query->propertyCondition('status', 1);
-
- foreach ($filters as $key_field_name => $key_filters) {
- foreach ($key_filters as $i => $filter) {
- $column_name = $filter['column'];
- $value = $filter['value'];
- $op = $filter['op'];
- $query->fieldCondition($key_field_name, $column_name, $value, $op);
- }
- }
-
- foreach ($order_by as $key_field_name => $key_order) {
- foreach ($key_order as $i => $order) {
- $column_name = $order['column'];
- $dir = $order['dir'];
- $query->fieldOrderBy($key_field_name, $column_name, $dir);
- }
- }
-
- $cquery = clone $query;
- $cquery->count();
- $num_records = $cquery->execute();
- if (!$num_records) {
- $num_records = 0;
- }
-
- $response['totalItems'] = $num_records;
- $limit = array_key_exists('limit', $this->params) ? $this->params['limit'] : 25;
- $total_pages = ceil($num_records / $limit);
- $page = array_key_exists('page', $this->params) ? $this->params['page'] : 1;
-
- $start = ($page - 1) * $limit;
- $query->range($start, $limit);
-
- $results = $query->execute();
- $this->resource->initPager($num_records, $limit, $page);
-
- $entity_ids = array();
- if (isset($results['TripalEntity']) AND is_array($results['TripalEntity'])) {
- $entity_ids = $results['TripalEntity'];
- }
-
- foreach ($entity_ids as $entity_id => $stub) {
-
-
-
- $query = db_select('tripal_entity', 'TE');
- $query->join('tripal_term', 'TT', 'TE.term_id = TT.id');
- $query->fields('TE');
- $query->fields('TT', array('name'));
- $query->condition('TE.id', $entity_id);
- $entity = $query->execute()->fetchObject();
- $itemPage = tripal_get_term_details('schema', 'ItemPage');
- $label = tripal_get_term_details('rdfs', 'label');
- $member = new TripalWebServiceResource($service_path);
- $member->setID($entity->id);
- $this->setResourceType($member, $term);
- $this->addResourceProperty($member, $label, $entity->title);
- $this->addResourceProperty($member, $itemPage, url('/bio_data/' . $entity->id, array('absolute' => TRUE)));
- $this->resource->addMember($member);
- }
- }
-
- private function doAllTypesList() {
- $service_path = $this->getServicePath();
- $this->resource = new TripalWebServiceCollection($service_path, $this->params);
- $this->resource->addContextItem('vocab', 'http://localhost/web-services/vocab/v0.1/');
- $this->resource->addContextItem('ContentCollection', 'http://localhost/web-services/vocab/v0.1#ContentCollection');
- $this->resource->setType('ContentCollection');
- $label = tripal_get_term_details('rdfs', 'label');
- $this->addResourceProperty($this->resource, $label, 'Content Types');
-
- $bundles = db_select('tripal_bundle', 'tb')
- ->fields('tb')
- ->orderBy('tb.label', 'ASC')
- ->execute();
-
- $i = 0;
- while ($bundle = $bundles->fetchObject()) {
- $entity = entity_load('TripalTerm', array('id' => $bundle->term_id));
- $term = reset($entity);
- $vocab = $term->vocab;
-
- $term = tripal_get_term_details($term->vocab->vocabulary, $term->accession);
- $member = new TripalWebServiceResource($service_path);
- $member->setID(urlencode($bundle->label));
-
- $url = $term['url'];
- if (!$url) {
- throw new Exception(t('Missing a URL for the term: @term.', array('@term' => $term['vocabulary']['short_name'] . ':' . $term['accession'])));
- }
- $this->setResourceType($member, $term);
- $this->addResourceProperty($member, $label, $bundle->label);
- $member->addContextItem('description', 'rdfs:comment');
-
-
- $description = tripal_get_bundle_variable('description', $bundle->id);
- if (!$description) {
- $description = $term->definition;
- }
- if (!$description) {
- $description = '';
- }
- $member->addProperty('description', $description);
- $this->resource->addMember($member);
- }
- }
-
- private function addDocContentCollectionClass() {
- $details = array(
- 'id' => 'vocab:ContentCollection',
- 'title' => 'Content Collection',
- );
- $vocab = tripal_get_vocabulary_details('hydra');
- $url = preg_replace('/{accession}/', 'member', $vocab['urlprefix']);
- $propeties = array();
- $propeties[] = array(
- 'type' => $url,
- 'title' => 'member',
- 'description' => "The list of available content types.",
- "required" => null,
- "readonly" => FALSE,
- "writeonly" => FALSE,
- );
- $url = preg_replace('/{accession}/', 'totalItems', $vocab['urlprefix']);
- $propeties[] = array(
- "type" => $url,
- "title" => "totalItems",
- "description" => "The total number of content types.",
- "required" => null,
- "readonly" => FALSE,
- "writeonly" => FALSE
- );
- $url = preg_replace('/{accession}/', 'label', $vocab['urlprefix']);
- $propeties[] = array(
- "type" => $url,
- "title" => "label",
- "description" => "The type content.",
- "required" => null,
- "readonly" => FALSE,
- "writeonly" => FALSE
- );
- $operations = array();
- $operations['GET'] = array(
- 'label' => 'Retrieves a collection (a list) of available content types.',
- 'expects' => NULL,
- 'returns' => $this->getServicePath(),
- 'type' => '_:content_collection_retrieve'
- );
- $this->addDocClass($details,$operations, $propeties);
- }
-
- private function addDocBundleClasses() {
- global $user;
-
- $bundles = db_select('tripal_bundle', 'tb')
- ->fields('tb')
- ->orderBy('tb.label', 'ASC')
- ->execute();
-
- $i = 0;
- while ($bundle = $bundles->fetchObject()) {
- $entity = entity_load('TripalTerm', array('id' => $bundle->term_id));
- $term = reset($entity);
- $vocab = $term->vocab;
-
-
- $description = tripal_get_bundle_variable('description', $bundle->id);
- if (!$description) {
- $description = $term->definition;
- }
-
- $class_id = $this->getServicePath() . '/' . urlencode($bundle->label);
- $details = array(
- 'id' => $term->url,
- 'title' => $bundle->label,
- 'description' => $description,
- );
-
- $operations = array();
-
- if (user_access('view ' . $bundle->name)) {
- $label = "Retrieves a " . $bundle->label . " entity.";
- if (preg_match('/^[aeiou]/i', $bundle->label)) {
- $label = "Retrieves an " . $bundle->label . " entity.";
- }
- $operations['GET'] = array(
- 'label' => $label,
- 'description' => NULL,
- 'returns' => $class_id,
- 'type' => '_:' . preg_replace('/[^\w]/', '_', strtolower($bundle->label)) . '_retrieve',
- );
- }
-
- if (user_access('edit ' . $bundle->name)) {
- $label = "Update and replace a " . $bundle->label . " entity.";
- if (preg_match('/^[aeiou]/i', $bundle->label)) {
- $label = "Update and replace an " . $bundle->label . " entity.";
- }
- $operations['PUT'] = array(
- 'label' => $label,
- 'description' => NULL,
- 'returns' => $class_id,
- 'type' => '_:' . preg_replace('/[^\w]/', '_', strtolower($bundle->label)) . '_update',
- );
- }
-
- if (user_access('delete ' . $bundle->name)) {
- $label = "Deletes a " . $bundle->label . " entity.";
- if (preg_match('/^[aeiou]/i', $bundle->label)) {
- $label = "Deletes an " . $bundle->label . " entity.";
- }
- $operations['DELETE'] = array(
- 'label' => $label,
- 'description' => NULL,
- 'returns' => $class_id,
- 'type' => '_:' . preg_replace('/[^\w]/', '_', strtolower($bundle->label)) . '_delete',
- );
- }
- $properties = array();
- $this->addDocClass($details, $operations, $properties);
-
- $this->addDocBundleCollectionClass($bundle);
- }
- }
-
- private function addDocBundleCollectionClass($bundle) {
- $details = array(
- 'id' => 'vocab:' . urlencode($bundle->label) . 'Collection',
- 'title' => $bundle->label . ' Collection',
- 'subClassOf' => 'hydra:Collection',
- 'description' => 'A collection (or list) of ' . $bundle->label . ' resources.',
- );
- $vocab = tripal_get_vocabulary_details('hydra');
- $url = preg_replace('/{accession}/', 'member', $vocab['urlprefix']);
- $propeties = array();
- $propeties[] = array(
- 'type' => $url,
- 'title' => 'member',
- 'description' => "The list of available " . $bundle->label . '(s).',
- "required" => null,
- "readonly" => FALSE,
- "writeonly" => FALSE,
- );
- $url = preg_replace('/{accession}/', 'totalItems', $vocab['urlprefix']);
- $propeties[] = array(
- "type" => $url,
- "title" => "totalItems",
- "description" => "The total number of resources.",
- "required" => null,
- "readonly" => FALSE,
- "writeonly" => FALSE
- );
- $url = preg_replace('/{accession}/', 'label', $vocab['urlprefix']);
- $propeties[] = array(
- "type" => $url,
- "title" => "label",
- "description" => "A label or name for the resource.",
- "required" => null,
- "readonly" => FALSE,
- "writeonly" => FALSE
- );
- $class_id = $this->getServicePath() . '/' . urlencode($bundle->label);
- $operations = array();
- $operations['GET'] = array(
- 'label' => 'Retrieves a list of all ' . $bundle->label . ' resources.',
- 'description' => NULL,
- 'expects' => NULL,
- 'returns' => $class_id,
- 'type' => '_:' . preg_replace('/[^\w]/', '_', strtolower($bundle->label)) . '_retrieve',
- );
-
-
- if (user_access('create ' . $bundle->name)) {
- $label = "Creates a " . $bundle->label;
- if (preg_match('/^[aeiou]/i', $bundle->label)) {
- $label = "Creates an " . $bundle->label;
- }
- $operations['POST'] = array(
- 'label' => $label,
- 'description' => NULL,
- 'expects' => $class_id,
- 'returns' => $class_id,
- 'type' => '_:' . preg_replace('/[^\w]/', '_', strtolower($bundle->label)) . '_create',
- 'statusCodes' => array(
- array(
- "code" => 201,
- "description" => "If the " . $bundle->label . " was created successfully."
- ),
- ),
- );
- }
- $this->addDocClass($details, $operations, $propeties);
- }
- }
|