chado_linker__prop.inc 9.8 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. parent::widgetForm($widget, $form, $form_state, $langcode, $items, $delta, $element);
  43. $field_name = $this->field['field_name'];
  44. $field_type = $this->field['type'];
  45. $field_table = $this->field['settings']['chado_table'];
  46. $field_column = $this->field['settings']['chado_column'];
  47. // Get the table name and cvterm that this field maps to.
  48. $matches = array();
  49. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  50. // If the field name is not properly formatted then we can't tell what
  51. // table and type this is. So just return.
  52. if (count($matches) != 3) {
  53. return $widget;
  54. }
  55. $table_name = $matches[1];
  56. $cvterm_id = $matches[2];
  57. // Get the name of the pkey field for this property table and the name
  58. // of the FK field that links to the base table.
  59. $schema = chado_get_schema($table_name);
  60. $pkey = $schema['primary key'][0];
  61. $base_table = $this->field['settings']['base_table'];
  62. $lfkey_field = key($schema['foreign keys'][$base_table]['columns']);
  63. $rfkey_field = $schema['foreign keys'][$base_table]['columns'][$lfkey_field];
  64. // Get the field defaults.
  65. $fk_value =key_exists(0, $items) ? $items[0]['chado-' . $field_table . '__' . $lfkey_field] : '';
  66. $propval = '';
  67. if (array_key_exists($delta, $items)) {
  68. $propval = tripal_get_field_item_keyval($items, $delta, 'chado-' . $table_name . '__value', $propval);
  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' => 'value',
  82. '#value' => key_exists($delta, $items) ? $items[$delta]['value'] : '',
  83. );
  84. $widget['chado-' . $table_name . '__' . $pkey] = array(
  85. '#type' => 'hidden',
  86. '#default_value' => !empty($items[$delta]['chado-' . $field_table . '__' . $pkey]) ? $items[$delta]['chado-' . $field_table . '__' . $pkey] : '',
  87. );
  88. $widget['chado-' . $table_name . '__' . $lfkey_field] = array(
  89. '#type' => 'hidden',
  90. '#value' => $fk_value,
  91. );
  92. $widget['chado-' . $table_name . '__value'] = array(
  93. '#type' => 'textfield',
  94. '#default_value' => $propval,
  95. );
  96. $widget['chado-' . $table_name . '__type_id'] = array(
  97. '#type' => 'hidden',
  98. '#value' => $cvterm_id,
  99. );
  100. $widget['chado-' . $table_name . '__rank'] = array(
  101. '#type' => 'hidden',
  102. '#value' => $delta,
  103. );
  104. }
  105. /**
  106. * @see TripalField::validate()
  107. */
  108. public function validate($entity_type, $entity, $field, $items, &$errors) {
  109. /* $field_name = $this->field['field_name'];
  110. $matches = array();
  111. // Get the record and table mapping info.
  112. $chado_table = $entity->chado_table;
  113. $chado_column = $entity->chado_column;
  114. // Get the table name and cvterm_id for this field.
  115. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  116. $table_name = $matches[1];
  117. $cvterm_id = $matches[2];
  118. // Get the name of the pkey field for this property table and the name
  119. // of the FK field that links to the base table.
  120. $schema = chado_get_schema($table_name);
  121. $pkey = $schema['primary key'][0];
  122. $lfkey_field = key($schema['foreign keys'][$chado_table]['columns']);
  123. // If we don't have a property value then we need to set all other fields
  124. // to be empty so that when the module tries to save the field on the
  125. // entity it won't try to save a partial record.
  126. // $pkey_val = tripal_chado_get_field_form_values($field_name, $form_state, $delta);
  127. // $prop_value = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . "__value");
  128. // $fk_val = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__' . $lfkey_field);
  129. // $type_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__type_id');
  130. if (!$prop_value) {
  131. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $lfkey_field);
  132. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__value');
  133. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__type_id');
  134. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__rank');
  135. }
  136. else {
  137. $rank = tripal_chado_get_field_form_values($field_name, $form_state, $delta, '_weight');
  138. tripal_chado_set_field_form_values($field_name, $form_state, $rank, $delta, $table_name . '__rank');
  139. }
  140. // Remove the properties for this record. We will re-add it. Otherwise,
  141. // if we change ranks, we wind up with multiple records in the property table.
  142. if ($pkey_val) {
  143. $match = array(
  144. $pkey => $pkey_val
  145. );
  146. chado_delete_record($table_name, $match);
  147. } */
  148. }
  149. /**
  150. * @see TripalField::widgetFormSubmit()
  151. */
  152. public function widgetFormSubmit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  153. $field_table = $this->field['settings']['chado_table'];
  154. // Don't save if there is no value for prop
  155. if (!trim($form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . 'value'])) {
  156. foreach($form_state['values'][$field_name][$langcode][$delta] AS $key => $value) {
  157. if (preg_match('/^chado-/', $key)) {
  158. $form_state['values'][$field_name][$langcode][$delta][$key] = '__NULL__';
  159. }
  160. }
  161. }
  162. }
  163. /**
  164. * @see TripalField::load()
  165. */
  166. public function load($entity, $details = array()) {
  167. $field_name = $this->field['field_name'];
  168. $field_type = $this->field['type'];
  169. $field_table = $this->field['settings']['chado_table'];
  170. $field_column = $this->field['settings']['chado_column'];
  171. $base_table = $this->field['settings']['base_table'];
  172. $matches = array();
  173. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  174. $table_name = $matches[1];
  175. $cvterm_id = $matches[2];
  176. // Get the FK that links to the base record.
  177. $schema = chado_get_schema($field_table);
  178. $pkey = $schema['primary key'][0];
  179. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  180. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  181. // Set some defaults for the empty record.
  182. $chado_record = $entity->chado_record;
  183. $entity->{$field_name}['und'][0] = array(
  184. 'value' => '',
  185. 'chado-' . $field_table . '__' . $pkey => '',
  186. 'chado-' . $field_table . '__' . $fkey_lcolumn => $chado_record->{$fkey_lcolumn},
  187. 'chado-' . $field_table . '__value' => '',
  188. 'chado-' . $field_table . '__type_id' => '',
  189. 'chado-' . $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 => $chado_record->{$fkey_lcolumn},
  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' => array(),
  208. 'chado-' . $field_table . '__' . $pkey => $property->$pkey,
  209. 'chado-' . $field_table . '__' . $fkey_lcolumn => $property->$fkey_lcolumn,
  210. 'chado-' . $field_table . '__value' => $property->value,
  211. 'chado-' . $field_table . '__type_id' => $property->type_id,
  212. 'chado-' . $field_table . '__rank' => $property->rank,
  213. );
  214. }
  215. }
  216. }
  217. }
  218. /**
  219. *
  220. * @param unknown $form
  221. * @param unknown $form_state
  222. */
  223. function chado_linker__prop_widget_form_ajax_callback($form, $form_state) {
  224. $field_name = $form_state['triggering_element']['#parents'][0];
  225. return $form[$field_name];
  226. }