TripalEntityService_v0_1.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. <?php
  2. class TripalEntityService_v0_1 extends TripalWebService {
  3. /**
  4. * The human-readable label for this web service.
  5. */
  6. public static $label = 'Content Types';
  7. /**
  8. * A bit of text to describe what this service provides.
  9. */
  10. public static $description = 'Provides acesss to the biological and ' .
  11. 'ancilliary data available on this site. Each content type represents ' .
  12. 'biological data that is defined in a controlled vocabulary (e.g. ' .
  13. 'Sequence Ontology term: gene (SO:0000704)).';
  14. /**
  15. * A machine-readable type for this service. This name must be unique
  16. * among all Tripal web services and is used to form the URL to access
  17. * this service.
  18. */
  19. public static $type = 'content';
  20. /**
  21. * Implements the constructor
  22. */
  23. public function __construct($base_path) {
  24. parent::__construct($base_path);
  25. }
  26. /**
  27. * @see TripalWebService::handleRequest()
  28. */
  29. public function handleRequest() {
  30. // Get the content type.
  31. $ctype = (count($this->path) > 0) ? $this->path[0] : '';
  32. $entity_id = (count($this->path) > 1) ? $this->path[1] : '';
  33. $expfield = (count($this->path) > 2) ? $this->path[2] : '';
  34. // If we have a content type then list all of the entities that belong
  35. // to it.
  36. if ($ctype and !$entity_id and !$expfield) {
  37. $this->doContentTypeList($ctype);
  38. }
  39. // If we have an entity ID then build the resource for a single entity.
  40. else if ($ctype and $entity_id and !$expfield) {
  41. $this->doEntity($ctype, $entity_id);
  42. }
  43. else if ($ctype and $entity_id and $expfield) {
  44. $this->doExpandedField($ctype, $entity_id, $expfield);
  45. }
  46. // Otherwise just list all of the available content types.
  47. else {
  48. $this->doAllTypesList();
  49. }
  50. }
  51. /**
  52. * Creates a resource for an expanded field of an entity.
  53. */
  54. private function doExpandedField($ctype, $entity_id, $expfield) {
  55. $service_path = $this->getServicePath() . '/' . urlencode($ctype) . '/' . $entity_id;
  56. $this->resource = new TripalWebServiceResource($service_path);
  57. // Get the TripalBundle, TripalTerm and TripalVocab for this type.
  58. $bundle = tripal_load_bundle_entity(array('label' => $ctype));
  59. $term = entity_load('TripalTerm', array('id' => $bundle->term_id));
  60. $term = reset($term);
  61. $vocab = $term->vocab;
  62. // Get the TripalEntity
  63. $entity = tripal_load_entity('TripalEntity', array('id' => $entity_id));
  64. $entity = reset($entity);
  65. // If we couldn't match this field argument to a field and entity then return
  66. if (!$entity) {
  67. throw new Exception("Cannot find this entity.");
  68. }
  69. list($field, $instance, $term) = $this->findField($bundle, $expfield);
  70. // Next add in the ID and Type for this resources.
  71. $key = $term['name'];
  72. $key_adj = strtolower(preg_replace('/ /', '_', $term['name']));
  73. $this->resource->addContextItem($key_adj, $term['url']);
  74. $this->resource->setID(urlencode($key));
  75. $this->resource->setType($key_adj);
  76. // Attach the field and then add it's values to the response.
  77. field_attach_load($entity->type, array($entity->id => $entity),
  78. FIELD_LOAD_CURRENT, array('field_id' => $field['id']));
  79. $this->addEntityField($key_adj, $term, $entity, $bundle, $field, $instance, $service_path, $expfield);
  80. }
  81. /**
  82. * Find the field whose term matches the one provied.
  83. */
  84. private function findField($bundle, $expfield) {
  85. $value = array();
  86. $instances = field_info_instances('TripalEntity', $bundle->name);
  87. foreach ($instances as $instance) {
  88. $field_name = $instance['field_name'];
  89. $field = field_info_field($field_name);
  90. $vocabulary = $instance['settings']['term_vocabulary'];
  91. $accession = $instance['settings']['term_accession'];
  92. $temp_term = tripal_get_term_details($vocabulary, $accession);
  93. if ($temp_term['name'] == $expfield) {
  94. return array($field, $instance, $temp_term);
  95. }
  96. }
  97. }
  98. /**
  99. * Creates a resource for a single entity.
  100. */
  101. private function doEntity($ctype, $entity_id) {
  102. $service_path = $this->getServicePath() . '/' . urlencode($ctype);
  103. $this->resource = new TripalWebServiceResource($service_path);
  104. // Get the TripalBundle, TripalTerm and TripalVocab type for this type.
  105. $bundle = tripal_load_bundle_entity(array('label' => $ctype));
  106. $term = entity_load('TripalTerm', array('id' => $bundle->term_id));
  107. $term = reset($term);
  108. $vocab = $term->vocab;
  109. // Add the vocabulary to the context.
  110. $this->resource->addContextItem($term->name, $term->url);
  111. // Get the TripalEntity
  112. $entity = tripal_load_entity('TripalEntity', array('id' => $entity_id));
  113. $entity = reset($entity);
  114. $itemPage = tripal_get_term_details('schema', 'ItemPage');
  115. $label = tripal_get_term_details('rdfs', 'label');
  116. $this->resource->setID($entity_id);
  117. $this->resource->setType($term->name);
  118. $this->resource->addContextItem('label', $label['url']);
  119. $this->resource->addContextItem('ItemPage', $itemPage['url']);
  120. $this->resource->addProperty('label', $entity->title);
  121. $this->resource->addProperty('ItemPage', url('/bio_data/' . $entity->id, array('absolute' => TRUE)));
  122. $this->addEntityFields($entity, $bundle, $term, $service_path);
  123. // tripal_ws_services_v0_1_get_content_add_fields($entity, $bundle, $api_url, $response, $ws_path, $ctype, $entity_id, $params);
  124. // tripal_ws_services_v0_1_write_context($response, $ctype);
  125. }
  126. /**
  127. * Adds the fields as properties of an entity resource.
  128. */
  129. private function addEntityFields($entity, $bundle, $term, $service_path) {
  130. // If the entity is set to hide fields that have no values then we
  131. // want to honor that in the web services too.
  132. $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id, 'hide');
  133. // Get information about the fields attached to this bundle and sort them
  134. // in the order they were set for the display.
  135. $instances = field_info_instances('TripalEntity', $bundle->name);
  136. uasort($instances, function($a, $b) {
  137. $a_weight = (is_array($a) && isset($a['widget']['weight'])) ? $a['widget']['weight'] : 0;
  138. $b_weight = (is_array($b) && isset($b['widget']['weight'])) ? $b['widget']['weight'] : 0;
  139. if ($a_weight == $b_weight) {
  140. return 0;
  141. }
  142. return ($a_weight < $b_weight) ? -1 : 1;
  143. });
  144. // Iterate through the fields and add each value to the response.
  145. //$response['fields'] = $fields;
  146. foreach ($instances as $field_name => $instance) {
  147. // Skip hidden fields.
  148. if ($instance['display']['default']['type'] == 'hidden') {
  149. continue;
  150. }
  151. // Get the information about this field.
  152. $field = field_info_field($field_name);
  153. // By default, the label for the key in the output should be the
  154. // term from the vocabulary that the field is assigned. But in the
  155. // case that the field is not assigned a term, we must use the field name.
  156. $field_name = $instance['field_name'];
  157. $vocabulary = $instance['settings']['term_vocabulary'];
  158. $accession = $instance['settings']['term_accession'];
  159. $term = tripal_get_term_details($vocabulary, $accession);
  160. if (!$term) {
  161. continue;
  162. }
  163. $key = $term['name'];
  164. $key_adj = strtolower(preg_replace('/ /', '_', $key));
  165. // If this field should not be attached by default then just add a link
  166. // so that the caller can get the information separately.
  167. $instance_settings = $instance['settings'];
  168. if (array_key_exists('auto_attach', $instance['settings']) and
  169. $instance_settings['auto_attach'] == FALSE) {
  170. // Add a URL only if there are values. If there are no values then
  171. // don't add a URL which would make the end-user think they can get
  172. // that information.
  173. $items = field_get_items('TripalEntity', $entity, $field_name);
  174. if ($items and count($items) > 0 and $items[0]['value']) {
  175. $this->resource->addContextItem($key_adj, $term['url']);
  176. $this->resource->addProperty($key_adj, $service_path . '/' . $entity->id . '/' . urlencode($term['name']));
  177. }
  178. else {
  179. if ($hide_fields == 'show') {
  180. $this->resource->addContextItem($key_adj, $term['url']);
  181. $this->resource->addProperty($key_adj, NULL);
  182. }
  183. }
  184. continue;
  185. }
  186. // Get the details for this field for the JSON-LD response.
  187. $this->addEntityField($key_adj, $term, $entity, $bundle, $field, $instance, $service_path);
  188. }
  189. }
  190. /**
  191. * Adds the field as a property of the entity resource.
  192. */
  193. private function addEntityField($key, $term, $entity, $bundle, $field, $instance,
  194. $service_path, $expfield = NULL) {
  195. // If the entity is set to hide fields that have no values then we
  196. // want to honor that in the web services too.
  197. $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id, 'hide');
  198. // Get the field settings.
  199. $field_name = $field['field_name'];
  200. $field_settings = $field['settings'];
  201. $items = field_get_items('TripalEntity', $entity, $field_name);
  202. if (!$items) {
  203. return;
  204. }
  205. // Give modules the opportunity to edit values for web services. This hook
  206. // really should be used sparingly. Where it helps is with non Tripal fields
  207. // that are added to a TripalEntity content type and it doesn't follow
  208. // the rules (e.g. Image field).
  209. drupal_alter('tripal_ws_value', $items, $field, $instance);
  210. $values = array();
  211. for ($i = 0; $i < count($items); $i++) {
  212. $values[$i] = $this->sanitizeFieldKeys($items[$i]['value'], $bundle, $service_path);
  213. }
  214. if ($hide_fields == 'hide' and empty($values[0])) {
  215. return;
  216. }
  217. // If the field cardinality is 1
  218. if ($field['cardinality'] == 1) {
  219. // If the value is an array and this is the field page then all of those
  220. // key/value pairs should be added directly to the response.
  221. if (is_array($values[0])) {
  222. if ($expfield) {
  223. foreach ($values[0] as $k => $v) {
  224. $this->resource->addProperty($k, $v);
  225. }
  226. }
  227. else {
  228. $this->resource->addContextItem($key, $term['url']);
  229. $this->resource->addProperty($key, $values[0]);
  230. }
  231. }
  232. // If the value is not an array it's a scalar so add it as is to the
  233. // response.
  234. else {
  235. $this->resource->addContextItem($key, $term['url']);
  236. $this->resource->addProperty($key, $values[0]);
  237. }
  238. }
  239. // If the field cardinality is > 1
  240. if ($field['cardinality'] != 1) {
  241. // If this is the expanded field page then we need to swap out
  242. // the resource for a collection.
  243. $response = new TripalWebServiceCollection($service_path . '/' . urlencode($expfield));
  244. $label = tripal_get_term_details('rdfs', 'label');
  245. $response->addContextItem('label', $label['url']);
  246. $response->addProperty('label', $instance['label']);
  247. $i = 0;
  248. foreach ($values as $delta => $element) {
  249. $member = new TripalWebServiceResource($service_path . '/' . urlencode($expfield));
  250. $member->setID($i);
  251. // Add the context of the parent resource because all of the keys
  252. // were santizied and set to match the proper context.
  253. $member->setContext($this->resource);
  254. $member->setType($key);
  255. foreach ($element as $key => $value) {
  256. $member->addProperty($key, $value);
  257. }
  258. $response->addMember($member);
  259. $i++;
  260. }
  261. if ($expfield) {
  262. $this->resource = $response;
  263. }
  264. else {
  265. $this->resource->addProperty($key, $response);
  266. }
  267. }
  268. }
  269. /**
  270. * Rewrites the keys of a field's items array for use with web services.
  271. */
  272. private function sanitizeFieldKeys($value, $bundle, $service_path) {
  273. // If the entity is set to hide fields that have no values then we
  274. // want to honor that in the web services too.
  275. $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id, 'hide');
  276. $new_value = '';
  277. // If the value is an array rather than a scalar then map the sub elements
  278. // to controlled vocabulary terms.
  279. if (is_array($value)) {
  280. $temp = array();
  281. foreach ($value as $k => $v) {
  282. // exclude fields that have no values so we can hide them
  283. if (!isset($v) and $hide_fields == 'hide') {
  284. continue;
  285. }
  286. $matches = array();
  287. if (preg_match('/^(.+):(.+)$/', $k, $matches)) {
  288. $vocabulary = $matches[1];
  289. $accession = $matches[2];
  290. $term = tripal_get_term_details($vocabulary, $accession);
  291. $key_adj = strtolower(preg_replace('/ /', '_', $term['name']));
  292. if (is_array($v)) {
  293. $temp[$key_adj] = $this->sanitizeFieldKeys($v, $bundle, $service_path);
  294. }
  295. else {
  296. $temp[$key_adj] = $v;
  297. }
  298. $this->resource->addContextItem($key_adj, $term['url']);
  299. }
  300. else {
  301. // TODO: this is an error, if we get here then we have
  302. // a key that isn't using the proper format... what to do?
  303. }
  304. }
  305. $new_value = $temp;
  306. // Recurse through the values array and set the entity elements
  307. // and add the fields to the context.
  308. $this->sanitizeFieldEntity($new_value, $service_path);
  309. }
  310. else {
  311. $new_value = $value;
  312. }
  313. return $new_value;
  314. }
  315. /**
  316. * Rewrites any TripalEntity elements in the values array for use with WS.
  317. */
  318. private function sanitizeFieldEntity(&$items, $service_path) {
  319. if (!$items) {
  320. return;
  321. }
  322. foreach ($items as $key => $value) {
  323. if (is_array($value)) {
  324. $this->sanitizeFieldEntity($items[$key], $service_path);
  325. continue;
  326. }
  327. if ($key == 'entity') {
  328. list($item_etype, $item_eid) = explode(':', $items['entity']);
  329. if ($item_eid) {
  330. $item_entity = tripal_load_entity($item_etype, array($item_eid));
  331. $item_entity = reset($item_entity);
  332. $bundle = tripal_load_bundle_entity(array('name' => $item_entity->bundle));
  333. $items['@id'] = $this->getServicePath() . '/' . urlencode($bundle->label) . '/' . $item_eid;
  334. }
  335. unset($items['entity']);
  336. }
  337. }
  338. }
  339. /**
  340. * Creates a collection of resources for a given type.
  341. */
  342. private function doContentTypeList($ctype) {
  343. $service_path = $this->getServicePath() . '/' . urlencode($ctype);
  344. $label = tripal_get_term_details('rdfs', 'label');
  345. $this->resource = new TripalWebServiceCollection($service_path);
  346. $this->resource->addContextItem('label', $label['url']);
  347. // Get the TripalBundle, TripalTerm and TripalVocab type for this type.
  348. $bundle = tripal_load_bundle_entity(array('label' => $ctype));
  349. $term = entity_load('TripalTerm', array('id' => $bundle->term_id));
  350. $term = reset($term);
  351. // Set the label for this collection.
  352. $this->resource->addProperty('label', $bundle->label . " collection");
  353. // Iterate through the fields and create a $field_mapping array that makes
  354. // it easier to determine which filter criteria belongs to which field. The
  355. // key is the label for the field and the value is the field name. This way
  356. // user's can use the field label or the field name to form a query.
  357. $field_mapping = array();
  358. $fields = field_info_fields();
  359. foreach ($fields as $field) {
  360. if (array_key_exists('TripalEntity', $field['bundles'])) {
  361. foreach ($field['bundles']['TripalEntity'] as $bundle_name) {
  362. if ($bundle_name == $bundle->name) {
  363. $instance = field_info_instance('TripalEntity', $field['field_name'], $bundle_name);
  364. if (array_key_exists('term_accession', $instance['settings'])){
  365. $vocabulary = $instance['settings']['term_vocabulary'];
  366. $accession = $instance['settings']['term_accession'];
  367. $fterm = tripal_get_term_details($vocabulary, $accession);
  368. $key = $fterm['name'];
  369. $key = strtolower(preg_replace('/ /', '_', $key));
  370. $field_mapping[$key] = $field['field_name'];
  371. $field_mapping[$field['field_name']] = $field['field_name'];
  372. }
  373. }
  374. }
  375. }
  376. }
  377. // Convert the filters to their field names
  378. $new_params = array();
  379. $order = array();
  380. $order_dir = array();
  381. $URL_add = array();
  382. foreach ($this->params as $param => $value) {
  383. $URL_add[] = "$param=$value";
  384. // Ignore non filter parameters
  385. if ($param == 'page' or $param == 'limit') {
  386. continue;
  387. }
  388. // Handle order separately
  389. if ($param == 'order') {
  390. $temp = explode(',', $value);
  391. foreach ($temp as $key) {
  392. $matches = array();
  393. $dir = 'ASC';
  394. // The user can provide a direction by separating the field key and the
  395. // direction with a '|' character.
  396. if (preg_match('/^(.*)\|(.*)$/', $key, $matches)) {
  397. $key = $matches[1];
  398. if ($matches[2] == 'ASC' or $matches[2] == 'DESC') {
  399. $dir = $matches[2];
  400. }
  401. else {
  402. // TODO: handle error of providing an incorrect direction.
  403. }
  404. }
  405. if (array_key_exists($key, $field_mapping)) {
  406. $order[$field_mapping[$key]] = $key;
  407. $order_dir[] = $dir;
  408. }
  409. else {
  410. // TODO: handle error of providing a non existing field name.
  411. }
  412. }
  413. continue;
  414. }
  415. // Break apart any operators
  416. $key = $param;
  417. $op = '=';
  418. $matches = array();
  419. if (preg_match('/^(.+);(.+)$/', $key, $matches)) {
  420. $key = $matches[1];
  421. $op = $matches[2];
  422. }
  423. // Break apart any subkeys and pull the first one as this is the parent
  424. // field.
  425. $subkeys = explode(',', $key);
  426. if (count($subkeys) > 0) {
  427. $key = $subkeys[0];
  428. }
  429. // Map the values in the filters to their appropriate field names.
  430. if (array_key_exists($key, $field_mapping)) {
  431. $key_field_name = $field_mapping[$key];
  432. $key_field = field_info_field($key_field_name);
  433. $key_instance = field_info_instance('TripalEntity', $key_field_name, $bundle->name);
  434. // Complex fields provied by the TripalField class may have sub
  435. // elements that support filtering. We need to see if the user
  436. // wants to filter on those.
  437. if (tripal_load_include_field_class($key_field_name)) {
  438. // To find out which fields are searchable we'll call the wsData()
  439. // function.
  440. $key_field = new $key_field_name($key_field, $key_instance);
  441. $searchable_keys = $key_field->webServicesData();
  442. $criteria = implode('.', $subkeys);
  443. if (array_key_exists($criteria, $searchable_keys)) {
  444. $new_params[$key_field_name]['value'] = $value;
  445. $new_params[$key_field_name]['op'] = $op;
  446. $new_params[$key_field_name]['column'] = $searchable_keys[$criteria];
  447. }
  448. else {
  449. throw new Exception("The filter term, '$criteria', is not available for use.");
  450. }
  451. }
  452. // If this field is not a TripalField then it should just have
  453. // a simple value and we can query for that.
  454. else {
  455. $key_field_id = $key_instance['settings']['term_vocabulary'] . ':' . $key_instance['settings']['term_accession'];
  456. $new_params[$key_field_name]['value'] = $value;
  457. $new_params[$key_field_name]['op'] = $op;
  458. $new_params[$key_field_name]['column'] = $key_field_id;
  459. }
  460. }
  461. else {
  462. throw new Exception("The filter term, '$key', is not available for use.");
  463. }
  464. }
  465. // Get the list of entities for this bundle.
  466. $query = new TripalFieldQuery();
  467. $query->entityCondition('entity_type', 'TripalEntity');
  468. $query->entityCondition('bundle', $bundle->name);
  469. foreach($new_params as $field_name => $details) {
  470. $value = $details['value'];
  471. $column_name = $details['column'];
  472. switch ($details['op']) {
  473. case 'eq':
  474. $op = '=';
  475. break;
  476. case 'gt':
  477. $op = '>';
  478. break;
  479. case 'gte':
  480. $op = '>=';
  481. break;
  482. case 'lt':
  483. $op = '<';
  484. break;
  485. case 'lte':
  486. $op = '<=';
  487. break;
  488. case 'ne':
  489. $op = '<>';
  490. break;
  491. case 'contains':
  492. $op = 'CONTAINS';
  493. break;
  494. case 'starts':
  495. $op = 'STARTS WITH';
  496. break;
  497. default:
  498. $op = '=';
  499. }
  500. // We pass in the $column_name as an identifier for any sub fields
  501. // that are present for the fields.
  502. $query->fieldCondition($field_name, $column_name, $value, $op);
  503. }
  504. // Perform the query just as a count first to get the number of records.
  505. $cquery = clone $query;
  506. $cquery->count();
  507. $num_records = $cquery->execute();
  508. if (!$num_records) {
  509. $num_records = 0;
  510. }
  511. // Add in the pager to the response.
  512. $response['totalItems'] = $num_records;
  513. $limit = array_key_exists('limit', $this->params) ? $this->params['limit'] : 25;
  514. $total_pages = ceil($num_records / $limit);
  515. $page = array_key_exists('page', $this->params) ? $this->params['page'] : 1;
  516. // Set the query order
  517. $order_keys = array_keys($order);
  518. for($i = 0; $i < count($order_keys); $i++) {
  519. $query->fieldOrderBy($order_keys[$i], $order[$order_keys[$i]], $order_dir[$i]);
  520. }
  521. // Set the query range
  522. $start = ($page - 1) * $limit;
  523. $query->range($start, $limit);
  524. // Now perform the query.
  525. $results = $query->execute();
  526. $this->resource->initPager($num_records, $limit, $page);
  527. // Iterate through the entities and add them to the list.
  528. foreach ($results['TripalEntity'] as $entity_id => $stub) {
  529. // We don't need all of the attached fields for an entity so, we'll
  530. // not use the entity_load() function. Instead just pull it from the
  531. // database table.
  532. $query = db_select('tripal_entity', 'TE');
  533. $query->join('tripal_term', 'TT', 'TE.term_id = TT.id');
  534. $query->fields('TE');
  535. $query->fields('TT', array('name'));
  536. $query->condition('TE.id', $entity_id);
  537. $entity = $query->execute()->fetchObject();
  538. $itemPage = tripal_get_term_details('schema', 'ItemPage');
  539. $label = tripal_get_term_details('rdfs', 'label');
  540. $member = new TripalWebServiceResource($service_path);
  541. $member->addContextItem('label', $label['url']);
  542. $member->addContextItem('ItemPage', $itemPage['url']);
  543. $member->addContextItem($term->name, $term->url);
  544. $member->setID($entity->id);
  545. $member->setType($term->name);
  546. $member->addProperty('label', $entity->title);
  547. $member->addProperty('ItemPage', url('/bio_data/' . $entity->id, array('absolute' => TRUE)));
  548. $this->resource->addMember($member);
  549. }
  550. }
  551. /**
  552. * Creates a resources that contains the list of content types.
  553. */
  554. private function doAllTypesList() {
  555. $service_path = $this->getServicePath();
  556. $label = tripal_get_term_details('rdfs', 'label');
  557. $this->resource = new TripalWebServiceCollection($service_path);
  558. $this->resource->addContextItem('label', $label['url']);
  559. $this->resource->addProperty('label', 'Content Types');
  560. // Get the list of published terms (these are the bundle IDs)
  561. $bundles = db_select('tripal_bundle', 'tb')
  562. ->fields('tb')
  563. ->orderBy('tb.label', 'ASC')
  564. ->execute();
  565. // Iterate through the terms and add an entry in the collection.
  566. $i = 0;
  567. while ($bundle = $bundles->fetchObject()) {
  568. $entity = entity_load('TripalTerm', array('id' => $bundle->term_id));
  569. $term = reset($entity);
  570. $vocab = $term->vocab;
  571. // Get the bundle description. If no description is provided then
  572. // use the term definition
  573. $description = tripal_get_bundle_variable('description', $bundle->id);
  574. if (!$description) {
  575. $description = $term->definition;
  576. }
  577. $member = new TripalWebServiceResource($service_path);
  578. $member->addContextItem($term->name, $term->url);
  579. $member->addContextItem('label', $label['url']);
  580. $member->addContextItem('description', 'hydra:description');
  581. $member->setID(urlencode($bundle->label));
  582. $member->setType($term->name);
  583. $member->addProperty('label', $bundle->label);
  584. $member->addProperty('description', $description);
  585. $this->resource->addMember($member);
  586. }
  587. }
  588. }