chado_linker__pub.inc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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__pub_info() {
  9. return array(
  10. 'label' => t('Publications'),
  11. 'description' => t('Associates a publication (e.g. journal article,
  12. conference proceedings, book chapter, etc.) with this record.'),
  13. 'default_widget' => 'chado_linker__pub_widget',
  14. 'default_formatter' => 'chado_linker__pub_formatter',
  15. 'settings' => array(),
  16. 'storage' => array(
  17. 'type' => 'field_chado_storage',
  18. 'module' => 'tripal_chado',
  19. 'active' => TRUE
  20. ),
  21. );
  22. }
  23. /**
  24. * Implements hook_widget_info.
  25. *
  26. * This is a hook provided by the tripal_chado module for offloading
  27. * the hook_field_widget_info() hook for each field to specify.
  28. */
  29. function chado_linker__pub_widget_info() {
  30. return array(
  31. 'label' => t('Publications'),
  32. 'field types' => array('chado_linker__pub'),
  33. );
  34. }
  35. /**
  36. * Implements hook_formatter_info.
  37. *
  38. * This is a hook provided by the tripal_chado module for
  39. * offloading the hook_field_formatter_info() for each field
  40. * to specify.
  41. *
  42. */
  43. function chado_linker__pub_formatter_info() {
  44. return array(
  45. 'label' => t('Publications'),
  46. 'field types' => array('chado_linker__pub'),
  47. 'settings' => array(
  48. ),
  49. );
  50. }
  51. /**
  52. *
  53. * @param unknown $entity_type
  54. * @param unknown $entity
  55. * @param unknown $field
  56. * @param unknown $instance
  57. * @param unknown $langcode
  58. * @param unknown $items
  59. * @param unknown $display
  60. */
  61. function chado_linker__pub_formatter(&$element, $entity_type, $entity, $field,
  62. $instance, $langcode, $items, $display) {
  63. $list_items = array();
  64. $chado_table = $field['settings']['chado_table'];
  65. foreach ($items as $delta => $item) {
  66. if ($item[$chado_table . '__pub_id']) {
  67. $pub = chado_generate_var('pub', array('pub_id' => $item[$chado_table . '__pub_id']));
  68. $list_items[$pub->pyear] = $pub->uniquename;
  69. }
  70. }
  71. krsort($list_items, SORT_NUMERIC);
  72. $list = array(
  73. 'title' => '',
  74. 'items' => $list_items,
  75. 'type' => 'ol',
  76. 'attributes' => array(),
  77. );
  78. $element[0] = array(
  79. '#type' => 'markup',
  80. '#markup' => theme_item_list($list),
  81. );
  82. }
  83. /**
  84. *
  85. * @param unknown $field_name
  86. * @param unknown $widget
  87. * @param unknown $form
  88. * @param unknown $form_state
  89. * @param unknown $field
  90. * @param unknown $instance
  91. * @param unknown $langcode
  92. * @param unknown $items
  93. * @param unknown $delta
  94. * @param unknown $element
  95. */
  96. function chado_linker__pub_widget(&$widget, $form, $form_state, $field,
  97. $instance, $langcode, $items, $delta, $element) {
  98. $entity = $form['#entity'];
  99. $field_name = $field['field_name'];
  100. // Get the FK column that links to the base table.
  101. $table_name = $field['settings']['chado_table'];
  102. $base_table = $field['settings']['base_table'];
  103. $schema = chado_get_schema($table_name);
  104. $pkey = $schema['primary key'][0];
  105. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  106. $fkey = $fkeys[0];
  107. // Get the field defaults.
  108. $record_id = '';
  109. $fkey_value = $element['#entity']->chado_record_id;
  110. $pub_id = '';
  111. $title = '';
  112. // If the field already has a value then it will come through the $items
  113. // array. This happens when editing an existing record.
  114. if (array_key_exists($delta, $items)) {
  115. $record_id = $items[$delta]['value'];
  116. $fkey_value = $items[$delta][$table_name . '__' . $fkey];
  117. $pub_id = $items[$delta][$table_name . '__pub_id'];
  118. $title = $items[$delta][$table_name . '--pub__uniquename'];
  119. }
  120. // Check $form_state['values'] to see if an AJAX call set the values.
  121. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  122. $record_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name);
  123. $fkey_value = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $fkey);
  124. $pub_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__pub_id');
  125. $title = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__title');
  126. }
  127. $schema = chado_get_schema('pub');
  128. $widget['#table_name'] = $table_name;
  129. $widget['#fkey_field'] = $fkey;
  130. $widget['#element_validate'] = array('chado_linker__pub_widget_validate');
  131. $widget['#theme'] = 'chado_linker__pub_widget';
  132. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  133. $widget['#suffix'] = "</span>";
  134. $widget['value'] = array(
  135. '#type' => 'value',
  136. '#default_value' => $record_id,
  137. );
  138. $widget[$table_name . '__' . $fkey] = array(
  139. '#type' => 'value',
  140. '#default_value' => $fkey_value,
  141. );
  142. $widget[$table_name . '__pub_id'] = array(
  143. '#type' => 'value',
  144. '#default_value' => $pub_id,
  145. );
  146. $widget[$table_name . '--pub__uniquename'] = array(
  147. '#type' => 'textfield',
  148. '#title' => t('Publication ID'),
  149. '#default_value' => $title,
  150. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/pub',
  151. '#ajax' => array(
  152. 'callback' => "chado_linker__pub_widget_form_ajax_callback",
  153. 'wrapper' => "$table_name-$delta",
  154. 'effect' => 'fade',
  155. 'method' => 'replace'
  156. ),
  157. '#maxlength' => 100000,
  158. );
  159. }
  160. /**
  161. * An Ajax callback for the pub widget.
  162. */
  163. function chado_linker__pub_widget_form_ajax_callback($form, $form_state) {
  164. $field_name = $form_state['triggering_element']['#parents'][0];
  165. $delta = $form_state['triggering_element']['#parents'][2];
  166. return $form[$field_name]['und'][$delta];
  167. }
  168. /**
  169. * Callback function for validating the tripal_chado_organism_select_widget.
  170. */
  171. function chado_linker__pub_widget_validate($element, &$form_state) {
  172. $field_name = $element['#field_name'];
  173. $delta = $element['#delta'];
  174. $table_name = $element['#table_name'];
  175. $fkey = $element['#fkey_field'];
  176. // If the form ID is field_ui_field_edit_form, then the user is editing the
  177. // field's values in the manage fields form of Drupal. We don't want
  178. // to validate it as if it were being used in a data entry form.
  179. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  180. return;
  181. }
  182. // Get the field values.
  183. $fkey_value = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $fkey);
  184. $pub_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__pub_id');
  185. $uname = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '--pub__uniquename');
  186. // If the user provided a uniquename then we want to set the
  187. // foreign key value to be the chado_record_idd
  188. if ($uname) {
  189. // Get the pub. If one with the same name and type is already present
  190. // then use that. Otherwise, insert a new one.
  191. if (!$pub_id) {
  192. $pub = chado_generate_var('pub', array('uniquename' => $uname));
  193. // Set the pub_id and FK value
  194. tripal_chado_set_field_form_values($field_name, $form_state, $pub->pub_id, $delta, $table_name . '__pub_id');
  195. $fkey_value = $element['#entity']->chado_record_id;
  196. tripal_chado_set_field_form_values($field_name, $form_state, $fkey_value, $delta, $table_name . '__' . $fkey);
  197. }
  198. }
  199. else {
  200. // If the $syn_name is not set, then remove the linker FK value to the base table.
  201. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $fkey);
  202. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__pub_id');
  203. }
  204. }
  205. /**
  206. * Theme function for the pub widget.
  207. *
  208. * @param $variables
  209. */
  210. function theme_chado_linker__pub_widget($variables) {
  211. $element = $variables['element'];
  212. // These two fields were added to the widget to help identify the fields
  213. // for layout.
  214. $table_name = $element['#table_name'];
  215. $fkey = $element['#fkey_field'];
  216. $layout = "
  217. <div class=\"pub-widget\">
  218. <div class=\"pub-widget-item\">" .
  219. drupal_render($element[$table_name . '--pub__uniquename']) . "
  220. </div>
  221. </div>
  222. ";
  223. return $layout;
  224. }
  225. /**
  226. * Loads the field values with appropriate data.
  227. *
  228. * This function is called by the tripal_chado_field_storage_load() for
  229. * each property managed by the field_chado_storage storage type. This is
  230. * an optional hook function that is only needed if the field has
  231. * multiple form elements.
  232. *
  233. * @param $field
  234. * @param $entity
  235. * @param $base_table
  236. * @param $record
  237. */
  238. function chado_linker__pub_load($field, $entity, $base_table, $record) {
  239. $field_name = $field['field_name'];
  240. $field_type = $field['type'];
  241. $field_table = $field['settings']['chado_table'];
  242. $field_column = $field['settings']['chado_column'];
  243. // Get the FK that links to the base record.
  244. $schema = chado_get_schema($field_table);
  245. $pkey = $schema['primary key'][0];
  246. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  247. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  248. // Set some defaults for the empty record.
  249. $entity->{$field_name}['und'][0] = array(
  250. 'value' => '',
  251. $field_table . '__' . $fkey_lcolumn => '',
  252. $field_table . '__' . 'pub_id' => '',
  253. $field_table . '--' . 'pub__uniquename' => '',
  254. );
  255. $linker_table = $base_table . '_pub';
  256. $options = array(
  257. 'return_array' => 1,
  258. );
  259. $record = chado_expand_var($record, 'table', $linker_table, $options);
  260. if (count($record->$linker_table) > 0) {
  261. $i = 0;
  262. foreach ($record->$linker_table as $index => $linker) {
  263. $pub = $linker->pub_id;
  264. $entity->{$field_name}['und'][$i] = array(
  265. 'value' => $linker->$pkey,
  266. $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn->$fkey_lcolumn,
  267. $field_table . '__' . 'pub_id' => $pub->pub_id,
  268. $field_table . '--' . 'pub__uniquename' => $pub->uniquename,
  269. );
  270. $i++;
  271. }
  272. }
  273. }