chado_linker__cvterm.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. /**
  3. * Implements hook_info() for fields.
  4. *
  5. * This is a hook provided by the tripal_chado module for offloading the
  6. * hook_field_info() hook for each field to specify.
  7. */
  8. function chado_linker__cvterm_info() {
  9. return array(
  10. 'label' => t('Annotations'),
  11. 'description' => t('This record can be annotated with terms
  12. from other vocabularies.'),
  13. 'default_widget' => 'chado_linker__cvterm_widget',
  14. 'default_formatter' => 'chado_linker__cvterm_formatter',
  15. 'settings' => array(),
  16. 'storage' => array(
  17. 'type' => 'field_chado_storage',
  18. 'module' => 'tripal_chado',
  19. 'active' => TRUE
  20. ),
  21. );
  22. }
  23. /**
  24. * Implements hook_widget_info.
  25. *
  26. * This is a hook provided by the tripal_chado module for offloading
  27. * the hook_field_widget_info() hook for each field to specify.
  28. */
  29. function chado_linker__cvterm_widget_info() {
  30. return array(
  31. 'label' => t('Annotations'),
  32. 'field types' => array('chado_linker__cvterm'),
  33. 'description' => t('This record can be annotated with terms
  34. from other vocabularies.'),
  35. );
  36. }
  37. /**
  38. * Implements hook_formatter_info.
  39. *
  40. * This is a hook provided by the tripal_chado module for
  41. * offloading the hook_field_formatter_info() for each field
  42. * to specify.
  43. *
  44. */
  45. function chado_linker__cvterm_formatter_info() {
  46. return array(
  47. 'label' => t('Annotations'),
  48. 'field types' => array('chado_linker__cvterm'),
  49. 'settings' => array(
  50. ),
  51. );
  52. }
  53. /**
  54. *
  55. * @param unknown $entity_type
  56. * @param unknown $entity
  57. * @param unknown $field
  58. * @param unknown $instance
  59. * @param unknown $langcode
  60. * @param unknown $items
  61. * @param unknown $display
  62. */
  63. function chado_linker__cvterm_formatter(&$element, $entity_type, $entity, $field,
  64. $instance, $langcode, $items, $display) {
  65. $headers = array('Term', 'Definition', 'Is Not', 'Reference');
  66. $rows = array();
  67. $chado_table = $field['settings']['chado_table'];
  68. foreach ($items as $delta => $item) {
  69. if ($item[$chado_table . '__cvterm_id']) {
  70. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $item[$chado_table . '__cvterm_id']));
  71. $dbxref = $cvterm->dbxref_id;
  72. // Build the accession.
  73. $accession = $dbxref->db_id->name . ':' . $dbxref->accession;
  74. if ($dbxref->db_id->urlprefix) {
  75. $accession = l($accession, $dbxref->db_id->urlprefix . '/' . $dbxref->accession, array('attributes' => array('target' => '_blank')));
  76. }
  77. // Build the publication reference.
  78. $pub_ref = '';
  79. $pub_id = $item[$chado_table . '__pub_id'];
  80. if ($pub_id) {
  81. $pub = chado_generate_var('pub', array('pub_id' => $pub_id));
  82. $pub_ref = $pub->title;
  83. }
  84. $rows[] = array(
  85. $accession,
  86. $cvterm->definition,
  87. $item[$chado_table . '__is_not'] ? 'Yes' : '',
  88. '',
  89. );
  90. }
  91. }
  92. // the $table array contains the headers and rows array as well as other
  93. // options for controlling the display of the table. Additional
  94. // documentation can be found here:
  95. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  96. $table = array(
  97. 'header' => $headers,
  98. 'rows' => $rows,
  99. 'attributes' => array(
  100. 'id' => "$chado_table-table-terms",
  101. 'class' => 'tripal-data-table'
  102. ),
  103. 'caption' => '',
  104. 'sticky' => FALSE,
  105. 'colgroups' => array(),
  106. 'empty' => 'There are no annotations of this type',
  107. );
  108. $element[0] = array(
  109. '#type' => 'markup',
  110. '#markup' => theme_table($table),
  111. );
  112. }
  113. /**
  114. *
  115. * @param unknown $field_name
  116. * @param unknown $widget
  117. * @param unknown $form
  118. * @param unknown $form_state
  119. * @param unknown $field
  120. * @param unknown $instance
  121. * @param unknown $langcode
  122. * @param unknown $items
  123. * @param unknown $delta
  124. * @param unknown $element
  125. */
  126. function chado_linker__cvterm_widget(&$widget, $form, $form_state, $field,
  127. $instance, $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. '#default_value' => $record_id,
  183. );
  184. $widget[$table_name . '--cvterm__cv_id--cv__cv_id'] = array(
  185. '#type' => 'value',
  186. '#default_value' => $cv_id,
  187. );
  188. $widget[$table_name . '__cvterm_id'] = array(
  189. '#type' => 'value',
  190. '#default_value' => $cvterm ? $cvterm->cvterm_id : '',
  191. );
  192. $widget[$table_name . '__' . $fkey] = array(
  193. '#type' => 'value',
  194. '#default_value' => $fkey_value,
  195. );
  196. $widget[$table_name . '--cvterm__name'] = array(
  197. '#type' => 'textfield',
  198. '#title' => t('Term Name'),
  199. '#default_value' => $cvterm_name,
  200. '#required' => $element['#required'],
  201. '#maxlength' => array_key_exists('length', $schema['fields']['name']) ? $schema['fields']['name']['length'] : 255,
  202. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/cvterm/' . $cv_id,
  203. '#ajax' => array(
  204. 'callback' => "chado_linker__cvterm_widget_form_ajax_callback",
  205. 'wrapper' => "$table_name-$delta",
  206. 'effect' => 'fade',
  207. 'method' => 'replace'
  208. ),
  209. );
  210. $widget[$table_name . '__is_not'] = array(
  211. '#type' => 'checkbox',
  212. '#title' => t('Is Not'),
  213. '#default_value' => $is_not,
  214. '#required' => $element['#required'],
  215. );
  216. $widget[$table_name . '--cvterm__definition'] = array(
  217. '#type' => 'item',
  218. '#markup' => '',
  219. );
  220. }
  221. /**
  222. * An Ajax callback for the dbxref widget.
  223. */
  224. function chado_linker__cvterm_widget_form_ajax_callback($form, $form_state) {
  225. $field_name = $form_state['triggering_element']['#parents'][0];
  226. $delta = $form_state['triggering_element']['#parents'][2];
  227. return $form[$field_name]['und'][$delta];
  228. }
  229. /**
  230. * Callback function for validating the tripal_chado_organism_select_widget.
  231. */
  232. function chado_linker__cvterm_widget_validate($element, &$form_state) {
  233. $field_name = $element['#field_name'];
  234. $delta = $element['#delta'];
  235. $table_name = $element['#table_name'];
  236. $fkey = $element['#fkey_field'];
  237. // If the form ID is field_ui_field_edit_form, then the user is editing the
  238. // field's values in the manage fields form of Drupal. We don't want
  239. // to validate it as if it were being used in a data entry form.
  240. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  241. return;
  242. }
  243. // Get the field values.
  244. $cvterm_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__cvterm_id');
  245. $cv_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '--cvterm__cv_id--cv__cv_id');
  246. $cvterm_name = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '--cvterm__name');
  247. $cvterm_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__cvterm_id');
  248. $pub_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__pub_id');
  249. // If the user provided a cv_id and a name then we want to set the
  250. // foreign key value to be the chado_record_idd
  251. if ($cvterm_name) {
  252. $fkey_value = $element['#entity']->chado_record_id;
  253. tripal_chado_set_field_form_values($field_name, $form_state, $fkey_value, $delta, $table_name . '__' . $fkey);
  254. // Get the cvterm ID. If one is not available because it's a newly added
  255. // record, then we need to retrieve it and set the form element.
  256. if (!$cvterm_id) {
  257. $cvterm = tripal_get_cvterm(array('cv_id' => $cv_id, 'name' => $cvterm_name));
  258. tripal_chado_set_field_form_values($field_name, $form_state, $cvterm->cvterm_id, $delta, $table_name . '__cvterm_id');
  259. }
  260. if (!$pub_id) {
  261. $pub = chado_generate_var('pub', array('uniquename' => 'null'));
  262. tripal_chado_set_field_form_values($field_name, $form_state, $pub->pub_id, $delta, $table_name . '__pub_id');
  263. }
  264. }
  265. else {
  266. // If the $cv_id and name are not set, then remove the linker FK value to the base table.
  267. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $fkey);
  268. }
  269. }
  270. /**
  271. * Theme function for the dbxref_id_widget.
  272. *
  273. * @param $variables
  274. */
  275. function theme_chado_linker__cvterm_widget($variables) {
  276. $element = $variables['element'];
  277. // These two fields were added to the widget to help identify the fields
  278. // for layout.
  279. $table_name = $element['#table_name'];
  280. $fkey = $element['#fkey_field'];
  281. $layout = "
  282. <div class=\"annotation-cvterm-widget\">
  283. <div class=\"annotation-cvterm-widget-item\">" .
  284. drupal_render($element[$table_name . '--cvterm__cv_id--cv__cv_id']) . "
  285. </div>
  286. <div class=\"annotation-cvterm-widget-item\">" .
  287. drupal_render($element[$table_name . '--cvterm__name']) . "
  288. </div>
  289. <div class=\"annotation-cvterm-widget-item\">" .
  290. drupal_render($element[$table_name . '__is_not']) . "
  291. </div>
  292. </div>
  293. ";
  294. return $layout;
  295. }
  296. /**
  297. * Loads the field values with appropriate data.
  298. *
  299. * This function is called by the tripal_chado_field_storage_load() for
  300. * each property managed by the field_chado_storage storage type. This is
  301. * an optional hook function that is only needed if the field has
  302. * multiple form elements.
  303. *
  304. * @param $field
  305. * @param $entity
  306. * @param $base_table
  307. * @param $record
  308. */
  309. function chado_linker__cvterm_load($field, $entity, $base_table, $record) {
  310. $field_name = $field['field_name'];
  311. $field_type = $field['type'];
  312. $field_table = $field['settings']['chado_table'];
  313. $field_column = $field['settings']['chado_column'];
  314. $matches = array();
  315. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  316. $table_name = $matches[1];
  317. $cv_id = $matches[2];
  318. // Get the FK that links to the base record.
  319. $schema = chado_get_schema($field_table);
  320. $pkey = $schema['primary key'][0];
  321. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  322. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  323. // Set some defaults for the empty record.
  324. $entity->{$field_name}['und'][0] = array(
  325. 'value' => '',
  326. $field_table . '__' . $fkey_lcolumn => '',
  327. $field_table . '__' . 'cvterm_id' => '',
  328. // The pub column is present in the cell_line_cvterm, feature_cvterm,
  329. // library_cvterm, phenotype_comparision_cvterm, phenotype_cvterm,
  330. // stock_cvterm, and stock_relationship_cvterm.
  331. $field_table . '__' . 'pub_id' => '',
  332. // The is_not column is present in feature_cvterm and stock_cvterm tables.
  333. $field_table . '__' . 'is_not' => '',
  334. // The rank column is present in the cell_line_cvterm, expression_cvterm,
  335. // feature_cvterm, phenotype_comparision_cvterm, phenotype_cvterm,
  336. // and stock_cvterm tables.
  337. $field_table . '__' . 'rank' => '',
  338. // The cvterm_type_id is present in the expression_cvterm table.
  339. $field_table . '--' . 'cvterm_type_id' => '',
  340. // The following field are to help link the cvterm.
  341. $field_table . '--' . 'cvterm__cv_id--cv__cv_id' => '',
  342. $field_table . '--' . 'cvterm__name' => '',
  343. );
  344. // Get the annotations associated with this base record for this fields type.
  345. $columns = array('*');
  346. $match = array(
  347. $fkey_lcolumn => $record->$fkey_rcolumn,
  348. 'cvterm_id' => array(
  349. 'cv_id' => $cv_id,
  350. ),
  351. );
  352. $options = array(
  353. 'return_array' => TRUE,
  354. 'order_by' => array('rank' => 'ASC')
  355. );
  356. $fcvterms = chado_select_record($field_table, $columns, $match, $options);
  357. for ($i = 0; $i < count($fcvterms); $i++) {
  358. $linker = $fcvterms[$i];
  359. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $linker->cvterm_id));
  360. $entity->{$field_name}['und'][$i] = array(
  361. 'value' => $linker->$pkey,
  362. $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn,
  363. $field_table . '__' . 'cvterm_id' => $linker->cvterm_id,
  364. $field_table . '__' . 'pub_id' => property_exists($linker, 'pub_id') ? $linker->pub_id : '',
  365. $field_table . '__' . 'is_not' => property_exists($linker, 'is_not') ? $linker->is_not : '',
  366. $field_table . '__' . 'rank' => property_exists($linker, 'rank') ? $linker->rank : '',
  367. $field_table . '__' . 'cvterm_type_id' => property_exists($linker, 'cvterm_type_id') ? $linker->cvterm_type_id : '',
  368. $field_table . '--' . 'cvterm__cv_id--cv__cv_id' => $cvterm->cv_id->cv_id,
  369. $field_table . '--' . 'cvterm__name' => $cvterm->name,
  370. );
  371. }
  372. }