chado_linker__prop.inc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 fieldSettingsForm()
  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. static function formatterView(&$element, $entity_type, $entity, $field,
  24. $instance, $langcode, $items, $display) {
  25. $field_name = $field['field_name'];
  26. $chado_table = $field['settings']['chado_table'];
  27. $properties = array();
  28. foreach ($items as $delta => $item) {
  29. $properties[] = $item[$chado_table . '__value'];
  30. }
  31. $content = implode(', ', $properties);
  32. if (count($items) > 0) {
  33. $element[0] = array(
  34. '#type' => 'markup',
  35. '#markup' => $content,
  36. );
  37. }
  38. }
  39. /**
  40. * @see TripalField::widgetForm()
  41. */
  42. static function widgetForm(&$widget, &$form, &$form_state, $field, $instance,
  43. $langcode, $items, $delta, $element) {
  44. $entity = $form['#entity'];
  45. $field_name = $field['field_name'];
  46. // Get the record and table mapping info.
  47. $chado_table = $entity->chado_table;
  48. $chado_column = $entity->chado_column;
  49. $chado_record = $entity->chado_record;
  50. $matches = array();
  51. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  52. // If the field name is not properly formatted then we can't tell what
  53. // table and type this is. So just return.
  54. if (count($matches) != 3) {
  55. return $widget;
  56. }
  57. $table_name = $matches[1];
  58. $cvterm_id = $matches[2];
  59. // Get the name of the pkey field for this property table and the name
  60. // of the FK field that links to the base table.
  61. $schema = chado_get_schema($table_name);
  62. $pkey = $schema['primary key'][0];
  63. $lfkey_field = key($schema['foreign keys'][$chado_table]['columns']);
  64. $rfkey_field = $schema['foreign keys'][$chado_table]['columns'][$lfkey_field];
  65. // Get the field defaults.
  66. $fk_value = '';
  67. $propval = '';
  68. if (array_key_exists($delta, $items)) {
  69. $propval = $items[$delta][$table_name . '__value'];
  70. }
  71. if ($chado_record) {
  72. $fk_value = $chado_record->$rfkey_field;
  73. }
  74. // The group of elements all-together need some extra functionality
  75. // after building up the full list (like draggable table rows).
  76. $widget['#theme'] = 'field_multiple_value_form';
  77. $widget['#title'] = $element['#title'];
  78. $widget['#description'] = $element['#description'];
  79. $widget['#field_name'] = $element['#field_name'];
  80. $widget['#language'] = $element['#language'];
  81. $widget['#weight'] = isset($element['#weight']) ? $element['#weight'] : 0;
  82. $widget['#element_validate'] = array('chado_linker__prop_widget_validate');
  83. $widget['#cardinality'] = 1;
  84. $widget['value'] = array(
  85. '#type' => 'value',
  86. '#value' => $items[$delta]['value'],
  87. );
  88. $widget[$table_name . '__' . $pkey] = array(
  89. '#type' => 'hidden',
  90. '#default_value' => !empty($items[$delta]['value']) ? $items[$delta]['value'] : '',
  91. );
  92. $widget[$table_name . '__' . $lfkey_field] = array(
  93. '#type' => 'hidden',
  94. '#value' => $fk_value,
  95. );
  96. $widget[$table_name . '__value'] = array(
  97. '#type' => 'textfield',
  98. '#default_value' => $propval,
  99. );
  100. $widget[$table_name . '__type_id'] = array(
  101. '#type' => 'hidden',
  102. '#value' => $cvterm_id,
  103. );
  104. $widget[$table_name . '__rank'] = array(
  105. '#type' => 'hidden',
  106. '#value' => $delta,
  107. );
  108. return $widget;
  109. }
  110. /**
  111. * @see TripalField::validate()
  112. */
  113. static function widgetFormValidate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  114. $field_name = $element['#field_name'];
  115. $delta = $element['#delta'];
  116. $entity = $element['#entity'];
  117. $matches = array();
  118. // Get the record and table mapping info.
  119. $chado_table = $entity->chado_table;
  120. $chado_column = $entity->chado_column;
  121. // Get the table name and cvterm_id for this field.
  122. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  123. $table_name = $matches[1];
  124. $cvterm_id = $matches[2];
  125. // Get the name of the pkey field for this property table and the name
  126. // of the FK field that links to the base table.
  127. $schema = chado_get_schema($table_name);
  128. $pkey = $schema['primary key'][0];
  129. $lfkey_field = key($schema['foreign keys'][$chado_table]['columns']);
  130. // If we don't have a property value then we need to set all other fields
  131. // to be empty so that when the module tries to save the field on the
  132. // entity it won't try to save a partial record.
  133. // $pkey_val = tripal_chado_get_field_form_values($field_name, $form_state, $delta);
  134. // $prop_value = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . "__value");
  135. // $fk_val = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__' . $lfkey_field);
  136. // $type_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__type_id');
  137. if (!$prop_value) {
  138. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $lfkey_field);
  139. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__value');
  140. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__type_id');
  141. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__rank');
  142. }
  143. else {
  144. $rank = tripal_chado_get_field_form_values($field_name, $form_state, $delta, '_weight');
  145. tripal_chado_set_field_form_values($field_name, $form_state, $rank, $delta, $table_name . '__rank');
  146. }
  147. // Remove the properties for this record. We will re-add it. Otherwise,
  148. // if we change ranks, we wind up with multiple records in the property table.
  149. if ($pkey_val) {
  150. $match = array(
  151. $pkey => $pkey_val
  152. );
  153. chado_delete_record($table_name, $match);
  154. }
  155. }
  156. /**
  157. * @see TripalField::load()
  158. */
  159. static function load($field, $entity, $details = array()) {
  160. $field_name = $details['field_name'];
  161. $field_type = $details['type'];
  162. $field_table = $details['settings']['chado_table'];
  163. $field_column = $details['settings']['chado_column'];
  164. $matches = array();
  165. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  166. $table_name = $matches[1];
  167. $cvterm_id = $matches[2];
  168. // Get the FK that links to the base record.
  169. $schema = chado_get_schema($field_table);
  170. $pkey = $schema['primary key'][0];
  171. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  172. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  173. // Set some defaults for the empty record.
  174. $entity->{$field_name}['und'][0] = array(
  175. 'value' => '',
  176. $field_table . '__' . $pkey => '',
  177. $field_table . '__' . $fkey_lcolumn => '',
  178. $field_table . '__value' => '',
  179. $field_table . '__type_id' => '',
  180. $field_table . '__rank' => '',
  181. );
  182. // Get the properties associated with this base record for this fields
  183. // given type.
  184. $columns = array('*');
  185. $match = array(
  186. $fkey_lcolumn => $record->$fkey_rcolumn,
  187. 'type_id' => $cvterm_id,
  188. );
  189. $options = array(
  190. 'return_array' => TRUE,
  191. 'order_by' => array('rank' => 'ASC')
  192. );
  193. $properties = chado_select_record($field_table, $columns, $match, $options);
  194. for ($i = 0; $i < count($properties); $i++) {
  195. $property = $properties[$i];
  196. foreach ($schema['fields'] as $fname => $details) {
  197. $entity->{$field_name}['und'][$i] = array(
  198. 'value' => array(),
  199. $field_table . '__' . $pkey => $property->$pkey,
  200. $field_table . '__' . $fkey_lcolumn => $property->$fkey_lcolumn,
  201. $field_table . '__value' => $property->value,
  202. $field_table . '__type_id' => $property->type_id,
  203. $field_table . '__rank' => $property->rank,
  204. );
  205. }
  206. }
  207. }
  208. }
  209. /**
  210. *
  211. * @param unknown $form
  212. * @param unknown $form_state
  213. */
  214. function chado_linker__prop_widget_form_ajax_callback($form, $form_state) {
  215. $field_name = $form_state['triggering_element']['#parents'][0];
  216. return $form[$field_name];
  217. }