12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352 |
- <?php
- class TripalContentService_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);
- }
-
- public function getDocumentation() {
-
- $this->addDocBundleClasses();
- $this->addDocContentCollectionClass();
- return parent::getDocumentation();
- }
-
- 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) {
-
- $bquery = db_select('tripal_bundle', 'tb');
- $bquery->join('tripal_term', 'tt', 'tt.id = tb.term_id');
- $bquery->join('tripal_vocab', 'tv', 'tv.id = tt.vocab_id');
- $bquery->fields('tb', array('label'));
- $bquery->fields('tt', array('accession'));
- $bquery->fields('tv', array('vocabulary'));
- $bquery->orderBy('tb.label', 'ASC');
- $bundles = $bquery->execute();
-
- $i = 0;
- $ctype_lookup = array();
- $found = FALSE;
- while ($bundle = $bundles->fetchObject()) {
-
-
- $label = preg_replace('/[^\w]/', '_', $bundle->label);
- if (preg_match("/^$label$/i", $ctype)) {
- $ctype = $bundle->label;
- $found = TRUE;
- }
-
- if ($ctype == $bundle->vocabulary . ':' . $bundle->accession) {
- $ctype = $bundle->label;
- $found = TRUE;
- }
- }
- if (!$found) {
- throw new Exception('Invalid content type: ' . $ctype);
- }
- }
-
-
- if ($ctype and !$entity_id and !$expfield) {
- $this->doEntityList($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->doContentTypesList();
- }
- }
-
- 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($this->resource, $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 (strtolower(preg_replace('/[^\w]/', '_', $temp_term['name'])) == strtolower($expfield)) {
- return array($field, $instance, $temp_term);
- }
-
- if ($vocabulary . ':' . $accession == $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($this->resource, $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($resource, $entity, $bundle, $term, $service_path) {
-
-
- $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id);
- $ajax_field = tripal_get_bundle_variable('ajax_field', $bundle->id);
-
-
- if ($ajax_field == TRUE) {
- $hide_fields = FALSE;
- }
-
-
- $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 (tripal_load_include_field_class($field['type'])) {
- $field_class = $field['type'];
- if ($field_class::$no_data) {
- return;
- }
- }
-
-
- 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);
- $term_key = $this->getContextTerm($term, array('lowercase', 'spacing'));
- $resource->addContextItem($term_key, $vocabulary . ':' . $accession);
- $resource->addContextItem($vocabulary . ':' . $accession, array(
- '@id' => $term['url'],
- '@type' => '@id'
- ));
- if ($items and count($items) > 0 and $items[0]['value']) {
- $this->addResourceProperty($resource, $term, $service_path . '/' . $entity->id . '/' . urlencode($term['name']), array('lowercase', 'spacing'));
- }
- else {
- if ($hide_fields == FALSE) {
- $this->addResourceProperty($resource, $term, NULL, array('lowercase', 'spacing'));
- }
- }
- continue;
- }
-
- $this->addEntityField($resource, $term, $entity, $bundle, $field, $instance, $service_path);
- }
- }
-
- private function addEntityField($resource, $term, $entity, $bundle, $field, $instance,
- $service_path, $expfield = NULL) {
-
-
- $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id);
- $ajax_field = tripal_get_bundle_variable('ajax_field', $bundle->id);
-
-
- if ($ajax_field == TRUE) {
- $hide_fields = FALSE;
- }
-
- $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++) {
- if (array_key_exists('value', $items[$i])) {
- $values[$i] = $this->sanitizeFieldKeys($resource, $items[$i]['value'], $bundle, $service_path);
- }
- elseif ($field['type'] == 'image') {
- $url = file_create_url($items[$i]['uri']);
- $values[$i] = $this->sanitizeFieldKeys($resource, $url, $bundle, $service_path);
- }
- else {
-
- }
- }
- if ($hide_fields == TRUE and empty($values[0])) {
- return;
- }
-
- if ($field['cardinality'] == 1) {
-
-
- if (is_array($values[0])) {
- if ($expfield) {
- foreach ($values[0] as $k => $v) {
- $resource->addProperty($k, $v);
- }
- }
- else {
- $this->addResourceProperty($resource, $term, $values[0], array('lowercase', 'spacing'));
- }
- }
-
-
- else {
- $this->addResourceProperty($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($resource);
- $this->setResourceType($member, $term);
- foreach ($element as $key => $value) {
- $member->addProperty($key, $value);
- }
- $response->addMember($member);
- $i++;
- }
- if ($expfield) {
- $resource = $response;
- }
- else {
-
- $this->addResourceProperty($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);
- $ajax_field = tripal_get_bundle_variable('ajax_field', $bundle->id);
-
-
- if ($ajax_field == TRUE) {
- $hide_fields = FALSE;
- }
- $new_value = '';
-
-
- if (is_array($value)) {
- $temp = array();
- foreach ($value as $k => $v) {
-
- if (!isset($v) and $hide_fields == TRUE) {
- 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 getFieldFilters($field_mapping, $bundle) {
- $filters = array();
-
- foreach ($this->params as $param => $value) {
-
- if ($param == 'page' or $param == 'limit' or $param == 'order' or
- $param == 'ids' or $param == 'fields') {
- 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 doEntityList($ctype) {
- $service_path = $this->getServicePath() . '/' . preg_replace('/[^\w]/', '_', $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 TripalDocService_v0_1($this->base_path);
- $this->resource->addContextItem('vocab', $vocab_service->getServicePath() . '#');
- $accession = preg_replace('/[^\w]/', '_', $bundle->label . ' Collection');
- $this->resource->addContextItem($accession, 'vocab:' . $accession);
- $this->resource->setType($accession);
-
- $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->getFieldFilters($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);
- if (array_key_exists('ids', $this->params)) {
- $eids = explode(',', $this->params['ids']);
- if (count($eids) > 1000) {
- throw new Exception('Please provide no more than 1000 ids.');
- }
- if (!is_numeric(implode('', $eids))) {
- throw new Exception('All supplied ids must be numeric.');
- }
- $query->entityCondition('entity_id', $eids, 'IN');
- }
-
- 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'];
- }
-
-
- $add_fields = array();
- $add_field_ids = array();
- if (array_key_exists('fields', $this->params)) {
- $fields = explode(',', $this->params['fields']);
- foreach ($fields as $expfield) {
- list($field, $instance, $temp_term) = $this->findField($bundle, $expfield);
- if ($field) {
- $add_fields[$expfield]['field'] = $field;
- $add_fields[$expfield]['instance'] = $instance;
- $add_fields[$expfield]['term'] = $temp_term;
- $add_field_ids[] = $field['id'];
- }
- else {
- throw new Exception(t('The field named, "!field", does not exist.', array('!field' => $expfield)));
- }
- }
- }
-
- 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)));
- $entity = tripal_load_entity('TripalEntity', array($entity_id), FALSE, $add_field_ids);
- $entity = $entity[$entity_id];
-
- foreach ($add_fields as $expfield => $expfield_details) {
- $this->addEntityField($member, $expfield_details['term'], $entity,
- $bundle, $expfield_details['field'], $expfield_details['instance'],
- $service_path);
- }
- $this->resource->addMember($member);
- }
- }
-
- private function doContentTypesList() {
- $service_path = $this->getServicePath();
- $service_vocab = new TripalDocService_v0_1($this->base_path);
- $this->resource = new TripalWebServiceCollection($service_path, $this->params);
- $this->resource->addContextItem('vocab', $service_vocab->getServicePath());
- $this->resource->addContextItem('Content_Collection', $service_vocab->getServicePath() . '#Content_Collection');
- $this->resource->setType('Content_Collection');
- $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(preg_replace('/[^\w]/', '_', $bundle->label));
- $vocab_service = new TripalDocService_v0_1($this->base_path);
- $member->addContextItem('vocab', $vocab_service->getServicePath() . '#');
- $accession = preg_replace('/[^\w]/', '_', $bundle->label . ' Collection');
- $member->addContextItem($accession, 'vocab:' . $accession);
- $member->setType($accession);
- $this->addResourceProperty($member, $label, $bundle->label . ' Collection');
- $member->addContextItem('description', 'rdfs:comment');
-
-
- $description = trim(tripal_get_bundle_variable('description', $bundle->id));
- if (!$description) {
- $description = $term['definition'];
- }
- if (!$description) {
- $description = '';
- }
- $member->addProperty('description', 'A collection of ' . $bundle->label . ' resources: ' . lcfirst($description));
- $this->resource->addMember($member);
- }
- }
-
- private function addDocContentCollectionClass() {
- $details = array(
- 'id' => 'vocab:Content_Collection',
- 'term' => 'vocab:Content_Collection',
- 'title' => 'Content Collection',
- );
- $vocab = tripal_get_vocabulary_details('hydra');
- $properties = array();
- $properties[] = array(
- 'type' => $vocab['sw_url'],
- 'title' => 'member',
- 'description' => "The list of available content types.",
- "required" => null,
- "readonly" => FALSE,
- "writeonly" => FALSE,
- );
- $properties[] = array(
- "type" => $vocab['sw_url'],
- "title" => "totalItems",
- "description" => "The total number of content types.",
- "required" => null,
- "readonly" => FALSE,
- "writeonly" => FALSE
- );
- $properties[] = array(
- "type" => $vocab['sw_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.',
- 'type' => '_:content_collection_retrieve',
- 'expects' => NULL,
- 'returns' => 'vocab:ContentCollection',
- );
- $this->addDocClass($details,$operations, $properties);
- }
-
- 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->getDefinition();
- }
-
- $class_id = $this->getServicePath() . '/' . urlencode($bundle->label);
- $details = array(
- 'id' => $term->getURL(),
- 'term' => $term->getAccession(),
- 'title' => preg_replace('/[^\w]/', '_', $bundle->label),
- 'description' => $description,
- );
-
- $operations = array();
-
- if (user_access('view ' . $bundle->name)) {
- $label = "Retrieves the " . $bundle->label . " resource.";
- $operations['GET'] = array(
- 'label' => $label,
- 'description' => NULL,
- 'returns' => $term->url,
- 'type' => '_:' . preg_replace('/[^\w]/', '_', strtolower($bundle->label)) . '_retrieve',
- );
- }
-
- if (user_access('edit ' . $bundle->name)) {
- $label = "Update and replace the " . $bundle->label . " resource.";
- if (preg_match('/^[aeiou]/i', $bundle->label)) {
- $label = "Update and replace an " . $bundle->label . " resource.";
- }
- }
-
- if (user_access('delete ' . $bundle->name)) {
- $label = "Deletes the " . $bundle->label . " resource.";
- if (preg_match('/^[aeiou]/i', $bundle->label)) {
- $label = "Deletes an " . $bundle->label . " resource.";
- }
- }
-
- $properties = $this->addDocBundleFieldProperties($bundle, $term);
- $this->addDocClass($details, $operations, $properties);
-
- $this->addDocBundleCollectionClass($bundle, $term);
- }
- }
-
- private function addDocBundleFieldProperties($bundle, $bundle_term) {
- $properties = array();
- $content_type_accession = $bundle_term->vocab->vocabulary . ':' . $bundle_term->accession;
- $instances = field_info_instances('TripalEntity', $bundle->name);
- foreach ($instances as $instance) {
-
- if ($instance['deleted']) {
- continue;
- }
-
- if ($instance['display']['default']['type'] == 'hidden') {
- continue;
- }
- $accession = $instance['settings']['term_vocabulary'] . ":" . $instance['settings']['term_accession'];
- $field_name = $instance['field_name'];
- $field = field_info_field($field_name);
- $field_type = $field['type'];
-
- if ($field_type == 'remote__data') {
- continue;
- }
-
-
- $proptype = $instance['settings']['term_vocabulary'] . ':' . $instance['settings']['term_accession'];
- if ($instance['settings']['auto_attach'] == FALSE) {
-
- $id = $content_type_accession . '/' . $accession;
- $link = new TripalWebServiceResource($this->base_path);
- $link->setID($accession);
- $link->setType('hydra:Link');
- $link->addContextItem('domain', array(
- "@id" => "rdfs:domain",
- "@type" => "@id"
- ));
- $link->addContextItem('range', array(
- "@id" => "rdfs:range",
- "@type" => "@id"
- ));
- $link->addContextItem('readable', 'hydra:readable');
- $link->addContextItem('writeable', 'hydra:writeable');
- $link->addContextItem('required', 'hydra:required');
- $link->addContextItem('description', 'rdfs:comment');
- $link->addContextItem('label', 'rdfs:label');
- $link->addProperty('hydra:title', $instance['label']);
- $link->addProperty('hydra:description', $instance['description']);
-
-
- $ops = array();
- $op = new TripalWebServiceResource($this->base_path);
- $op->setID('_:' . $field_name . '_retrieve');
- $op->setType('hydra:Operation');
- $op->addContextItem('method', 'hydra:method');
- $op->addContextItem('label', 'rdfs:label');
- $op->addContextItem('description', 'rdfs:comment');
- $op->addContextItem('expects', array(
- "@id" => "hydra:expects",
- "@type" => "@id"
- ));
- $op->addContextItem('returns', array(
- "@id" => "hydra:returns",
- "@type" => "@id"
- ));
- $op->addContextItem('statusCodes', 'hydra:statusCodes');
- $op->addProperty('method', "GET");
- $op->addProperty('label', 'Retrieves the ' . $instance['label'] . ' resource.');
- $op->addProperty('description', $instance['description']);
- $op->addProperty('expects', NULL);
- $op->addProperty('returns', $accession);
- $op->addProperty('statusCodes', array());
- $ops[] = $op;
- $link->addContextItem('supportedOperation', 'hydra:supportedOperation');
- $link->addProperty('supportedOperation', $ops);
- $proptype = $link;
- }
- $formatters = tripal_get_field_field_formatters($field, $instance);
- $property = array(
- 'type' => $proptype,
- 'title' => $instance['label'],
- 'description' => $instance['description'],
- "required" => $instance['required'] ? TRUE : FALSE,
- "readonly" => FALSE,
- "writeonly" => TRUE,
- "tripal_formatters" => $formatters,
- );
- $properties[] = $property;
- }
- return $properties;
- }
-
- private function addDocBundleCollectionClass($bundle, $term) {
- $accession = preg_replace('/[^\w]/', '_', $bundle->label . ' Collection');
- $details = array(
- 'id' => 'vocab:' . $accession,
- 'term' => 'vocab:' . $accession,
- 'title' => $bundle->label . ' Collection',
- 'subClassOf' => 'hydra:Collection',
- 'description' => 'A collection (or list) of ' . $bundle->label . ' resources.',
- );
- $vocab = tripal_get_vocabulary_details('hydra');
- $properties = array();
- $properties[] = array(
- 'type' => $vocab['sw_url'],
- 'title' => 'member',
- 'description' => "The list of available " . $bundle->label . '(s).',
- "required" => null,
- "readonly" => FALSE,
- "writeonly" => FALSE,
- );
- $properties[] = array(
- "type" => $vocab['sw_url'],
- "title" => "totalItems",
- "description" => "The total number of resources.",
- "required" => null,
- "readonly" => FALSE,
- "writeonly" => FALSE
- );
- $properties[] = array(
- "type" => $vocab['sw_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' => $term->url,
- 'type' => '_:' . preg_replace('/[^\w]/', '_', strtolower($bundle->label)) . '_collection_retrieve',
- );
-
-
- if (user_access('create ' . $bundle->name)) {
- $label = "Creates a " . $bundle->label;
- if (preg_match('/^[aeiou]/i', $bundle->label)) {
- $label = "Creates an " . $bundle->label;
- }
- }
- $this->addDocClass($details, $operations, $properties);
- }
- }
|