chado_linker__contact.inc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. class chado_linker__contact extends TripalField {
  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 = 'Contacts';
  12. // The default description for this field.
  13. public static $description = 'Associates an indviddual or organization with
  14. this record';
  15. // Provide a list of global settings. These can be accessed witihn the
  16. // globalSettingsForm. When the globalSettingsForm is submitted then
  17. // Drupal will automatically change these settings for all fields.
  18. public static $default_settings = array(
  19. 'chado_table' => '',
  20. 'chado_column' => '',
  21. 'base_table' => '',
  22. );
  23. // Provide a list of instance specific settings. These can be access within
  24. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  25. // then Drupal with automatically change these settings for the instnace.
  26. // It is recommended to put settings at the instance level whenever possible.
  27. // If you override this variable in a child class be sure to replicate the
  28. // term_name, term_vocab, term_accession and term_fixed keys as these are
  29. // required for all TripalFields.
  30. public static $default_instance_settings = array(
  31. // The short name for the vocabulary (e.g. shcema, SO, GO, PATO, etc.).
  32. 'term_vocabulary' => 'local',
  33. // The name of the term.
  34. 'term_name' => 'contact',
  35. // The unique ID (i.e. accession) of the term.
  36. 'term_accession' => 'contact',
  37. // Set to TRUE if the site admin is allowed to change the term
  38. // type. This will create form elements when editing the field instance
  39. // to allow the site admin to change the term settings above.
  40. 'term_fixed' => TRUE,
  41. );
  42. // Set this to the name of the storage backend that by default will support
  43. // this field.
  44. public static $storage = 'field_chado_storage';
  45. // The default widget for this field.
  46. public static $default_widget = 'chado_linker__contact_widget';
  47. // The default formatter for this field.
  48. public static $default_formatter = 'chado_linker__contact_formatter';
  49. // --------------------------------------------------------------------------
  50. // PROTECTED CLASS MEMBERS -- DO NOT OVERRIDE
  51. // --------------------------------------------------------------------------
  52. // An array containing details about the field. The format of this array
  53. // is the same as that returned by field_info_fields()
  54. protected $field;
  55. // An array containing details about an instance of the field. A field does
  56. // not have to have an instance. But if dealing with an instance (such as
  57. // when using the widgetForm, formatterSettingsForm, etc.) it should be set.
  58. protected $instance;
  59. /**
  60. *
  61. * @see TripalField::validate()
  62. */
  63. public function validate($entity_type, $entity, $field, $items, &$errors) {
  64. }
  65. /**
  66. *
  67. * @see TripalField::load()
  68. */
  69. public function load($entity, $details = array()) {
  70. $record = $details['record'];
  71. $field_name = $this->field['field_name'];
  72. $field_type = $this->field['type'];
  73. $field_table = $this->field['settings']['chado_table'];
  74. $field_column = $this->field['settings']['chado_column'];
  75. // Get the FK that links to the base record.
  76. $schema = chado_get_schema($field_table);
  77. $base_table = $details['record']->tablename;
  78. $pkey = $schema['primary key'][0];
  79. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  80. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  81. // Set some defaults for the empty record.
  82. $entity->{$field_name}['und'][0] = array(
  83. 'value' => array(),
  84. 'chado-' . $field_table . '__' . $pkey => '',
  85. 'chado-' . $field_table . '__' . $fkey_lcolumn => '',
  86. 'chado-' . $field_table . '__' . 'contact_id' => '',
  87. // Ignore the synonym_sgml column for now.
  88. );
  89. $linker_table = $base_table . '_contact';
  90. $options = array(
  91. 'return_array' => 1,
  92. 'include_fk' => array(
  93. 'contact_id' => array(
  94. 'type_id' => array(
  95. 'dbxref_id' => array(
  96. 'db_id' => TRUE,
  97. ),
  98. ),
  99. ),
  100. $fkey_lcolumn => TRUE,
  101. ),
  102. );
  103. $record = chado_expand_var($record, 'table', $linker_table, $options);
  104. $contact_linkers = $record->$linker_table;
  105. if ($contact_linkers) {
  106. foreach ($contact_linkers as $i => $contact_linker) {
  107. $contact = $contact_linker->contact_id;
  108. $entity->{$field_name}['und'][$i] = array(
  109. 'value' => array(
  110. 'type' => $contact->type_id ? $contact->type_id->name : '',
  111. 'name' => $contact->name,
  112. 'description' => $contact->description,
  113. ),
  114. // Add in the semantic web settings. This array is expected by
  115. // other Tripal modules that handle semantic web for fields.
  116. 'semantic_web' => array(
  117. 'type' => $contact->type_id ? $contact->type_id->dbxref_id->db_id->name . ':' . $contact->type_id->dbxref_id->accession : '',
  118. 'name' => tripal_get_chado_semweb_term('contact', 'name'),
  119. 'description' => tripal_get_chado_semweb_term('contact', 'description'),
  120. ),
  121. // Add in subfield mapping to Chado tables. This is used by the
  122. // chado_field_storage for performing queries on sub element values.
  123. // It should be a comma-separated list (no spacing) of the field names
  124. // as foreign keys are followed starting from the Chado table to which
  125. // this field maps.
  126. 'chado_mapping' => array(
  127. 'type' => 'type_id,name',
  128. 'name' => 'contact_id,name',
  129. 'description' => 'contact_id,name'
  130. ),
  131. 'chado-' . $field_table . '__' . $pkey => $contact_linker->$pkey,
  132. 'chado-' . $field_table . '__' . $fkey_lcolumn => $contact_linker->$fkey_lcolumn->$fkey_lcolumn,
  133. 'chado-' . $field_table . '__' . 'contact_id' => $contact->contact_id
  134. );
  135. if (property_exists($contact, 'entity_id')) {
  136. $entity->{$field_name}['und'][$i]['value']['entity'] = 'TripalEntity:' . $contact->entity_id;
  137. }
  138. }
  139. }
  140. }
  141. }
  142. /**
  143. * An Ajax callback for the pub widget.
  144. */
  145. function chado_linker__contact_widget_form_ajax_callback($form, $form_state) {
  146. $field_name = $form_state['triggering_element']['#parents'][0];
  147. $delta = $form_state['triggering_element']['#parents'][2];
  148. return $form[$field_name]['und'][$delta];
  149. }
  150. /**
  151. * Theme function for the pub widget.
  152. *
  153. * @param $variables
  154. */
  155. function theme_chado_linker__contact_widget($variables) {
  156. $element = $variables['element'];
  157. // These two fields were added to the widget to help identify the fields
  158. // for layout.
  159. $table_name = $element['#table_name'];
  160. $fkey = $element['#fkey_field'];
  161. $layout = "
  162. <div class=\"pub-widget\">
  163. <div class=\"pub-widget-item\">" .
  164. drupal_render($element['name']) . "
  165. </div>
  166. </div>
  167. ";
  168. return $layout;
  169. }