chado_linker__pub.inc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. class chado_linker__pub extends TripalField {
  3. // The default lable for this field.
  4. public static $default_label = 'Publications';
  5. // The default description for this field.
  6. public static $default_description = 'Associates a publication (e.g. journal article,
  7. conference proceedings, book chapter, etc.) with this record.';
  8. // Add any default settings elements. If you override the globalSettingsForm()
  9. // or the instanceSettingsForm() functions then you need to be sure that
  10. // any settings you want those functions to manage are listed in this
  11. // array.
  12. public static $default_settings = array(
  13. 'chado_table' => '',
  14. 'chado_column' => '',
  15. 'base_table' => '',
  16. 'semantic_web' => '',
  17. );
  18. // Set this to the name of the storage backend that by default will support
  19. // this field.
  20. public static $default_storage = 'field_chado_storage';
  21. /**
  22. * @see TripalField::formatterView()
  23. */
  24. public function formatterView(&$element, $entity_type, $entity, $langcode, $items, $display) {
  25. $list_items = array();
  26. $chado_table = $this->field['settings']['chado_table'];
  27. foreach ($items as $delta => $item) {
  28. if ($item[$chado_table . '__pub_id']) {
  29. $pub = chado_generate_var('pub', array('pub_id' => $item[$chado_table . '__pub_id']));
  30. $list_items[$pub->pyear] = $pub->uniquename;
  31. }
  32. }
  33. krsort($list_items, SORT_NUMERIC);
  34. $list = array(
  35. 'title' => '',
  36. 'items' => $list_items,
  37. 'type' => 'ol',
  38. 'attributes' => array(),
  39. );
  40. if (count($items) > 0) {
  41. $element[0] = array(
  42. '#type' => 'markup',
  43. '#markup' => theme_item_list($list),
  44. );
  45. }
  46. }
  47. /**
  48. * @see TripalField::widgetForm()
  49. */
  50. public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  51. $entity = $form['#entity'];
  52. $field_name = $this->field['field_name'];
  53. // Get the FK column that links to the base table.
  54. $table_name = $this->field['settings']['chado_table'];
  55. $base_table = $this->field['settings']['base_table'];
  56. $schema = chado_get_schema($table_name);
  57. $pkey = $schema['primary key'][0];
  58. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  59. $fkey = $fkeys[0];
  60. // Get the field defaults.
  61. $record_id = '';
  62. $fkey_value = $element['#entity']->chado_record_id;
  63. $pub_id = '';
  64. $title = '';
  65. // If the field already has a value then it will come through the $items
  66. // array. This happens when editing an existing record.
  67. if (count($items) > 0 and array_key_exists($delta, $items)) {
  68. $record_id = $items[$delta][$table_name . '__' . $pkey];
  69. $fkey_value = $items[$delta][$table_name . '__' . $fkey];
  70. $pub_id = $items[$delta][$table_name . '__pub_id'];
  71. $title = $items[$delta][$table_name . '--pub__uniquename'];
  72. }
  73. // Check $form_state['values'] to see if an AJAX call set the values.
  74. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  75. // $record_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $pkey);
  76. // $fkey_value = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $fkey);
  77. // $pub_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__pub_id');
  78. // $title = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__title');
  79. }
  80. $schema = chado_get_schema('pub');
  81. $widget['#table_name'] = $table_name;
  82. $widget['#fkey_field'] = $fkey;
  83. // $widget['#element_validate'] = array('chado_linker__pub_widget_validate');
  84. $widget['#theme'] = 'chado_linker__pub_widget';
  85. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  86. $widget['#suffix'] = "</span>";
  87. $widget['value'] = array(
  88. '#type' => 'value',
  89. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  90. );
  91. $widget[$table_name . '__' . $pkey] = array(
  92. '#type' => 'value',
  93. '#default_value' => $record_id,
  94. );
  95. $widget[$table_name . '__' . $fkey] = array(
  96. '#type' => 'value',
  97. '#default_value' => $fkey_value,
  98. );
  99. $widget[$table_name . '__pub_id'] = array(
  100. '#type' => 'value',
  101. '#default_value' => $pub_id,
  102. );
  103. $widget[$table_name . '--pub__uniquename'] = array(
  104. '#type' => 'textfield',
  105. '#title' => t('Publication ID'),
  106. '#default_value' => $title,
  107. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/pub',
  108. '#ajax' => array(
  109. 'callback' => "chado_linker__pub_widget_form_ajax_callback",
  110. 'wrapper' => "$table_name-$delta",
  111. 'effect' => 'fade',
  112. 'method' => 'replace'
  113. ),
  114. '#maxlength' => 100000,
  115. );
  116. }
  117. public function widgetFormSubmit($entity_type, $entity, $langcode, &$items, $form, &$form_state) {
  118. $field_name = $element['#field_name'];
  119. $delta = $element['#delta'];
  120. $table_name = $element['#table_name'];
  121. $fkey = $element['#fkey_field'];
  122. // If the form ID is field_ui_field_edit_form, then the user is editing the
  123. // field's values in the manage fields form of Drupal. We don't want
  124. // to validate it as if it were being used in a data entry form.
  125. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  126. return;
  127. }
  128. // Get the field values.
  129. // $fkey_value = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $fkey);
  130. // $pub_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__pub_id');
  131. // $uname = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '--pub__uniquename');
  132. // If the user provided a uniquename then we want to set the
  133. // foreign key value to be the chado_record_idd
  134. if ($uname) {
  135. // Get the pub. If one with the same name and type is already present
  136. // then use that. Otherwise, insert a new one.
  137. if (!$pub_id) {
  138. $pub = chado_generate_var('pub', array('uniquename' => $uname));
  139. // Set the pub_id and FK value
  140. tripal_chado_set_field_form_values($field_name, $form_state, $pub->pub_id, $delta, $table_name . '__pub_id');
  141. $fkey_value = $element['#entity']->chado_record_id;
  142. tripal_chado_set_field_form_values($field_name, $form_state, $fkey_value, $delta, $table_name . '__' . $fkey);
  143. }
  144. }
  145. else {
  146. // If the $syn_name is not set, then remove the linker FK value to the base table.
  147. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $fkey);
  148. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__pub_id');
  149. }
  150. }
  151. /**
  152. * @see TripalField::load()
  153. */
  154. public function load($entity, $details = array()) {
  155. $record = $details['record'];
  156. $field_name = $this->field['field_name'];
  157. $field_type = $this->field['type'];
  158. $field_table = $this->field['settings']['chado_table'];
  159. $field_column = $this->field['settings']['chado_column'];
  160. $base_table = $this->field['settings']['base_table'];
  161. // Get the FK that links to the base record.
  162. $schema = chado_get_schema($field_table);
  163. $pkey = $schema['primary key'][0];
  164. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  165. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  166. // Set some defaults for the empty record.
  167. $entity->{$field_name}['und'][0] = array(
  168. 'value' => array(),
  169. $field_table . '__' . $pkey => '',
  170. $field_table . '__' . $fkey_lcolumn => '',
  171. $field_table . '__' . 'pub_id' => '',
  172. $field_table . '--' . 'pub__uniquename' => '',
  173. );
  174. $linker_table = $base_table . '_pub';
  175. $options = array(
  176. 'return_array' => 1,
  177. );
  178. $record = chado_expand_var($record, 'table', $linker_table, $options);
  179. if (count($record->$linker_table) > 0) {
  180. $i = 0;
  181. foreach ($record->$linker_table as $index => $linker) {
  182. $pub = $linker->pub_id;
  183. $pub_details = tripal_get_minimal_pub_info($pub);
  184. $pub_details['@type'] = $pub->type_id->dbxref_id->db_id->name . ':' . $pub->type_id->dbxref_id->accession;
  185. $pub_details['publication']['type'] = $pub->type_id->name;
  186. $entity->{$field_name}['und'][$i]['value'] = $pub_details;
  187. $entity->{$field_name}['und'][$i][$field_table . '__' . $pkey] = $linker->$pkey;
  188. $entity->{$field_name}['und'][$i][$field_table . '__' . $fkey_lcolumn] = $linker->$fkey_lcolumn->$fkey_lcolumn;
  189. $entity->{$field_name}['und'][$i][$field_table . '__' . 'pub_id'] = $pub->pub_id;
  190. $entity->{$field_name}['und'][$i][$field_table . '--' . 'pub__uniquename'] = $pub->uniquename;
  191. if (property_exists($pub, 'entity_id')) {
  192. $entity->{$field_name}['und'][$i]['value']['entity'] = 'TripalEntity:' . $pub->entity_id;
  193. }
  194. $i++;
  195. }
  196. }
  197. }
  198. }
  199. /**
  200. * An Ajax callback for the pub widget.
  201. */
  202. function chado_linker__pub_widget_form_ajax_callback($form, $form_state) {
  203. $field_name = $form_state['triggering_element']['#parents'][0];
  204. $delta = $form_state['triggering_element']['#parents'][2];
  205. return $form[$field_name]['und'][$delta];
  206. }
  207. /**
  208. * Theme function for the pub widget.
  209. *
  210. * @param $variables
  211. */
  212. function theme_chado_linker__pub_widget($variables) {
  213. $element = $variables['element'];
  214. // These two fields were added to the widget to help identify the fields
  215. // for layout.
  216. $table_name = $element['#table_name'];
  217. $fkey = $element['#fkey_field'];
  218. $layout = "
  219. <div class=\"pub-widget\">
  220. <div class=\"pub-widget-item\">" .
  221. drupal_render($element[$table_name . '--pub__uniquename']) . "
  222. </div>
  223. </div>
  224. ";
  225. return $layout;
  226. }