chado_linker__synonym.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. class chado_linker__synonym extends TripalField {
  3. // The default lable for this field.
  4. public static $default_label = 'Synonyms';
  5. // The default description for this field.
  6. public static $default_description = 'Adds an alternative name (synonym or alias) to this record.';
  7. // Add any default settings elements. If you override the globalSettingsForm()
  8. // or the instanceSettingsForm() functions then you need to be sure that
  9. // any settings you want those functions to manage are listed in this
  10. // array.
  11. public static $default_settings = array(
  12. 'chado_table' => '',
  13. 'chado_column' => '',
  14. 'base_table' => '',
  15. 'semantic_web' => '',
  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. $chado_table = $this->field['settings']['chado_table'];
  25. foreach ($items as $delta => $item) {
  26. if (array_key_exists('chado-' . $chado_table . '__synonym_id', $item) and
  27. $item['chado-' . $chado_table . '__synonym_id']) {
  28. $synonym = chado_generate_var('synonym', array('synonym_id' => $item['chado-' . $chado_table . '__synonym_id']));
  29. $name = $synonym->name;
  30. if ($synonym->type_id->name != 'exact') {
  31. $name .= ' (<i>' . $synonym->type_id->name . '</i>)';
  32. }
  33. $element[$delta] = array(
  34. '#type' => 'markup',
  35. '#markup' => $name,
  36. );
  37. }
  38. }
  39. }
  40. /**
  41. * @see TripalField::widgetForm()
  42. */
  43. public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  44. parent::widgetForm($widget, $form, $form_state, $langcode, $items, $delta, $element);
  45. $entity = $form['#entity'];
  46. $field_name = $this->field['field_name'];
  47. // Get the FK column that links to the base table.
  48. $table_name = $this->field['settings']['chado_table'];
  49. $base_table = $this->field['settings']['base_table'];
  50. $schema = chado_get_schema($table_name);
  51. $pkey = $schema['primary key'][0];
  52. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  53. $fkey = $fkeys[0];
  54. // Get the field defaults.
  55. $record_id = '';
  56. $fkey_value = $element['#entity']->chado_record_id;
  57. $synonym_id = '';
  58. $pub_id = '';
  59. $is_current = TRUE;
  60. $is_internal = FALSE;
  61. $syn_name = '';
  62. $syn_type = '';
  63. // If the field already has a value then it will come through the $items
  64. // array. This happens when editing an existing record.
  65. if (array_key_exists($delta, $items)) {
  66. $record_id = $items[$delta]['chado-' . $table_name . '__' . $pkey];
  67. $synonym_id = $items[$delta]['chado-' . $table_name . '__synonym_id'];
  68. $pub_id = $items[$delta]['chado-' . $table_name . '__pub_id'];
  69. $is_current = $items[$delta]['chado-' . $table_name . '__is_current'];
  70. $is_internal = $items[$delta]['chado-' . $table_name . '__is_internal'];
  71. $syn_name = $items[$delta]['name'];
  72. $syn_type = $items[$delta]['type_id'];
  73. }
  74. // Check $form_state['values'] to see if an AJAX call set the values.
  75. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  76. $record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $table_name . '__' . $pkey];
  77. $synonym_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $table_name . '__synonym_id'];
  78. $pub_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $table_name . '__pub_id'];
  79. $is_current = $form_state['values'][$field_name]['und'][$delta]['chado-' . $table_name . '__is_current'];
  80. $is_internal = $form_state['values'][$field_name]['und'][$delta]['chado-' . $table_name . '__is_internal'];
  81. $syn_name = $form_state['values'][$field_name]['und'][$delta]['name'];
  82. $syn_type = $form_state['values'][$field_name]['und'][$delta]['type_id'];
  83. }
  84. $options = array();
  85. $value = array('cv_id' => array('name' => 'synonym_type'));
  86. $op = array('return_array' => 1);
  87. $types = chado_generate_var('cvterm', $value, $op);
  88. if ($types) {
  89. foreach($types AS $type) {
  90. $options[$type->cvterm_id] = $type->name;
  91. }
  92. }
  93. // Get the schema for the synonym table so we can make sure we limit the
  94. // size of the name field to the proper size.
  95. $schema = chado_get_schema('synonym');
  96. $widget['#table_name'] = $table_name;
  97. $widget['#fkey_field'] = $fkey;
  98. $widget['#theme'] = 'chado_linker__synonym_widget';
  99. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  100. $widget['#suffix'] = "</span>";
  101. $widget['value'] = array(
  102. '#type' => 'value',
  103. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  104. );
  105. $widget['chado-' . $table_name . '__' . $pkey] = array(
  106. '#type' => 'value',
  107. '#default_value' => $record_id,
  108. );
  109. $widget['chado-' . $table_name . '__synonym_id'] = array(
  110. '#type' => 'value',
  111. '#default_value' => $synonym_id,
  112. );
  113. $widget['chado-' . $table_name . '__' . $fkey] = array(
  114. '#type' => 'value',
  115. '#default_value' => $fkey_value,
  116. );
  117. // TODO: add a widget for selecting a publication.
  118. $widget['chado-' . $table_name . '__pub_id'] = array(
  119. '#type' => 'value',
  120. '#default_value' => $pub_id,
  121. );
  122. $widget['type_id'] = array(
  123. '#type' => 'select',
  124. '#title' => t('Type'),
  125. '#options' => $options,
  126. '#default_value' => $syn_type,
  127. );
  128. $widget['name'] = array(
  129. '#type' => 'textfield',
  130. '#title' => t('Synonym Name'),
  131. '#default_value' => $syn_name,
  132. '#size' => 25,
  133. );
  134. $widget['chado-' . $table_name . '__is_current'] = array(
  135. '#type' => 'checkbox',
  136. '#title' => t('Is Current'),
  137. '#default_value' => $is_current,
  138. '#required' => $element['#required'],
  139. );
  140. $widget['chado-' . $table_name . '__is_internal'] = array(
  141. '#type' => 'checkbox',
  142. '#title' => t('Is Internal'),
  143. '#default_value' => $is_internal,
  144. '#required' => $element['#required'],
  145. );
  146. }
  147. /**
  148. * @see TripalField::widgetFormSubmit()
  149. */
  150. public function widgetFormSubmit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  151. $field_name = $this->field['field_name'];
  152. $field_type = $this->field['type'];
  153. $table_name = $this->field['settings']['chado_table'];
  154. $field_table = $this->field['settings']['chado_table'];
  155. $field_column = $this->field['settings']['chado_column'];
  156. $base_table = $this->field['settings']['base_table'];
  157. $schema = chado_get_schema($table_name);
  158. $pkey = $schema['primary key'][0];
  159. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  160. $fkey = $fkeys[0];
  161. $record_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $pkey]) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $pkey] : '';
  162. $fkey_value = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey]) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] : '';
  163. $synonym_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__synonym_id']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__synonym_id'] : '';
  164. $pub_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id'] : '';
  165. $is_current = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__is_current']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__is_current'] : '';
  166. $is_internal = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__is_internal']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__is_internal'] : '';
  167. $syn_name = isset($form_state['values'][$field_name][$langcode][$delta]['name']) ? $form_state['values'][$field_name][$langcode][$delta]['name'] : '';
  168. $syn_type = isset($form_state['values'][$field_name][$langcode][$delta]['type_id']) ? $form_state['values'][$field_name][$langcode][$delta]['type_id'] : '';
  169. // If the user provided a $syn_name and a $syn_type then we want to set
  170. // the foreign key value to be the chado_record_id.
  171. if ($syn_name and $syn_type) {
  172. // Get the synonym. If one with the same name and type is already present
  173. // then use that. Otherwise, insert a new one.
  174. if (!$synonym_id) {
  175. $synonym = chado_generate_var('synonym', array('name' => $syn_name, 'type_id' => $syn_type));
  176. if (!$synonym) {
  177. $synonym = chado_insert_record('synonym', array(
  178. 'name' => $syn_name,
  179. 'type_id' => $syn_type,
  180. 'synonym_sgml' => '',
  181. ));
  182. $synonym = (object) $synonym;
  183. }
  184. // Set the synonym_id and FK value
  185. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__synonym_id'] = $synonym->synonym_id;
  186. }
  187. if (!$pub_id) {
  188. $pub = chado_generate_var('pub', array('uniquename' => 'null'));
  189. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id'] = $pub->pub_id;
  190. }
  191. }
  192. else {
  193. // If the $syn_name is not set, then remove the linker FK value to the base table.
  194. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] = '';
  195. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__synonym_id'] = '';
  196. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__is_internal'] = '';
  197. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__is_current'] = '';
  198. }
  199. }
  200. /**
  201. * @see TripalField::load()
  202. */
  203. public function load($entity, $details = array()) {
  204. $record = $details['record'];
  205. $base_table = $this->field['settings']['base_table'];
  206. $field_name = $this->field['field_name'];
  207. $field_type = $this->field['type'];
  208. $field_table = $this->field['settings']['chado_table'];
  209. $field_column = $this->field['settings']['chado_column'];
  210. // Get the PKey for this table
  211. $schema = chado_get_schema($field_table);
  212. $pkey = $schema['primary key'][0];
  213. // Get the FK that links to the base record.
  214. $schema = chado_get_schema($field_table);
  215. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  216. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  217. // Set some defaults for the empty record.
  218. $entity->{$field_name}['und'][0] = array(
  219. 'value' => array(),
  220. 'chado-' . $field_table . '__' . $pkey => '',
  221. 'chado-' . $field_table . '__' . $fkey_lcolumn => '',
  222. 'chado-' . $field_table . '__' . 'synonym_id' => '',
  223. 'chado-' . $field_table . '__' . 'pub_id' => '',
  224. 'chado-' . $field_table . '__' . 'is_current' => TRUE,
  225. 'chado-' . $field_table . '__' . 'is_internal' => '',
  226. 'name' => '',
  227. 'type_id' => '',
  228. // Ignore the synonym_sgml column for now.
  229. );
  230. $linker_table = $base_table . '_synonym';
  231. $options = array('return_array' => 1);
  232. $record = chado_expand_var($record, 'table', $linker_table, $options);
  233. if (count($record->$linker_table) > 0) {
  234. $i = 0;
  235. foreach ($record->$linker_table as $index => $linker) {
  236. $synonym = $linker->synonym_id;
  237. $entity->{$field_name}['und'][$i] = array(
  238. 'value' => array(
  239. '@type' => $synonym->type_id->dbxref_id->db_id->name . ':' . $synonym->type_id->dbxref_id->accession,
  240. 'type' => $synonym->type_id->name,
  241. 'name' => $synonym->name,
  242. ),
  243. 'chado-' . $field_table . '__' . $pkey => $linker->$pkey,
  244. 'chado-' . $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn->$fkey_lcolumn,
  245. 'chado-' . $field_table . '__' . 'synonym_id' => $synonym->synonym_id,
  246. 'chado-' . $field_table . '__' . 'pub_id' => $linker->pub_id->pub_id,
  247. 'chado-' . $field_table . '__' . 'is_current' => $linker->is_current,
  248. 'chado-' . $field_table . '__' . 'is_internal' => $linker->is_internal,
  249. 'name' => $synonym->name,
  250. 'type_id' => $synonym->type_id->cvterm_id,
  251. );
  252. $i++;
  253. }
  254. }
  255. }
  256. }
  257. /**
  258. * Theme function for the synonym widget.
  259. *
  260. * @param $variables
  261. */
  262. function theme_chado_linker__synonym_widget($variables) {
  263. $element = $variables['element'];
  264. // These two fields were added to the widget to help identify the fields
  265. // for layout.
  266. $table_name = $element['#table_name'];
  267. $fkey = $element['#fkey_field'];
  268. $layout = "
  269. <div class=\"synonym-widget\">
  270. <div class=\"synonym-widget-item\">" .
  271. drupal_render($element['name']) . "
  272. </div>
  273. <div>" .
  274. drupal_render($element['type_id']) . "
  275. </div>
  276. <div class=\"synonym-widget-item\">" .
  277. drupal_render($element['chado-' . $table_name . '__is_internal']) . "
  278. </div>
  279. <div>" .
  280. drupal_render($element['chado-' . $table_name . '__is_current']) . "
  281. </div>
  282. </div>
  283. ";
  284. return $layout;
  285. }
  286. /**
  287. * An Ajax callback for the synonym widget.
  288. */
  289. function chado_linker__synonym_widget_form_ajax_callback($form, $form_state) {
  290. $field_name = $form_state['triggering_element']['#parents'][0];
  291. $delta = $form_state['triggering_element']['#parents'][2];
  292. return $form[$field_name]['und'][$delta];
  293. }