chado_linker__contact.inc 7.4 KB

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