chado_linker__pub.inc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. $pub_id = $items[$delta][$table_name . '__pub_id'];
  70. $title = $items[$delta][$table_name . '--pub__uniquename'];
  71. }
  72. // Check $form_state['values'] to see if an AJAX call set the values.
  73. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  74. // $record_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $pkey);
  75. // $fkey_value = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $fkey);
  76. // $pub_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__pub_id');
  77. // $title = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__title');
  78. }
  79. $schema = chado_get_schema('pub');
  80. $widget['#table_name'] = $table_name;
  81. $widget['#fkey_field'] = $fkey;
  82. $widget['#theme'] = 'chado_linker__pub_widget';
  83. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  84. $widget['#suffix'] = "</span>";
  85. $widget['value'] = array(
  86. '#type' => 'value',
  87. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  88. );
  89. $widget[$table_name . '__' . $pkey] = array(
  90. '#type' => 'value',
  91. '#default_value' => $record_id,
  92. );
  93. $widget[$table_name . '__' . $fkey] = array(
  94. '#type' => 'value',
  95. '#default_value' => $fkey_value,
  96. );
  97. $widget[$table_name . '__pub_id'] = array(
  98. '#type' => 'value',
  99. '#default_value' => $pub_id,
  100. );
  101. $widget[$table_name . '--pub__uniquename'] = array(
  102. '#type' => 'textfield',
  103. '#title' => t('Publication'),
  104. '#default_value' => $title,
  105. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/pub',
  106. '#ajax' => array(
  107. 'callback' => "chado_linker__pub_widget_form_ajax_callback",
  108. 'wrapper' => "$table_name-$delta",
  109. 'effect' => 'fade',
  110. 'method' => 'replace'
  111. ),
  112. '#maxlength' => 100000,
  113. );
  114. }
  115. /**
  116. * @see TripalField::widgetFormSubmit()
  117. */
  118. public function widgetFormSubmit($entity_type, $entity, $langcode, &$items, $form, &$form_state) {
  119. // Get the FK column that links to the base table.
  120. $table_name = $this->field['settings']['chado_table'];
  121. $base_table = $this->field['settings']['base_table'];
  122. $schema = chado_get_schema($table_name);
  123. $pkey = $schema['primary key'][0];
  124. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  125. $fkey = $fkeys[0];
  126. // If the form ID is field_ui_field_edit_form, then the user is editing the
  127. // field's values in the manage fields form of Drupal. We don't want
  128. // to validate it as if it were being used in a data entry form.
  129. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  130. return;
  131. }
  132. // Get the field values.
  133. foreach ($items as $delta => $values) {
  134. $fkey_value = $values['value'];
  135. $pub_id = $values[$table_name . '__pub_id'];
  136. $uname = $values[$table_name . '--pub__uniquename'];
  137. // If the user provided a uniquename then we want to set the foreign key
  138. // value to be the chado_record_id
  139. if ($uname and !$pub_id) {
  140. $pub = chado_generate_var('pub', array('uniquename' => $uname));
  141. $items[$delta][$table_name . '__pub_id'] = $pub->pub_id;
  142. }
  143. // In the widgetFrom function we automatically add the foreign key
  144. // record. But if the user did not provide a publication we want to take
  145. // it out so that the Chado field_storage infrastructure won't try to
  146. // write a record.
  147. if (!$uname and !$pub_id) {
  148. $items[$delta][$table_name . '__' . $fkey] = '';
  149. }
  150. }
  151. }
  152. /**
  153. * @see TripalField::load()
  154. */
  155. public function load($entity, $details = array()) {
  156. $record = $details['record'];
  157. $field_name = $this->field['field_name'];
  158. $field_type = $this->field['type'];
  159. $field_table = $this->field['settings']['chado_table'];
  160. $field_column = $this->field['settings']['chado_column'];
  161. $base_table = $this->field['settings']['base_table'];
  162. // Get the FK that links to the base record.
  163. $schema = chado_get_schema($field_table);
  164. $pkey = $schema['primary key'][0];
  165. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  166. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  167. // Set some defaults for the empty record.
  168. $entity->{$field_name}['und'][0] = array(
  169. 'value' => array(),
  170. $field_table . '__' . $pkey => '',
  171. $field_table . '__' . $fkey_lcolumn => '',
  172. $field_table . '__' . 'pub_id' => '',
  173. $field_table . '--' . 'pub__uniquename' => '',
  174. );
  175. $linker_table = $base_table . '_pub';
  176. $options = array(
  177. 'return_array' => 1,
  178. );
  179. $record = chado_expand_var($record, 'table', $linker_table, $options);
  180. if (count($record->$linker_table) > 0) {
  181. $i = 0;
  182. foreach ($record->$linker_table as $index => $linker) {
  183. $pub = $linker->pub_id;
  184. $pub_details = tripal_get_minimal_pub_info($pub);
  185. $pub_details['@type'] = $pub->type_id->dbxref_id->db_id->name . ':' . $pub->type_id->dbxref_id->accession;
  186. $pub_details['publication']['type'] = $pub->type_id->name;
  187. $entity->{$field_name}['und'][$i]['value'] = $pub_details;
  188. $entity->{$field_name}['und'][$i][$field_table . '__' . $pkey] = $linker->$pkey;
  189. $entity->{$field_name}['und'][$i][$field_table . '__' . $fkey_lcolumn] = $linker->$fkey_lcolumn->$fkey_lcolumn;
  190. $entity->{$field_name}['und'][$i][$field_table . '__' . 'pub_id'] = $pub->pub_id;
  191. $entity->{$field_name}['und'][$i][$field_table . '--' . 'pub__uniquename'] = $pub->uniquename;
  192. if (property_exists($pub, 'entity_id')) {
  193. $entity->{$field_name}['und'][$i]['value']['entity'] = 'TripalEntity:' . $pub->entity_id;
  194. }
  195. $i++;
  196. }
  197. }
  198. }
  199. }
  200. /**
  201. * An Ajax callback for the pub widget.
  202. */
  203. function chado_linker__pub_widget_form_ajax_callback($form, $form_state) {
  204. $field_name = $form_state['triggering_element']['#parents'][0];
  205. $delta = $form_state['triggering_element']['#parents'][2];
  206. return $form[$field_name]['und'][$delta];
  207. }
  208. /**
  209. * Theme function for the pub widget.
  210. *
  211. * @param $variables
  212. */
  213. function theme_chado_linker__pub_widget($variables) {
  214. $element = $variables['element'];
  215. // These two fields were added to the widget to help identify the fields
  216. // for layout.
  217. $table_name = $element['#table_name'];
  218. $fkey = $element['#fkey_field'];
  219. $layout = "
  220. <div class=\"pub-widget\">
  221. <div class=\"pub-widget-item\">" .
  222. drupal_render($element[$table_name . '--pub__uniquename']) . "
  223. </div>
  224. </div>
  225. ";
  226. return $layout;
  227. }