chado_linker__cvterm.inc 15 KB

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