chado_linker__prop.inc 9.6 KB

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