kvproperty.inc 8.1 KB

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