chado_linker__pub.inc 13 KB

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