chado_linker__synonym.inc 14 KB

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