chado_linker__cvterm.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. $form['value'] = array(
  199. '#type' => 'value',
  200. '#value' => $items[$delta]['value'],
  201. );
  202. $widget[$table_name . '__' . $pkey] = array(
  203. '#type' => 'value',
  204. '#default_value' => $record_id,
  205. );
  206. $widget[$table_name . '--cvterm__cv_id--cv__cv_id'] = array(
  207. '#type' => 'value',
  208. '#default_value' => $cv_id,
  209. );
  210. $widget[$table_name . '__cvterm_id'] = array(
  211. '#type' => 'value',
  212. '#default_value' => $cvterm ? $cvterm->cvterm_id : '',
  213. );
  214. $widget[$table_name . '__' . $fkey] = array(
  215. '#type' => 'value',
  216. '#default_value' => $fkey_value,
  217. );
  218. $widget[$table_name . '--cvterm__name'] = array(
  219. '#type' => 'textfield',
  220. '#title' => t('Term Name'),
  221. '#default_value' => $cvterm_name,
  222. '#required' => $element['#required'],
  223. '#maxlength' => array_key_exists('length', $schema['fields']['name']) ? $schema['fields']['name']['length'] : 255,
  224. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/cvterm/' . $cv_id,
  225. '#ajax' => array(
  226. 'callback' => "chado_linker__cvterm_widget_form_ajax_callback",
  227. 'wrapper' => "$table_name-$delta",
  228. 'effect' => 'fade',
  229. 'method' => 'replace'
  230. ),
  231. );
  232. $widget[$table_name . '__is_not'] = array(
  233. '#type' => 'checkbox',
  234. '#title' => t('Is Not'),
  235. '#default_value' => $is_not,
  236. '#required' => $element['#required'],
  237. );
  238. $widget[$table_name . '--cvterm__definition'] = array(
  239. '#type' => 'item',
  240. '#markup' => '',
  241. );
  242. }
  243. /**
  244. * An Ajax callback for the dbxref widget.
  245. */
  246. function chado_linker__cvterm_widget_form_ajax_callback($form, $form_state) {
  247. $field_name = $form_state['triggering_element']['#parents'][0];
  248. $delta = $form_state['triggering_element']['#parents'][2];
  249. return $form[$field_name]['und'][$delta];
  250. }
  251. /**
  252. * Callback function for validating the chado_linker__cvterm_widget.
  253. */
  254. function chado_linker__cvterm_widget_validate($element, &$form_state) {
  255. $field_name = $element['#field_name'];
  256. $delta = $element['#delta'];
  257. $table_name = $element['#table_name'];
  258. $fkey = $element['#fkey_field'];
  259. // If the form ID is field_ui_field_edit_form, then the user is editing the
  260. // field's values in the manage fields form of Drupal. We don't want
  261. // to validate it as if it were being used in a data entry form.
  262. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  263. return;
  264. }
  265. // Get the field values.
  266. $cvterm_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__cvterm_id');
  267. $cv_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '--cvterm__cv_id--cv__cv_id');
  268. $cvterm_name = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '--cvterm__name');
  269. $cvterm_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__cvterm_id');
  270. $pub_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__pub_id');
  271. // If the user provided a cv_id and a name then we want to set the
  272. // foreign key value to be the chado_record_idd
  273. if ($cvterm_name) {
  274. $fkey_value = $element['#entity']->chado_record_id;
  275. tripal_chado_set_field_form_values($field_name, $form_state, $fkey_value, $delta, $table_name . '__' . $fkey);
  276. // Get the cvterm ID. If one is not available because it's a newly added
  277. // record, then we need to retrieve it and set the form element.
  278. if (!$cvterm_id) {
  279. $cvterm = tripal_get_cvterm(array('cv_id' => $cv_id, 'name' => $cvterm_name));
  280. tripal_chado_set_field_form_values($field_name, $form_state, $cvterm->cvterm_id, $delta, $table_name . '__cvterm_id');
  281. }
  282. if (!$pub_id) {
  283. $pub = chado_generate_var('pub', array('uniquename' => 'null'));
  284. tripal_chado_set_field_form_values($field_name, $form_state, $pub->pub_id, $delta, $table_name . '__pub_id');
  285. }
  286. }
  287. else {
  288. // If the $cv_id and name are not set, then remove the linker FK value to the base table.
  289. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $fkey);
  290. }
  291. }
  292. /**
  293. * Theme function for the dbxref_id_widget.
  294. *
  295. * @param $variables
  296. */
  297. function theme_chado_linker__cvterm_widget($variables) {
  298. $element = $variables['element'];
  299. // These two fields were added to the widget to help identify the fields
  300. // for layout.
  301. $table_name = $element['#table_name'];
  302. $fkey = $element['#fkey_field'];
  303. $layout = "
  304. <div class=\"annotation-cvterm-widget\">
  305. <div class=\"annotation-cvterm-widget-item\">" .
  306. drupal_render($element[$table_name . '--cvterm__cv_id--cv__cv_id']) . "
  307. </div>
  308. <div class=\"annotation-cvterm-widget-item\">" .
  309. drupal_render($element[$table_name . '--cvterm__name']) . "
  310. </div>
  311. <div class=\"annotation-cvterm-widget-item\">" .
  312. drupal_render($element[$table_name . '__is_not']) . "
  313. </div>
  314. </div>
  315. ";
  316. return $layout;
  317. }
  318. /**
  319. * Loads the field values with appropriate data.
  320. *
  321. * This function is called by the tripal_chado_field_storage_load() for
  322. * each property managed by the field_chado_storage storage type. This is
  323. * an optional hook function that is only needed if the field has
  324. * multiple form elements.
  325. *
  326. * @param $field
  327. * @param $entity
  328. * @param $base_table
  329. * @param $record
  330. */
  331. function chado_linker__cvterm_load($field, $entity, $base_table, $record) {
  332. $field_name = $field['field_name'];
  333. $field_type = $field['type'];
  334. $field_table = $field['settings']['chado_table'];
  335. $field_column = $field['settings']['chado_column'];
  336. $matches = array();
  337. preg_match('/(.*?)__(\d+)/', $field_name, $matches);
  338. $table_name = $matches[1];
  339. $cv_id = $matches[2];
  340. // Get the FK that links to the base record.
  341. $schema = chado_get_schema($field_table);
  342. $pkey = $schema['primary key'][0];
  343. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  344. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  345. // Set some defaults for the empty record.
  346. $entity->{$field_name}['und'][0] = array(
  347. 'value' => '',
  348. $field_table . '__' . $fkey_lcolumn => '',
  349. $field_table . '__' . 'cvterm_id' => '',
  350. // The pub column is present in the cell_line_cvterm, feature_cvterm,
  351. // library_cvterm, phenotype_comparision_cvterm, phenotype_cvterm,
  352. // stock_cvterm, and stock_relationship_cvterm.
  353. $field_table . '__' . 'pub_id' => '',
  354. // The is_not column is present in feature_cvterm and stock_cvterm tables.
  355. $field_table . '__' . 'is_not' => '',
  356. // The rank column is present in the cell_line_cvterm, expression_cvterm,
  357. // feature_cvterm, phenotype_comparision_cvterm, phenotype_cvterm,
  358. // and stock_cvterm tables.
  359. $field_table . '__' . 'rank' => '',
  360. // The cvterm_type_id is present in the expression_cvterm table.
  361. $field_table . '--' . 'cvterm_type_id' => '',
  362. // The following field are to help link the cvterm.
  363. $field_table . '--' . 'cvterm__cv_id--cv__cv_id' => '',
  364. $field_table . '--' . 'cvterm__name' => '',
  365. );
  366. // Get the annotations associated with this base record for this fields type.
  367. $columns = array('*');
  368. $match = array(
  369. $fkey_lcolumn => $record->$fkey_rcolumn,
  370. 'cvterm_id' => array(
  371. 'cv_id' => $cv_id,
  372. ),
  373. );
  374. $options = array(
  375. 'return_array' => TRUE,
  376. 'order_by' => array('rank' => 'ASC')
  377. );
  378. $fcvterms = chado_select_record($field_table, $columns, $match, $options);
  379. for ($i = 0; $i < count($fcvterms); $i++) {
  380. $linker = $fcvterms[$i];
  381. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $linker->cvterm_id));
  382. $entity->{$field_name}['und'][$i] = array(
  383. 'value' => $linker->$pkey,
  384. $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn,
  385. $field_table . '__' . 'cvterm_id' => $linker->cvterm_id,
  386. $field_table . '__' . 'pub_id' => property_exists($linker, 'pub_id') ? $linker->pub_id : '',
  387. $field_table . '__' . 'is_not' => property_exists($linker, 'is_not') ? $linker->is_not : '',
  388. $field_table . '__' . 'rank' => property_exists($linker, 'rank') ? $linker->rank : '',
  389. $field_table . '__' . 'cvterm_type_id' => property_exists($linker, 'cvterm_type_id') ? $linker->cvterm_type_id : '',
  390. $field_table . '--' . 'cvterm__cv_id--cv__cv_id' => $cvterm->cv_id->cv_id,
  391. $field_table . '--' . 'cvterm__name' => $cvterm->name,
  392. );
  393. }
  394. }