chado_linker__prop.inc 8.9 KB

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