chado_linker__prop.inc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. if (count($items) > 0) {
  86. $element[0] = array(
  87. '#type' => 'markup',
  88. '#markup' => $content,
  89. );
  90. }
  91. }
  92. /**
  93. * @see TripalField::widget_form()
  94. */
  95. function widget_form(&$widget, &$form, &$form_state, $field, $instance,
  96. $langcode, $items, $delta, $element) {
  97. $entity = $form['#entity'];
  98. $field_name = $field['field_name'];
  99. // Get the record and table mapping info.
  100. $chado_table = $entity->chado_table;
  101. $chado_column = $entity->chado_column;
  102. $chado_record = $entity->chado_record;
  103. $matches = array();
  104. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  105. // If the field name is not properly formatted then we can't tell what
  106. // table and type this is. So just return.
  107. if (count($matches) != 3) {
  108. return $widget;
  109. }
  110. $table_name = $matches[1];
  111. $cvterm_id = $matches[2];
  112. // Get the name of the pkey field for this property table and the name
  113. // of the FK field that links to the base table.
  114. $schema = chado_get_schema($table_name);
  115. $pkey = $schema['primary key'][0];
  116. $lfkey_field = key($schema['foreign keys'][$chado_table]['columns']);
  117. $rfkey_field = $schema['foreign keys'][$chado_table]['columns'][$lfkey_field];
  118. // Get the field defaults.
  119. $fk_value = '';
  120. $propval = '';
  121. if (array_key_exists($delta, $items)) {
  122. $propval = $items[$delta][$table_name . '__value'];
  123. }
  124. if ($chado_record) {
  125. $fk_value = $chado_record->$rfkey_field;
  126. }
  127. // The group of elements all-together need some extra functionality
  128. // after building up the full list (like draggable table rows).
  129. $widget['#theme'] = 'field_multiple_value_form';
  130. $widget['#title'] = $element['#title'];
  131. $widget['#description'] = $element['#description'];
  132. $widget['#field_name'] = $element['#field_name'];
  133. $widget['#language'] = $element['#language'];
  134. $widget['#weight'] = isset($element['#weight']) ? $element['#weight'] : 0;
  135. $widget['#element_validate'] = array('chado_linker__prop_widget_validate');
  136. $widget['#cardinality'] = 1;
  137. $widget['value'] = array(
  138. '#type' => 'value',
  139. '#value' => $items[$delta]['value'],
  140. );
  141. $widget[$table_name . '__' . $pkey] = array(
  142. '#type' => 'hidden',
  143. '#default_value' => !empty($items[$delta]['value']) ? $items[$delta]['value'] : '',
  144. );
  145. $widget[$table_name . '__' . $lfkey_field] = array(
  146. '#type' => 'hidden',
  147. '#value' => $fk_value,
  148. );
  149. $widget[$table_name . '__value'] = array(
  150. '#type' => 'textfield',
  151. '#default_value' => $propval,
  152. );
  153. $widget[$table_name . '__type_id'] = array(
  154. '#type' => 'hidden',
  155. '#value' => $cvterm_id,
  156. );
  157. $widget[$table_name . '__rank'] = array(
  158. '#type' => 'hidden',
  159. '#value' => $delta,
  160. );
  161. return $widget;
  162. }
  163. /**
  164. * @see TripalField::validate()
  165. */
  166. function validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  167. $field_name = $element['#field_name'];
  168. $delta = $element['#delta'];
  169. $entity = $element['#entity'];
  170. $matches = array();
  171. // Get the record and table mapping info.
  172. $chado_table = $entity->chado_table;
  173. $chado_column = $entity->chado_column;
  174. // Get the table name and cvterm_id for this field.
  175. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  176. $table_name = $matches[1];
  177. $cvterm_id = $matches[2];
  178. // Get the name of the pkey field for this property table and the name
  179. // of the FK field that links to the base table.
  180. $schema = chado_get_schema($table_name);
  181. $pkey = $schema['primary key'][0];
  182. $lfkey_field = key($schema['foreign keys'][$chado_table]['columns']);
  183. // If we don't have a property value then we need to set all other fields
  184. // to be empty so that when the module tries to save the field on the
  185. // entity it won't try to save a partial record.
  186. $pkey_val = tripal_chado_get_field_form_values($field_name, $form_state, $delta);
  187. $prop_value = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . "__value");
  188. $fk_val = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__' . $lfkey_field);
  189. $type_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__type_id');
  190. if (!$prop_value) {
  191. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $lfkey_field);
  192. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__value');
  193. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__type_id');
  194. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__rank');
  195. }
  196. else {
  197. $rank = tripal_chado_get_field_form_values($field_name, $form_state, $delta, '_weight');
  198. tripal_chado_set_field_form_values($field_name, $form_state, $rank, $delta, $table_name . '__rank');
  199. }
  200. // Remove the properties for this record. We will re-add it. Otherwise,
  201. // if we change ranks, we wind up with multiple records in the property table.
  202. if ($pkey_val) {
  203. $match = array(
  204. $pkey => $pkey_val
  205. );
  206. chado_delete_record($table_name, $match);
  207. }
  208. }
  209. /**
  210. * @see TripalField::load()
  211. */
  212. function load($field, $entity, $details) {
  213. $field_name = $details['field_name'];
  214. $field_type = $details['type'];
  215. $field_table = $details['settings']['chado_table'];
  216. $field_column = $details['settings']['chado_column'];
  217. $matches = array();
  218. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  219. $table_name = $matches[1];
  220. $cvterm_id = $matches[2];
  221. // Get the FK that links to the base record.
  222. $schema = chado_get_schema($field_table);
  223. $pkey = $schema['primary key'][0];
  224. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  225. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  226. // Set some defaults for the empty record.
  227. $entity->{$field_name}['und'][0] = array(
  228. 'value' => '',
  229. $field_table . '__' . $pkey => '',
  230. $field_table . '__' . $fkey_lcolumn => '',
  231. $field_table . '__value' => '',
  232. $field_table . '__type_id' => '',
  233. $field_table . '__rank' => '',
  234. );
  235. // Get the properties associated with this base record for this fields
  236. // given type.
  237. $columns = array('*');
  238. $match = array(
  239. $fkey_lcolumn => $record->$fkey_rcolumn,
  240. 'type_id' => $cvterm_id,
  241. );
  242. $options = array(
  243. 'return_array' => TRUE,
  244. 'order_by' => array('rank' => 'ASC')
  245. );
  246. $properties = chado_select_record($field_table, $columns, $match, $options);
  247. for ($i = 0; $i < count($properties); $i++) {
  248. $property = $properties[$i];
  249. foreach ($schema['fields'] as $fname => $details) {
  250. $entity->{$field_name}['und'][$i] = array(
  251. 'value' => array(),
  252. $field_table . '__' . $pkey => $property->$pkey,
  253. $field_table . '__' . $fkey_lcolumn => $property->$fkey_lcolumn,
  254. $field_table . '__value' => $property->value,
  255. $field_table . '__type_id' => $property->type_id,
  256. $field_table . '__rank' => $property->rank,
  257. );
  258. }
  259. }
  260. }
  261. }
  262. /**
  263. *
  264. * @param unknown $form
  265. * @param unknown $form_state
  266. */
  267. function chado_linker__prop_widget_form_ajax_callback($form, $form_state) {
  268. $field_name = $form_state['triggering_element']['#parents'][0];
  269. return $form[$field_name];
  270. }