123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <?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() {
- parent::__construct();
- }
-
- public function handleRequest() {
-
- $ctype = (count($this->path) > 0) ? $this->path[0] : '';
- $entity_id = (count($this->path) > 1) ? $this->path[1] : '';
-
- if ($ctype and !$entity_id) {
- $this->doContentTypeList($ctype);
- }
-
-
- else if ($ctype and !$entity_id) {
- }
-
- else {
- $this->doAllTypesList();
- }
- }
-
- private function doContentTypeList($ctype) {
- $this->resource = new TripalWebServiceCollection();
- $this->resource->addContextItem('label', 'rdfs:label');
-
- $bundle = tripal_load_bundle_entity(array('label' => $ctype));
- $term = entity_load('TripalTerm', array('id' => $bundle->term_id));
- $term = reset($term);
-
- $this->resource->addProperty('label', $bundle->label . " collection");
-
-
-
-
- $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'];
- }
- }
- }
- }
- }
-
- $new_params = array();
- $order = array();
- $order_dir = array();
- $URL_add = array();
- foreach ($this->params as $param => $value) {
- $URL_add[] = "$param=$value";
-
- if ($param == 'page' or $param == 'limit') {
- continue;
- }
-
- if ($param == 'order') {
- $temp = explode(',', $value);
- foreach ($temp as $key) {
- $matches = array();
- $dir = 'ASC';
-
-
- if (preg_match('/^(.*)\|(.*)$/', $key, $matches)) {
- $key = $matches[1];
- if ($matches[2] == 'ASC' or $matches[2] == 'DESC') {
- $dir = $matches[2];
- }
- else {
-
- }
- }
- if (array_key_exists($key, $field_mapping)) {
- $order[$field_mapping[$key]] = $key;
- $order_dir[] = $dir;
- }
- else {
-
- }
- }
- 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 = array_shift($subkeys);
- }
- $column_name = $key;
-
- if (array_key_exists($key, $field_mapping)) {
- $field_name = $field_mapping[$key];
- if (count($subkeys) > 0) {
- $column_name .= '.' . implode('.', $subkeys);
- }
- $new_params[$field_name]['value'] = $value;
- $new_params[$field_name]['op'] = $op;
- $new_params[$field_name]['column'] = $column_name;
- }
- else {
- throw new Exception("The filter term, '$key', is not available for use.");
- }
- }
-
- $query = new TripalFieldQuery();
- $query->entityCondition('entity_type', 'TripalEntity');
- $query->entityCondition('bundle', $bundle->name);
- foreach($new_params as $field_name => $details) {
- $value = $details['value'];
- $column_name = $details['column'];
- switch ($details['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 = '=';
- }
-
-
- $query->fieldCondition($field_name, $column_name, $value, $op);
- }
-
- $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;
-
- $order_keys = array_keys($order);
- for($i = 0; $i < count($order_keys); $i++) {
- $query->fieldOrderBy($order_keys[$i], $order[$order_keys[$i]], $order_dir[$i]);
- }
-
- $start = ($page - 1) * $limit;
- $query->range($start, $limit);
-
- $results = $query->execute();
-
- $this->resource->initPager($num_records, 25);
-
- foreach ($results['TripalEntity'] 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();
- $member = new TripalWebServiceResource();
- $member->addContextItem('label', 'rdfs:label');
- $member->addContextItem('itemPage', 'schema:itemPage');
- $member->addContextItem($term->name, $term->url);
- $member->setID(urldecode($bundle->label) . '/' . $entity->id);
- $member->setType($term->name);
- $member->addProperty('label', $entity->title);
- $member->addProperty('itemPage', url('/bio_data/' . $entity->id, array('absolute' => TRUE)));
- $this->resource->addMember($member);
- }
- }
-
- private function doAllTypesList() {
- $this->resource = new TripalWebServiceCollection();
- $this->resource->addContextItem('label', 'rdfs:label');
- $this->resource->addProperty('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;
-
-
- $description = tripal_get_bundle_variable('description', $bundle->id);
- if (!$description) {
- $description = $term->definition;
- }
- $member = new TripalWebServiceResource();
- $member->addContextItem($term->name, $term->url);
- $member->addContextItem('label', 'rdfs:label');
- $member->addContextItem('description', 'hydra:description');
- $member->setID(urlencode($bundle->label));
- $member->setType($term->name);
- $member->addProperty('label', $bundle->label);
- $member->addProperty('description', $description);
- $this->resource->addMember($member);
- }
- }
- }
|