chado_linker__cvterm.inc 15 KB

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