Browse Source

Updated remote__data field... Also some other fixes that got tagged along after merging

Stephen Ficklin 7 years ago
parent
commit
04c747ed22

+ 30 - 0
legacy/tripal_analysis/tripal_analysis.module

@@ -249,5 +249,35 @@ function tripal_analysis_form_alter(&$form, &$form_state, $form_id) {
 }
 
 
+/**
+ * Register tripal_analysis_api sub-modules
+ *
+ * @param $modulename
+ *  The name of the module to be registered as a tripal analysis submodule
+ *
+ * @ingroup tripal_analysis_api
+ */
+function tripal_register_analysis_child($modulename) {
+  $sql = "SELECT * FROM {tripal_analysis} WHERE modulename = :modname";
+  if (!db_query($sql, array(':modname' => $modulename))->fetchField()) {
+    $sql = "INSERT INTO {tripal_analysis} (modulename) VALUES (:modname)";
+    db_query($sql, array(':modname' => $modulename));
+  }
+}
+
+/**
+ * Un-register a tripal analysis sub-module
+ *
+ * @param $modulename
+ *  The name of the module to un-register
+ *
+ * @ingroup tripal_analysis_api
+ */
+function tripal_unregister_analysis_child($modulename) {
+  if (db_table_exists('tripal_analysis')) {
+    $sql = "DELETE FROM {tripal_analysis} WHERE modulename = :modname";
+    db_query($sql, array(':modname' => $modulename));
+  }
+}
 
 

+ 0 - 31
tripal_chado/api/modules/tripal_chado.analysis.api.inc

@@ -14,36 +14,6 @@
  * @}
  */
 
-/**
- * Register tripal_analysis_api sub-modules
- *
- * @param $modulename
- *  The name of the module to be registered as a tripal analysis submodule
- *
- * @ingroup tripal_analysis_api
- */
-function tripal_register_analysis_child($modulename) {
-  $sql = "SELECT * FROM {tripal_analysis} WHERE modulename = :modname";
-  if (!db_query($sql, array(':modname' => $modulename))->fetchField()) {
-    $sql = "INSERT INTO {tripal_analysis} (modulename) VALUES (:modname)";
-    db_query($sql, array(':modname' => $modulename));
-  }
-}
-
-/**
- * Un-register a tripal analysis sub-module
- *
- * @param $modulename
- *  The name of the module to un-register
- *
- * @ingroup tripal_analysis_api
- */
-function tripal_unregister_analysis_child($modulename) {
-  if (db_table_exists('tripal_analysis')) {
-      $sql = "DELETE FROM {tripal_analysis} WHERE modulename = :modname";
-      db_query($sql, array(':modname' => $modulename));
-  }
-}
 
 /**
  * Retrieves an chado analysis variable
@@ -165,7 +135,6 @@ function tripal_get_analysis($identifier, $options) {
  *   An array of analyses sync'd with Drupal where each value is the analysis scientific
  *   name and the keys are analysis_id's
  *
- * @ingroup tripal_analysis_api
  */
 function tripal_get_analysis_select_options($syncd_only = TRUE) {
   $analysis_list = array();

+ 0 - 1
tripal_chado/includes/setup/tripal_chado.chado_v1_1.inc

@@ -3,7 +3,6 @@
  * Create a legacy custom chado table (analysisfeatureprop) to store properties of
  * analysisfeature links.
  *
- * @ingroup tripal_analysis
  */
 function tripal_chado_add_analysisfeatureprop_table() {
 

+ 0 - 1
tripal_chado/includes/setup/tripal_chado.chado_vx_x.inc

@@ -352,7 +352,6 @@ function tripal_chado_add_organism_feature_count_mview() {
 /**
  * Creates a view showing the link between an organism & it's analysis through associated features.
  *
- * @ingroup tripal_analysis
  */
 function tripal_chado_add_analysis_organism_mview() {
   $view_name = 'analysis_organism';

+ 127 - 77
tripal_ws/includes/TripalFields/remote__data/remote__data.inc

@@ -16,10 +16,10 @@ class remote__data extends WebServicesField {
   // the field and it's default widget and formatter.
   // --------------------------------------------------------------------------
   // The default label for this field.
-  public static $default_label = 'Remote Data';
+  public static $default_label = 'Remote Tripal Site';
 
   // The default description for this field.
-  public static $default_description = 'remote data';
+  public static $default_description = 'Allows for inclusion of remote data from another Tripal site.';
 
   // The default widget for this field.
   public static $default_widget = 'remote__data_widget';
@@ -67,11 +67,11 @@ class remote__data extends WebServicesField {
     // attached asynchronously.
     'auto_attach' => FALSE,
     // Settings to allow the site admin to set the remote data source info.
-			'data_info' => array(
-				'query' => '',
-				'remote_site' => '',
-				'description' => '',
-			),
+      'data_info' => array(
+        'query' => '',
+        'remote_site' => '',
+        'description' => '',
+      ),
   );
   // A boolean specifying that users should not be allowed to create
   // fields and instances of this field type through the UI. Such
@@ -84,81 +84,130 @@ class remote__data extends WebServicesField {
   // the user but otherwise provides no data.
   public static $no_data = FALSE;
 
+  // Holds an object describing the remote site that tihs field connects to.
+  private $remote_site = NULL;
+
+  public function __construct($field, $instance) {
+    parent::__construct($field, $instance);
+
+    //Get the site url from the tripal_sites table.
+    $site_id_ws = $this->instance['settings']['data_info']['remote_site'];
+    $this->remote_site = db_select('tripal_sites', 'ts')
+      ->fields('ts')
+      ->condition('ts.id', $site_id_ws)
+      ->execute()
+      ->fetchObject();
+  }
   /**
    * @see WebServicesField::load()
    */
   public function load($entity) {
-    $site_id_ws = $this->instance['settings']['data_info']['remote_site'];
-    $query = $this->instance['settings']['data_info']['query'];
-    $options = array();
-
-    // Check for tripal tokens and replace if present.
-    $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
-    $query = tripal_replace_entity_tokens($query, $entity, $bundle_entity);
-
-     // Get the site url from the tripal_sites table.
-     $site_url_ws = db_select('tripal_sites', 's')
-       ->fields('s',array('url'))
-       ->condition('s.id', $site_id_ws, '=')
-       ->execute()->fetchAll();
-
-     $full_url = $site_url_ws[0]->url . '/web-services/content/v0.1/';
-
-     //Remove trailing slash if one is included.$_COOKIE
-     $full_url = trim($full_url, '/');
-     $full_url = $full_url . '/' . $query;
-
-     //Make the call and pull the data down.
-     $data = drupal_http_request($full_url, $options);
-
-     if($data){
-       $data = drupal_json_decode($data->data);
-       //Check the returned data is not an error.
-       if(array_key_exists('error', $data)){
-          watchdog('Tripal WS', '<pre>Web Services data unavailable because site is returning error: '. print_r($data['error'], TRUE) .'</pre>');
-          $data = '';
-       }
-       //Check the returned data isn't empty.
-       if(count($data['members']) >= 1){
-        //If multiple records, if single follow @id and pull down data
-        if(array_key_exists('value', $data)){
-          $members = $data['value'][0]['members'];
-          if($members){
-            if(count($members) > 1){
-                $field_name = $this->field['field_name'];
-                $entity->{$field_name}['und'][0]['value'] = $data;
-            }
-            else {
-              $single_record_url = $data['members'][0]['@id'];
-              $data = drupal_http_request($single_record_url, $options);
-              $data = drupal_json_decode($data->data);
-              if(array_key_exists('error', $data)){
-                watchdog('Tripal WS', '<pre>Web Services data unavailable because site is returning error: '. print_r($data['error'], TRUE) .'</pre>');
-                $data = '';
-             }
-            }
-          }
-        }
-        else {
-            $single_record_url = $data['members'][0]['@id'];
-            $data = drupal_http_request($single_record_url, $options);
-            $data = drupal_json_decode($data->data);
-            if(array_key_exists('error', $data)){
-              watchdog('Tripal WS', '<pre>Web Services data unavailable because site is returning error: '. print_r($data['error'], TRUE) .'</pre>');
-              $data = '';
-           }
-        }
-        $field_name = $this->field['field_name'];
-        $entity->{$field_name}['und'][0]['value'] = $data;
+
+    $field_name = $this->field['field_name'];
+    $field_type = $this->field['type'];
+
+    // Set some defaults for the empty record.
+    $entity->{$field_name}['und'][0] = array(
+      'value' => array(),
+    );
+
+    // Get the query set by the admin for this field and replace any tokesn
+    $query_str = $this->instance['settings']['data_info']['query'];
+    $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
+    $query_str = tripal_replace_entity_tokens($query_str, $entity, $bundle);
+
+    // Make the request.
+    $data = $this->makeRemoteRequest($query_str);
+    if(!$data){
+     return;
+    }
+
+    $total_items = $data['totalItems'];
+
+    // Iterate through the members returned and save those for the field.
+    for ($i = 0; $i < count($data['members']); $i++) {
+      $member = $data['members'][$i];
+
+      // Get the cotent type and remote entity id
+      $content_type = $member['@type'];
+      $remote_entity_id = $member['@id'];
+      $remote_entity_id = preg_replace('/^.*\/(\d+)/', '$1', $remote_entity_id);
+
+      // Save the member information for use later.
+      $entity->{$field_name}['und'][$i]['remote_entity'] = $member;
+
+      // Next get the the details about this member.
+      $query_field = 'relationship';
+      $query_field_url =  $content_type . '/' . $remote_entity_id . '/' . $query_field;
+      $field_data = $this->makeRemoteRequest($query_field_url);
+      if(!$field_data){
+        // If we encounter any type of error, we'll reset the field and return.
+        $entity->{$field_name}['und'] = array();
+        $entity->{$field_name}['und'][0] = array(
+          'value' => array(),
+        );
+        return;
       }
+
+      // Set the field data as the value.
+      $field_data_type = $field_data['@type'];
+      $entity->{$field_name}['und'][$i]['value'] = $field_data;
     }
-   }
-   /**
-	 *
-	 * @see TripalField::settingsForm()
+  }
+  /**
+   * Makes a request to a remote Tripal web services site.
+   *
+   * @param $query
+   *   The query string. This string is added to the URL for the remote
+   *   website.
+   */
+  private function makeRemoteRequest($query) {
+
+    // Build the URL to the remote web services.
+    $ws_url = $this->remote_site->url;
+    $ws_url = trim($ws_url, '/');
+    $ws_url .= '/web-services/content/v0.1';
+
+    // Build the Query and make and substitions needed.
+    $query_url = $ws_url . '/' . $query;
+    $options = array('timeout' => 360);
+    $data = drupal_http_request($query_url, $options);
+
+    if (!$data) {
+      tripal_report_error('tripal_ws', TRIPAL_ERROR,
+          t('Could not connect to the remote web service.'));
+      return FALSE;
+    }
+
+    // If the data object has an error then this is some sort of
+    // connection error (not a Tripal web servcies error).
+    if (property_exists($data, 'error')) {
+      tripal_report_error('tripal_ws', TRIPAL_ERROR,
+          'Web Services error on remote site: %error.',
+          array('%error' => $data->error));
+      return FALSE;
+    }
+
+    // We got a response, so convert it to a PHP array.
+    $data = drupal_json_decode($data->data);
+
+    // Check if there was a Tripal Web Services error.
+    if (array_key_exists('error', $data)) {
+      $error = '</pre>' . print_r($data['error'], TRUE) . '</pre>';
+      tripal_report_error('tripal_ws', TRIPAL_ERROR,
+        'Web Services error on remote site: %error.',
+        array('%error' => $error));
+      return FALSE;
+    }
+
+    return $data;
+  }
+  /**
+   *
+   * @see TripalField::settingsForm()
    */
-	public function instanceSettingsForm() {
-		$element = parent::instanceSettingsForm();
+  public function instanceSettingsForm() {
+    $element = parent::instanceSettingsForm();
     // Get the setting for the option for how this widget.
     $instance = $this->instance;
     $settings = '';
@@ -166,7 +215,8 @@ class remote__data extends WebServicesField {
 
     $tokens = array();
     // Get the form info from the bundle about to be saved.
-    $bundle_info = tripal_load_bundle_entity(array($instance['bundle']));
+    $bundle_info = tripal_load_bundle_entity(array('name' => $instance['bundle']));
+
     // Retrieve all available tokens.
     $tokens = tripal_get_entity_tokens($bundle_info);
 
@@ -247,7 +297,7 @@ class remote__data extends WebServicesField {
         //…
     );*/
 
-			return $element;
+      return $element;
   }
 
     /**

+ 142 - 52
tripal_ws/includes/TripalFields/remote__data/remote__data_formatter.inc

@@ -73,75 +73,165 @@ class remote__data_formatter extends WebServicesFieldFormatter {
     $settings = $display['settings'];
     $field_name = $this->field['field_name'];
 
-    //Check that the load function returned content.
-    if (!empty($items[0]['value'])){
-      $list = array();
-      foreach ($items as $index => $item) {
-        $list[$index] = $item['value'];
-      }
-      // If more than one value has been found display all values in an unordered
-      // list.
-      if (count($list) > 1) {
-        $content = theme_item_list(array(
-          'items' => $list,
-          'title' => '',
-          'attributes' => array('class' => array($entity->bundle . '-remote-data-list', 'remote-data-field-list')),
-          'type' => 'ul'
-        ));
-      }
-      else {
-        $content = $list[0];
-      }
+    // Get the site name where the data came from.
+    $site_id_ws = $this->instance['settings']['data_info']['remote_site'];
+    $site = db_select('tripal_sites', 'ts')
+      ->fields('ts', array('name', 'url'))
+      ->condition('ts.id', $site_id_ws)
+      ->execute()
+      ->fetchObject();
+
+    $content = '';
+    if (count($items) > 0) {
+      $remote_entity_label = $items[0]['remote_entity']['label'];
+      $remote_entity_page = $items[0]['remote_entity']['ItemPage'];
+      $content = t('The data below about !label was obtained from the !site database.',
+        array('!label' => l($remote_entity_label, $remote_entity_page),
+          '!site' => l($site->name, $site->url)));
+    }
+    else {
+      $content = t('There is no data about this record from the !site database.',
+          array('!site' => l($site->name, $site->url)));
+    }
+    dpm($items);
+    foreach ($items as $index => $item) {
+      $value = $item['value'];
+      if (is_array($value)) {
+        $headers = array('');
 
-      if(array_key_exists('members', $content)){
-        $header = array($items[0]['value']['label']);
-        $members = $content['members'];
-        $rows = array();
-        // Wrap each item in an array incase it is an array so that
-        // it works with the table_theme
-        foreach($members as $index => $member){
-          $rows[] = array('data' => array($index, $item));
+        // If this is a collection then handle it as a list of members.
+        if (array_key_exists('members', $value)) {
+          foreach ($value['members'] as $subvalue) {
+            $content .= $this->createTable($subvalue);
+          }
+        }
+        else {
+          $content .= $this->createTable($subvalue);
         }
-        //Remove the context information which is not needed.
-        unset($rows[0]);
       }
       else {
-        $header = array($items[0]['value']['label']);
-        $rows = array();
-        // Wrap each item in an array incase it is an array so that
-        // it works with the table_theme.
-        foreach($content as $index => $item){
-          $rows[] = array('data' => array($index, $item));
-        }
-        //Remove the context information which is not needed.
-        unset($rows[0]);
+        $content .= $this->createDL($value);
       }
+    }
 
+    // Return the content for this field.
+    $element[0] = array(
+      '#type' => 'markup',
+      '#markup' => $content,
+    );
+  }
 
-      $table = array(
-        'header' => $header,
+  /**
+   * A recursive function for displaying an item in a table.
+   *
+   * @param $item
+   *   An item from the $items array passed to the view() function.
+   * @return
+   *   An HTML formatted Table.
+   */
+  private function createTable($item, &$pkey = '', &$rows = array(), $depth = 0) {
+    foreach ($item as $key => $value) {
+      // Skip JSON-LD keys.
+      if (preg_match('/^\@/', $key)) {
+        continue;
+      }
+      $key = preg_replace('/_/', ' ', $key);
+      $key = ucwords($key);
+      if ($pkey) {
+        $key = $pkey . ' ' . $key;
+      }
+      if (is_array($value)) {
+        $this->createTable($value, $key, $rows, $depth + 1);
+      }
+      else {
+        $rows[] = array(
+          'data'=> array(
+            $key,
+            $value
+          ),
+          'no_striping' => TRUE,
+        );
+      }
+    }
+    if ($depth == 0) {
+      $headers = array('Data Type', 'Value');
+      return theme_table(array(
+        'header' => $headers,
         'rows' => $rows,
         'attributes' => array(
-          'id' => 'tripal_table-remote-data-object',
-          'class' => 'tripal-data-table'
+          'class' => 'tripal-remote--data-field-table',
         ),
         'sticky' => FALSE,
         'caption' => "",
         'colgroups' => array(),
-        'empty' => 'There is no remote data available.',
-      );
+        'empty' => 'There are no results.',
+      ));
+    }
+  }
 
-      $content = theme_table($table);
+  /**
+   * A recursive function for creating an HTML dictionary list from
+   * the results for the item provided.
+   *
+   * @param $item
+   *   An item from the $items array passed to the view() function.
+   * @return
+   *   An HTML formatted DL.
+   */
+  private function createDL($item, &$pkey = '', &$content= '', $depth = 0) {
+    if ($depth == 0) {
+      $content = '<dl class="tripal-remote--data-field-dl">';
+    }
+    foreach ($item as $key => $value) {
+      // Skip JSON-LD keys.
+      if (preg_match('/^\@/', $key)) {
+        continue;
+      }
 
-      if (count($items) > 0) {
-        // once we have our table array structure defined, we call Drupal's theme_table()
-        // function to generate the table.
-        $element[0] = array(
-          '#type' => 'markup',
-          '#markup' => $content,
-        );
+      $key = preg_replace('/_/', ' ', $key);
+      $key = ucwords($key);
+      if ($pkey) {
+        $key = $pkey . ' ' . $key;
+      }
+      if (is_array($value)) {
+        $this->createDL($value, $key, $content, $depth + 1);
+      }
+      else {
+        $content .= '<dt>' . $key . '&nbsp;:&nbsp;</dt><dd>' . $value . '</dd>';
+      }
+    }
+    if ($depth == 0) {
+      $content .= '</dl>';
+      return $content;
+    }
+  }
+
+  /**
+   * A recursive function for creating an HTML dictionary list from
+   * the results for the item provided.
+   *
+   * @param $item
+   *   An item from the $items array passed to the view() function.
+   * @return
+   *   An HTML formatted DL.
+   */
+  private function createNestedDL($item) {
+    $content = '<dl>';
+    foreach ($item as $key => $value) {
+      // Skip JSON-LD keys.
+      if (preg_match('/^\@/', $key)) {
+        continue;
+      }
+
+      $key = preg_replace('/_/', ' ', $key);
+      $key = ucwords($key);
+      if (is_array($value)) {
+        $value = $this->createDL($value);
       }
+      $content .= '<dt>' . $key . '</dt><dd>' . $value . '</dd>';
     }
+    $content .= '</dl>';
+    return $content;
   }
   /**
    * Provides a summary of the formatter settings.

+ 7 - 0
tripal_ws/includes/TripalWebService/TripalEntityService_v0_1.inc

@@ -221,6 +221,12 @@ class TripalEntityService_v0_1 extends TripalWebService {
       // Get the information about this field.
       $field = field_info_field($field_name);
 
+      // Skip the remote__data field that is provided by the tripal_Ws
+      // module.
+      if ($field['type'] == 'remote__data') {
+        continue;
+      }
+
       // By default, the label for the key in the output should be the
       // term from the vocabulary that the field is assigned. But in the
       // case that the field is not assigned a term, we must use the field name.
@@ -267,6 +273,7 @@ class TripalEntityService_v0_1 extends TripalWebService {
   private function addEntityField($key, $term, $entity, $bundle, $field, $instance,
       $service_path, $expfield = NULL) {
 
+
     // If the entity is set to hide fields that have no values then we
     // want to honor that in the web services too.
     $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id, 'hide');

+ 1 - 1
tripal_ws/includes/tripal_ws.fields.inc

@@ -72,7 +72,7 @@ function tripal_ws_bundle_create_user_field($new_field, $bundle) {
       'description' => '',
       'required' => FALSE,
       'settings' => array(
-        'auto_attach' => TRUE,
+        'auto_attach' => FALSE,
         'term_vocabulary' => '',
         'term_accession' => '',
         'term_name' => ''

+ 3 - 1
tripal_ws/tripal_ws.info

@@ -5,4 +5,6 @@ project = tripal
 package = Tripal
 version = 7.x-3.0-rc1
 
-dependencies[] = tripal
+stylesheets[all][] = theme/css/tripal_ws.css
+
+dependencies[] = tripal