chado_linker__synonym.inc 15 KB

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