chado_linker__synonym.inc 16 KB

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