chado_linker__synonym.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. 'type' => '',
  66. 'ns' => '',
  67. 'nsurl' => '',
  68. ),
  69. ),
  70. );
  71. return $field_info;
  72. }
  73. /**
  74. * Implements hook_widget_info.
  75. *
  76. * This is a hook provided by the tripal_chado module for offloading
  77. * the hook_field_widget_info() hook for each field to specify.
  78. */
  79. function chado_linker__synonym_widget_info() {
  80. return array(
  81. 'label' => t('Synonyms'),
  82. 'field types' => array('chado_linker__synonym'),
  83. );
  84. }
  85. /**
  86. * Implements hook_formatter_info.
  87. *
  88. * This is a hook provided by the tripal_chado module for
  89. * offloading the hook_field_formatter_info() for each field
  90. * to specify.
  91. *
  92. */
  93. function chado_linker__synonym_formatter_info() {
  94. return array(
  95. 'label' => t('Synonyms'),
  96. 'field types' => array('chado_linker__synonym'),
  97. 'settings' => array(
  98. ),
  99. );
  100. }
  101. /**
  102. *
  103. * @param unknown $entity_type
  104. * @param unknown $entity
  105. * @param unknown $field
  106. * @param unknown $instance
  107. * @param unknown $langcode
  108. * @param unknown $items
  109. * @param unknown $display
  110. */
  111. function chado_linker__synonym_formatter(&$element, $entity_type, $entity, $field,
  112. $instance, $langcode, $items, $display) {
  113. $chado_table = $field['settings']['chado_table'];
  114. foreach ($items as $delta => $item) {
  115. if ($item[$chado_table . '__synonym_id']) {
  116. $synonym = chado_generate_var('synonym', array('synonym_id' => $item[$chado_table . '__synonym_id']));
  117. $name = $synonym->name;
  118. if ($synonym->type_id->name != 'exact') {
  119. $name .= ' (<i>' . $synonym->type_id->name . '</i>)';
  120. }
  121. $element[$delta] = array(
  122. '#type' => 'markup',
  123. '#markup' => $name,
  124. );
  125. }
  126. }
  127. }
  128. /**
  129. *
  130. */
  131. function chado_linker__synonym_widget(&$widget, $form, $form_state, $field,
  132. $instance, $langcode, $items, $delta, $element) {
  133. $entity = $form['#entity'];
  134. $field_name = $field['field_name'];
  135. // Get the FK column that links to the base table.
  136. $table_name = $field['settings']['chado_table'];
  137. $base_table = $field['settings']['base_table'];
  138. $schema = chado_get_schema($table_name);
  139. $pkey = $schema['primary key'][0];
  140. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  141. $fkey = $fkeys[0];
  142. // Get the field defaults.
  143. $record_id = '';
  144. $fkey_value = $element['#entity']->chado_record_id;
  145. $synonym_id = '';
  146. $pub_id = '';
  147. $is_current = TRUE;
  148. $is_internal = FALSE;
  149. $syn_name = '';
  150. $syn_type = '';
  151. // If the field already has a value then it will come through the $items
  152. // array. This happens when editing an existing record.
  153. if (array_key_exists($delta, $items)) {
  154. $record_id = $items[$delta]['value'];
  155. $fkey_value = $items[$delta][$table_name . '__' . $fkey];
  156. $synonym_id = $items[$delta][$table_name . '__synonym_id'];
  157. $pub_id = $items[$delta][$table_name . '__pub_id'];
  158. $is_current = $items[$delta][$table_name . '__is_current'];
  159. $is_internal = $items[$delta][$table_name . '__is_internal'];
  160. $syn_name = $items[$delta][$table_name . '--synonym__name'];
  161. $syn_type = $items[$delta][$table_name . '--synonym__type_id'];
  162. }
  163. // Check $form_state['values'] to see if an AJAX call set the values.
  164. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  165. $record_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name);
  166. $fkey_value = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $fkey);
  167. $synonym_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__synonym_id');
  168. $pub_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__pub_id');
  169. $is_current = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__is_current');
  170. $is_internal = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__is_internal');
  171. $syn_name = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '--synonym__name');
  172. $syn_type = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '--synonym__type_id');
  173. }
  174. // Get the synonym type terms. There shouldn't be too many.
  175. $cv = tripal_get_default_cv('synonym', 'type_id');
  176. $options = tripal_get_cvterm_select_options($cv->cv_id);
  177. // Get the schema for the synonym table so we can make sure we limit the
  178. // size of the name field to the proper size.
  179. $schema = chado_get_schema('synonym');
  180. $widget['#table_name'] = $table_name;
  181. $widget['#fkey_field'] = $fkey;
  182. $widget['#element_validate'] = array('chado_linker__synonym_widget_validate');
  183. $widget['#theme'] = 'chado_linker__synonym_widget';
  184. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  185. $widget['#suffix'] = "</span>";
  186. $widget['value'] = array(
  187. '#type' => 'value',
  188. '#default_value' => $record_id,
  189. );
  190. $widget[$table_name . '__' . $fkey] = array(
  191. '#type' => 'value',
  192. '#default_value' => $fkey_value,
  193. );
  194. $widget[$table_name . '--synonym__type_id'] = array(
  195. '#type' => 'select',
  196. '#title' => t('Type'),
  197. '#options' => $options,
  198. '#default_value' => $syn_type,
  199. );
  200. $widget[$table_name . '--synonym__name'] = array(
  201. '#type' => 'textfield',
  202. '#title' => t('Synonym Name'),
  203. '#default_value' => $syn_name,
  204. '#size' => 25,
  205. );
  206. $widget[$table_name . '__is_current'] = array(
  207. '#type' => 'checkbox',
  208. '#title' => t('Is Current'),
  209. '#default_value' => $is_current,
  210. '#required' => $element['#required'],
  211. );
  212. $widget[$table_name . '__is_internal'] = array(
  213. '#type' => 'checkbox',
  214. '#title' => t('Is Internal'),
  215. '#default_value' => $is_internal,
  216. '#required' => $element['#required'],
  217. );
  218. }
  219. /**
  220. * An Ajax callback for the synonym widget.
  221. */
  222. function chado_linker__synonym_widget_form_ajax_callback($form, $form_state) {
  223. $field_name = $form_state['triggering_element']['#parents'][0];
  224. $delta = $form_state['triggering_element']['#parents'][2];
  225. return $form[$field_name]['und'][$delta];
  226. }
  227. /**
  228. * Callback function for validating the chado_linker__synonym_widget.
  229. */
  230. function chado_linker__synonym_widget_validate($element, &$form_state) {
  231. $field_name = $element['#field_name'];
  232. $delta = $element['#delta'];
  233. $table_name = $element['#table_name'];
  234. $fkey = $element['#fkey_field'];
  235. // If the form ID is field_ui_field_edit_form, then the user is editing the
  236. // field's values in the manage fields form of Drupal. We don't want
  237. // to validate it as if it were being used in a data entry form.
  238. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  239. return;
  240. }
  241. // Get the field values.
  242. $fkey_value = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $fkey);
  243. $synonym_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__synonym_id');
  244. $pub_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__pub_id');
  245. $is_current = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__is_current');
  246. $is_internal = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__is_internal');
  247. $syn_name = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '--synonym__name');
  248. $syn_type = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '--synonym__type_id');
  249. // Make sure that if a synonym is provided that a type is also
  250. // provided.
  251. if ($syn_name and !$syn_type) {
  252. form_set_error(implode('][', $element ['#parents']) . '][' . $table_name . '--synonym__type_id', t("Please set a synonym type."));
  253. }
  254. if (!$syn_name and $syn_type) {
  255. form_set_error(implode('][', $element ['#parents']) . '][' . $table_name . '--synonym__name', t("Please set a synonym name."));
  256. }
  257. // If the user provided a cv_id and a name then we want to set the
  258. // foreign key value to be the chado_record_idd
  259. if ($syn_name and $syn_type) {
  260. // Get the synonym. If one with the same name and type is already present
  261. // then use that. Otherwise, insert a new one.
  262. if (!$synonym_id) {
  263. $synonym = chado_generate_var('synonym', array('name' => $syn_name, 'type_id' => $syn_type));
  264. if (!$synonym) {
  265. $synonym = chado_insert_record('synonym', array(
  266. 'name' => $syn_name,
  267. 'type_id' => $syn_type,
  268. 'synonym_sgml' => '',
  269. ));
  270. $synonym = (object) $synonym;
  271. }
  272. // Set the synonym_id and FK value
  273. tripal_chado_set_field_form_values($field_name, $form_state, $synonym->synonym_id, $delta, $table_name . '__synonym_id');
  274. $fkey_value = $element['#entity']->chado_record_id;
  275. tripal_chado_set_field_form_values($field_name, $form_state, $fkey_value, $delta, $table_name . '__' . $fkey);
  276. }
  277. if (!$pub_id) {
  278. $pub = chado_generate_var('pub', array('uniquename' => 'null'));
  279. tripal_chado_set_field_form_values($field_name, $form_state, $pub->pub_id, $delta, $table_name . '__pub_id');
  280. }
  281. }
  282. else {
  283. // If the $syn_name is not set, then remove the linker FK value to the base table.
  284. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $fkey);
  285. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__synonym_id');
  286. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__is_internal');
  287. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__is_current');
  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__name']) . "
  305. </div>
  306. <div>" .
  307. drupal_render($element[$table_name . '--synonym__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. * Loads the field values with appropriate data.
  321. *
  322. * This function is called by the tripal_chado_field_storage_load() for
  323. * each property managed by the field_chado_storage storage type. This is
  324. * an optional hook function that is only needed if the field has
  325. * multiple form elements.
  326. *
  327. * @param $field
  328. * @param $entity
  329. * @param $base_table
  330. * @param $record
  331. */
  332. function chado_linker__synonym_load($field, $entity, $base_table, $record) {
  333. $field_name = $field['field_name'];
  334. $field_type = $field['type'];
  335. $field_table = $field['settings']['chado_table'];
  336. $field_column = $field['settings']['chado_column'];
  337. // Get the FK that links to the base record.
  338. $schema = chado_get_schema($field_table);
  339. $pkey = $schema['primary key'][0];
  340. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  341. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  342. // Set some defaults for the empty record.
  343. $entity->{$field_name}['und'][0] = array(
  344. 'value' => '',
  345. $field_table . '__' . $fkey_lcolumn => '',
  346. $field_table . '__' . 'synonym_id' => '',
  347. $field_table . '__' . 'pub_id' => '',
  348. $field_table . '__' . 'is_current' => TRUE,
  349. $field_table . '__' . 'is_internal' => '',
  350. $field_table . '--' . 'synonym__name' => '',
  351. $field_table . '--' . 'synonym__type_id' => '',
  352. // Ignore the synonym_sgml column for now.
  353. );
  354. $linker_table = $base_table . '_synonym';
  355. $options = array('return_array' => 1);
  356. $record = chado_expand_var($record, 'table', $linker_table, $options);
  357. if (count($record->$linker_table) > 0) {
  358. $i = 0;
  359. foreach ($record->$linker_table as $index => $linker) {
  360. $synonym = $linker->synonym_id;
  361. $entity->{$field_name}['und'][$i] = array(
  362. 'value' => $linker->$pkey,
  363. $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn->$fkey_lcolumn,
  364. $field_table . '__' . 'synonym_id' => $synonym->synonym_id,
  365. $field_table . '__' . 'pub_id' => $linker->pub_id->pub_id,
  366. $field_table . '__' . 'is_current' => $linker->is_current,
  367. $field_table . '__' . 'is_internal' => $linker->is_internal,
  368. $field_table . '--' . 'synonym__name' => $synonym->name,
  369. $field_table . '--' . 'synonym__type_id' => $synonym->type_id->cvterm_id,
  370. );
  371. $i++;
  372. }
  373. }
  374. }