chado_linker__prop.inc 8.2 KB

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