chado_linker__cvterm.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. class chado_linker__cvterm extends TripalField {
  3. /**
  4. * @see TripalField::field_info()
  5. */
  6. function field_info() {
  7. return array(
  8. 'label' => t('Annotations'),
  9. 'description' => t('This record can be annotated with terms
  10. from other vocabularies.'),
  11. 'default_widget' => 'chado_linker__cvterm_widget',
  12. 'default_formatter' => 'chado_linker__cvterm_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::attach_info()
  23. */
  24. function attach_info($entity_type, $bundle, $target) {
  25. $field_info = array();
  26. // This field is only attached by the chado_linker__cvterm_addr field.
  27. return $field_info;
  28. }
  29. /**
  30. * @see TripalField::widget_info()
  31. */
  32. function widget_info() {
  33. return array(
  34. 'label' => t('Annotations'),
  35. 'field types' => array('chado_linker__cvterm'),
  36. 'description' => t('This record can be annotated with terms
  37. from other vocabularies.'),
  38. );
  39. }
  40. /**
  41. * @see TripalField::formatter_info()
  42. */
  43. function formatter_info() {
  44. return array(
  45. 'label' => t('Annotations'),
  46. 'field types' => array('chado_linker__cvterm'),
  47. 'settings' => array(
  48. ),
  49. );
  50. }
  51. /**
  52. * @see TripalField::formatter_view()
  53. */
  54. function formatter_view(&$element, $entity_type, $entity, $field,
  55. $instance, $langcode, $items, $display) {
  56. $headers = array('Term', 'Definition', 'Is Not', 'Reference');
  57. $rows = array();
  58. $chado_table = $field['settings']['chado_table'];
  59. foreach ($items as $delta => $item) {
  60. if ($item[$chado_table . '__cvterm_id']) {
  61. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $item[$chado_table . '__cvterm_id']));
  62. $dbxref = $cvterm->dbxref_id;
  63. // Build the accession.
  64. $accession = $dbxref->db_id->name . ':' . $dbxref->accession;
  65. if ($dbxref->db_id->urlprefix) {
  66. $accession = l($accession, $dbxref->db_id->urlprefix . '/' . $dbxref->accession, array('attributes' => array('target' => '_blank')));
  67. }
  68. // Build the publication reference.
  69. $pub_ref = '';
  70. $pub_id = $item[$chado_table . '__pub_id'];
  71. if ($pub_id) {
  72. $pub = chado_generate_var('pub', array('pub_id' => $pub_id));
  73. $pub_ref = $pub->title;
  74. }
  75. $rows[] = array(
  76. $accession,
  77. $cvterm->definition,
  78. $item[$chado_table . '__is_not'] ? 'Yes' : '',
  79. '',
  80. );
  81. }
  82. }
  83. // the $table array contains the headers and rows array as well as other
  84. // options for controlling the display of the table. Additional
  85. // documentation can be found here:
  86. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  87. $table = array(
  88. 'header' => $headers,
  89. 'rows' => $rows,
  90. 'attributes' => array(
  91. 'id' => "$chado_table-table-terms",
  92. 'class' => 'tripal-data-table'
  93. ),
  94. 'caption' => '',
  95. 'sticky' => FALSE,
  96. 'colgroups' => array(),
  97. 'empty' => 'There are no annotations of this type',
  98. );
  99. $element[0] = array(
  100. '#type' => 'markup',
  101. '#markup' => theme_table($table),
  102. );
  103. }
  104. /**
  105. * @see TripalField::widget_form()
  106. */
  107. function widget_form(&$widget, $form, $form_state, $field, $instance,
  108. $langcode, $items, $delta, $element) {
  109. $entity = $form['#entity'];
  110. $field_name = $field['field_name'];
  111. $matches = array();
  112. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  113. // If the field name is not properly formatted then we can't tell what
  114. // table and type this is. So just return.
  115. if (count($matches) != 3) {
  116. return $widget;
  117. }
  118. $table_name = $matches[1];
  119. $cv_id = $matches[2];
  120. // Get the FK column that links to the base table.
  121. $chado_table = $field['settings']['chado_table'];
  122. $base_table = $field['settings']['base_table'];
  123. $schema = chado_get_schema($chado_table);
  124. $pkey = $schema['primary key'][0];
  125. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  126. $fkey = $fkeys[0];
  127. // Get the field defaults.
  128. $record_id = '';
  129. $fkey_value = $element['#entity']->chado_record_id;
  130. $cvterm_name = '';
  131. $cvterm_id = '';
  132. $is_not = '';
  133. $cvterm = NULL;
  134. // If the field already has a value then it will come through the $items
  135. // array. This happens when editing an existing record.
  136. if (array_key_exists($delta, $items)) {
  137. $record_id = $items[$delta]['value'];
  138. $fkey_value = $items[$delta][$table_name . '__' . $fkey];
  139. $cvterm_name = $items[$delta][$table_name . '--cvterm__name'];
  140. $is_not = $items[$delta][$table_name . '__is_not'];
  141. $cvterm_id = $items[$delta][$table_name . '__cvterm_id'];
  142. }
  143. // Check $form_state['values'] to see if an AJAX call set the values.
  144. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  145. $record_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name);
  146. $fkey_value = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $fkey);
  147. $is_not = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__is_not');
  148. $cvterm_name = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '--cvterm__name');
  149. }
  150. if ($cvterm_name) {
  151. $cvterm = chado_generate_var('cvterm', array('cv_id' => $cv_id, 'name' => $cvterm_name));
  152. }
  153. $schema = chado_get_schema('cvterm');
  154. $options = tripal_get_cv_select_options();
  155. $widget['#table_name'] = $chado_table;
  156. $widget['#fkey_field'] = $fkey;
  157. $widget['#element_validate'] = array('chado_linker__cvterm_widget_validate');
  158. $widget['#theme'] = 'chado_linker__cvterm_widget';
  159. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  160. $widget['#suffix'] = "</span>";
  161. $widget['value'] = array(
  162. '#type' => 'value',
  163. '#value' => $items[$delta]['value'],
  164. );
  165. $widget[$table_name . '__' . $pkey] = array(
  166. '#type' => 'value',
  167. '#default_value' => $record_id,
  168. );
  169. $widget[$table_name . '--cvterm__cv_id--cv__cv_id'] = array(
  170. '#type' => 'value',
  171. '#default_value' => $cv_id,
  172. );
  173. $widget[$table_name . '__cvterm_id'] = array(
  174. '#type' => 'value',
  175. '#default_value' => $cvterm ? $cvterm->cvterm_id : '',
  176. );
  177. $widget[$table_name . '__' . $fkey] = array(
  178. '#type' => 'value',
  179. '#default_value' => $fkey_value,
  180. );
  181. $widget[$table_name . '--cvterm__name'] = array(
  182. '#type' => 'textfield',
  183. '#title' => t('Term Name'),
  184. '#default_value' => $cvterm_name,
  185. '#required' => $element['#required'],
  186. '#maxlength' => array_key_exists('length', $schema['fields']['name']) ? $schema['fields']['name']['length'] : 255,
  187. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/cvterm/' . $cv_id,
  188. '#ajax' => array(
  189. 'callback' => "chado_linker__cvterm_widget_form_ajax_callback",
  190. 'wrapper' => "$table_name-$delta",
  191. 'effect' => 'fade',
  192. 'method' => 'replace'
  193. ),
  194. );
  195. $widget[$table_name . '__is_not'] = array(
  196. '#type' => 'checkbox',
  197. '#title' => t('Is Not'),
  198. '#default_value' => $is_not,
  199. '#required' => $element['#required'],
  200. );
  201. $widget[$table_name . '--cvterm__definition'] = array(
  202. '#type' => 'item',
  203. '#markup' => '',
  204. );
  205. }
  206. /**
  207. * @see TripalField::load()
  208. */
  209. function load($field, $entity, $details) {
  210. $record = $details['record'];
  211. $field_name = $field['field_name'];
  212. $field_type = $field['type'];
  213. $field_table = $field['settings']['chado_table'];
  214. $field_column = $field['settings']['chado_column'];
  215. $matches = array();
  216. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  217. $table_name = $matches[1];
  218. $cv_id = $matches[2];
  219. // Get the FK that links to the base record.
  220. $schema = chado_get_schema($field_table);
  221. $pkey = $schema['primary key'][0];
  222. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  223. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  224. // Set some defaults for the empty record.
  225. $entity->{$field_name}['und'][0] = array(
  226. 'value' => '',
  227. $field_table . '__' . $fkey_lcolumn => '',
  228. $field_table . '__' . 'cvterm_id' => '',
  229. // The pub column is present in the cell_line_cvterm, feature_cvterm,
  230. // library_cvterm, phenotype_comparision_cvterm, phenotype_cvterm,
  231. // stock_cvterm, and stock_relationship_cvterm.
  232. $field_table . '__' . 'pub_id' => '',
  233. // The is_not column is present in feature_cvterm and stock_cvterm tables.
  234. $field_table . '__' . 'is_not' => '',
  235. // The rank column is present in the cell_line_cvterm, expression_cvterm,
  236. // feature_cvterm, phenotype_comparision_cvterm, phenotype_cvterm,
  237. // and stock_cvterm tables.
  238. $field_table . '__' . 'rank' => '',
  239. // The cvterm_type_id is present in the expression_cvterm table.
  240. $field_table . '--' . 'cvterm_type_id' => '',
  241. // The following field are to help link the cvterm.
  242. $field_table . '--' . 'cvterm__cv_id--cv__cv_id' => '',
  243. $field_table . '--' . 'cvterm__name' => '',
  244. );
  245. // Get the annotations associated with this base record for this fields type.
  246. $columns = array('*');
  247. $match = array(
  248. $fkey_lcolumn => $record->$fkey_rcolumn,
  249. 'cvterm_id' => array(
  250. 'cv_id' => $cv_id,
  251. ),
  252. );
  253. $options = array(
  254. 'return_array' => TRUE,
  255. 'order_by' => array('rank' => 'ASC')
  256. );
  257. $fcvterms = chado_select_record($field_table, $columns, $match, $options);
  258. for ($i = 0; $i < count($fcvterms); $i++) {
  259. $linker = $fcvterms[$i];
  260. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $linker->cvterm_id));
  261. $entity->{$field_name}['und'][$i] = array(
  262. 'value' => $linker->$pkey,
  263. $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn,
  264. $field_table . '__' . 'cvterm_id' => $linker->cvterm_id,
  265. $field_table . '__' . 'pub_id' => property_exists($linker, 'pub_id') ? $linker->pub_id : '',
  266. $field_table . '__' . 'is_not' => property_exists($linker, 'is_not') ? $linker->is_not : '',
  267. $field_table . '__' . 'rank' => property_exists($linker, 'rank') ? $linker->rank : '',
  268. $field_table . '__' . 'cvterm_type_id' => property_exists($linker, 'cvterm_type_id') ? $linker->cvterm_type_id : '',
  269. $field_table . '--' . 'cvterm__cv_id--cv__cv_id' => $cvterm->cv_id->cv_id,
  270. $field_table . '--' . 'cvterm__name' => $cvterm->name,
  271. );
  272. }
  273. }
  274. }
  275. /**
  276. * Theme function for the dbxref_id_widget.
  277. *
  278. * @param $variables
  279. */
  280. function theme_chado_linker__cvterm_widget($variables) {
  281. $element = $variables['element'];
  282. // These two fields were added to the widget to help identify the fields
  283. // for layout.
  284. $table_name = $element['#table_name'];
  285. $fkey = $element['#fkey_field'];
  286. $layout = "
  287. <div class=\"annotation-cvterm-widget\">
  288. <div class=\"annotation-cvterm-widget-item\">" .
  289. drupal_render($element[$table_name . '--cvterm__cv_id--cv__cv_id']) . "
  290. </div>
  291. <div class=\"annotation-cvterm-widget-item\">" .
  292. drupal_render($element[$table_name . '--cvterm__name']) . "
  293. </div>
  294. <div class=\"annotation-cvterm-widget-item\">" .
  295. drupal_render($element[$table_name . '__is_not']) . "
  296. </div>
  297. </div>
  298. ";
  299. return $layout;
  300. }
  301. /**
  302. * Callback function for validating the chado_linker__cvterm_widget.
  303. */
  304. function chado_linker__cvterm_widget_validate($element, &$form_state) {
  305. $field_name = $element['#field_name'];
  306. $delta = $element['#delta'];
  307. $table_name = $element['#table_name'];
  308. $fkey = $element['#fkey_field'];
  309. // If the form ID is field_ui_field_edit_form, then the user is editing the
  310. // field's values in the manage fields form of Drupal. We don't want
  311. // to validate it as if it were being used in a data entry form.
  312. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  313. return;
  314. }
  315. // Get the field values.
  316. $cvterm_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__cvterm_id');
  317. $cv_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '--cvterm__cv_id--cv__cv_id');
  318. $cvterm_name = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '--cvterm__name');
  319. $cvterm_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__cvterm_id');
  320. $pub_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__pub_id');
  321. // If the user provided a cv_id and a name then we want to set the
  322. // foreign key value to be the chado_record_idd
  323. if ($cvterm_name) {
  324. $fkey_value = $element['#entity']->chado_record_id;
  325. tripal_chado_set_field_form_values($field_name, $form_state, $fkey_value, $delta, $table_name . '__' . $fkey);
  326. // Get the cvterm ID. If one is not available because it's a newly added
  327. // record, then we need to retrieve it and set the form element.
  328. if (!$cvterm_id) {
  329. $cvterm = tripal_get_cvterm(array('cv_id' => $cv_id, 'name' => $cvterm_name));
  330. tripal_chado_set_field_form_values($field_name, $form_state, $cvterm->cvterm_id, $delta, $table_name . '__cvterm_id');
  331. }
  332. if (!$pub_id) {
  333. $pub = chado_generate_var('pub', array('uniquename' => 'null'));
  334. tripal_chado_set_field_form_values($field_name, $form_state, $pub->pub_id, $delta, $table_name . '__pub_id');
  335. }
  336. }
  337. else {
  338. // If the $cv_id and name are not set, then remove the linker FK value to the base table.
  339. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $fkey);
  340. }
  341. }
  342. /**
  343. * An Ajax callback for the dbxref widget.
  344. */
  345. function chado_linker__cvterm_widget_form_ajax_callback($form, $form_state) {
  346. $field_name = $form_state['triggering_element']['#parents'][0];
  347. $delta = $form_state['triggering_element']['#parents'][2];
  348. return $form[$field_name]['und'][$delta];
  349. }