chado_linker__pub.inc 11 KB

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