cvterm.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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[0] = 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. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  94. $fkey = $fkeys[0];
  95. // Get the field defaults.
  96. $record_id = '';
  97. $fkey_value = $element['#entity']->chado_record_id;
  98. $cvterm_name = '';
  99. $cvterm_id = '';
  100. $is_not = '';
  101. $cvterm = NULL;
  102. // If the field already has a value then it will come through the $items
  103. // array. This happens when editing an existing record.
  104. if (array_key_exists($delta, $items)) {
  105. $record_id = $items[$delta]['value'];
  106. $fkey_value = $items[$delta][$table_name . '__' . $fkey];
  107. $cvterm_name = $items[$delta][$table_name . '--cvterm__name'];
  108. $is_not = $items[$delta][$table_name . '__is_not'];
  109. $cvterm_id = $items[$delta][$table_name . '__cvterm_id'];
  110. }
  111. // Check $form_state['values'] to see if an AJAX call set the values.
  112. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  113. $record_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name);
  114. $fkey_value = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $fkey);
  115. $is_not = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__is_not');
  116. $cvterm_name = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '--cvterm__name');
  117. }
  118. if ($cvterm_name) {
  119. $cvterm = chado_generate_var('cvterm', array('cv_id' => $cv_id, 'name' => $cvterm_name));
  120. }
  121. $schema = chado_get_schema('cvterm');
  122. $options = tripal_get_cv_select_options();
  123. $widget['#table_name'] = $chado_table;
  124. $widget['#fkey_field'] = $fkey;
  125. $widget['#element_validate'] = array('tripal_chado_cvterm_widget_validate');
  126. $widget['#theme'] = 'tripal_chado_cvterm_widget';
  127. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  128. $widget['#suffix'] = "</span>";
  129. $widget['value'] = array(
  130. '#type' => 'value',
  131. '#default_value' => $record_id,
  132. );
  133. $widget[$table_name . '--cvterm__cv_id--cv__cv_id'] = array(
  134. '#type' => 'value',
  135. '#default_value' => $cv_id,
  136. );
  137. $widget[$table_name . '__cvterm_id'] = array(
  138. '#type' => 'value',
  139. '#default_value' => $cvterm ? $cvterm->cvterm_id : '',
  140. );
  141. $widget[$table_name . '__' . $fkey] = array(
  142. '#type' => 'value',
  143. '#default_value' => $fkey_value,
  144. );
  145. $widget[$table_name . '--cvterm__name'] = array(
  146. '#type' => 'textfield',
  147. '#title' => t('Term Name'),
  148. '#default_value' => $cvterm_name,
  149. '#required' => $element['#required'],
  150. '#maxlength' => array_key_exists('length', $schema['fields']['name']) ? $schema['fields']['name']['length'] : 255,
  151. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/cvterm/' . $cv_id,
  152. '#ajax' => array(
  153. 'callback' => "tripal_chado_cvterm_widget_form_ajax_callback",
  154. 'wrapper' => "$table_name-$delta",
  155. 'effect' => 'fade',
  156. 'method' => 'replace'
  157. ),
  158. );
  159. $widget[$table_name . '__is_not'] = array(
  160. '#type' => 'checkbox',
  161. '#title' => t('Is Not'),
  162. '#default_value' => $is_not,
  163. '#required' => $element['#required'],
  164. );
  165. $widget[$table_name . '--cvterm__definition'] = array(
  166. '#type' => 'item',
  167. '#markup' => '',
  168. );
  169. }
  170. /**
  171. * An Ajax callback for the dbxref widget.
  172. */
  173. function tripal_chado_cvterm_widget_form_ajax_callback($form, $form_state) {
  174. $field_name = $form_state['triggering_element']['#parents'][0];
  175. $delta = $form_state['triggering_element']['#parents'][2];
  176. return $form[$field_name]['und'][$delta];
  177. }
  178. /**
  179. * Callback function for validating the tripal_chado_organism_select_widget.
  180. */
  181. function tripal_chado_cvterm_widget_validate($element, &$form_state) {
  182. $field_name = $element['#field_name'];
  183. $delta = $element['#delta'];
  184. $table_name = $element['#table_name'];
  185. $fkey = $element['#fkey_field'];
  186. // If the form ID is field_ui_field_edit_form, then the user is editing the
  187. // field's values in the manage fields form of Drupal. We don't want
  188. // to validate it as if it were being used in a data entry form.
  189. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  190. return;
  191. }
  192. // Get the field values.
  193. $cvterm_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__cvterm_id');
  194. $cv_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '--cvterm__cv_id--cv__cv_id');
  195. $cvterm_name = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '--cvterm__name');
  196. $cvterm_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__cvterm_id');
  197. $pub_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__pub_id');
  198. // If the user provided a cv_id and a name then we want to set the
  199. // foreign key value to be the chado_record_idd
  200. if ($cvterm_name) {
  201. $fkey_value = $element['#entity']->chado_record_id;
  202. tripal_chado_set_field_form_values($field_name, $form_state, $fkey_value, $delta, $table_name . '__' . $fkey);
  203. // Get the cvterm ID. If one is not available because it's a newly added
  204. // record, then we need to retrieve it and set the form element.
  205. if (!$cvterm_id) {
  206. $cvterm = tripal_get_cvterm(array('cv_id' => $cv_id, 'name' => $cvterm_name));
  207. tripal_chado_set_field_form_values($field_name, $form_state, $cvterm->cvterm_id, $delta, $table_name . '__cvterm_id');
  208. }
  209. if (!$pub_id) {
  210. $pub = chado_generate_var('pub', array('uniquename' => 'null'));
  211. tripal_chado_set_field_form_values($field_name, $form_state, $pub->pub_id, $delta, $table_name . '__pub_id');
  212. }
  213. }
  214. else {
  215. // If the $cv_id and name are not set, then remove the linker FK value to the base table.
  216. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $fkey);
  217. }
  218. }
  219. /**
  220. * Theme function for the dbxref_id_widget.
  221. *
  222. * @param $variables
  223. */
  224. function theme_tripal_chado_cvterm_widget($variables) {
  225. $element = $variables['element'];
  226. // These two fields were added to the widget to help identify the fields
  227. // for layout.
  228. $table_name = $element['#table_name'];
  229. $fkey = $element['#fkey_field'];
  230. $layout = "
  231. <div class=\"annotation-cvterm-widget\">
  232. <div class=\"annotation-cvterm-widget-item\">" .
  233. drupal_render($element[$table_name . '--cvterm__cv_id--cv__cv_id']) . "
  234. </div>
  235. <div class=\"annotation-cvterm-widget-item\">" .
  236. drupal_render($element[$table_name . '--cvterm__name']) . "
  237. </div>
  238. <div class=\"annotation-cvterm-widget-item\">" .
  239. drupal_render($element[$table_name . '__is_not']) . "
  240. </div>
  241. </div>
  242. ";
  243. return $layout;
  244. }
  245. /**
  246. * Loads the field values with appropriate data.
  247. *
  248. * This function is called by the tripal_chado_field_storage_load() for
  249. * each property managed by the field_chado_storage storage type. This is
  250. * an optional hook function that is only needed if the field has
  251. * multiple form elements.
  252. *
  253. * @param $field
  254. * @param $entity
  255. * @param $base_table
  256. * @param $record
  257. */
  258. function tripal_chado_cvterm_field_load($field, $entity, $base_table, $record) {
  259. $field_name = $field['field_name'];
  260. $field_type = $field['type'];
  261. $field_table = $field['settings']['chado_table'];
  262. $field_column = $field['settings']['chado_column'];
  263. $matches = array();
  264. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  265. $table_name = $matches[1];
  266. $cv_id = $matches[2];
  267. // Get the FK that links to the base record.
  268. $schema = chado_get_schema($field_table);
  269. $pkey = $schema['primary key'][0];
  270. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  271. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  272. // Set some defaults for the empty record.
  273. $entity->{$field_name}['und'][0] = array(
  274. 'value' => '',
  275. $field_table . '__' . $fkey_lcolumn => '',
  276. $field_table . '__' . 'cvterm_id' => '',
  277. // The pub column is present in the cell_line_cvterm, feature_cvterm,
  278. // library_cvterm, phenotype_comparision_cvterm, phenotype_cvterm,
  279. // stock_cvterm, and stock_relationship_cvterm.
  280. $field_table . '__' . 'pub_id' => '',
  281. // The is_not column is present in feature_cvterm and stock_cvterm tables.
  282. $field_table . '__' . 'is_not' => '',
  283. // The rank column is present in the cell_line_cvterm, expression_cvterm,
  284. // feature_cvterm, phenotype_comparision_cvterm, phenotype_cvterm,
  285. // and stock_cvterm tables.
  286. $field_table . '__' . 'rank' => '',
  287. // The cvterm_type_id is present in the expression_cvterm table.
  288. $field_table . '--' . 'cvterm_type_id' => '',
  289. // The following field are to help link the cvterm.
  290. $field_table . '--' . 'cvterm__cv_id--cv__cv_id' => '',
  291. $field_table . '--' . 'cvterm__name' => '',
  292. );
  293. // Get the annotations associated with this base record for this fields type.
  294. $columns = array('*');
  295. $match = array(
  296. $fkey_lcolumn => $record->$fkey_rcolumn,
  297. 'cvterm_id' => array(
  298. 'cv_id' => $cv_id,
  299. ),
  300. );
  301. $options = array(
  302. 'return_array' => TRUE,
  303. 'order_by' => array('rank' => 'ASC')
  304. );
  305. $fcvterms = chado_select_record($field_table, $columns, $match, $options);
  306. for ($i = 0; $i < count($fcvterms); $i++) {
  307. $linker = $fcvterms[$i];
  308. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $linker->cvterm_id));
  309. $entity->{$field_name}['und'][$i] = array(
  310. 'value' => $linker->$pkey,
  311. $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn,
  312. $field_table . '__' . 'cvterm_id' => $linker->cvterm_id,
  313. $field_table . '__' . 'pub_id' => property_exists($linker, 'pub_id') ? $linker->pub_id : '',
  314. $field_table . '__' . 'is_not' => property_exists($linker, 'is_not') ? $linker->is_not : '',
  315. $field_table . '__' . 'rank' => property_exists($linker, 'rank') ? $linker->rank : '',
  316. $field_table . '__' . 'cvterm_type_id' => property_exists($linker, 'cvterm_type_id') ? $linker->cvterm_type_id : '',
  317. $field_table . '--' . 'cvterm__cv_id--cv__cv_id' => $cvterm->cv_id->cv_id,
  318. $field_table . '--' . 'cvterm__name' => $cvterm->name,
  319. );
  320. }
  321. }