chado_linker__pub.inc 13 KB

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