chado_linker__prop.inc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. class chado_linker__prop extends TripalField {
  3. /**
  4. * @see TripalField::field_info()
  5. */
  6. function field_info() {
  7. return array(
  8. 'label' => t('Add a Property'),
  9. 'description' => t('Add details about this property.'),
  10. 'default_widget' => 'chado_linker__prop_widget',
  11. 'default_formatter' => 'chado_linker__prop_formatter',
  12. 'settings' => array(),
  13. 'storage' => array(
  14. 'type' => 'field_chado_storage',
  15. 'module' => 'tripal_chado',
  16. 'active' => TRUE
  17. ),
  18. );
  19. }
  20. /**
  21. * @see TripalField::can_attach()
  22. */
  23. protected function can_attach($entity_type, $bundle, $details) {
  24. // This field is only attached by the chado_linker__prop_addr field.
  25. return FALSE;
  26. }
  27. /**
  28. * @see TripalField::create_info()
  29. */
  30. function create_info($entity_type, $bundle, $details) {
  31. if (!$this->can_attach($entity_type, $bundle, $details)) {
  32. return;
  33. }
  34. // This field is only attached by the chado_linker__prop_addr field.
  35. }
  36. /**
  37. * @see TripalField::create_instance_info()
  38. */
  39. function create_instance_info($entity_type, $bundle, $details) {
  40. if (!$this->can_attach($entity_type, $bundle, $details)) {
  41. return;
  42. }
  43. // This field is only attached by the chado_linker__prop_addr field.
  44. }
  45. /**
  46. * @see TripalField::attach_info()
  47. */
  48. function attach_info($entity_type, $bundle, $target) {
  49. $field_info = array();
  50. // This field is only attached by the chado_linker__prop_addr field.
  51. return $field_info;
  52. }
  53. /**
  54. * @see TripalField::widget_info()
  55. */
  56. function widget_info() {
  57. return array(
  58. 'label' => t('Property'),
  59. 'field types' => array('chado_linker__prop'),
  60. );
  61. }
  62. /**
  63. * @see TripalField::formatter_info()
  64. */
  65. function formatter_info() {
  66. return array(
  67. 'label' => t('Property'),
  68. 'field types' => array('chado_linker__prop'),
  69. 'settings' => array(
  70. ),
  71. );
  72. }
  73. /**
  74. * @see TripalField::formatter_view()
  75. */
  76. function formatter_view(&$element, $entity_type, $entity, $field,
  77. $instance, $langcode, $items, $display) {
  78. $field_name = $field['field_name'];
  79. $chado_table = $field['settings']['chado_table'];
  80. $properties = array();
  81. foreach ($items as $delta => $item) {
  82. $properties[] = $item[$chado_table . '__value'];
  83. }
  84. $content = implode(', ', $properties);
  85. $element[$delta] = array(
  86. '#type' => 'markup',
  87. '#markup' => $content,
  88. );
  89. }
  90. /**
  91. * @see TripalField::widget_form()
  92. */
  93. function widget_form(&$widget, &$form, &$form_state, $field, $instance,
  94. $langcode, $items, $delta, $element) {
  95. $entity = $form['#entity'];
  96. $field_name = $field['field_name'];
  97. // Get the record and table mapping info.
  98. $chado_table = $entity->chado_table;
  99. $chado_column = $entity->chado_column;
  100. $chado_record = $entity->chado_record;
  101. $matches = array();
  102. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  103. // If the field name is not properly formatted then we can't tell what
  104. // table and type this is. So just return.
  105. if (count($matches) != 3) {
  106. return $widget;
  107. }
  108. $table_name = $matches[1];
  109. $cvterm_id = $matches[2];
  110. // Get the name of the pkey field for this property table and the name
  111. // of the FK field that links to the base table.
  112. $schema = chado_get_schema($table_name);
  113. $pkey = $schema['primary key'][0];
  114. $lfkey_field = key($schema['foreign keys'][$chado_table]['columns']);
  115. $rfkey_field = $schema['foreign keys'][$chado_table]['columns'][$lfkey_field];
  116. // Get the field defaults.
  117. $fk_value = '';
  118. $propval = '';
  119. if (array_key_exists($delta, $items)) {
  120. $propval = $items[$delta][$table_name . '__value'];
  121. }
  122. if ($chado_record) {
  123. $fk_value = $chado_record->$rfkey_field;
  124. }
  125. // The group of elements all-together need some extra functionality
  126. // after building up the full list (like draggable table rows).
  127. $widget['#theme'] = 'field_multiple_value_form';
  128. $widget['#title'] = $element['#title'];
  129. $widget['#description'] = $element['#description'];
  130. $widget['#field_name'] = $element['#field_name'];
  131. $widget['#language'] = $element['#language'];
  132. $widget['#weight'] = isset($element['#weight']) ? $element['#weight'] : 0;
  133. $widget['#element_validate'] = array('chado_linker__prop_widget_validate');
  134. $widget['#cardinality'] = 1;
  135. $widget['value'] = array(
  136. '#type' => 'value',
  137. '#value' => $items[$delta]['value'],
  138. );
  139. $widget[$table_name . '__' . $pkey] = array(
  140. '#type' => 'hidden',
  141. '#default_value' => !empty($items[$delta]['value']) ? $items[$delta]['value'] : '',
  142. );
  143. $widget[$table_name . '__' . $lfkey_field] = array(
  144. '#type' => 'hidden',
  145. '#value' => $fk_value,
  146. );
  147. $widget[$table_name . '__value'] = array(
  148. '#type' => 'textfield',
  149. '#default_value' => $propval,
  150. );
  151. $widget[$table_name . '__type_id'] = array(
  152. '#type' => 'hidden',
  153. '#value' => $cvterm_id,
  154. );
  155. $widget[$table_name . '__rank'] = array(
  156. '#type' => 'hidden',
  157. '#value' => $delta,
  158. );
  159. return $widget;
  160. }
  161. /**
  162. * @see TripalField::validate()
  163. */
  164. function validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  165. $field_name = $element['#field_name'];
  166. $delta = $element['#delta'];
  167. $entity = $element['#entity'];
  168. $matches = array();
  169. // Get the record and table mapping info.
  170. $chado_table = $entity->chado_table;
  171. $chado_column = $entity->chado_column;
  172. // Get the table name and cvterm_id for this field.
  173. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  174. $table_name = $matches[1];
  175. $cvterm_id = $matches[2];
  176. // Get the name of the pkey field for this property table and the name
  177. // of the FK field that links to the base table.
  178. $schema = chado_get_schema($table_name);
  179. $pkey = $schema['primary key'][0];
  180. $lfkey_field = key($schema['foreign keys'][$chado_table]['columns']);
  181. // If we don't have a property value then we need to set all other fields
  182. // to be empty so that when the module tries to save the field on the
  183. // entity it won't try to save a partial record.
  184. $pkey_val = tripal_chado_get_field_form_values($field_name, $form_state, $delta);
  185. $prop_value = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . "__value");
  186. $fk_val = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__' . $lfkey_field);
  187. $type_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__type_id');
  188. if (!$prop_value) {
  189. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $lfkey_field);
  190. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__value');
  191. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__type_id');
  192. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__rank');
  193. }
  194. else {
  195. $rank = tripal_chado_get_field_form_values($field_name, $form_state, $delta, '_weight');
  196. tripal_chado_set_field_form_values($field_name, $form_state, $rank, $delta, $table_name . '__rank');
  197. }
  198. // Remove the properties for this record. We will re-add it. Otherwise,
  199. // if we change ranks, we wind up with multiple records in the property table.
  200. if ($pkey_val) {
  201. $match = array(
  202. $pkey => $pkey_val
  203. );
  204. chado_delete_record($table_name, $match);
  205. }
  206. }
  207. /**
  208. * @see TripalField::load()
  209. */
  210. function load($field, $entity, $details) {
  211. $field_name = $details['field_name'];
  212. $field_type = $details['type'];
  213. $field_table = $details['settings']['chado_table'];
  214. $field_column = $details['settings']['chado_column'];
  215. $matches = array();
  216. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  217. $table_name = $matches[1];
  218. $cvterm_id = $matches[2];
  219. // Get the FK that links to the base record.
  220. $schema = chado_get_schema($field_table);
  221. $pkey = $schema['primary key'][0];
  222. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  223. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  224. // Set some defaults for the empty record.
  225. $entity->{$field_name}['und'][0] = array(
  226. 'value' => '',
  227. $field_table . '__' . $pkey => '',
  228. $field_table . '__' . $fkey_lcolumn => '',
  229. $field_table . '__value' => '',
  230. $field_table . '__type_id' => '',
  231. $field_table . '__rank' => '',
  232. );
  233. // Get the properties associated with this base record for this fields
  234. // given type.
  235. $columns = array('*');
  236. $match = array(
  237. $fkey_lcolumn => $record->$fkey_rcolumn,
  238. 'type_id' => $cvterm_id,
  239. );
  240. $options = array(
  241. 'return_array' => TRUE,
  242. 'order_by' => array('rank' => 'ASC')
  243. );
  244. $properties = chado_select_record($field_table, $columns, $match, $options);
  245. for ($i = 0; $i < count($properties); $i++) {
  246. $property = $properties[$i];
  247. foreach ($schema['fields'] as $fname => $details) {
  248. $entity->{$field_name}['und'][$i] = array(
  249. 'value' => array(),
  250. $field_table . '__' . $pkey => $property->$pkey,
  251. $field_table . '__' . $fkey_lcolumn => $property->$fkey_lcolumn,
  252. $field_table . '__value' => $property->value,
  253. $field_table . '__type_id' => $property->type_id,
  254. $field_table . '__rank' => $property->rank,
  255. );
  256. }
  257. }
  258. }
  259. }
  260. /**
  261. *
  262. * @param unknown $form
  263. * @param unknown $form_state
  264. */
  265. function chado_linker__prop_widget_form_ajax_callback($form, $form_state) {
  266. $field_name = $form_state['triggering_element']['#parents'][0];
  267. return $form[$field_name];
  268. }