Просмотр исходного кода

Working on making pagers easier for fields

Stephen Ficklin 7 лет назад
Родитель
Сommit
e78e7ab8c1

+ 13 - 1
tripal/includes/TripalFields/TripalField.inc

@@ -57,6 +57,9 @@ class TripalField {
     // should ahve auto_attach set to FALSE so tha their values can be
     // should ahve auto_attach set to FALSE so tha their values can be
     // attached asyncronously.
     // attached asyncronously.
     'auto_attach' => TRUE,
     'auto_attach' => TRUE,
+    // Set to TRUE to turn on paging capabilities.  Implementations of
+    // a new field should honor this setting when loading results and
+    // when formatting for dipslay.
   );
   );
 
 
   // The default widget for this field.
   // The default widget for this field.
@@ -87,6 +90,7 @@ class TripalField {
   // An array containing details about the field. The format of this array
   // An array containing details about the field. The format of this array
   // is the same as that returned by field_info_fields()
   // is the same as that returned by field_info_fields()
   protected $field;
   protected $field;
+
   // An array containing details about an instance of the field. A field does
   // An array containing details about an instance of the field. A field does
   // not have to have an instance.  But if dealing with an instance (such as
   // not have to have an instance.  But if dealing with an instance (such as
   // when using the widgetForm, formatterSettingsForm, etc.) it should be set.
   // when using the widgetForm, formatterSettingsForm, etc.) it should be set.
@@ -166,6 +170,15 @@ class TripalField {
   public function getInstance() {
   public function getInstance() {
     return $this->instance;
     return $this->instance;
   }
   }
+
+  /**
+   * When constructing a pager for use by a field, all pagers must have
+   * a unique ID
+   */
+  protected function getPagerElementID() {
+    return $this->field['id'];
+  }
+
   // --------------------------------------------------------------------------
   // --------------------------------------------------------------------------
   //                            OVERRIDEABLE FUNCTIONS
   //                            OVERRIDEABLE FUNCTIONS
   // --------------------------------------------------------------------------
   // --------------------------------------------------------------------------
@@ -234,7 +247,6 @@ class TripalField {
 
 
   }
   }
 
 
-
   /**
   /**
    * Provides a form for the 'Field Settings' of the field management page.
    * Provides a form for the 'Field Settings' of the field management page.
    *
    *

+ 31 - 0
tripal/includes/TripalFields/TripalFieldFormatter.inc

@@ -150,5 +150,36 @@ class TripalFieldFormatter {
 
 
   }
   }
 
 
+  /**
+   * When constructing a pager for use by a field, all pagers must have
+   * a unique ID
+   */
+  protected function getPagerElementID() {
+    return $this->field['id'];
+  }
+
+  /**
+   * Updates a pager generated by theme('pager') for use with AJAX.
+   *
+   * Because fields are meant to be updated by AJAX we need clicks in the pager
+   * to not reload the entire page but rather to only reload the field.
+   * Therefore the links in the pager must be adjusted to support this.
+   *
+   * @param $pager
+   *   The pager as created by theme('pager')
+   * @param $entity
+   *   The entity object.
+   */
+  protected function ajaxifyPager($pager, $entity) {
+
+    global $base_path;
+
+    $tmp_base_path = preg_replace('/\//', '\/', $base_path);
+    $field_id = 'tripal-entity-' . $entity->id . '--' . $this->field['field_name'];
+    $pager = preg_replace('/href="' . $tmp_base_path . 'bio_data\/ajax\/field_attach\/' . $field_id . '\?page=(.+?)"/', 'href="javascript:void(0)" onclick="tripal_navigate_field_pager(\'' . $field_id . '\', $1)"', $pager);
+    $pager = preg_replace('/href="' . $tmp_base_path . 'bio_data\/ajax\/field_attach\/' . $field_id . '"/', 'href="javascript:void(0)" onclick="tripal_navigate_field_pager(\'' . $field_id . '\', 0)"', $pager);
+
+    return $pager;
+  }
 
 
 }
 }

+ 6 - 1
tripal_chado/includes/TripalFields/sbo__relationship/sbo__relationship.inc

@@ -80,7 +80,7 @@ class sbo__relationship extends ChadoField {
     $schema = chado_get_schema($field_table);
     $schema = chado_get_schema($field_table);
     $pkey = $schema['primary key'][0];
     $pkey = $schema['primary key'][0];
 
 
-    // Get the Pkeys for the subject and object tables
+    // Get the foreign keys for the subject and object tables
     $subject_fkey_table = '';
     $subject_fkey_table = '';
     $object_fkey_table = '';
     $object_fkey_table = '';
     $fkeys = $schema['foreign keys'];
     $fkeys = $schema['foreign keys'];
@@ -175,6 +175,11 @@ class sbo__relationship extends ChadoField {
     if (array_key_exists('rank', $schema['fields'])) {
     if (array_key_exists('rank', $schema['fields'])) {
       $options['order_by'] = array('rank' => 'ASC');
       $options['order_by'] = array('rank' => 'ASC');
     }
     }
+    // We want a pager for relationships.
+    $options['pager'] = array(
+      'limit' => 25,
+      'element' => $this->getPagerElementID(),
+    );
     $record = chado_expand_var($record, 'table', $rel_table, $options);
     $record = chado_expand_var($record, 'table', $rel_table, $options);
     if (!$record->$rel_table) {
     if (!$record->$rel_table) {
       return;
       return;

+ 23 - 30
tripal_chado/includes/TripalFields/sbo__relationship/sbo__relationship_formatter.inc

@@ -52,13 +52,14 @@ class sbo__relationship_formatter extends ChadoFieldFormatter {
    * @see TripalFieldFormatter::view()
    * @see TripalFieldFormatter::view()
    */
    */
   public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
   public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
+
     // Get the settings
     // Get the settings
     $settings = $display['settings'];
     $settings = $display['settings'];
     $rows = array();
     $rows = array();
     $headers = array($settings['title']);
     $headers = array($settings['title']);
 
 
     foreach ($items as $delta => $item) {
     foreach ($items as $delta => $item) {
-      if (!$item['value']) {
+      if (empty($item['value'])) {
         continue;
         continue;
       }
       }
       $subject_name = $item['value']['local:relationship_subject']['schema:name'];
       $subject_name = $item['value']['local:relationship_subject']['schema:name'];
@@ -70,7 +71,8 @@ class sbo__relationship_formatter extends ChadoFieldFormatter {
       // Handle some special cases.
       // Handle some special cases.
       // For mRNA objects we don't want to show the CDS, exons, 5' UTR, etc.
       // For mRNA objects we don't want to show the CDS, exons, 5' UTR, etc.
       // we want to show the parent gene and the protein.
       // we want to show the parent gene and the protein.
-      if ($object_type == 'mRNA' and (in_array($subject_type, array('CDS', 'exon', 'five_prime_UTR', 'three_prime_UTR')))) {
+      if ($object_type == 'mRNA' and
+          (in_array($subject_type, array('CDS', 'exon', 'five_prime_UTR', 'three_prime_UTR')))) {
         continue;
         continue;
       }
       }
 
 
@@ -92,35 +94,26 @@ class sbo__relationship_formatter extends ChadoFieldFormatter {
         }
         }
       }
       }
 
 
-      $rows[][] = array('data' => $phrase, 'class' => array('tripal-entity-unattached field-items'));
+      $rows[][] = array(
+        'data' => $phrase,
+        'class' => array('tripal-entity-unattached field-items')
+      );
     }
     }
 
 
     //$pager = $this->generate_pager(count($rows), $per_page);
     //$pager = $this->generate_pager(count($rows), $per_page);
-
-    $per_page = 10;
-    // Initialize the pager
-    $current_page = pager_default_initialize(count($rows), $per_page);
-    // Split your list into page sized chunks
-    $chunks = array_chunk($rows, $per_page, TRUE);
-    //format pager
-    $pager = theme('pager', array('quantity', count($rows)));
-
-
-    global $base_path;
-    $tmp_base_path = preg_replace('/\//', '\/', $base_path);
-    $field_id = 'tripal-entity-' . $entity->id . '--' . $this->field['field_name'];
-    $pager = preg_replace('/href="' . $tmp_base_path . 'bio_data\/ajax\/field_attach\/' . $field_id . '\?page=(.+?)"/', 'href="javascript:void(0)" onclick="tripal_navigate_field_pager(\'' . $field_id . '\', $1)"', $pager);
-    $pager = preg_replace('/href="' . $tmp_base_path . 'bio_data\/ajax\/field_attach\/' . $field_id . '"/', 'href="javascript:void(0)" onclick="tripal_navigate_field_pager(\'' . $field_id . '\', 0)"', $pager);
-
-    //$pager = preg_replace("/href=\"" . $tmp_base_path . "gensas\/load_job_view_panel\/" . $job_id . "\/\d\"/", 'href="javascript:void(0)" onclick="gensas.show_job_view_panel(\'' . $job_id . '\', \'' . $job->getName() . '\')"', $pager);
-    // the $table array contains the headers and rows array as well as other
-    // options for controlling the display of the table.  Additional
-    // documentation can be found here:
-    // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
-
-    $table = array(
+    // Get the total number of records from the pager query.
+    $total_records = chado_pager_get_count($this->getPagerElementID());
+    $pager = theme('pager', array(
+      'tags' => array(),
+      'element' => $this->getPagerElementID(),
+      'parameters' => array(),
+      'quantity' => ($total_records / 25) + 1,
+    ));
+    $pager = $this->ajaxifyPager($pager, $entity);
+
+    $table = theme_table(array(
       'header' => $headers,
       'header' => $headers,
-      'rows' => $current_page ? $chunks[$current_page] : array(),
+      'rows' => $rows, //$current_page ? $chunks[$current_page] : array(),
       'attributes' => array(
       'attributes' => array(
         'id' => 'sbo--relationship-table',
         'id' => 'sbo--relationship-table',
       ),
       ),
@@ -128,14 +121,14 @@ class sbo__relationship_formatter extends ChadoFieldFormatter {
       'caption' => '',
       'caption' => '',
       'colgroups' => array(),
       'colgroups' => array(),
       'empty' => $settings['empty'],
       'empty' => $settings['empty'],
-    );
+    ));
 
 
-    // once we have our table array structure defined, we call Drupal's theme_table()
+    // Once we have our table array structure defined, we call Drupal's theme_table()
     // function to generate the table.
     // function to generate the table.
     if (count($items) > 0) {
     if (count($items) > 0) {
       $element[0] = array(
       $element[0] = array(
         '#type' => 'markup',
         '#type' => 'markup',
-        '#markup' => theme_table($table),
+        '#markup' => $table . $pager,
         '#suffix' => '<img src=\'/sites/all/modules/tripal-7.x-3.x/tripal/theme/images/ajax-loader.gif\' id=\'spinner\'/>' . $pager,
         '#suffix' => '<img src=\'/sites/all/modules/tripal-7.x-3.x/tripal/theme/images/ajax-loader.gif\' id=\'spinner\'/>' . $pager,
       );
       );
     }
     }