chado_linker__cvterm.inc 15 KB

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