chado_linker__synonym.inc 14 KB

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