chado_linker__synonym.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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_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. $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->field['settings']['chado_table'];
  48. $base_table = $this->field['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. $fkey_value = $items[$delta]['chado-' . $table_name . '__' . $fkey];
  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. $fkey_value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $table_name . '__' . $fkey];
  78. $synonym_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $table_name . '__synonym_id'];
  79. $pub_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $table_name . '__pub_id'];
  80. $is_current = $form_state['values'][$field_name]['und'][$delta]['chado-' . $table_name . '__is_current'];
  81. $is_internal = $form_state['values'][$field_name]['und'][$delta]['chado-' . $table_name . '__is_internal'];
  82. $syn_name = $form_state['values'][$field_name]['und'][$delta]['name'];
  83. $syn_type = $form_state['values'][$field_name]['und'][$delta]['type_id'];
  84. }
  85. $options = array();
  86. // Get the schema for the synonym table so we can make sure we limit the
  87. // size of the name field to the proper size.
  88. $schema = chado_get_schema('synonym');
  89. $widget['#table_name'] = $table_name;
  90. $widget['#fkey_field'] = $fkey;
  91. $widget['#theme'] = 'chado_linker__synonym_widget';
  92. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  93. $widget['#suffix'] = "</span>";
  94. $widget['value'] = array(
  95. '#type' => 'value',
  96. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  97. );
  98. $widget['chado-' . $table_name . '__' . $pkey] = array(
  99. '#type' => 'value',
  100. '#default_value' => $record_id,
  101. );
  102. $widget['chado-' . $table_name . '__synonym_id'] = array(
  103. '#type' => 'value',
  104. '#default_value' => $fkey_value,
  105. );
  106. $widget['chado-' . $table_name . '__' . $fkey] = array(
  107. '#type' => 'value',
  108. '#default_value' => $fkey_value,
  109. );
  110. // TODO: add a widget for selecting a publication.
  111. $widget['chado-' . $table_name . '__pub_id'] = array(
  112. '#type' => 'value',
  113. '#default_value' => $pub_id,
  114. );
  115. $widget['type_id'] = array(
  116. '#type' => 'select',
  117. '#title' => t('Type'),
  118. '#options' => $options,
  119. '#default_value' => $syn_type,
  120. );
  121. $widget['name'] = array(
  122. '#type' => 'textfield',
  123. '#title' => t('Synonym Name'),
  124. '#default_value' => $syn_name,
  125. '#size' => 25,
  126. );
  127. $widget['chado-' . $table_name . '__is_current'] = array(
  128. '#type' => 'checkbox',
  129. '#title' => t('Is Current'),
  130. '#default_value' => $is_current,
  131. '#required' => $element['#required'],
  132. );
  133. $widget['chado-' . $table_name . '__is_internal'] = array(
  134. '#type' => 'checkbox',
  135. '#title' => t('Is Internal'),
  136. '#default_value' => $is_internal,
  137. '#required' => $element['#required'],
  138. );
  139. }
  140. /**
  141. * @see TripalField::widgetFormValidate()
  142. */
  143. public function widgetFormValidate($entity_type, $entity, $field, $items, &$errors) {
  144. $field_name = $this->field['field_name'];
  145. $field_type = $this->field['type'];
  146. $table_name = $this->field['settings']['chado_table'];
  147. $field_table = $this->field['settings']['chado_table'];
  148. $field_column = $this->field['settings']['chado_column'];
  149. $base_table = $this->field['settings']['base_table'];
  150. $schema = chado_get_schema($table_name);
  151. $pkey = $schema['primary key'][0];
  152. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  153. $fkey = $fkeys[0];
  154. // Get the field values.
  155. foreach ($items as $delta => $values) {
  156. $record_id = $values['chado-' . $table_name . '__' . $pkey];
  157. $fkey_value = $values['chado-' . $table_name . '__' . $fkey];
  158. $synonym_id = $values['chado-' . $table_name . '__synonym_id'];
  159. $pub_id = $values['chado-' . $table_name . '__pub_id'];
  160. $is_current = $values['chado-' . $table_name . '__is_current'];
  161. $is_internal = $values['chado-' . $table_name . '__is_internal'];
  162. $syn_name = $values['name'];
  163. $syn_type = $values['type_id'];
  164. // Make sure that if a synonym is provided that a type is also
  165. // provided.
  166. if ($syn_name and !$syn_type) {
  167. $errors[$field_name][$delta]['und'][] = array(
  168. 'message' => t("Please set a synonym type."),
  169. 'error' => 'chado_linker__synonym',
  170. );
  171. }
  172. if (!$syn_name and $syn_type) {
  173. $errors[$field_name][$delta]['und'][] = array(
  174. 'message' => t("Please set a synonym name."),
  175. 'error' => 'chado_linker__synonym',
  176. );
  177. }
  178. }
  179. }
  180. /**
  181. * @see TripalField::widgetFormSubmit()
  182. */
  183. public function widgetFormSubmit($entity_type, $entity, $langcode, &$items, $form, &$form_state) {
  184. $field_name = $this->field['field_name'];
  185. $field_type = $this->field['type'];
  186. $table_name = $this->field['settings']['chado_table'];
  187. $field_table = $this->field['settings']['chado_table'];
  188. $field_column = $this->field['settings']['chado_column'];
  189. $base_table = $this->field['settings']['base_table'];
  190. $schema = chado_get_schema($table_name);
  191. $pkey = $schema['primary key'][0];
  192. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  193. $fkey = $fkeys[0];
  194. // Get the field values.
  195. foreach ($items as $delta => $values) {
  196. $record_id = $values['chado-' . $table_name . '__' . $pkey];
  197. $fkey_value = $values['chado-' . $table_name . '__' . $fkey];
  198. $synonym_id = $values['chado-' . $table_name . '__synonym_id'];
  199. $pub_id = $values['chado-' . $table_name . '__pub_id'];
  200. $is_current = $values['chado-' . $table_name . '__is_current'];
  201. $is_internal = $values['chado-' . $table_name . '__is_internal'];
  202. $syn_name = $values['name'];
  203. $syn_type = $values['type_id'];
  204. // If the user provided a $syn_name and a $syn_type then we want to set
  205. // the foreign key value to be the chado_record_id.
  206. if ($syn_name and $syn_type) {
  207. // Get the synonym. If one with the same name and type is already present
  208. // then use that. Otherwise, insert a new one.
  209. if (!$synonym_id) {
  210. $synonym = chado_generate_var('synonym', array('name' => $syn_name, 'type_id' => $syn_type));
  211. if (!$synonym) {
  212. $synonym = chado_insert_record('synonym', array(
  213. 'name' => $syn_name,
  214. 'type_id' => $syn_type,
  215. 'synonym_sgml' => '',
  216. ));
  217. $synonym = (object) $synonym;
  218. }
  219. // Set the synonym_id and FK value
  220. $items[$delta]['chado-' . $table_name . '__synonym_id'] = $synonym->synonym_id;
  221. }
  222. if (!$pub_id) {
  223. $pub = chado_generate_var('pub', array('uniquename' => 'null'));
  224. $items[$delta]['chado-' . $table_name . '__pub_id'] = $pub->pub_id;
  225. }
  226. }
  227. else {
  228. // If the $syn_name is not set, then remove the linker FK value to the base table.
  229. $items[$delta]['chado-' . $table_name . '__' . $fkey] = '';
  230. $items[$delta]['chado-' . $table_name . '__synonym_id'] = '';
  231. $items[$delta]['chado-' . $table_name . '__is_internal'] = '';
  232. $items[$delta]['chado-' . $table_name . '__is_current'] = '';
  233. }
  234. }
  235. }
  236. /**
  237. * @see TripalField::load()
  238. */
  239. public function load($entity, $details = array()) {
  240. $record = $details['record'];
  241. $base_table = $this->field['settings']['base_table'];
  242. $field_name = $this->field['field_name'];
  243. $field_type = $this->field['type'];
  244. $field_table = $this->field['settings']['chado_table'];
  245. $field_column = $this->field['settings']['chado_column'];
  246. // Get the PKey for this table
  247. $schema = chado_get_schema($field_table);
  248. $pkey = $schema['primary key'][0];
  249. // Get the FK that links to the base record.
  250. $schema = chado_get_schema($field_table);
  251. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  252. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  253. // Set some defaults for the empty record.
  254. $entity->{$field_name}['und'][0] = array(
  255. 'value' => array(),
  256. 'chado-' . $field_table . '__' . $pkey => '',
  257. 'chado-' . $field_table . '__' . $fkey_lcolumn => '',
  258. 'chado-' . $field_table . '__' . 'synonym_id' => '',
  259. 'chado-' . $field_table . '__' . 'pub_id' => '',
  260. 'chado-' . $field_table . '__' . 'is_current' => TRUE,
  261. 'chado-' . $field_table . '__' . 'is_internal' => '',
  262. 'name' => '',
  263. 'type_id' => '',
  264. // Ignore the synonym_sgml column for now.
  265. );
  266. $linker_table = $base_table . '_synonym';
  267. $options = array('return_array' => 1);
  268. $record = chado_expand_var($record, 'table', $linker_table, $options);
  269. if (count($record->$linker_table) > 0) {
  270. $i = 0;
  271. foreach ($record->$linker_table as $index => $linker) {
  272. $synonym = $linker->synonym_id;
  273. $entity->{$field_name}['und'][$i] = array(
  274. 'value' => array(
  275. '@type' => $synonym->type_id->dbxref_id->db_id->name . ':' . $synonym->type_id->dbxref_id->accession,
  276. 'type' => $synonym->type_id->name,
  277. 'name' => $synonym->name,
  278. ),
  279. 'chado-' . $field_table . '__' . $pkey = $linker->$pkey,
  280. 'chado-' . $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn->$fkey_lcolumn,
  281. 'chado-' . $field_table . '__' . 'synonym_id' => $synonym->synonym_id,
  282. 'chado-' . $field_table . '__' . 'pub_id' => $linker->pub_id->pub_id,
  283. 'chado-' . $field_table . '__' . 'is_current' => $linker->is_current,
  284. 'chado-' . $field_table . '__' . 'is_internal' => $linker->is_internal,
  285. 'name' => $synonym->name,
  286. 'type_id' => $synonym->type_id->cvterm_id,
  287. );
  288. $i++;
  289. }
  290. }
  291. }
  292. }
  293. /**
  294. * Theme function for the synonym widget.
  295. *
  296. * @param $variables
  297. */
  298. function theme_chado_linker__synonym_widget($variables) {
  299. $element = $variables['element'];
  300. // These two fields were added to the widget to help identify the fields
  301. // for layout.
  302. $table_name = $element['#table_name'];
  303. $fkey = $element['#fkey_field'];
  304. $layout = "
  305. <div class=\"synonym-widget\">
  306. <div class=\"synonym-widget-item\">" .
  307. drupal_render($element['name']) . "
  308. </div>
  309. <div>" .
  310. drupal_render($element['type_id']) . "
  311. </div>
  312. <div class=\"synonym-widget-item\">" .
  313. drupal_render($element['chado-' . $table_name . '__is_internal']) . "
  314. </div>
  315. <div>" .
  316. drupal_render($element['chado-' . $table_name . '__is_current']) . "
  317. </div>
  318. </div>
  319. ";
  320. return $layout;
  321. }
  322. /**
  323. * An Ajax callback for the synonym widget.
  324. */
  325. function chado_linker__synonym_widget_form_ajax_callback($form, $form_state) {
  326. $field_name = $form_state['triggering_element']['#parents'][0];
  327. $delta = $form_state['triggering_element']['#parents'][2];
  328. return $form[$field_name]['und'][$delta];
  329. }