cvterm.inc 13 KB

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