tripal_pub_libraries.tpl.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. $pub = $variables['node']->pub;
  3. $libraries = [];
  4. // get the libraries that are associated with this publication. But we only
  5. // want 25 and we want a pager to let the user cycle between pages of libraries.
  6. // so we, use the chado_select_record API function to get the results and
  7. // generate the pager. The function is smart enough to know which page the user is
  8. // on and retrieves the proper set of libraries
  9. $element = 3; // an index to specify the pager this must be unique amongst all pub templates
  10. $num_per_page = 25; // the number of libraries to show per page$num_results_per_page = 25;
  11. // get the libraries from the library_pub table
  12. $options = [
  13. 'return_array' => 1,
  14. 'pager' => [
  15. 'limit' => $num_per_page,
  16. 'element' => $element,
  17. ],
  18. ];
  19. $pub = chado_expand_var($pub, 'table', 'library_pub', $options);
  20. $library_pubs = $pub->library_pub;
  21. if (count($library_pubs) > 0) {
  22. foreach ($library_pubs as $library_pub) {
  23. $libraries[] = $library_pub->library_id;
  24. }
  25. }
  26. // get the total number of records
  27. $total_records = chado_pager_get_count($element);
  28. if (count($libraries) > 0) { ?>
  29. <div class="tripal_pub-data-block-desc tripal-data-block-desc">This
  30. publication contains information
  31. about <?php print number_format($total_records) ?> libraries:
  32. </div> <?php
  33. // the $headers array is an array of fields to use as the colum headers.
  34. // additional documentation can be found here
  35. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  36. $headers = ['Library Name', 'Unique Name', 'Organism'];
  37. // the $rows array contains an array of rows where each row is an array
  38. // of values for each column of the table in that row. Additional documentation
  39. // can be found here:
  40. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  41. $rows = [];
  42. foreach ($libraries as $library) {
  43. $library_name = $library->name;
  44. if (property_exists($library, 'nid')) {
  45. $library_name = l($library_name, 'node/' . $library->nid, ['attributes' => ['target' => '_blank']]);
  46. }
  47. $organism = '<i>' . $library->organism_id->genus . ' ' . $library->organism_id->species . '</i>';
  48. if (property_exists($library->organism_id, 'nid')) {
  49. $organism = l($organism, 'node/' . $library->organism_id->nid, ['attributes' => ['target' => '_blank']]);
  50. }
  51. $rows[] = [
  52. $library_name,
  53. $library->uniquename,
  54. $organism,
  55. ];
  56. }
  57. // the $table array contains the headers and rows array as well as other
  58. // options for controlling the display of the table. Additional
  59. // documentation can be found here:
  60. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  61. $table = [
  62. 'header' => $headers,
  63. 'rows' => $rows,
  64. 'attributes' => [
  65. 'id' => 'tripal_pub-table-libraries',
  66. 'class' => 'tripal-data-table',
  67. ],
  68. 'sticky' => FALSE,
  69. 'caption' => '',
  70. 'colgroups' => [],
  71. 'empty' => '',
  72. ];
  73. // once we have our table array structure defined, we call Drupal's theme_table()
  74. // function to generate the table.
  75. print theme_table($table);
  76. // the $pager array values that control the behavior of the pager. For
  77. // documentation on the values allows in this array see:
  78. // https://api.drupal.org/api/drupal/includes!pager.inc/function/theme_pager/7
  79. // here we add the paramter 'block' => 'libraries'. This is because
  80. // the pager is not on the default block that appears. When the user clicks a
  81. // page number we want the browser to re-appear with the page is loaded.
  82. // We remove the 'pane' parameter from the original query parameters because
  83. // Drupal won't reset the parameter if it already exists.
  84. $get = $_GET;
  85. unset($_GET['pane']);
  86. $pager = [
  87. 'tags' => [],
  88. 'element' => $element,
  89. 'parameters' => [
  90. 'pane' => 'libraries',
  91. ],
  92. 'quantity' => $num_per_page,
  93. ];
  94. print theme_pager($pager);
  95. $_GET = $get;
  96. }