chado_linker__pub.inc 9.5 KB

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