chado_linker__prop.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. class chado_linker__prop extends TripalField {
  3. // The default lable for this field.
  4. public static $default_label = 'Property';
  5. // The default description for this field.
  6. public static $default_description = 'Add details about this property.';
  7. // Add any default settings elements. If you override the globalSettingsForm()
  8. // or the instanceSettingsForm() functions then you need to be sure that
  9. // any settings you want those functions to manage are listed in this
  10. // array.
  11. public static $default_settings = array(
  12. 'chado_table' => '',
  13. 'chado_column' => '',
  14. 'base_table' => '',
  15. );
  16. // Set this to the name of the storage backend that by default will support
  17. // this field.
  18. public static $default_storage = 'field_chado_storage';
  19. /**
  20. * @see TripalField::formatterView()
  21. */
  22. public function formatterView(&$element, $entity_type, $entity, $langcode, $items, $display) {
  23. $field_name = $this->field['field_name'];
  24. $chado_table = $this->instance['settings']['chado_table'];
  25. $properties = array();
  26. foreach ($items as $delta => $item) {
  27. $properties[] = $item['chado-' . $chado_table . '__value'];
  28. }
  29. $content = implode(', ', $properties);
  30. if (count($items) > 0) {
  31. $element[0] = array(
  32. '#type' => 'markup',
  33. '#markup' => $content,
  34. );
  35. }
  36. }
  37. /**
  38. * @see TripalField::widgetForm()
  39. */
  40. public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  41. parent::widgetForm($widget, $form, $form_state, $langcode, $items, $delta, $element);
  42. $field_name = $this->field['field_name'];
  43. $field_type = $this->field['type'];
  44. $field_table = $this->instance['settings']['chado_table'];
  45. $field_column = $this->instance['settings']['chado_column'];
  46. $instance = $this->instance;
  47. // Get the table name and cvterm that this field maps to.
  48. $matches = array();
  49. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  50. // If the field name is not properly formatted then we can't tell what
  51. // table and type this is. So just return.
  52. if (count($matches) != 3) {
  53. return $widget;
  54. }
  55. $table_name = $matches[1];
  56. $cvterm_id = $matches[2];
  57. // Get the name of the pkey field for this property table and the name
  58. // of the FK field that links to the base table.
  59. $schema = chado_get_schema($table_name);
  60. $pkey = $schema['primary key'][0];
  61. $base_table = $this->instance['settings']['base_table'];
  62. $lfkey_field = key($schema['foreign keys'][$base_table]['columns']);
  63. $rfkey_field = $schema['foreign keys'][$base_table]['columns'][$lfkey_field];
  64. // Get the field defaults.
  65. $fk_value =key_exists(0, $items) ? $items[0]['chado-' . $field_table . '__' . $lfkey_field] : '';
  66. $propval = '';
  67. if (array_key_exists($delta, $items)) {
  68. $propval = tripal_get_field_item_keyval($items, $delta, 'chado-' . $table_name . '__value', $propval);
  69. }
  70. $widget['value'] = array(
  71. '#type' => 'value',
  72. '#value' => key_exists($delta, $items) ? $items[$delta]['value'] : '',
  73. );
  74. $widget['chado-' . $table_name . '__' . $pkey] = array(
  75. '#type' => 'hidden',
  76. '#default_value' => !empty($items[$delta]['chado-' . $field_table . '__' . $pkey]) ? $items[$delta]['chado-' . $field_table . '__' . $pkey] : '',
  77. );
  78. $widget['chado-' . $table_name . '__' . $lfkey_field] = array(
  79. '#type' => 'hidden',
  80. '#value' => $fk_value,
  81. );
  82. $widget['chado-' . $table_name . '__value'] = array(
  83. '#type' => 'textfield',
  84. '#default_value' => $propval,
  85. '#title' => $instance['label'] . ' value',
  86. '#description' => $instance['description'],
  87. );
  88. $widget['chado-' . $table_name . '__type_id'] = array(
  89. '#type' => 'hidden',
  90. '#value' => $cvterm_id,
  91. );
  92. $widget['chado-' . $table_name . '__rank'] = array(
  93. '#type' => 'hidden',
  94. '#value' => $delta,
  95. );
  96. }
  97. /**
  98. * @see TripalField::validate()
  99. */
  100. public function validate($entity_type, $entity, $field, $items, &$errors) {
  101. }
  102. /**
  103. * @see TripalField::widgetFormSubmit()
  104. */
  105. public function widgetFormSubmit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  106. $field_name = $this->field['field_name'];
  107. $table_name = $this->instance['settings']['chado_table'];
  108. $schema = chado_get_schema($table_name);
  109. $pkey = $schema['primary key'][0];
  110. $base_table = $this->instance['settings']['base_table'];
  111. $lfkey_field = key($schema['foreign keys'][$base_table]['columns']);
  112. $rfkey_field = $schema['foreign keys'][$base_table]['columns'][$lfkey_field];
  113. $prop_value = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__value']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__value'] : '';
  114. // If the user removed the contact from the contact_name field
  115. // then we want to clear out the rest of the hidden values.
  116. // Leave the primary key so the record can be deleted.
  117. if (!$prop_value) {
  118. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $pkey] = '';
  119. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__rank'] = '';
  120. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__type_id'] = '';
  121. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $lfkey_field] = '';
  122. }
  123. }
  124. /**
  125. * @see TripalField::load()
  126. */
  127. public function load($entity, $details = array()) {
  128. $field_name = $this->field['field_name'];
  129. $field_type = $this->field['type'];
  130. $field_table = $this->instance['settings']['chado_table'];
  131. $field_column = $this->instance['settings']['chado_column'];
  132. $base_table = $this->instance['settings']['base_table'];
  133. $matches = array();
  134. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  135. $table_name = $matches[1];
  136. $cvterm_id = $matches[2];
  137. // Get the FK that links to the base record.
  138. $schema = chado_get_schema($field_table);
  139. $pkey = $schema['primary key'][0];
  140. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  141. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  142. // Set some defaults for the empty record.
  143. $chado_record = $entity->chado_record;
  144. $entity->{$field_name}['und'][0] = array(
  145. 'value' => '',
  146. 'chado-' . $field_table . '__' . $pkey => '',
  147. 'chado-' . $field_table . '__' . $fkey_lcolumn => $chado_record->{$fkey_lcolumn},
  148. 'chado-' . $field_table . '__value' => '',
  149. 'chado-' . $field_table . '__type_id' => '',
  150. 'chado-' . $field_table . '__rank' => '',
  151. );
  152. // Get the properties associated with this base record for this fields
  153. // given type.
  154. $columns = array('*');
  155. $match = array(
  156. $fkey_lcolumn => $chado_record->{$fkey_lcolumn},
  157. 'type_id' => $cvterm_id,
  158. );
  159. $options = array(
  160. 'return_array' => TRUE,
  161. 'order_by' => array('rank' => 'ASC')
  162. );
  163. $properties = chado_select_record($field_table, $columns, $match, $options);
  164. for ($i = 0; $i < count($properties); $i++) {
  165. $property = $properties[$i];
  166. foreach ($schema['fields'] as $fname => $details) {
  167. $entity->{$field_name}['und'][$i] = array(
  168. 'value' => array(),
  169. 'chado-' . $field_table . '__' . $pkey => $property->$pkey,
  170. 'chado-' . $field_table . '__' . $fkey_lcolumn => $property->$fkey_lcolumn,
  171. 'chado-' . $field_table . '__value' => $property->value,
  172. 'chado-' . $field_table . '__type_id' => $property->type_id,
  173. 'chado-' . $field_table . '__rank' => $property->rank,
  174. );
  175. }
  176. }
  177. }
  178. }