chado_linker__cvterm.inc 15 KB

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