Browse Source

Fixing bugs with Views...

Stephen Ficklin 8 years ago
parent
commit
0dfb2c3815

+ 7 - 0
tripal/includes/TripalEntityViewsController.inc

@@ -49,6 +49,13 @@ class TripalEntityViewsController extends EntityDefaultViewsController {
     unset($data['tripal_entity']['term_id']);
     unset($data['tripal_entity']['type']);
 
+
+    // This should not be a base table. We're not going to allow
+    // for views based on TripalEntity content types or the list of
+    // fields would become outrageously overwhelming. Instead we will
+    // provide views by bundle.
+    unset($data['tripal_entity']['table']['base']);
+
     return $data;
   }
 

+ 1 - 0
tripal/includes/TripalFieldQuery.inc

@@ -8,6 +8,7 @@ class TripalFieldQuery extends EntityFieldQuery {
 
   protected $field_storage = array();
 
+
   public function execute() {
     // Give a chance for other modules to alter the query.
     drupal_alter('entity_query', $this);

+ 48 - 2
tripal/includes/TripalFields/TripalField.inc

@@ -25,6 +25,13 @@ class TripalField {
   // changed.
   public static $default_settings = array(
     'storage' => 'tripal_no_storage',
+    // It is expected that all fields set a 'value' in the load() function.
+    // In many cases, the value may be an associative array of key/value pairs.
+    // In order for Tripal to provide context for all data, the keys should
+    // be a controlled vocabulary term (e.g. rdfs:type). Keys in the load()
+    // function that are supported by the query() function should be
+    // listed here.
+    'searchable_keys' => array(),
   );
 
   // Provide a list of instance specific settings. These can be access within
@@ -67,6 +74,7 @@ class TripalField {
   // and field_create_instance().
   public static $no_ui = TRUE;
 
+
   // --------------------------------------------------------------------------
   //              PROTECTED CLASS MEMBERS -- DO NOT OVERRIDE
   // --------------------------------------------------------------------------
@@ -111,7 +119,6 @@ class TripalField {
         $accession = $instance['settings']['term_accession'];
         $term = tripal_get_term_details($vocabulary, $accession);
         if (!$term) {
-          dpm(debug_backtrace());
           throw new Error(t('Cannot create TripalField of type "%term" as that
               term does not exist.', array('%term' => "$vocabulary:$accession")));
         }
@@ -288,7 +295,7 @@ class TripalField {
         'click sortable' => TRUE,
       ),
       'filter' => array(
-        'handler' => 'views_handler_filter_string',
+        'handler' => 'tripal_views_handler_filter_string',
       ),
       'sort' => array(
         'handler' => 'views_handler_sort',
@@ -370,4 +377,43 @@ class TripalField {
 
   }
 
+  /**
+   * Used to filter records that match a given condition.
+   *
+   * Entities can be filtered using the fields.  This function should be
+   * implemented if the field  supports filtering.  To provide filtering,
+   * the $query object should be updated to including any joins and
+   * conditions necessary. The following rules should be followed when
+   * implementing this function:
+   * - Any keys from the value array that support filtering should be set
+   *   in the $default_settings['searchable_keys'] array of this class file.
+   * - Implement support for every key in the
+   *   $default_settings['searchable_keys'] array.
+   * - However, avoid making filteres for non-indexed database columns.
+   * - This function should never set the fields that should be returned
+   *   nor ordering or group by.
+   *
+   * @param $query
+   *   A query object appropriate for the data storage backend. For example,
+   *   The Tripal Chado module will provide a SelectQuery object.
+   * @param $condition
+   *   The field specific condition as set in the TripalFieldQuery object.
+   */
+  public function query($query, $condition) {
+
+  }
+
+  /**
+   * Used to sort records that have been filtered.
+   *
+   * @param $query
+   *   A query object appropriate for the data storage backend. For example,
+   *   The Tripal Chado module will provide a SelectQuery object.
+   * @param $order
+   *   The field ordering as set in the TripalFieldQuery object.  This function
+   *   should handle the ordering request as specified by this object.
+   */
+  public function queryOrder($query, $order) {
+
+  }
 }

+ 1 - 1
tripal/includes/tripal.field_storage.inc

@@ -49,7 +49,7 @@ function tripal_field_storage_load($entity_type, $entities, $age,
       $field_module = $field['module'];
 
       // Get the instnace for this field
-      $instance = field_info_instance($entity_type, $field_name, $entity->bundle); 
+      $instance = field_info_instance($entity_type, $field_name, $entity->bundle);
 
       // Allow the creating module to alter the value if desired.  The
       // module should do this if the field has any other form elements

+ 0 - 1
tripal/includes/tripal.fields.inc

@@ -478,7 +478,6 @@ function tripal_field_instance_settings_form_alter_process($element, &$form_stat
     $term_name = $instance['settings']['term_name'];
     $term = tripal_get_term_details($vocabulary, $accession);
   }
-  dpm($instance);
 
   // Construct a table for the vocabulary information.
   $headers = array();

+ 4 - 1
tripal/tripal.info

@@ -10,12 +10,15 @@ stylesheets[all][] = theme/css/tripal.css
 scripts[]          = theme/js/tripal.js
 
 files[] = views_handlers/tripal_views_handler_field.inc
-files[] = views_handlers/tripal_views_handler_filter_string_selectbox.inc
 files[] = views_handlers/tripal_views_handler_field_entity.inc
 files[] = views_handlers/tripal_views_handler_field_entity_status.inc
 files[] = views_handlers/tripal_views_handler_field_entity_default_formatter.inc
+files[] = views_handlers/tripal_views_handler_filter.inc
+files[] = views_handlers/tripal_views_handler_filter_string.inc
 files[] = views_handlers/tripal_views_handler_filter_entity_string.inc
+files[] = views_handlers/tripal_views_handler_filter_string_selectbox.inc
 files[] = views_handlers/tripal_views_handler_sort_entity_string.inc
+
 files[] = tripal_views_query.inc
 
 dependencies[] = views

+ 1 - 2
tripal/tripal.views.inc

@@ -33,7 +33,6 @@ function tripal_views_data() {
   tripal_entity_views_data($data);
   tripal_views_data_fields($data);
 
-  dpm($data);
   return $data;
 }
 
@@ -104,7 +103,7 @@ function tripal_entity_views_data(&$data) {
         'handler' => 'tripal_views_handler_field_entity',
       ),
       'filter' => array(
-        'handler' => 'views_handler_filter_numeric',
+        'handler' => 'tripal_views_handler_filter',
       ),
       'sort' => array(
         'handler' => 'views_handler_sort',

+ 41 - 15
tripal/tripal_views_query.inc

@@ -9,6 +9,32 @@ class tripal_views_query extends views_plugin_query {
 
   var $filters;
 
+
+  /**
+   * Ensure a table exists in the queue.
+   *
+   * This function overrides the views_plugin_query version of the function
+   * but does nothing other than return the "table" (or bundle) name as
+   * we won't be using aliases for bundles.
+   *
+   * @param $table
+   *   The unaliased name of the table to ensure.
+   * @param $relationship
+   *   The relationship to ensure the table links to. Each relationship will
+   *   get a unique instance of the table being added. If not specified, will
+   *   be the primary table.
+   * @param $join
+   *   A views_join object (or derived object) to join the alias in.
+   *
+   * @return
+   *   The alias used to refer to this specific table, or NULL if the table
+   *   cannot be ensured.
+   */
+  public function ensure_table($table, $relationship = NULL, $join = NULL) {
+    // Because we are not querying a table, we're querying a TripalFieldQuery
+    // object we don't need to ensure the table.
+    return $table;
+  }
   /**
    *
    */
@@ -38,11 +64,23 @@ class tripal_views_query extends views_plugin_query {
   }
 
   /**
+   * Add a simple WHERE clause to the query.
    *
    * @param $group
+   *   The WHERE group to add these to; groups are used to create AND/OR
+   *   sections. Groups cannot be nested. Use 0 as the default group. If the
+   *   group does not yet exist it will be created as an AND group.
    * @param $field
+   *   The name of the field to check.
    * @param $value
+   *   The value to test the field against. In most cases, this is a scalar.
+   *   For more complex options, it is an array. The meaning of each element
+   *   in the array is dependent on the $operator.
    * @param $operator
+   *   The comparison operator, such as =, <, or >=. It also accepts more
+   *   complex options such as IN, LIKE, or BETWEEN. Defaults to IN if $value
+   *   is an array = otherwise. If $field is a string you have to use 'formula'
+   *   here.
    */
   public function add_where($group, $field_name, $value = NULL, $operator = NULL) {
     // Remove the preceeding period from the $field_name
@@ -84,7 +122,6 @@ class tripal_views_query extends views_plugin_query {
       $entities = array($entity_id => $stub);
       $view->result[$i] = new stdClass();
       $view->result[$i]->entity = $stub;
-      $view->result[$i]->entity_id = $entity_id;
       foreach ($this->fields as $details) {
         $field_name = $details['field_name'];
         $field = field_info_field($field_name);
@@ -92,24 +129,13 @@ class tripal_views_query extends views_plugin_query {
             $entities, FIELD_LOAD_CURRENT, array($field['id'] => array($entity_id)));
         $view->result[$i]->$field_name = $entities[$entity_id]->$field_name['und'][0]['value'];
       }
+      $view->result[$i]->entity_id = $stub->id;
       $i++;
     }
     $view->execute_time = microtime(TRUE) - $start;
     $view->current_page = 0;
-    dpm($view->query);
-    dpm($view->result);
+    //dpm($view->query);
+    //dpm($view->result);
   }
 
-  /**
-   * Generate a query and a countquery from all of the information supplied
-   * to the object.
-   *
-   * @param $get_count
-   *   Provide a countquery if this is true, otherwise provide a normal query.
-   *
-   * @return SelectQuery
-   *   A SelectQuery object.
-   */
-  function query($get_count = FALSE) {
-  }
 }

+ 13 - 1
tripal/views_handlers/tripal_views_handler_field.inc

@@ -9,7 +9,9 @@
  */
 class tripal_views_handler_field extends views_handler_field {
 
-
+  /**
+   *
+   */
   function query() {
     parent::query();
     // We need to add an alias to our TripalFields so that the
@@ -33,6 +35,16 @@ class tripal_views_handler_field extends views_handler_field {
    */
   function get_value($values, $field = NULL) {
     $alias = isset($field) ? $this->aliases[$field] : $this->field_alias;
+
+    // For some reason the alias isn't being added to the $this->aliases
+    // variable when the user clicks the checkbox 'Link this field to the
+    // original piece of content'.  That may need to be investigated and is
+    // probably a side effect of using the  views_handler_field as a parent
+    // class that is expecting something more for SQL. We don't use aliases
+    // for fields so the following if statement will fix the problem.
+    if (!$alias) {
+      $alias = $this->field_alias;
+    }
     if (isset($values->{$alias})) {
       return $values->{$alias};
     }

+ 1 - 1
tripal/views_handlers/tripal_views_handler_field_entity.inc

@@ -12,7 +12,7 @@
  *
  * @ingroup views_field_handlers
  */
-class tripal_views_handler_field_entity extends views_handler_field {
+class tripal_views_handler_field_entity extends tripal_views_handler_field {
 
   function init(&$view, &$options) {
     parent::init($view, $options);

+ 1 - 1
tripal/views_handlers/tripal_views_handler_filter_select_string.inc

@@ -323,7 +323,7 @@ class tripal_views_handler_filter_string_selectbox extends views_handler_filter_
     }
 
     $this->ensure_my_table();
-    $field = $this->table_alias . "." . $this->real_field;
+    $field = $this->real_field;
     $table = $this->query->get_table_info($this->table);
 
     $this->options['values_form_type'] = (isset($this->options['values_form_type'])) ? $this->options['values_form_type'] : 'textfield';

+ 5 - 23
tripal_chado/includes/TripalFields/ChadoField.inc

@@ -48,27 +48,15 @@ class ChadoField extends TripalField {
   public static $module = 'tripal_chado';
 
   /**
-   * Used to filter records that match a given condition.
+   * @see TripalField::query()
+   *
+   * In addition to the rules to follow for the TripalField::query function
+   * these should also be followed for the ChadoField::query implementation.
    *
-   * Entities can be filtered using the fields.  This function should be
-   * implemented if the field  supports filtering.  To provide filtering,
-   * the $query object should be updated to including any joins and
-   * conditions necessary. The following rules should be followed when
-   * implementing this function:
-   * - Try to implement a filter for every element of the 'value' array
-   *   returned by the load() function.
-   * - However, avoid making filteres for non-indexed database columns.
    * - When giving alias to joined tables be sure to use aliases that are
    *   unique to avoid conflicts with other fields.
    * - When joining with the base table its alias is 'base'.
-   * - This function should never set the fields that should be returned
-   *   nor ordering or group by.
    * - You may join to materialized views if need be to help speed queries.
-   *
-   * @param $query
-   *   A SelectQuery object.
-   * @param $condition
-   *   The field specific condition as set in the TripalFieldQuery object.
    */
   public function query($query, $condition) {
     // If we are here it is because the child class did not implement the
@@ -94,13 +82,7 @@ class ChadoField extends TripalField {
   }
 
   /**
-   * Used to sort records that have been filtered.
-   *
-   * @param $query
-   *   A SelectQuery object.
-   * @param $order
-   *   The field ordering as set in the TripalFieldQuery object.  This function
-   *   should handle the ordering request as specified by this object.
+   * @see TripalField::queryOrder()
    */
   public function queryOrder($query, $order) {
 

+ 1 - 1
tripal_chado/includes/tripal_chado.field_storage.inc

@@ -415,7 +415,7 @@ function tripal_chado_field_storage_write_merge_fields($fields, $entity_type, $e
  * Implements hook_field_storage_query().
  */
 function tripal_chado_field_storage_query($query) {
-
+dpm($query);
   // Initialize the result array.
   $result = array(
     'TripalEntity' => array()

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

@@ -1680,7 +1680,7 @@ function tripal_chado_bundle_create_instances_linker(&$info, $entity_type, $bund
  *
  * A priviledged user has the ability to add new fields to the bundle. The
  * chado_linker__prop and chado_linker__cvterm fields are allowed to be
- * added dynamically by the user.  But, Drupal doesn't know whot to deal with
+ * added dynamically by the user.  But, Drupal doesn't know how to deal with
  * it, so this function is called for any field attached to a TripalEntity
  * bundle type. Any fields whose TripalField::$module argument is set to
  * 'tripal_chado' and that can be added dynamically will result in a call