chado_linker__contact.inc 7.5 KB

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