sbo__relationship_formatter.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. class sbo__relationship_formatter extends ChadoFieldFormatter {
  3. // The default lable for this field.
  4. public static $default_label = 'Relationship';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array('sbo__relationship');
  7. public static $default_settings = array(
  8. 'title' => 'Relationship',
  9. 'empty' => 'There are no relationships',
  10. );
  11. /**
  12. *
  13. * @see TripalFieldFormatter::settingsForm()
  14. */
  15. public function settingsForm($view_mode, $form, &$form_state) {
  16. $display = $this->instance['display'][$view_mode];
  17. $settings = $display['settings'];
  18. $element = array();
  19. $element['title'] = array(
  20. '#type' => 'textfield',
  21. '#title' => 'Table Header',
  22. '#default_value' => array_key_exists('title', $settings) ? $settings['title'] : 'Relationship',
  23. );
  24. $element['empty'] = array(
  25. '#type' => 'textfield',
  26. '#title' => 'Empty text',
  27. '#default_value' => array_key_exists('empty', $settings) ? $settings['empty'] : 'There are no relationships',
  28. );
  29. return $element;
  30. }
  31. /**
  32. * @see TripalFieldFormatter::settingsSummary()
  33. */
  34. public function settingsSummary($view_mode) {
  35. $display = $this->instance['display'][$view_mode];
  36. $settings = $display['settings'];
  37. $summary = t('Title: @title<br>Empty: @empty', array('@title' => $settings['title'], '@empty' => $settings['empty']));
  38. return $summary;
  39. }
  40. /**
  41. *
  42. * @see TripalFieldFormatter::view()
  43. */
  44. public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
  45. // Get the settings
  46. $settings = $display['settings'];
  47. $rows = array();
  48. $headers = array($settings['title']);
  49. foreach ($items as $delta => $item) {
  50. if (!$item['value']) {
  51. continue;
  52. }
  53. $subject_name = $item['value']['local:relationship_subject']['schema:name'];
  54. $subject_type = $item['value']['local:relationship_subject']['rdfs:type'];
  55. $object_name = $item['value']['local:relationship_object']['schema:name'];
  56. $object_type = $item['value']['local:relationship_object']['rdfs:type'];
  57. $phrase = $item['value']['SIO:000493'];
  58. // Handle some special cases.
  59. // For mRNA objects we don't want to show the CDS, exons, 5' UTR, etc.
  60. // we want to show the parent gene and the protein.
  61. if ($object_type == 'mRNA' and (in_array($subject_type, array('CDS', 'exon', 'five_prime_UTR', 'three_prime_UTR')))) {
  62. continue;
  63. }
  64. $phrase = preg_replace("/$subject_type/", "<b>$subject_type</b>", $phrase);
  65. $phrase = preg_replace("/$object_type/", "<b>$object_type</b>", $phrase);
  66. if (array_key_exists('entity', $item['value']['local:relationship_object'])) {
  67. list($entity_type, $object_entity_id) = explode(':', $item['value']['local:relationship_object']['entity']);
  68. if ($object_entity_id != $entity->id) {
  69. $link = l($object_name, 'bio_data/' . $object_entity_id);
  70. $phrase = preg_replace("/$object_name/", $link, $phrase);
  71. }
  72. }
  73. if (array_key_exists('entity', $item['value']['local:relationship_subject'])) {
  74. list($entity_type, $subject_entity_id) = explode(':', $item['value']['local:relationship_subject']['entity']);
  75. if ($subject_entity_id != $entity->id) {
  76. $link = l($subject_name, 'bio_data/' . $subject_entity_id);
  77. $phrase = preg_replace("/$subject_name/", $link, $phrase);
  78. }
  79. }
  80. $rows[][] = array('data' => $phrase, 'class' => array('tripal-entity-unattached field-items'));
  81. }
  82. //$pager = $this->generate_pager(count($rows), $per_page);
  83. $per_page = 10;
  84. // Initialize the pager
  85. $current_page = pager_default_initialize(count($rows), $per_page);
  86. // Split your list into page sized chunks
  87. $chunks = array_chunk($rows, $per_page, TRUE);
  88. //format pager
  89. $pager = theme('pager', array('quantity', count($rows)));
  90. global $base_path;
  91. $tmp_base_path = preg_replace('/\//', '\/', $base_path);
  92. $field_id = 'tripal-entity-' . $entity->id . '--' . $this->field['field_name'];
  93. $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);
  94. $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);
  95. //$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);
  96. // the $table array contains the headers and rows array as well as other
  97. // options for controlling the display of the table. Additional
  98. // documentation can be found here:
  99. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  100. $table = array(
  101. 'header' => $headers,
  102. 'rows' => $current_page ? $chunks[$current_page] : array(),
  103. 'attributes' => array(
  104. 'id' => 'sbo--relationship-table',
  105. ),
  106. 'sticky' => FALSE,
  107. 'caption' => '',
  108. 'colgroups' => array(),
  109. 'empty' => $settings['empty'],
  110. );
  111. // once we have our table array structure defined, we call Drupal's theme_table()
  112. // function to generate the table.
  113. if (count($items) > 0) {
  114. $element[0] = array(
  115. '#type' => 'markup',
  116. '#markup' => theme_table($table),
  117. '#suffix' => '<img src=\'/sites/all/modules/tripal-7.x-3.x/tripal/theme/images/ajax-loader.gif\' id=\'spinner\'/>' . $pager,
  118. );
  119. }
  120. }
  121. }