function tripal_ws_load_remote_entity

3.x tripal_ws.module tripal_ws_load_remote_entity($site_id, $api_version, $ctype, $id)

Parameters

$site_id:

$api_version:

$ctype:

$id:

Related topics

1 string reference to 'tripal_ws_load_remote_entity'
tripal_ws_menu in tripal_ws/tripal_ws.module
Implements hook_menu(). Defines all menu items needed by Tripal Core

File

tripal_ws/tripal_ws.module, line 309
The Tripal Web Service Module

Code

function tripal_ws_load_remote_entity($site_id, $api_version, $ctype, $id) {

  // Get the content type on this site
  $bundle = tripal_load_bundle_entity(array('label' => $ctype));
  $term = entity_load('TripalTerm', array('id' => $bundle->term_id));
  $term = reset($term);
  $vocab = $term->vocab;

  $query = db_select('tripal_sites', 'ts');
  $query->fields('ts');
  $query->condition('id', $site_id);
  $site = $query->execute()->fetchObject();

  if (!$site) {
    return 'Could not find specified site.';
  }

  // Get the content from the web services of the remote site.
  $url = $site->url . "/ws/v0.1/content/" . $ctype . "/" . $id;
  $json = file_get_contents($url);
  $response = json_decode($json, TRUE);

  // Set the title for this page to match the title provided.
  drupal_set_title($response['label']);

  // Attribute this data to the proper source.
  $source_url = l($response['label'], $response['ItemPage'], array('attributes' => array('target' => '_blank')));
  $content = '<div><strong>Source:</strong> ' . $site->name . ': ' . $source_url . '</div>';

  // Fake an entity so we can display this content using the same
  // entity type on this site.
  $entity = new TripalEntity(array(), 'TripalEntity');
  $entity->id = 807;
  $entity->type = 'TripalEntity';
  $entity->bundle = $bundle->name;
  $entity->term_id = $term->id;
  $entity->title = $response['label'];
  $entity->uid = 1;
  $entity->status = 1;

  // Get the fields and create a list of those that are attached to the bundle.
  $fields = field_info_fields();
  $my_fields = array();
  foreach ($fields as $field) {
    if (isset($field['bundles']['TripalEntity'])) {
      foreach ($field['bundles']['TripalEntity'] as $bundle_name) {
        if ($bundle_name == $bundle->name) {
          $my_fields[] = $field;
        }
      }
    }
  }

  // Add in the value for the 'content_type' field.
  $entity->content_type = array();
  $entity->content_type['und'][0]['value'] = $bundle->label;

  // For each field we know about that should be attached to our bundle,
  // see if we can find a corresponding entry in the results returned from
  // the web service call. If so, then add the field to our fake entity.
  foreach ($my_fields as $field) {
    // Get the semantic web term for this field.
    $field_name = $field['field_name'];
    $settings = $field['settings'];

    // If the field does not have a semantic web mapping, then skip it.
    if (!isset($settings['semantic_web'])) {
      continue;
    }

    // Convert the term into it's db and accession elements and look it up
    // for more details.
    list($vocabulary, $accession) = explode(':', $settings['semantic_web']);
    $term = tripal_get_term_details($vocabulary, $accession);

    // Convert the term to lowercase and remove spaces so we can compare
    // correctly.
    $term_name = strtolower(preg_replace('/ /', '_', $term['name']));

    // TODO: check for the term in the response makes the assumption
    // that the term is the same on both sides. This may not be true. The
    // acutal vocab and accession for both terms should be compared.
    if (isset($response[$term_name])) {

      // If this field is of type '@id' then this links out to another
      // URL where that information can be retrieved. We'll have to
      // handle that separately.
      if (isset($response['@context'][$term_name]['@type']) and 
        $response['@context'][$term_name]['@type'] == '@id') {
        $subquery = json_decode(file_get_contents($response[$term_name]), TRUE);

        // If the result is a collection then we want to add each value with
        // it's own delta value.
        if (array_key_exists('@type', $subquery) and $subquery['@type'] == 'Collection') {
          $i = 0;
          $f = array();
          foreach ($subquery['member'] as $member) {
            $f['und'][$i]['value'] = $member;
            $i++;
          }
          $entity->$field_name = $f;
        }
        // If the result is not a collection then just add it.
        else {
          unset($subquery['@context']);
          unset($subquery['@id']);
          $f = array();
          $f['und'][0]['value'] = $subquery;
          $entity->$field_name = $f;
        }
      }
      // For all fields that are currently attached, add the field and
      // value to the entity.
      else {
        $f = array();
        $f['und'][0]['value'] = $response[$term_name];
        $entity->$field_name = $f;
      }
    }
  }

  // Generate the View for this entity
  $entities = array();
  $entities[] = $entity;
  $view = entity_view('TripalEntity', $entities);
  $content .= drupal_render($view['TripalEntity'][807]);

  return $content;

}