chado_linker__prop.inc 7.6 KB

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