chado_linker__prop.inc 10.0 KB

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