chado_linker__cvterm.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. if (count($items) > 0) {
  117. $element[0] = array(
  118. '#type' => 'markup',
  119. '#markup' => theme_table($table),
  120. );
  121. }
  122. }
  123. /**
  124. * @see TripalField::widget_form()
  125. */
  126. public function widget_form(&$widget, &$form, &$form_state, $field, $instance,
  127. $langcode, $items, $delta, $element) {
  128. $entity = $form['#entity'];
  129. $field_name = $field['field_name'];
  130. $matches = array();
  131. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  132. // If the field name is not properly formatted then we can't tell what
  133. // table and type this is. So just return.
  134. if (count($matches) != 3) {
  135. return $widget;
  136. }
  137. $table_name = $matches[1];
  138. $cv_id = $matches[2];
  139. // Get the FK column that links to the base table.
  140. $chado_table = $field['settings']['chado_table'];
  141. $base_table = $field['settings']['base_table'];
  142. $schema = chado_get_schema($chado_table);
  143. $pkey = $schema['primary key'][0];
  144. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  145. $fkey = $fkeys[0];
  146. // Get the field defaults.
  147. $record_id = '';
  148. $fkey_value = $element['#entity']->chado_record_id;
  149. $cvterm_name = '';
  150. $cvterm_id = '';
  151. $is_not = '';
  152. $cvterm = NULL;
  153. // If the field already has a value then it will come through the $items
  154. // array. This happens when editing an existing record.
  155. if (array_key_exists($delta, $items)) {
  156. $record_id = $items[$delta]['value'];
  157. $fkey_value = $items[$delta][$table_name . '__' . $fkey];
  158. $cvterm_name = $items[$delta][$table_name . '--cvterm__name'];
  159. $is_not = $items[$delta][$table_name . '__is_not'];
  160. $cvterm_id = $items[$delta][$table_name . '__cvterm_id'];
  161. }
  162. // Check $form_state['values'] to see if an AJAX call set the values.
  163. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  164. $record_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name);
  165. $fkey_value = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $fkey);
  166. $is_not = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__is_not');
  167. $cvterm_name = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '--cvterm__name');
  168. }
  169. if ($cvterm_name) {
  170. $cvterm = chado_generate_var('cvterm', array('cv_id' => $cv_id, 'name' => $cvterm_name));
  171. }
  172. $schema = chado_get_schema('cvterm');
  173. $options = tripal_get_cv_select_options();
  174. $widget['#table_name'] = $chado_table;
  175. $widget['#fkey_field'] = $fkey;
  176. $widget['#element_validate'] = array('chado_linker__cvterm_widget_validate');
  177. $widget['#theme'] = 'chado_linker__cvterm_widget';
  178. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  179. $widget['#suffix'] = "</span>";
  180. $widget['value'] = array(
  181. '#type' => 'value',
  182. '#value' => $items[$delta]['value'],
  183. );
  184. $widget[$table_name . '__' . $pkey] = array(
  185. '#type' => 'value',
  186. '#default_value' => $record_id,
  187. );
  188. $widget[$table_name . '--cvterm__cv_id--cv__cv_id'] = array(
  189. '#type' => 'value',
  190. '#default_value' => $cv_id,
  191. );
  192. $widget[$table_name . '__cvterm_id'] = array(
  193. '#type' => 'value',
  194. '#default_value' => $cvterm ? $cvterm->cvterm_id : '',
  195. );
  196. $widget[$table_name . '__' . $fkey] = array(
  197. '#type' => 'value',
  198. '#default_value' => $fkey_value,
  199. );
  200. $widget[$table_name . '--cvterm__name'] = array(
  201. '#type' => 'textfield',
  202. '#title' => t('Term Name'),
  203. '#default_value' => $cvterm_name,
  204. '#required' => $element['#required'],
  205. '#maxlength' => array_key_exists('length', $schema['fields']['name']) ? $schema['fields']['name']['length'] : 255,
  206. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/cvterm/' . $cv_id,
  207. '#ajax' => array(
  208. 'callback' => "chado_linker__cvterm_widget_form_ajax_callback",
  209. 'wrapper' => "$table_name-$delta",
  210. 'effect' => 'fade',
  211. 'method' => 'replace'
  212. ),
  213. );
  214. $widget[$table_name . '__is_not'] = array(
  215. '#type' => 'checkbox',
  216. '#title' => t('Is Not'),
  217. '#default_value' => $is_not,
  218. '#required' => $element['#required'],
  219. );
  220. $widget[$table_name . '--cvterm__definition'] = array(
  221. '#type' => 'item',
  222. '#markup' => '',
  223. );
  224. }
  225. /**
  226. * @see TripalField::load()
  227. */
  228. function load($field, $entity, $details) {
  229. $record = $details['record'];
  230. $field_name = $field['field_name'];
  231. $field_type = $field['type'];
  232. $field_table = $field['settings']['chado_table'];
  233. $field_column = $field['settings']['chado_column'];
  234. $matches = array();
  235. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  236. $table_name = $matches[1];
  237. $cv_id = $matches[2];
  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' => '',
  246. $field_table . '__' . $fkey_lcolumn => '',
  247. $field_table . '__' . 'cvterm_id' => '',
  248. // The pub column is present in the cell_line_cvterm, feature_cvterm,
  249. // library_cvterm, phenotype_comparision_cvterm, phenotype_cvterm,
  250. // stock_cvterm, and stock_relationship_cvterm.
  251. $field_table . '__' . 'pub_id' => '',
  252. // The is_not column is present in feature_cvterm and stock_cvterm tables.
  253. $field_table . '__' . 'is_not' => '',
  254. // The rank column is present in the cell_line_cvterm, expression_cvterm,
  255. // feature_cvterm, phenotype_comparision_cvterm, phenotype_cvterm,
  256. // and stock_cvterm tables.
  257. $field_table . '__' . 'rank' => '',
  258. // The cvterm_type_id is present in the expression_cvterm table.
  259. $field_table . '--' . 'cvterm_type_id' => '',
  260. // The following field are to help link the cvterm.
  261. $field_table . '--' . 'cvterm__cv_id--cv__cv_id' => '',
  262. $field_table . '--' . 'cvterm__name' => '',
  263. );
  264. // Get the annotations associated with this base record for this fields type.
  265. $columns = array('*');
  266. $match = array(
  267. $fkey_lcolumn => $record->$fkey_rcolumn,
  268. 'cvterm_id' => array(
  269. 'cv_id' => $cv_id,
  270. ),
  271. );
  272. $options = array(
  273. 'return_array' => TRUE,
  274. 'order_by' => array('rank' => 'ASC')
  275. );
  276. $fcvterms = chado_select_record($field_table, $columns, $match, $options);
  277. for ($i = 0; $i < count($fcvterms); $i++) {
  278. $linker = $fcvterms[$i];
  279. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $linker->cvterm_id));
  280. $entity->{$field_name}['und'][$i] = array(
  281. 'value' => $linker->$pkey,
  282. $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn,
  283. $field_table . '__' . 'cvterm_id' => $linker->cvterm_id,
  284. $field_table . '__' . 'pub_id' => property_exists($linker, 'pub_id') ? $linker->pub_id : '',
  285. $field_table . '__' . 'is_not' => property_exists($linker, 'is_not') ? $linker->is_not : '',
  286. $field_table . '__' . 'rank' => property_exists($linker, 'rank') ? $linker->rank : '',
  287. $field_table . '__' . 'cvterm_type_id' => property_exists($linker, 'cvterm_type_id') ? $linker->cvterm_type_id : '',
  288. $field_table . '--' . 'cvterm__cv_id--cv__cv_id' => $cvterm->cv_id->cv_id,
  289. $field_table . '--' . 'cvterm__name' => $cvterm->name,
  290. );
  291. }
  292. }
  293. }
  294. /**
  295. * Theme function for the dbxref_id_widget.
  296. *
  297. * @param $variables
  298. */
  299. function theme_chado_linker__cvterm_widget($variables) {
  300. $element = $variables['element'];
  301. // These two fields were added to the widget to help identify the fields
  302. // for layout.
  303. $table_name = $element['#table_name'];
  304. $fkey = $element['#fkey_field'];
  305. $layout = "
  306. <div class=\"annotation-cvterm-widget\">
  307. <div class=\"annotation-cvterm-widget-item\">" .
  308. drupal_render($element[$table_name . '--cvterm__cv_id--cv__cv_id']) . "
  309. </div>
  310. <div class=\"annotation-cvterm-widget-item\">" .
  311. drupal_render($element[$table_name . '--cvterm__name']) . "
  312. </div>
  313. <div class=\"annotation-cvterm-widget-item\">" .
  314. drupal_render($element[$table_name . '__is_not']) . "
  315. </div>
  316. </div>
  317. ";
  318. return $layout;
  319. }
  320. /**
  321. * Callback function for validating the chado_linker__cvterm_widget.
  322. */
  323. function chado_linker__cvterm_widget_validate($element, &$form_state) {
  324. $field_name = $element['#field_name'];
  325. $delta = $element['#delta'];
  326. $table_name = $element['#table_name'];
  327. $fkey = $element['#fkey_field'];
  328. // If the form ID is field_ui_field_edit_form, then the user is editing the
  329. // field's values in the manage fields form of Drupal. We don't want
  330. // to validate it as if it were being used in a data entry form.
  331. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  332. return;
  333. }
  334. // Get the field values.
  335. $cvterm_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__cvterm_id');
  336. $cv_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '--cvterm__cv_id--cv__cv_id');
  337. $cvterm_name = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '--cvterm__name');
  338. $cvterm_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__cvterm_id');
  339. $pub_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__pub_id');
  340. // If the user provided a cv_id and a name then we want to set the
  341. // foreign key value to be the chado_record_idd
  342. if ($cvterm_name) {
  343. $fkey_value = $element['#entity']->chado_record_id;
  344. tripal_chado_set_field_form_values($field_name, $form_state, $fkey_value, $delta, $table_name . '__' . $fkey);
  345. // Get the cvterm ID. If one is not available because it's a newly added
  346. // record, then we need to retrieve it and set the form element.
  347. if (!$cvterm_id) {
  348. $cvterm = tripal_get_cvterm(array('cv_id' => $cv_id, 'name' => $cvterm_name));
  349. tripal_chado_set_field_form_values($field_name, $form_state, $cvterm->cvterm_id, $delta, $table_name . '__cvterm_id');
  350. }
  351. if (!$pub_id) {
  352. $pub = chado_generate_var('pub', array('uniquename' => 'null'));
  353. tripal_chado_set_field_form_values($field_name, $form_state, $pub->pub_id, $delta, $table_name . '__pub_id');
  354. }
  355. }
  356. else {
  357. // If the $cv_id and name are not set, then remove the linker FK value to the base table.
  358. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $fkey);
  359. }
  360. }
  361. /**
  362. * An Ajax callback for the dbxref widget.
  363. */
  364. function chado_linker__cvterm_widget_form_ajax_callback($form, $form_state) {
  365. $field_name = $form_state['triggering_element']['#parents'][0];
  366. $delta = $form_state['triggering_element']['#parents'][2];
  367. return $form[$field_name]['und'][$delta];
  368. }