tripal_jbrowse_mgmt_configure.form.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * The settings form.
  4. *
  5. * @param $form
  6. * @param $form_state
  7. *
  8. * @return mixed
  9. */
  10. function tripal_jbrowse_mgmt_configure_form($form, &$form_state) {
  11. $message = '<p>Before filling out this form, ensure you have downloaded and installed JBrowse on your server. At this time, JBrowse needs to be on the same server as this Tripal site. <br /><strong>This form allows you to configure the location of JBrowse.</strong></p>';
  12. $form['message'] = [
  13. '#type' => 'markup',
  14. '#prefix' => '<p>',
  15. '#markup' => t($message),
  16. '#suffix' => '</p>',
  17. ];
  18. $settings = tripal_jbrowse_mgmt_get_settings();
  19. $form['data_dir'] = [
  20. '#title' => t('Data Directory'),
  21. '#description' => t(
  22. 'Absolute path to where data directories should live.
  23. This directory must be accessible by the apache user.'
  24. ),
  25. '#type' => 'textfield',
  26. '#maxlength' => 255,
  27. '#default_value' => $settings['data_dir'],
  28. '#required' => TRUE,
  29. ];
  30. $form['link'] = [
  31. '#title' => t('Path to JBrowse\'s Index File'),
  32. '#description' => t(
  33. 'Path to index.html that JBrowse provides.
  34. We suggest using tools/jbrowse.'
  35. ),
  36. '#type' => 'textfield',
  37. '#default_value' => $settings['link'],
  38. '#maxlength' => 255,
  39. '#required' => TRUE,
  40. ];
  41. $form['bin_path'] = [
  42. '#title' => t('Path to JBrowse\'s bin Directory'),
  43. '#description' => t(
  44. 'The absolute path to the bin directory that JBrowse provides.
  45. We suggest using PATH_TO_DRUPAL/tools/jbrowse/bin.'
  46. ),
  47. '#type' => 'textfield',
  48. '#default_value' => $settings['bin_path'],
  49. '#maxlength' => 255,
  50. '#required' => TRUE,
  51. ];
  52. $form['menu_template'] = [
  53. '#title' => t('Default menuTemplate'),
  54. '#description' => t(
  55. 'Default menuTemplate in the trackList.json file. See
  56. the JBrowse documentation for more info.'
  57. ),
  58. '#type' => 'textarea',
  59. '#required' => FALSE,
  60. '#default_value' => json_encode(
  61. $settings['menu_template'],
  62. JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
  63. ),
  64. ];
  65. $form['submit'] = [
  66. '#type' => 'submit',
  67. '#value' => 'Save Settings',
  68. ];
  69. return $form;
  70. }
  71. /**
  72. * @param $form
  73. * @param $form_state
  74. */
  75. function tripal_jbrowse_mgmt_configure_form_validate($form, &$form_state) {
  76. $values = $form_state['values'];
  77. // DATA DIR
  78. if (!file_exists($values['data_dir'])) {
  79. form_set_error('data_dir', 'The data directory does not exist.');
  80. }
  81. elseif (!is_writable($values['data_dir']) || !is_dir($values['data_dir'])) {
  82. form_set_error('data_dir', 'The data directory is not writeable.');
  83. }
  84. // BIN PATH
  85. $bin_path = $values['bin_path'];
  86. if (!file_exists($bin_path)) {
  87. form_set_error(
  88. 'bin_path',
  89. 'The bin directory (' . $bin_path . ') does not exist'
  90. );
  91. }
  92. elseif (!file_exists($bin_path . '/prepare-refseqs.pl')) {
  93. form_set_error(
  94. 'bin_path',
  95. 'The bin directory does not contain the required scripts.
  96. Please make sure the prepare-refseqs.pl script exists in ' . $bin_path
  97. );
  98. }
  99. $link_path = DRUPAL_ROOT . '/' . trim($values['link'], '/') . '/index.html';
  100. if (!file_exists($link_path)) {
  101. form_set_error('link', 'index.html does not exist in ' . $link_path);
  102. }
  103. $menu_template = $values['menu_template'];
  104. if (!empty($menu_template) && !json_decode(
  105. '{"menuTemplate": ' . $menu_template . '}'
  106. )) {
  107. form_set_error(
  108. 'menu_template',
  109. 'Please provide valid JSON for menuTemplate.'
  110. );
  111. }
  112. }
  113. /**
  114. * @param $form
  115. * @param $form_state
  116. */
  117. function tripal_jbrowse_mgmt_configure_form_submit($form, &$form_state) {
  118. $values = $form_state['values'];
  119. $bin_path = rtrim($values['bin_path'], '/');
  120. if (!empty($values['menu_template'])) {
  121. $menu_template = json_decode(
  122. '{"menuTemplate": ' . $values['menu_template'] . '}',
  123. TRUE
  124. );
  125. $menu_template = $menu_template['menuTemplate'];
  126. }
  127. else {
  128. $menu_template = [];
  129. }
  130. $settings = [
  131. 'data_dir' => rtrim($values['data_dir'], '/'),
  132. 'bin_path' => $bin_path,
  133. 'link' => $values['link'],
  134. 'menu_template' => $menu_template,
  135. ];
  136. tripal_jbrowse_mgmt_save_settings($settings);
  137. drupal_set_message('Settings saved successfully');
  138. }