local__gbrowse_image.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. class local__gbrowse_image extends ChadoField {
  3. // --------------------------------------------------------------------------
  4. // EDITABLE STATIC CONSTANTS
  5. //
  6. // The following constants SHOULD be set for each descendent class. They are
  7. // used by the static functions to provide information to Drupal about
  8. // the field and it's default widget and formatter.
  9. // --------------------------------------------------------------------------
  10. // The default lable for this field.
  11. public static $default_label = 'GBrowse View';
  12. // The default description for this field.
  13. public static $description = 'A GBrowse image of a feature alignment to another sequence.';
  14. // Provide a list of instance specific settings. These can be access within
  15. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  16. // then Drupal with automatically change these settings for the instnace.
  17. // It is recommended to put settings at the instance level whenever possible.
  18. // If you override this variable in a child class be sure to replicate the
  19. // term_name, term_vocab, term_accession and term_fixed keys as these are
  20. // required for all TripalFields.
  21. public static $default_instance_settings = array(
  22. // The short name for the vocabulary (e.g. shcema, SO, GO, PATO, etc.).
  23. 'term_vocabulary' => 'local',
  24. // The name of the term.
  25. 'term_name' => 'Gbrowse Image',
  26. // The unique ID (i.e. accession) of the term.
  27. 'term_accession' => 'gbrowse_image',
  28. // Set to TRUE if the site admin is allowed to change the term
  29. // type. This will create form elements when editing the field instance
  30. // to allow the site admin to change the term settings above.
  31. 'term_fixed' => FALSE,
  32. // The URL for the GBrowse image
  33. 'gbrowse_url' => '',
  34. );
  35. // The default widget for this field.
  36. public static $default_widget = 'local__gbrowse_image_widget';
  37. // The default formatter for this field.
  38. public static $default_formatter = 'local__gbrowse_image_formatter';
  39. /**
  40. * @see TripalField::elementInfo()
  41. */
  42. public function elementInfo() {
  43. $field_term = $this->getFieldTermID();
  44. return array(
  45. $field_term => array(
  46. 'sortable' => FALSE,
  47. 'searchable' => FALSE,
  48. 'type' => 'xs:string',
  49. 'readonly' => TRUE,
  50. ),
  51. );
  52. }
  53. /**
  54. * @see TripalField::load()
  55. */
  56. public function load($entity) {
  57. $field_name = $this->field['field_name'];
  58. $feature = $entity->chado_record;
  59. $record = $entity->chado_record;
  60. $settings = $this->instance['settings'];
  61. // Set some defauls for the empty record
  62. $entity->{$field_name}['und'][0] = array(
  63. 'value' => '',
  64. );
  65. // If we don't have a record then just return
  66. if (!$record) {
  67. return;
  68. }
  69. // Replace any tokens in the string.
  70. $bundle = tripal_load_bundle_entity(['name' => $this->instance['bundle']]);
  71. $gbrowse_url = $settings['gbrowse_url'];
  72. $gbrowse_url = tripal_replace_entity_tokens($gbrowse_url, $entity, $bundle);
  73. // Use the term associated to the db.url table of Chado.
  74. $url_term = chado_get_semweb_term('db', 'url');
  75. // Add the URL to the values
  76. $entity->{$field_name}['und'][0]['value'] = [
  77. $url_term => $gbrowse_url
  78. ];
  79. }
  80. /**
  81. * @see TripalField::globalSettingsForm()
  82. */
  83. public function instanceSettingsForm() {
  84. $element = parent::instanceSettingsForm();
  85. $settings = $this->instance['settings'];
  86. $element['gbrowse_url'] = array(
  87. '#type' => 'textarea',
  88. '#title' => 'GBrowse URL',
  89. '#description' => t('Please enter the URL for generating feature ' .
  90. 'images on the remote GBrowse site. You may use tokens to substitute ' .
  91. 'field values from this content type. Open the "Available Tokens" fieldset below. ' .
  92. 'For example, to show a view of a gene from the rice genome GBrowse the ' .
  93. 'following URL with tokens can be used: ' .
  94. 'http://rice.plantbiology.msu.edu/cgi-bin/gbrowse_img/rice/?name=[schema__name];type=Landmarks%3Aregion+Genes+Rice_Annotation'),
  95. '#default_value' => $settings['gbrowse_url'],
  96. );
  97. $element['tokens'] = array(
  98. '#type' => 'fieldset',
  99. '#collapsed' => TRUE,
  100. '#collapsible' => TRUE,
  101. '#title' => 'Available Tokens'
  102. );
  103. $headers = array('Token', 'Description');
  104. $rows = array();
  105. // Get the bundle tokens
  106. $bundle = tripal_load_bundle_entity(['name' => $this->instance['bundle']]);
  107. $tokens = tripal_get_entity_tokens($bundle);
  108. foreach ($tokens as $token) {
  109. $rows[] = array(
  110. $token['token'],
  111. $token['description'],
  112. );
  113. }
  114. $table_vars = array(
  115. 'header' => $headers,
  116. 'rows' => $rows,
  117. 'attributes' => array(),
  118. 'sticky' => FALSE,
  119. 'caption' => '',
  120. 'colgroups' => array(),
  121. 'empty' => 'There are no tokens',
  122. );
  123. $element['tokens']['list'] = array(
  124. '#type' => 'item',
  125. '#markup' => theme_table($table_vars),
  126. );
  127. return $element;
  128. }
  129. }