chado_linker__contact.inc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. * @see TripalField::elements()
  50. */
  51. public function elementInfo() {
  52. $field_term = $this->getFieldTermID();
  53. $type_term = tripal_get_chado_semweb_term('contact', 'type_id');
  54. $name_term = tripal_get_chado_semweb_term('contact', 'name');
  55. $description_term = tripal_get_chado_semweb_term('contact', 'description');
  56. return array(
  57. $field_term => array(
  58. 'operations' => array('eq', 'contains', 'starts'),
  59. 'sortable' => TRUE,
  60. 'searchable' => TRUE,
  61. 'type' => 'string',
  62. 'elements' => array(
  63. $type_term => array(
  64. 'searchable' => TRUE,
  65. 'name' => 'type',
  66. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  67. 'sortable' => TRUE,
  68. ),
  69. $name_term => array(
  70. 'searchable' => TRUE,
  71. 'name' => 'name',
  72. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  73. 'sortable' => TRUE,
  74. ),
  75. $description_term => array(
  76. 'searchable' => TRUE,
  77. 'name' => 'description',
  78. 'operations' => array('contains'),
  79. 'sortable' => FALSE,
  80. ),
  81. 'entity' => array(
  82. 'searchable' => FALSE,
  83. ),
  84. ),
  85. )
  86. );
  87. }
  88. /**
  89. *
  90. * @see TripalField::load()
  91. */
  92. public function load($entity) {
  93. $record = $entity->chado_record;
  94. $field_name = $this->field['field_name'];
  95. $field_type = $this->field['type'];
  96. $field_table = $this->instance['settings']['chado_table'];
  97. $field_column = $this->instance['settings']['chado_column'];
  98. $type_term = tripal_get_chado_semweb_term('contact', 'type');
  99. $name_term = tripal_get_chado_semweb_term('contact', 'name');
  100. $description_term = tripal_get_chado_semweb_term('contact', 'description');
  101. // Get the FK that links to the base record.
  102. $schema = chado_get_schema($field_table);
  103. $base_table = $entity->chado_table;
  104. $pkey = $schema['primary key'][0];
  105. if (!isset($schema['foreign keys'][$base_table]['columns'])) {
  106. return;
  107. }
  108. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  109. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  110. // Set some defaults for the empty record.
  111. $entity->{$field_name}['und'][0] = array(
  112. 'value' => array(),
  113. 'chado-' . $field_table . '__' . $pkey => '',
  114. 'chado-' . $field_table . '__' . $fkey_lcolumn => '',
  115. 'chado-' . $field_table . '__' . 'contact_id' => '',
  116. // Ignore the synonym_sgml column for now.
  117. );
  118. $linker_table = $base_table . '_contact';
  119. $options = array(
  120. 'return_array' => 1,
  121. 'include_fk' => array(
  122. 'contact_id' => array(
  123. 'type_id' => array(
  124. 'dbxref_id' => array(
  125. 'db_id' => TRUE,
  126. ),
  127. ),
  128. ),
  129. $fkey_lcolumn => TRUE,
  130. ),
  131. );
  132. $record = chado_expand_var($record, 'table', $linker_table, $options);
  133. $contact_linkers = $record->$linker_table;
  134. if ($contact_linkers) {
  135. foreach ($contact_linkers as $i => $contact_linker) {
  136. $contact = $contact_linker->contact_id;
  137. $entity->{$field_name}['und'][$i] = array(
  138. 'value' => array(
  139. $type_term => $contact->type_id ? $contact->type_id->name : '',
  140. $name_term => $contact->name,
  141. $description_term => $contact->description,
  142. ),
  143. // Add elements that are not part of the values but used by
  144. // the chado storage back-end for saving values.
  145. 'chado-' . $field_table . '__' . $pkey => $contact_linker->$pkey,
  146. 'chado-' . $field_table . '__' . $fkey_lcolumn => $contact_linker->$fkey_lcolumn->$fkey_lcolumn,
  147. 'chado-' . $field_table . '__' . 'contact_id' => $contact->contact_id
  148. );
  149. if (property_exists($contact, 'entity_id')) {
  150. $entity->{$field_name}['und'][$i]['value']['entity'] = 'TripalEntity:' . $contact->entity_id;
  151. }
  152. }
  153. }
  154. }
  155. /**
  156. * @see ChadoField::query()
  157. */
  158. public function query($query, $condition) {
  159. $contact_linker = $this->instance['settings']['chado_table'];
  160. $base_table = $this->instance['settings']['base_table'];
  161. $bschema = chado_get_schema($base_table);
  162. $bpkey = $bschema['primary key'][0];
  163. $alias = 'contact_linker';
  164. $operator = $condition['operator'];
  165. $type_term = tripal_get_chado_semweb_term('contact', 'type');
  166. $name_term = tripal_get_chado_semweb_term('contact', 'name');
  167. $description_term = tripal_get_chado_semweb_term('contact', 'description');
  168. if ($condition['column'] == $name_term) {
  169. $query->join($contact_linker, $alias, "base.$bpkey = $alias.$bpkey");
  170. $query->join('contact', 'C', "C.contact_id = $alias.contact_id");
  171. $query->condition("C.name", $condition['value'], $operator);
  172. }
  173. if ($condition['column'] == 'contact.type') {
  174. }
  175. }
  176. }
  177. /**
  178. * An Ajax callback for the pub widget.
  179. */
  180. function chado_linker__contact_widget_form_ajax_callback($form, $form_state) {
  181. $field_name = $form_state['triggering_element']['#parents'][0];
  182. $delta = $form_state['triggering_element']['#parents'][2];
  183. return $form[$field_name]['und'][$delta];
  184. }
  185. /**
  186. * Theme function for the pub widget.
  187. *
  188. * @param $variables
  189. */
  190. function theme_chado_linker__contact_widget($variables) {
  191. $element = $variables['element'];
  192. // These two fields were added to the widget to help identify the fields
  193. // for layout.
  194. $table_name = $element['#table_name'];
  195. $fkey = $element['#fkey_field'];
  196. $layout = "
  197. <div class=\"pub-widget\">
  198. <div class=\"pub-widget-item\">" .
  199. drupal_render($element['name']) . "
  200. </div>
  201. </div>
  202. ";
  203. return $layout;
  204. }