chado_linker__synonym.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. class chado_linker__synonym extends TripalField {
  3. /**
  4. * @see TripalField::field_info()
  5. */
  6. function field_info() {
  7. return array(
  8. 'label' => t('Synonyms'),
  9. 'description' => t('Adds an alternative name (synonym or alias) to this record.'),
  10. 'default_widget' => 'chado_linker__synonym_widget',
  11. 'default_formatter' => 'chado_linker__synonym_formatter',
  12. 'settings' => array(),
  13. 'storage' => array(
  14. 'type' => 'field_chado_storage',
  15. 'module' => 'tripal_chado',
  16. 'active' => TRUE
  17. ),
  18. );
  19. }
  20. /**
  21. * @see TripalField::attach_info()
  22. */
  23. function attach_info($entity_type, $bundle, $target) {
  24. $field_info = array();
  25. $table_name = $target['data_table'];
  26. $type_table = $target['type_table'];
  27. $type_field = $target['field'];
  28. $cv_id = $target['cv_id'];
  29. $cvterm_id = $target['cvterm_id'];
  30. // If the linker table does not exists then we don't want to add attach.
  31. $syn_table = $table_name . '_synonym';
  32. if (!chado_table_exists($syn_table)) {
  33. return $field_info;
  34. }
  35. $schema = chado_get_schema($syn_table);
  36. $pkey = $schema['primary key'][0];
  37. // Initialize the field array.
  38. $field_info = array(
  39. 'field_name' => $table_name . '_synonym',
  40. 'field_type' => 'chado_linker__synonym',
  41. 'widget_type' => 'chado_linker__synonym_widget',
  42. 'widget_settings' => array('display_label' => 1),
  43. 'description' => '',
  44. 'label' => 'Synonyms',
  45. 'is_required' => 0,
  46. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  47. 'storage' => 'field_chado_storage',
  48. 'field_settings' => array(
  49. 'chado_table' => $syn_table,
  50. 'chado_column' => $pkey,
  51. 'base_table' => $table_name,
  52. 'semantic_web' => 'schema:alternateName',
  53. ),
  54. );
  55. return $field_info;
  56. }
  57. /**
  58. * @see TripalField::widget_info()
  59. */
  60. function widget_info() {
  61. return array(
  62. 'label' => t('Synonyms'),
  63. 'field types' => array('chado_linker__synonym'),
  64. );
  65. }
  66. /**
  67. * @see TripalField::formatter_info()
  68. */
  69. function formatter_info() {
  70. return array(
  71. 'label' => t('Synonyms'),
  72. 'field types' => array('chado_linker__synonym'),
  73. 'settings' => array(
  74. ),
  75. );
  76. }
  77. /**
  78. * @see TripalField::formatter_view()
  79. */
  80. function formatter_view(&$element, $entity_type, $entity, $field,
  81. $instance, $langcode, $items, $display) {
  82. $chado_table = $field['settings']['chado_table'];
  83. foreach ($items as $delta => $item) {
  84. if ($item[$chado_table . '__synonym_id']) {
  85. $synonym = chado_generate_var('synonym', array('synonym_id' => $item[$chado_table . '__synonym_id']));
  86. $name = $synonym->name;
  87. if ($synonym->type_id->name != 'exact') {
  88. $name .= ' (<i>' . $synonym->type_id->name . '</i>)';
  89. }
  90. $element[$delta] = array(
  91. '#type' => 'markup',
  92. '#markup' => $name,
  93. );
  94. }
  95. }
  96. }
  97. /**
  98. * @see TripalField::widget_form()
  99. */
  100. function widget_form(&$widget, $form, $form_state, $field,
  101. $instance, $langcode, $items, $delta, $element) {
  102. $entity = $form['#entity'];
  103. $field_name = $field['field_name'];
  104. // Get the FK column that links to the base table.
  105. $table_name = $field['settings']['chado_table'];
  106. $base_table = $field['settings']['base_table'];
  107. $schema = chado_get_schema($table_name);
  108. $pkey = $schema['primary key'][0];
  109. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  110. $fkey = $fkeys[0];
  111. // Get the field defaults.
  112. $record_id = '';
  113. $fkey_value = $element['#entity']->chado_record_id;
  114. $synonym_id = '';
  115. $pub_id = '';
  116. $is_current = TRUE;
  117. $is_internal = FALSE;
  118. $syn_name = '';
  119. $syn_type = '';
  120. // If the field already has a value then it will come through the $items
  121. // array. This happens when editing an existing record.
  122. if (array_key_exists($delta, $items)) {
  123. $record_id = $items[$delta][$table_name . '__' . $pkey];
  124. $fkey_value = $items[$delta][$table_name . '__' . $fkey];
  125. $synonym_id = $items[$delta][$table_name . '__synonym_id'];
  126. $pub_id = $items[$delta][$table_name . '__pub_id'];
  127. $is_current = $items[$delta][$table_name . '__is_current'];
  128. $is_internal = $items[$delta][$table_name . '__is_internal'];
  129. $syn_name = $items[$delta][$table_name . '__synonym_id--name'];
  130. $syn_type = $items[$delta][$table_name . '__synonym_id--type_id'];
  131. }
  132. // Check $form_state['values'] to see if an AJAX call set the values.
  133. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  134. $record_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $pkey);
  135. $fkey_value = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $fkey);
  136. $synonym_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__synonym_id');
  137. $pub_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__pub_id');
  138. $is_current = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__is_current');
  139. $is_internal = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__is_internal');
  140. $syn_name = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__synonym_id--name');
  141. $syn_type = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__synonym_id--type_id');
  142. }
  143. // Get the synonym type terms. There shouldn't be too many.
  144. $cv = tripal_get_default_cv('synonym', 'type_id');
  145. $options = tripal_get_cvterm_select_options($cv->cv_id);
  146. // Get the schema for the synonym table so we can make sure we limit the
  147. // size of the name field to the proper size.
  148. $schema = chado_get_schema('synonym');
  149. $widget['#table_name'] = $table_name;
  150. $widget['#fkey_field'] = $fkey;
  151. $widget['#element_validate'] = array('chado_linker__synonym_widget_validate');
  152. $widget['#theme'] = 'chado_linker__synonym_widget';
  153. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  154. $widget['#suffix'] = "</span>";
  155. $widget['value'] = array(
  156. '#type' => 'value',
  157. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  158. );
  159. $widget[$table_name . '__' . $pkey] = array(
  160. '#type' => 'value',
  161. '#default_value' => $record_id,
  162. );
  163. $widget[$table_name . '__' . $fkey] = array(
  164. '#type' => 'value',
  165. '#default_value' => $fkey_value,
  166. );
  167. // TODO: add a widget for selecting a publication.
  168. $widget[$table_name . '__pub_id'] = array(
  169. '#type' => 'value',
  170. '#default_value' => $pub_id,
  171. );
  172. $widget[$table_name . '__synonym_id--type_id'] = array(
  173. '#type' => 'select',
  174. '#title' => t('Type'),
  175. '#options' => $options,
  176. '#default_value' => $syn_type,
  177. );
  178. $widget[$table_name . '__synonym_id--name'] = array(
  179. '#type' => 'textfield',
  180. '#title' => t('Synonym Name'),
  181. '#default_value' => $syn_name,
  182. '#size' => 25,
  183. );
  184. $widget[$table_name . '__is_current'] = array(
  185. '#type' => 'checkbox',
  186. '#title' => t('Is Current'),
  187. '#default_value' => $is_current,
  188. '#required' => $element['#required'],
  189. );
  190. $widget[$table_name . '__is_internal'] = array(
  191. '#type' => 'checkbox',
  192. '#title' => t('Is Internal'),
  193. '#default_value' => $is_internal,
  194. '#required' => $element['#required'],
  195. );
  196. }
  197. /**
  198. * @see TripalField::load()
  199. */
  200. function load($field, $entity, $details) {
  201. $record = $details['record'];
  202. $base_table = $field['settings']['base_table'];
  203. $field_name = $field['field_name'];
  204. $field_type = $field['type'];
  205. $field_table = $field['settings']['chado_table'];
  206. $field_column = $field['settings']['chado_column'];
  207. // Get the PKey for this table
  208. $schema = chado_get_schema($field_table);
  209. $pkey = $schema['primary key'][0];
  210. // Get the FK that links to the base record.
  211. $schema = chado_get_schema($field_table);
  212. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  213. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  214. // Set some defaults for the empty record.
  215. $entity->{$field_name}['und'][0] = array(
  216. 'value' => array(),
  217. $field_table . '__' . $pkey => '',
  218. $field_table . '__' . $fkey_lcolumn => '',
  219. $field_table . '__' . 'synonym_id' => '',
  220. $field_table . '__' . 'pub_id' => '',
  221. $field_table . '__' . 'is_current' => TRUE,
  222. $field_table . '__' . 'is_internal' => '',
  223. $field_table . '__synonym_id--name' => '',
  224. $field_table . '__synonym_id--type_id' => '',
  225. // Ignore the synonym_sgml column for now.
  226. );
  227. $linker_table = $base_table . '_synonym';
  228. $options = array('return_array' => 1);
  229. $record = chado_expand_var($record, 'table', $linker_table, $options);
  230. if (count($record->$linker_table) > 0) {
  231. $i = 0;
  232. foreach ($record->$linker_table as $index => $linker) {
  233. $synonym = $linker->synonym_id;
  234. $entity->{$field_name}['und'][$i] = array(
  235. 'value' => array(
  236. '@type' => $synonym->type_id->dbxref_id->db_id->name . ':' . $synonym->type_id->dbxref_id->accession,
  237. 'type' => $synonym->type_id->name,
  238. 'name' => $synonym->name,
  239. ),
  240. $field_table . '__' . $pkey = $linker->$pkey,
  241. $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn->$fkey_lcolumn,
  242. $field_table . '__' . 'synonym_id' => $synonym->synonym_id,
  243. $field_table . '__' . 'pub_id' => $linker->pub_id->pub_id,
  244. $field_table . '__' . 'is_current' => $linker->is_current,
  245. $field_table . '__' . 'is_internal' => $linker->is_internal,
  246. $field_table . '__synonym_id--name' => $synonym->name,
  247. $field_table . '__synonym_id--type_id' => $synonym->type_id->cvterm_id,
  248. );
  249. $i++;
  250. }
  251. }
  252. }
  253. }
  254. /**
  255. * Theme function for the synonym widget.
  256. *
  257. * @param $variables
  258. */
  259. function theme_chado_linker__synonym_widget($variables) {
  260. $element = $variables['element'];
  261. // These two fields were added to the widget to help identify the fields
  262. // for layout.
  263. $table_name = $element['#table_name'];
  264. $fkey = $element['#fkey_field'];
  265. $layout = "
  266. <div class=\"synonym-widget\">
  267. <div class=\"synonym-widget-item\">" .
  268. drupal_render($element[$table_name . '__synonym_id--name']) . "
  269. </div>
  270. <div>" .
  271. drupal_render($element[$table_name . '__synonym_id--type_id']) . "
  272. </div>
  273. <div class=\"synonym-widget-item\">" .
  274. drupal_render($element[$table_name . '__is_internal']) . "
  275. </div>
  276. <div>" .
  277. drupal_render($element[$table_name . '__is_current']) . "
  278. </div>
  279. </div>
  280. ";
  281. return $layout;
  282. }
  283. /**
  284. * Callback function for validating the chado_linker__synonym_widget.
  285. */
  286. function chado_linker__synonym_widget_validate($element, &$form_state) {
  287. $field_name = $element['#field_name'];
  288. $delta = $element['#delta'];
  289. $table_name = $element['#table_name'];
  290. $fkey = $element['#fkey_field'];
  291. // If the form ID is field_ui_field_edit_form, then the user is editing the
  292. // field's values in the manage fields form of Drupal. We don't want
  293. // to validate it as if it were being used in a data entry form.
  294. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  295. return;
  296. }
  297. // Get the field values.
  298. $fkey_value = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $fkey);
  299. $synonym_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__synonym_id');
  300. $pub_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__pub_id');
  301. $is_current = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__is_current');
  302. $is_internal = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__is_internal');
  303. $syn_name = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '--synonym__name');
  304. $syn_type = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '--synonym__type_id');
  305. // Make sure that if a synonym is provided that a type is also
  306. // provided.
  307. if ($syn_name and !$syn_type) {
  308. form_set_error(implode('][', $element ['#parents']) . '][' . $table_name . '--synonym__type_id', t("Please set a synonym type."));
  309. }
  310. if (!$syn_name and $syn_type) {
  311. form_set_error(implode('][', $element ['#parents']) . '][' . $table_name . '--synonym__name', t("Please set a synonym name."));
  312. }
  313. // If the user provided a cv_id and a name then we want to set the
  314. // foreign key value to be the chado_record_idd
  315. if ($syn_name and $syn_type) {
  316. // Get the synonym. If one with the same name and type is already present
  317. // then use that. Otherwise, insert a new one.
  318. if (!$synonym_id) {
  319. $synonym = chado_generate_var('synonym', array('name' => $syn_name, 'type_id' => $syn_type));
  320. if (!$synonym) {
  321. $synonym = chado_insert_record('synonym', array(
  322. 'name' => $syn_name,
  323. 'type_id' => $syn_type,
  324. 'synonym_sgml' => '',
  325. ));
  326. $synonym = (object) $synonym;
  327. }
  328. // Set the synonym_id and FK value
  329. tripal_chado_set_field_form_values($field_name, $form_state, $synonym->synonym_id, $delta, $table_name . '__synonym_id');
  330. $fkey_value = $element['#entity']->chado_record_id;
  331. tripal_chado_set_field_form_values($field_name, $form_state, $fkey_value, $delta, $table_name . '__' . $fkey);
  332. }
  333. if (!$pub_id) {
  334. $pub = chado_generate_var('pub', array('uniquename' => 'null'));
  335. tripal_chado_set_field_form_values($field_name, $form_state, $pub->pub_id, $delta, $table_name . '__pub_id');
  336. }
  337. }
  338. else {
  339. // If the $syn_name is not set, then remove the linker FK value to the base table.
  340. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $fkey);
  341. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__synonym_id');
  342. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__is_internal');
  343. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__is_current');
  344. }
  345. }
  346. /**
  347. * An Ajax callback for the synonym widget.
  348. */
  349. function chado_linker__synonym_widget_form_ajax_callback($form, $form_state) {
  350. $field_name = $form_state['triggering_element']['#parents'][0];
  351. $delta = $form_state['triggering_element']['#parents'][2];
  352. return $form[$field_name]['und'][$delta];
  353. }