chado_linker__pub.inc 12 KB

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