tripal_ws.api.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * This file provides the Tripal Web Services API: a set of functions for
  6. * interacting with the Tripal Web Services.
  7. */
  8. /**
  9. * @defgroup tripal_ws_api Web Services
  10. *
  11. * @ingroup tripal_api
  12. * The Tripal Web Services API provides a set of functions for interacting
  13. * with the Tripal Web Services.
  14. *
  15. */
  16. /**
  17. * Adjust the values of a field for display in web services.
  18. *
  19. * This hook should be used sparingly. It is meant primarily to adjust 3rd
  20. * Party (non Tripal) fields so that they work with web
  21. * services. The caller should adjust the $items array as needed.
  22. * This change only affects the value displayed in web services. Web services
  23. * expect that every field have a 'value' element for each of the items. If a
  24. * field for some reason does not have a 'value' element then this hook will
  25. * allow setting of that element.
  26. *
  27. * @param $items
  28. * The list of items for the field.
  29. * @param $field
  30. * The field array.
  31. * @param $instance
  32. * The field instance array.
  33. *
  34. * @ingroup tripal_ws_api
  35. */
  36. function hook_tripal_ws_value(&$items, $field, $instance) {
  37. // The image module doesn't properly set the 'value' field, so we'll do it
  38. // here.
  39. if($field['type'] == 'image' and $field['module'] == 'image') {
  40. foreach ($items as $delta => $details) {
  41. if ($items[$delta] and array_key_exists('uri', $items[$delta])) {
  42. $items[$delta]['value']['schema:url'] = file_create_url($items[$delta]['uri']);
  43. }
  44. }
  45. }
  46. }
  47. /**
  48. * Retrieves a list of TripalWebService implementations.
  49. *
  50. * The TripalWebService classes can be added by a site developer that wishes
  51. * to create a new Tripal compatible web serivce. The class file should
  52. * be placed in the [module]/includes/TripalWebService directory. Tripal will
  53. * support any service as long as it is in this directory and extends the
  54. * TripalWebService class.
  55. *
  56. * @return
  57. * A list of TripalWebService names.
  58. */
  59. function tripal_get_web_services() {
  60. $services = array();
  61. $modules = module_list(TRUE);
  62. foreach ($modules as $module) {
  63. // Find all of the files in the tripal_chado/includes/fields directory.
  64. $service_path = drupal_get_path('module', $module) . '/includes/TripalWebService';
  65. $service_files = file_scan_directory($service_path, '/.inc$/');
  66. // Iterate through the fields, include the file and run the info function.
  67. foreach ($service_files as $file) {
  68. $class = $file->name;
  69. module_load_include('inc', $module, 'includes/TripalWebService/' . $class);
  70. if (class_exists($class) and is_subclass_of($class, 'TripalWebService')) {
  71. $services[] = $class;
  72. }
  73. }
  74. }
  75. return $services;
  76. }
  77. /**
  78. * Loads the TripalWebService class file into scope.
  79. *
  80. * @param $class
  81. * The TripalWebService class to include.
  82. *
  83. * @return
  84. * TRUE if the field type class file was found, FALSE otherwise.
  85. */
  86. function tripal_load_include_web_service_class($class) {
  87. $modules = module_list(TRUE);
  88. foreach ($modules as $module) {
  89. $file_path = realpath(".") . '/' . drupal_get_path('module', $module) . '/includes/TripalWebService/' . $class . '.inc';
  90. if (file_exists($file_path)) {
  91. module_load_include('inc', $module, 'includes/TripalWebService/' . $class);
  92. if (class_exists($class)) {
  93. return TRUE;
  94. }
  95. }
  96. }
  97. return FALSE;
  98. }
  99. /**
  100. * Adds a new site to the web services table.
  101. *
  102. * @param $name
  103. * Name of site to be included.
  104. * @param $url
  105. * URL of site to be added.
  106. * @param $version
  107. * Version of the API being used. default to 1
  108. * @param $description
  109. * A description of the site and any additional info that
  110. * would be helpful for admins.
  111. *
  112. * @return
  113. * TRUE if the site is successfully added, FALSE otherwise.
  114. */
  115. function tripal_add_site($name, $url, $version, $description) {
  116. $check_url = NULL;
  117. $check_name = NULL;
  118. $write_to_db = TRUE;
  119. // When inserting a record.
  120. $check_url =
  121. db_select('tripal_sites', 'ts')
  122. ->fields('ts', array('id'))
  123. ->condition('url', $url)
  124. ->condition('version', $version)
  125. ->execute()
  126. ->fetchField();
  127. $check_name =
  128. db_select('tripal_sites', 'ts')
  129. ->fields('ts', array('id'))
  130. ->condition('name', $name)
  131. ->execute()
  132. ->fetchField();
  133. if ($check_url) {
  134. drupal_set_message(t('The URL and version is used by another site.'), 'error');
  135. $write_to_db = FALSE;
  136. }
  137. if ($check_name) {
  138. drupal_set_message(t('The name is used by another site.'), 'error');
  139. $write_to_db = FALSE;
  140. }
  141. if ($write_to_db === TRUE) {
  142. db_insert('tripal_sites')
  143. ->fields(array(
  144. 'name' => $name,
  145. 'url' => $url,
  146. 'version' => $version,
  147. 'description' => $description
  148. ))
  149. ->execute();
  150. drupal_set_message(t('Tripal site \'' . $name . '\' has been added.'));
  151. return $write_to_db;
  152. }
  153. return $write_to_db;
  154. }
  155. /**
  156. * Remove a site from the web services table.
  157. *
  158. * @param $record_id
  159. * ID of the record to be deleted.
  160. *
  161. * @return
  162. * TRUE if the record was successfully deleted, FALSE otherwise.
  163. */
  164. function tripal_remove_site($record_id) {
  165. if ($record_id) {
  166. db_delete('tripal_sites')
  167. ->condition('id', $record_id)
  168. ->execute();
  169. drupal_set_message('The Tripal site \'' . $record_id . '\' has been removed.');
  170. return TRUE;
  171. }
  172. return FALSE;
  173. }
  174. /**
  175. * Makes a request to the "content"service of a remote Tripal web site.
  176. *
  177. * @param $site_id
  178. * The numeric site ID for the remote Tripal site.
  179. *
  180. * @param $query
  181. * The query string. This string is added to the URL for the remote
  182. * website.
  183. *
  184. * @return
  185. * The JSON response formatted in a PHP array or FALSE if a problem occured.
  186. */
  187. function tripal_query_remote_site($site_id, $query) {
  188. $ctype = $query;
  189. $qdata = '';
  190. if (preg_match('/\?/', $query)) {
  191. list($ctype, $qdata) = explode('?', $query);
  192. }
  193. if ($site_id) {
  194. $remote_site = db_select('tripal_sites', 'ts')
  195. ->fields('ts')
  196. ->condition('ts.id', $site_id)
  197. ->execute()
  198. ->fetchObject();
  199. }
  200. // Build the URL to the remote web services.
  201. $ws_version = $remote_site->version;
  202. $ws_url = $remote_site->url;
  203. $ws_url = trim($ws_url, '/');
  204. $ws_url .= '/web-services/content/' . $ws_version . '/' . $ctype;
  205. // Build the Query and make and substitions needed.
  206. //dpm($ws_url . '?' . $query);
  207. $options = array(
  208. 'data' => $qdata,
  209. );
  210. $data = drupal_http_request($ws_url, $options);
  211. if (!$data) {
  212. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  213. t('Could not connect to the remote web service.'));
  214. return FALSE;
  215. }
  216. // If the data object has an error then this is some sort of
  217. // connection error (not a Tripal web servcies error).
  218. if (property_exists($data, 'error')) {
  219. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  220. 'Remote web Services reports the following error: !error. Using URL: !url',
  221. array('!error' => $error, '!url' => $ws_url));
  222. return FALSE;
  223. }
  224. // We got a response, so convert it to a PHP array.
  225. $data = drupal_json_decode($data->data);
  226. // Check if there was a Tripal Web Services error.
  227. if (array_key_exists('error', $data)) {
  228. $error = '</pre>' . print_r($data['error'], TRUE) . '</pre>';
  229. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  230. 'Tripal remote web services reports the following error: !error. Using URL: !url',
  231. array('!error' => $error, '!url' => $ws_url));
  232. return FALSE;
  233. }
  234. return $data;
  235. }
  236. /**
  237. * Retrieves the JSON-LD context for any remote Tripal web service.
  238. *
  239. * @param $context_url
  240. * The Full URL for the context file on the remote Tripal site. This URL
  241. * can be found in the '@context' key of any response from a remote Tripal
  242. * web services call.
  243. * @param $cache_id
  244. * A unique ID for caching of this context result to speed furture
  245. * queries.
  246. * @return
  247. * The JSON-LD context mapping array, or FALSE if the context could not
  248. * be retrieved.
  249. */
  250. function tripal_get_remote_context($context_url, $cache_id) {
  251. if (!$context_url) {
  252. throw new Exception('PLease provide a context_url for the tripal_get_remote_context function.');
  253. }
  254. if (!$cache_id) {
  255. throw new Exception('PLease provide unique $cache_id for the tripal_get_remote_context function.');
  256. }
  257. if ($cache = cache_get($cache_id)) {
  258. return $cache->data;
  259. }
  260. $context = drupal_http_request($context_url);
  261. if (!$context) {
  262. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  263. 'There was a poblem retrieving the context from the remote site: !context_url.',
  264. array('!context_url' => $context_url));
  265. return FALSE;
  266. }
  267. $context = drupal_json_decode($context->data);
  268. $context = $context['@context'];
  269. cache_set($cache_id, $context);
  270. return $context;
  271. }
  272. /**
  273. * Retrieves the JSON-LD context for a bundle or field on a remote Tripal site.
  274. *
  275. * The $site_id, $bundle_accession and $field_accession variables are not
  276. * needed to retrieve the context, but are used for caching the context to
  277. * make subsequent calls execute faster. This function is meant to be used
  278. * only for the 'content' service provided by Tripal.
  279. *
  280. * @param $site_id
  281. * The numeric site ID for the remote Tripal site.
  282. * @param $context_url
  283. * The Full URL for the context file on the remote Tripal site. This URL
  284. * can be found in the '@context' key of any response from a remote Tripal
  285. * web services call.
  286. * @param $bundle_accession
  287. * The controlled vocabulary term accession for the content type
  288. * on the remote Tripal site.
  289. * @param $field_accession
  290. * The controlled vocabulary term accession for the property (i.e. field) of
  291. * the Class (i.e. content type).
  292. * @return
  293. * The JSON-LD context mapping array.
  294. */
  295. function tripal_get_remote_content_context($site_id, $context_url, $bundle_accession, $field_accession = '') {
  296. $cache_id = substr('trp_ws_context_' . $site_id . '-' . $bundle_accession . '-' . $field_accession, 0, 254);
  297. $context = tripal_get_remote_context($context_url, $cache_id);
  298. return $context;
  299. }
  300. /**
  301. * Clears the cached remote site documentation and context.
  302. *
  303. * When querying a remote website, the site's API documenation and page context
  304. * is cached to make re-use of that information easier in the future. This
  305. * function can be used to clear those caches.
  306. *
  307. * @param $site_id
  308. * The numeric site ID for the remote Tripal site.
  309. */
  310. function tripal_clear_remote_cache($site_id) {
  311. if (!$site_id) {
  312. throw new Exception('PLease provide a numeric site ID for the tripal_get_remote_API_doc function.');
  313. }
  314. clear_cache_all('trp_ws_context_' . $site_id , 'cache', TRUE);
  315. clear_cache_all('trp_ws_doc_' . $site_id , 'cache', TRUE);
  316. }
  317. /**
  318. * Retrieves the API documentation for a remote Tripal web service.
  319. *
  320. * @param $site_id
  321. * The numeric site ID for the remote Tripal site.
  322. * @return
  323. * The vocabulary of a remote Tripal web service, or FALSE if an error
  324. * occured.
  325. */
  326. function tripal_get_remote_API_doc($site_id) {
  327. $site_doc = '';
  328. if (!$site_id) {
  329. throw new Exception('PLease provide a numeric site ID for the tripal_get_remote_API_doc function.');
  330. }
  331. $cache_name = 'trp_ws_doc_' . $site_id;
  332. if ($cache = cache_get($cache_name)) {
  333. return $cache->data;
  334. }
  335. // Get the site url from the tripal_sites table.
  336. $remote_site = db_select('tripal_sites', 'ts')
  337. ->fields('ts')
  338. ->condition('ts.id', $site_id)
  339. ->execute()
  340. ->fetchObject();
  341. if (!$remote_site) {
  342. throw new Exception(t('Cannot find a remote site with id: "!id"', array('!id' => $site_id)));
  343. }
  344. // Build the URL to the remote web services.
  345. $ws_version = $remote_site->version;
  346. $ws_url = $remote_site->url;
  347. $ws_url = trim($ws_url, '/');
  348. $ws_url .= '/web-services/doc/' . $ws_version;
  349. // Build and make the request.
  350. $options = [];
  351. $data = drupal_http_request($ws_url, $options);
  352. if (!$data) {
  353. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  354. t('Could not connect to the remote web service.'));
  355. return FALSE;
  356. }
  357. // If the data object has an error then this is some sort of
  358. // connection error (not a Tripal web servcies error).
  359. if (property_exists($data, 'error')) {
  360. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  361. 'Remote web services document reports the following error: !error. Using URL: !url',
  362. array('!error' => $error, '!url' => $ws_url));
  363. return FALSE;
  364. }
  365. // We got a response, so convert it to a PHP array.
  366. $site_doc = drupal_json_decode($data->data);
  367. // Check if there was a Tripal Web Services error.
  368. if (array_key_exists('error', $data)) {
  369. $error = '</pre>' . print_r($data['error'], TRUE) . '</pre>';
  370. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  371. 'Tripal Remote web services document reports the following error: !error. Using URL: !url',
  372. array('!error' => $error, '!url' => $ws_url));
  373. return FALSE;
  374. }
  375. cache_set($cache_name, $site_doc);
  376. return $site_doc;
  377. }
  378. /**
  379. * Build and return a "fake" entity for a record from a remote Tripal site.
  380. *
  381. * @param $remote_entity_id
  382. * Array of the remote ids.
  383. * @param $site_id
  384. * The numeric site ID for the remote Tripal site.
  385. * @param $bundle_accession
  386. * The controlled vocabulary term accession for the content type
  387. * on the remote Tripal site.
  388. * @param $field_ids
  389. * The controlled vocabulary term accessions for the fields available
  390. * on the remote content type. Any remote fields that matches these IDs will
  391. * be added to the entity returned.
  392. * @return
  393. * A fake entity object.
  394. */
  395. function tripal_load_remote_entity($remote_entity_id, $site_id, $bundle_accession, $field_ids) {
  396. // Get the site documentation (loads from cache if already retrieved).
  397. $site_doc = tripal_get_remote_API_doc($site_id);
  398. // Get the remote entity and create the fake entity.
  399. $query = $bundle_accession . '/' . $remote_entity_id;
  400. $remote_entity = tripal_query_remote_site($site_id, $query);
  401. if (!$remote_entity) {
  402. return FALSE;
  403. }
  404. // Start building the fake id.
  405. $entity = new stdClass();
  406. $entity->entityType = 'TripalEntity';
  407. $entity->entityInfo = [];
  408. $entity->id = $remote_entity_id;
  409. $entity->type = 'TripalEntity';
  410. $entity->bundle = $bundle_accession;
  411. $entity->site_id = $site_id;
  412. // Get the context JSON for this remote entity, we'll use it to map
  413. $context_url = $remote_entity['@context'];
  414. $context = tripal_get_remote_content_context($site_id, $context_url, $bundle_accession);
  415. if (!$context) {
  416. return $entity;
  417. }
  418. // Iterate through the fields and the those values to the entity.
  419. foreach ($field_ids as $field_id) {
  420. $field = tripal_get_remote_field_info($site_id, $bundle_accession, $field_id);
  421. $instance = tripal_get_remote_field_instance_info($site_id, $bundle_accession, $field_id);
  422. $field_name = $field['field_name'];
  423. $field_key = '';
  424. foreach ($context as $k => $v) {
  425. if (!is_array($v) and $v == $field_id) {
  426. $field_key = $k;
  427. }
  428. }
  429. // If the field is not in this remote bundle then add an empty value.
  430. if (!$field_key) {
  431. $entity->{$field_name}['und'][0]['value'] = '';
  432. continue;
  433. }
  434. if (!array_key_exists($field_key, $remote_entity)) {
  435. $entity->{$field_name}['und'][0]['value'] = '';
  436. continue;
  437. }
  438. // If the key is for a field that is not "auto attached' then we need
  439. // to get that field through a separate call.
  440. $attached = TRUE;
  441. if (array_key_exists($field_id, $context) and is_array($context[$field_id]) and
  442. array_key_exists('@type', $context[$field_id]) and $context[$field_id]['@type'] == '@id'){
  443. $attached = FALSE;
  444. }
  445. // Set the value for this field.
  446. $value = '';
  447. if (is_array($remote_entity[$field_key])) {
  448. $value = _tripal_update_remote_entity_field($remote_entity[$field_key], $context, 1);
  449. }
  450. else {
  451. $value = $remote_entity[$field_key];
  452. }
  453. // If the field is not attached then we have to query another level.
  454. if (!$attached) {
  455. $field_data = drupal_http_request($value);
  456. if (!$field_data) {
  457. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  458. 'There was a poblem retrieving the unattached field, "!field:", for the remote entity: !entity_id.',
  459. array('!field' => $field_id, '!entity_id' => $remote_entity_id));
  460. $value = '';
  461. }
  462. $field_data = drupal_json_decode($field_data->data);
  463. // Get the context for this field so we can map the keys to the
  464. // controlled vocabulary accessions. If it fails then skip this field.
  465. $field_context_url = $field_data['@context'];
  466. $field_context = tripal_get_remote_content_context($site_id, $field_context_url, $bundle_accession, $field_id);
  467. if (!$field_context) {
  468. continue;
  469. }
  470. $value = _tripal_update_remote_entity_field($field_data, $field_context);
  471. }
  472. $entity->{$field_name}['und'][0]['value'] = $value;
  473. }
  474. return $entity;
  475. }
  476. /**
  477. * A helper function for the tripal_get_remote_entity() function.
  478. *
  479. * This function converts the field's key elements to their
  480. * vocabulary term accessions.
  481. *
  482. * @param $field_data
  483. * The field array as returned by web services.
  484. * @param $context
  485. * The web service JSON-LD context for the bundle to which the field belongs.
  486. */
  487. function _tripal_update_remote_entity_field($field_data, $context, $depth = 0) {
  488. // If depth is 0 then we are tring to find out how to deal with this field.
  489. if ($depth == 0) {
  490. // Check if this is an array.
  491. if ($field_data['@type'] == 'Collection') {
  492. $members = array();
  493. foreach ($field_data['member'] as $member) {
  494. $next_depth = $depth + 1;
  495. $members[] = _tripal_update_remote_entity_field($member, $context, $next_depth);
  496. }
  497. // If we only have one item then just return it as a single item.
  498. // TODO: we may need to check cardinality of the field and be more
  499. // strict about how we return the value.
  500. if ($field_data['totalItems'] == 1){
  501. return $members[0];
  502. }
  503. else {
  504. return $members;
  505. }
  506. }
  507. else {
  508. $next_depth = $depth + 1;
  509. return _tripal_update_remote_entity_field($field_data, $context, $next_depth);
  510. }
  511. }
  512. // If this isn't depth 0 then we are rewriting keys as CV term accession.
  513. else {
  514. $value = array();
  515. foreach ($field_data as $k => $v) {
  516. // Skip the JSON-LD keys.
  517. if ($k == '@id' or $k == '@type' or $k == '@context') {
  518. continue;
  519. }
  520. // Find the term accession for this element, and if the key's value is an
  521. // array then recurse.
  522. $accession = $context[$k];
  523. if (is_array($v)) {
  524. $next_depth = $depth + 1;
  525. $subvalue = _tripal_update_remote_entity_field($v, $context, $next_depth);
  526. $value[$accession] = $value;
  527. }
  528. else {
  529. $value[$accession] = $v;
  530. }
  531. }
  532. return $value;
  533. }
  534. }
  535. /**
  536. * Behaves similar to the field_info_field() function but for remote fields.
  537. *
  538. * Returns a "fake" field info array for fields attached to content types
  539. * on remote Tripal sites.
  540. *
  541. * @param $site_id
  542. * The numeric site ID for the remote Tripal site.
  543. * @param $bundle_accession
  544. * The controlled vocabulary term accession for the content type
  545. * on the remote Tripal site.
  546. * @param $field_accession
  547. * The controlled vocabulary term accession for the property (i.e. field) of
  548. * the Class (i.e. content type).
  549. * @return
  550. * An array similar to that returned by the field_info_field function
  551. * of Drupal for local fields.
  552. */
  553. function tripal_get_remote_field_info($site_id, $bundle_accession, $field_accession){
  554. // Get the site documentation (loads from cache if already retrieved).
  555. $site_doc = tripal_get_remote_API_doc($site_id);
  556. // Get the property from the document for this field.
  557. $property = tripal_get_remote_field_doc($site_id, $bundle_accession, $field_accession);
  558. // Now create the fake field and instance.
  559. list($vocab, $accession) = explode(':', $field_accession);
  560. $field_name = 'tripal_remote_site_' . $site_id . '_' . $field_accession;
  561. $field = array(
  562. 'field_name' => $field_name,
  563. 'type' => $field_name,
  564. 'storage' => array(
  565. 'type' => 'tripal_remote_site'
  566. ),
  567. );
  568. return $field;
  569. }
  570. /**
  571. * Behaves similar to the field_info_instance() function but for remote fields.
  572. *
  573. * Returns a "fake" instance info array for fields attached to content types
  574. * on remote Tripal sites.
  575. *
  576. * @param $site_id
  577. * The numeric site ID for the remote Tripal site.
  578. * @param $bundle_accession
  579. * The controlled vocabulary term accession for the content type
  580. * on the remote Tripal site.
  581. * @param $field_accession
  582. * The controlled vocabulary term accession for the property (i.e. field) of
  583. * the Class (i.e. content type).
  584. * @return
  585. * An array similar to that returned by the field_info_instance function
  586. * of Drupal for local fields.
  587. */
  588. function tripal_get_remote_field_instance_info($site_id, $bundle_accession, $field_accession){
  589. // Get the site documentation (loads from cache if already retrieved).
  590. $site_doc = tripal_get_remote_API_doc($site_id);
  591. // Get the property from the document for this field.
  592. $property = tripal_get_remote_field_doc($site_id, $bundle_accession, $field_accession);
  593. list($vocab, $accession) = explode(':', $field_accession);
  594. $field_name = 'tripal_remote_site_' . $site_id . '_' . $field_accession;
  595. list($vocab, $accession) = explode(':', $field_accession);
  596. $instance = array(
  597. 'label' => $property['hydra:title'],
  598. 'description' => $property['hydra:description'],
  599. 'formatters' => $property['tripal_formatters'],
  600. 'settings' => array(
  601. 'term_vocabulary' => $vocab,
  602. 'term_accession' => $accession
  603. ),
  604. 'field_name' => $field_name,
  605. 'entity_type' => 'TripalEntity',
  606. 'bundle_name' => $bundle_accession,
  607. );
  608. return $instance;
  609. }
  610. /**
  611. * Retreive the bundle (content type) information from a remote Tripal site.
  612. *
  613. * The array returned is equivalent to the Hydra Vocabulary "supportedClass"
  614. * stanza.
  615. *
  616. * @param $site_id
  617. * The numeric site ID for the remote Tripal site.
  618. * @param $bundle_accession
  619. * The controlled vocabulary term accession for the content type
  620. * on the remote Tripal site.
  621. * @return
  622. * A PHP array corresponding to the Hydra Class stanza (i.e. a content type).
  623. * Returns NULL if the class ID cannot be found.
  624. */
  625. function tripal_get_remote_bundle_doc($site_id, $bundle_accession){
  626. // Get the site documentation (loads from cache if already retrieved).
  627. $site_doc = tripal_get_remote_API_doc($site_id);
  628. // Get the class that matches this bundle.
  629. $classes = $site_doc['supportedClass'];
  630. $class = NULL;
  631. foreach ($classes as $item) {
  632. if ($item['@id'] == $bundle_accession) {
  633. return $item;
  634. }
  635. }
  636. return NULL;
  637. }
  638. /**
  639. * Retrieves the field information for a content type from a remote Tripal site.
  640. *
  641. * The array returned is equivalent to the Hydra Vocabulary "supportedProperty"
  642. * stanza that belongs to a Hydra Class (content type).
  643. *
  644. * @param $site_id
  645. * The numeric site ID for the remote Tripal site.
  646. * @param $bundle_accession
  647. * The controlled vocabulary term accession for the content type
  648. * on the remote Tripal site.
  649. * @param $field_accession
  650. * The controlled vocabulary term accession for the property (i.e. field) of
  651. * the Class (i.e. content type).
  652. * @return
  653. * A PHP array corresponding to the Hydra property stanza (field) that
  654. * belongs to the given Class (i.e. a content type). Retruns NULL if the
  655. * property cannot be found.
  656. */
  657. function tripal_get_remote_field_doc($site_id, $bundle_accession, $field_accession){
  658. // Get the site documentation (loads from cache if already retrieved).
  659. $site_doc = tripal_get_remote_API_doc($site_id);
  660. $class = tripal_get_remote_bundle_doc($site_id, $bundle_accession);
  661. $properties = $class['supportedProperty'];
  662. foreach ($properties as $item) {
  663. if ($item['property'] == $field_accession) {
  664. return $item;
  665. }
  666. }
  667. return NULL;
  668. }
  669. /**
  670. * Retrieves the list of download formatters for a remote field.
  671. *
  672. * All Tripal fields support the abilty for inclusion in files that can
  673. * downloaded. This function is used to identify what formatters these
  674. * fields are appropriate for. If those download formatter classes exist
  675. * on this site then the field can be used with that formatter.
  676. *
  677. * @param $site_id
  678. * The numeric site ID for the remote Tripal site.
  679. * @param $bundle_accession
  680. * The controlled vocabulary term accession for the content type
  681. * on the remote Tripal site.
  682. * @param $field_accession
  683. * The controlled vocabulary term accession for the property (i.e. field) of
  684. * the Class (i.e. content type).
  685. * @return
  686. * An array of field downloader class names that are compoatible with the
  687. * field and which exist on this site.
  688. */
  689. function tripal_get_remote_field_formatters($site_id, $bundle_accession, $field_accession) {
  690. $flist = array();
  691. // Get the site documentation (loads from cache if already retrieved).
  692. $site_doc = tripal_get_remote_API_doc($site_id);
  693. $property = tripal_get_remote_field_doc($site_id, $bundle_accession, $field_accession);
  694. if (!$property) {
  695. return $flist;
  696. }
  697. $formatters = $property['tripal_formatters'];
  698. foreach($formatters as $formatter) {
  699. if (tripal_load_include_downloader_class($formatter)) {
  700. $flist[$formatter] = $formatter::$full_label;
  701. }
  702. }
  703. return $flist;
  704. }