chado_linker__pub.inc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. );
  17. // Set this to the name of the storage backend that by default will support
  18. // this field.
  19. public static $default_storage = 'field_chado_storage';
  20. /**
  21. * @see TripalField::formatterView()
  22. */
  23. public function formatterView(&$element, $entity_type, $entity, $langcode, $items, $display) {
  24. $list_items = array();
  25. $chado_table = $this->instance['settings']['chado_table'];
  26. foreach ($items as $delta => $item) {
  27. if ($item['chado-' . $chado_table . '__pub_id']) {
  28. $pub = chado_generate_var('pub', array('pub_id' => $item['chado-' . $chado_table . '__pub_id']));
  29. $list_items[$pub->pyear] = $pub->uniquename;
  30. }
  31. }
  32. krsort($list_items, SORT_NUMERIC);
  33. $list = array(
  34. 'title' => '',
  35. 'items' => $list_items,
  36. 'type' => 'ol',
  37. 'attributes' => array(),
  38. );
  39. if (count($items) > 0) {
  40. $element[0] = array(
  41. '#type' => 'markup',
  42. '#markup' => theme_item_list($list),
  43. );
  44. }
  45. }
  46. /**
  47. * @see TripalField::widgetForm()
  48. */
  49. public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  50. parent::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->instance['settings']['chado_table'];
  55. $base_table = $this->instance['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. $uname = '';
  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 = tripal_get_field_item_keyval($items, $delta, 'chado-' . $table_name . '__' . $pkey, $record_id);
  69. $pub_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $table_name . '__pub_id', $pub_id);
  70. $uname = tripal_get_field_item_keyval($items, $delta, 'uniquename', $uname);
  71. }
  72. $schema = chado_get_schema('pub');
  73. $widget['#table_name'] = $table_name;
  74. $widget['#fkey_field'] = $fkey;
  75. $widget['#theme'] = 'chado_linker__pub_widget';
  76. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  77. $widget['#suffix'] = "</span>";
  78. $widget['value'] = array(
  79. '#type' => 'value',
  80. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  81. );
  82. $widget['chado-' . $table_name . '__' . $pkey] = array(
  83. '#type' => 'value',
  84. '#default_value' => $record_id,
  85. );
  86. $widget['chado-' . $table_name . '__' . $fkey] = array(
  87. '#type' => 'value',
  88. '#default_value' => $fkey_value,
  89. );
  90. $widget['chado-' . $table_name . '__pub_id'] = array(
  91. '#type' => 'value',
  92. '#default_value' => $pub_id,
  93. );
  94. $widget['uniquename'] = array(
  95. '#type' => 'textfield',
  96. '#title' => t('Publication'),
  97. '#default_value' => $uname,
  98. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/pub',
  99. '#ajax' => array(
  100. 'callback' => "chado_linker__pub_widget_form_ajax_callback",
  101. 'wrapper' => "$table_name-$delta",
  102. 'effect' => 'fade',
  103. 'method' => 'replace'
  104. ),
  105. '#maxlength' => 100000,
  106. );
  107. }
  108. /**
  109. * @see TripalField::widgetFormSubmit()
  110. */
  111. public function widgetFormSubmit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  112. // Get the FK column that links to the base table.
  113. $table_name = $this->instance['settings']['chado_table'];
  114. $base_table = $this->instance['settings']['base_table'];
  115. $schema = chado_get_schema($table_name);
  116. $pkey = $schema['primary key'][0];
  117. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  118. $fkey = $fkeys[0];
  119. $field_name = $this->field['field_name'];
  120. // Get the field values.
  121. $fkey_value = isset($form_state['values'][$field_name][$langcode][$delta]['value']) ? $form_state['values'][$field_name][$langcode][$delta]['value'] : '';
  122. $pub_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id'] : '';
  123. $uname = isset($form_state['values'][$field_name][$langcode][$delta]['uniquename']) ? $form_state['values'][$field_name][$langcode][$delta]['uniquename'] : '';
  124. // If the user provided a uniquename then we want to set the foreign key
  125. // value to be the chado_record_id
  126. if ($uname and !$pub_id) {
  127. $pub = chado_generate_var('pub', array('uniquename' => $uname));
  128. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id'] = $pub->pub_id;
  129. }
  130. // In the widgetFrom function we automatically add the foreign key
  131. // record. But if the user did not provide a publication we want to take
  132. // it out so that the Chado field_storage infrastructure won't try to
  133. // write a record.
  134. if (!$uname and !$pub_id) {
  135. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] = '';
  136. }
  137. // If the user removed the publication from the pub_uniquename field
  138. // then we want to clear out the rest of the hidden values.
  139. // Leave the primary key so the record can be deleted.
  140. if (!$uname and $pub_id) {
  141. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] = '';
  142. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id'] = '';
  143. }
  144. }
  145. /**
  146. * @see TripalField::load()
  147. */
  148. public function load($entity, $details = array()) {
  149. $record = $details['record'];
  150. $field_name = $this->field['field_name'];
  151. $field_type = $this->field['type'];
  152. $field_table = $this->instance['settings']['chado_table'];
  153. $field_column = $this->instance['settings']['chado_column'];
  154. $base_table = $this->instance['settings']['base_table'];
  155. // Get the FK that links to the base record.
  156. $schema = chado_get_schema($field_table);
  157. $pkey = $schema['primary key'][0];
  158. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  159. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  160. // Set some defaults for the empty record.
  161. $entity->{$field_name}['und'][0] = array(
  162. 'value' => array(),
  163. 'chado-' . $field_table . '__' . $pkey => '',
  164. 'chado-' . $field_table . '__' . $fkey_lcolumn => '',
  165. 'chado-' . $field_table . '__' . 'pub_id' => '',
  166. 'uniquename' => '',
  167. );
  168. $linker_table = $base_table . '_pub';
  169. $options = array(
  170. 'return_array' => 1,
  171. );
  172. $record = chado_expand_var($record, 'table', $linker_table, $options);
  173. if (count($record->$linker_table) > 0) {
  174. $i = 0;
  175. foreach ($record->$linker_table as $index => $linker) {
  176. $pub = $linker->pub_id;
  177. $pub_details = tripal_get_minimal_pub_info($pub);
  178. $pub_details['@type'] = $pub->type_id->dbxref_id->db_id->name . ':' . $pub->type_id->dbxref_id->accession;
  179. $pub_details['publication']['type'] = $pub->type_id->name;
  180. $entity->{$field_name}['und'][$i]['value'] = $pub_details;
  181. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . $pkey] = $linker->$pkey;
  182. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . $fkey_lcolumn] = $linker->$fkey_lcolumn->$fkey_lcolumn;
  183. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . 'pub_id'] = $pub->pub_id;
  184. $entity->{$field_name}['und'][$i]['uniquename'] = $pub->uniquename;
  185. if (property_exists($pub, 'entity_id')) {
  186. $entity->{$field_name}['und'][$i]['value']['entity'] = 'TripalEntity:' . $pub->entity_id;
  187. }
  188. $i++;
  189. }
  190. }
  191. }
  192. }
  193. /**
  194. * An Ajax callback for the pub widget.
  195. */
  196. function chado_linker__pub_widget_form_ajax_callback($form, $form_state) {
  197. $field_name = $form_state['triggering_element']['#parents'][0];
  198. $delta = $form_state['triggering_element']['#parents'][2];
  199. return $form[$field_name]['und'][$delta];
  200. }
  201. /**
  202. * Theme function for the pub widget.
  203. *
  204. * @param $variables
  205. */
  206. function theme_chado_linker__pub_widget($variables) {
  207. $element = $variables['element'];
  208. // These two fields were added to the widget to help identify the fields
  209. // for layout.
  210. $table_name = $element['#table_name'];
  211. $fkey = $element['#fkey_field'];
  212. $layout = "
  213. <div class=\"pub-widget\">
  214. <div class=\"pub-widget-item\">" .
  215. drupal_render($element['uniquename']) . "
  216. </div>
  217. </div>
  218. ";
  219. return $layout;
  220. }