kvproperty.inc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 tripal_chado_kvproperty_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 tripal_chado_kvproperty_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  40. $entity = $form['#entity'];
  41. $field_name = $field['field_name'];
  42. // Get the bundle and the table it maps to.
  43. $bundle = tripal_load_bundle_entity($entity->bundle);
  44. $chado_table = tripal_get_bundle_variable('chado_table', $bundle->id);
  45. $chado_column = tripal_get_bundle_variable('chado_column', $bundle->id);
  46. // Get the chado record.
  47. $chado_entity = db_select('chado_entity' ,'ce')
  48. ->fields('ce')
  49. ->condition('ce.entity_id', $entity->id)
  50. ->execute()
  51. ->fetchObject();
  52. $schema = chado_get_schema($chado_table);
  53. $record = chado_select_record($chado_table, array('*'), array($schema['primary key'][0] => $chado_entity->record_id));
  54. $matches = array();
  55. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  56. // If the field name is not properly formatted then we can't tell what
  57. // table and type this is. So just return.
  58. if (count($matches) != 3) {
  59. return $widget;
  60. }
  61. $table_name = $matches[1];
  62. $cvterm_id = $matches[2];
  63. // Get the name of the pkey field for this property table and the name
  64. // of the FK field that links to the base table.
  65. $schema = chado_get_schema($table_name);
  66. $pkey = $schema['primary key'][0];
  67. $lfkey_field = key($schema['foreign keys'][$chado_table]['columns']);
  68. $rfkey_field = $schema['foreign keys'][$chado_table]['columns'][$lfkey_field];
  69. // Get the field defaults.
  70. $fk_value = '';
  71. $propval = '';
  72. if (array_key_exists($delta, $items)) {
  73. $propval = $items[$delta][$table_name . '__value'];
  74. }
  75. if (count($record) > 0) {
  76. $fk_value = $record[0]->$rfkey_field;
  77. }
  78. // The group of elements all-together need some extra functionality
  79. // after building up the full list (like draggable table rows).
  80. $widget['#theme'] = 'field_multiple_value_form';
  81. $widget['#title'] = $element['#title'];
  82. $widget['#description'] = $element['#description'];
  83. $widget['#field_name'] = $element['#field_name'];
  84. $widget['#language'] = $element['#language'];
  85. $widget['#weight'] = isset($element['#weight']) ? $element['#weight'] : 0;
  86. $widget['#element_validate'] = array('tripal_chado_kvproperty_widget_validate');
  87. $widget['#cardinality'] = 1;
  88. $widget['value'] = 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. *
  112. * @param unknown $form
  113. * @param unknown $form_state
  114. */
  115. function tripal_chado_kvproperty_widget_form_ajax_callback($form, $form_state) {
  116. $field_name = $form_state['triggering_element']['#parents'][0];
  117. return $form[$field_name];
  118. }
  119. /**
  120. * Callback function for validating the tripal_chado_kvproperty_widget.
  121. */
  122. function tripal_chado_kvproperty_widget_validate($element, &$form_state) {
  123. $field_name = $element['#field_name'];
  124. $delta = $element['#delta'];
  125. $entity = $element['#entity'];
  126. $matches = array();
  127. $bundle = tripal_load_bundle_entity($entity->bundle);
  128. $chado_table = tripal_get_bundle_variable('chado_table', $bundle->id);
  129. $chado_column = tripal_get_bundle_variable('chado_column', $bundle->id);
  130. // Get the table name and cvterm_id for this field.
  131. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  132. $table_name = $matches[1];
  133. $cvterm_id = $matches[2];
  134. // Get the name of the pkey field for this property table and the name
  135. // of the FK field that links to the base table.
  136. $schema = chado_get_schema($table_name);
  137. $pkey = $schema['primary key'][0];
  138. $lfkey_field = key($schema['foreign keys'][$chado_table]['columns']);
  139. // If we don't have a property value then we need to set all other fields
  140. // to be empty so that when the module tries to save the field on the
  141. // entity it won't try to save a partial record.
  142. $pkey_val = tripal_chado_get_field_form_values($field_name, $form_state, $delta);
  143. $prop_value = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . "__value");
  144. $fk_val = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__' . $lfkey_field);
  145. $type_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__type_id');
  146. if (!$prop_value) {
  147. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta);
  148. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $lfkey_field);
  149. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__value');
  150. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__type_id');
  151. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__rank');
  152. }
  153. else {
  154. $rank = tripal_chado_get_field_form_values($field_name, $form_state, $delta, '_weight');
  155. tripal_chado_set_field_form_values($field_name, $form_state, $rank, $delta, $table_name . '__rank');
  156. }
  157. // Remove the properties for this record. We will re-add it. Otherwise,
  158. // if we change ranks, we wind up with multiple records in the property table.
  159. if ($pkey_val) {
  160. $match = array(
  161. $pkey => $pkey_val
  162. );
  163. chado_delete_record($table_name, $match);
  164. }
  165. }
  166. /**
  167. * Callback function for submitting the tripal_chado_kvproperty_widget.
  168. */
  169. function tripal_chado_kvproperty_widget_submit($element, &$form_state) {
  170. }
  171. /**
  172. * Loads the field values with appropriate data.
  173. *
  174. * This function is called by the tripal_chado_field_storage_load() for
  175. * each property managed by the field_chado_storage storage type. This is
  176. * an optional hook function that is only needed if the field has
  177. * multiple form elements.
  178. *
  179. * @param $field
  180. * @param $entity
  181. * @param $base_table
  182. * @param $record
  183. */
  184. function tripal_chado_kvproperty_field_load($field, $entity, $base_table, $record) {
  185. $field_name = $field['field_name'];
  186. $field_type = $field['type'];
  187. $field_table = $field['settings']['chado_table'];
  188. $field_column = $field['settings']['chado_column'];
  189. $matches = array();
  190. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  191. $table_name = $matches[1];
  192. $cvterm_id = $matches[2];
  193. // Get the FK that links to the base record.
  194. $schema = chado_get_schema($field_table);
  195. $pkey = $schema['primary key'][0];
  196. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  197. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  198. // Set some defaults for the empty record.
  199. $entity->{$field_name}['und'][0] = array(
  200. 'value' => '',
  201. $field_table . '__' . $fkey_lcolumn => '',
  202. $field_table . '__value' => '',
  203. $field_table . '__type_id' => '',
  204. $field_table . '__rank' => '',
  205. );
  206. // Get the properties associated with this base record for this fields
  207. // given type.
  208. $columns = array('*');
  209. $match = array(
  210. $fkey_lcolumn => $record->$fkey_rcolumn,
  211. 'type_id' => $cvterm_id,
  212. );
  213. $options = array(
  214. 'return_array' => TRUE,
  215. 'order_by' => array('rank' => 'ASC')
  216. );
  217. $properties = chado_select_record($field_table, $columns, $match, $options);
  218. for ($i = 0; $i < count($properties); $i++) {
  219. $property = $properties[$i];
  220. foreach ($schema['fields'] as $fname => $details) {
  221. $entity->{$field_name}['und'][$i] = array(
  222. 'value' => $property->$pkey,
  223. $field_table . '__' . $fkey_lcolumn => $property->$fkey_lcolumn,
  224. $field_table . '__value' => $property->value,
  225. $field_table . '__type_id' => $property->type_id,
  226. $field_table . '__rank' => $property->rank,
  227. );
  228. }
  229. }
  230. }