chado_linker__contact.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. class chado_linker__contact extends TripalField {
  3. public function field_info() {
  4. return array(
  5. 'label' => t('Contacts'),
  6. 'description' => t('Associates an indviddual or organization with
  7. this record.'),
  8. 'default_widget' => 'chado_linker__contact_widget',
  9. 'default_formatter' => 'chado_linker__contact_formatter',
  10. 'settings' => array(),
  11. 'storage' => array(
  12. 'type' => 'field_chado_storage',
  13. 'module' => 'tripal_chado',
  14. 'active' => TRUE
  15. ),
  16. );
  17. }
  18. function widget_info() {
  19. return array(
  20. 'label' => t('Contacts'),
  21. 'field types' => array('chado_linker__contact'),
  22. );
  23. }
  24. public function formatter_info() {
  25. return array(
  26. 'label' => t('Contacts'),
  27. 'field types' => array('chado_linker__contact'),
  28. 'settings' => array(
  29. ),
  30. );
  31. }
  32. public function attach_info($entity_type, $bundle, $settings) {
  33. $field_info = array();
  34. $table_name = $settings['data_table'];
  35. $type_table = $settings['type_table'];
  36. $type_field = $settings['field'];
  37. $cv_id = $settings['cv_id'];
  38. $cvterm_id = $settings['cvterm_id'];
  39. // If the linker table does not exists then we don't want to add attach.
  40. $contact_table = $table_name . '_contact';
  41. if (!chado_table_exists($contact_table)) {
  42. return $field_info;
  43. }
  44. $schema = chado_get_schema($contact_table);
  45. $pkey = $schema['primary key'][0];
  46. // Initialize the field array.
  47. $field_info = array(
  48. 'field_name' => $table_name . '__contact',
  49. 'field_type' => 'chado_linker__contact',
  50. 'widget_type' => 'chado_linker__contact_widget',
  51. 'widget_settings' => array('display_label' => 1),
  52. 'description' => '',
  53. 'label' => 'Contacts',
  54. 'is_required' => 0,
  55. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  56. 'storage' => 'field_chado_storage',
  57. 'field_settings' => array(
  58. 'chado_table' => $contact_table,
  59. 'chado_column' => $pkey,
  60. 'base_table' => $table_name,
  61. 'semantic_web' => array(
  62. 'name' => 'contact',
  63. 'accession' => 'contact',
  64. 'ns' => 'local',
  65. 'nsurl' => '',
  66. ),
  67. ),
  68. );
  69. return $field_info;
  70. }
  71. public function formatter_settings_summary($field, $instance, $view_mode) {
  72. }
  73. public function formatter_settings_form($field, $instance,
  74. $view_mode, $form, &$form_state) {
  75. }
  76. public function formatter_view(&$element, $entity_type, $entity,
  77. $field, $instance, $langcode, $items, $display) {
  78. }
  79. public function widget_form(&$widget, $form, $form_state, $field, $instance,
  80. $langcode, $items, $delta, $element) {
  81. }
  82. public function load($field, $entity, $record) {
  83. $field_name = $field['field_name'];
  84. $field_type = $field['type'];
  85. $field_table = $field['settings']['chado_table'];
  86. $field_column = $field['settings']['chado_column'];
  87. // Get the FK that links to the base record.
  88. $schema = chado_get_schema($field_table);
  89. $base_table = $record['record']->tablename;
  90. $pkey = $schema['primary key'][0];
  91. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  92. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  93. // Set some defaults for the empty record.
  94. $entity->{$field_name}['und'][0] = array(
  95. 'value' => '',
  96. $field_table . '__' . $fkey_lcolumn => '',
  97. $field_table . '__' . 'contact_id' => '',
  98. $field_table . '--' . 'contact__name' => '',
  99. );
  100. $linker_table = $base_table . '_contact';
  101. $options = array(
  102. 'return_array' => 1,
  103. );
  104. $record = chado_expand_var($record, 'table', $linker_table, $options);
  105. if (count($record->$linker_table) > 0) {
  106. $i = 0;
  107. foreach ($record->$linker_table as $index => $linker) {
  108. $contact = $linker->contact_id;
  109. $entity->{$field_name}['und'][$i] = array(
  110. 'value' => $linker->$pkey,
  111. $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn->$fkey_lcolumn,
  112. $field_table . '__' . 'contact_id' => $contact->contact_id,
  113. $field_table . '--' . 'contact__name' => $contact->name,
  114. );
  115. if (property_exists($entity->chado_record->$field_column, 'entity_id')) {
  116. $fk_entity_id = $entity->chado_record->$field_column->entity_id;
  117. $entity->{$field_name}['und'][$i]['entity_id'] = $fk_entity_id;
  118. $entity->{$field_name}['und'][$i]['entity_type'] = 'TripalEntity';
  119. }
  120. $i++;
  121. }
  122. }
  123. }
  124. /**
  125. * @see TripalField::settings_form()
  126. */
  127. public function settings_form($field, $instance, $has_data) {
  128. $element = array();
  129. // TODO: add settings here.
  130. // Add in the semantic web fields.
  131. $parent_elements = parent::settings_form($field, $instance, $has_data);
  132. $element = array_merge($element, $parent_elements);
  133. return $element;
  134. }
  135. }