chado_linker__synonym.inc 16 KB

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