chado_linker__cvterm.inc 14 KB

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