tripal_jbrowse_mgmt_configure.form.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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['data_path'] = [
  31. '#title' => t('Data Path'),
  32. '#description' => t(
  33. 'Relative path to where data directories lives relative to the web browser root.'
  34. ),
  35. '#type' => 'textfield',
  36. '#maxlength' => 255,
  37. '#default_value' => $settings['data_path'],
  38. '#required' => TRUE,
  39. ];
  40. $form['link'] = [
  41. '#title' => t('Path to JBrowse\'s Index File'),
  42. '#description' => t(
  43. 'Path to index.html that JBrowse provides.
  44. We suggest using tools/jbrowse.'
  45. ),
  46. '#type' => 'textfield',
  47. '#default_value' => $settings['link'],
  48. '#maxlength' => 255,
  49. '#required' => TRUE,
  50. ];
  51. $form['bin_path'] = [
  52. '#title' => t('Path to JBrowse\'s bin Directory'),
  53. '#description' => t(
  54. 'The absolute path to the bin directory that JBrowse provides.
  55. We suggest using PATH_TO_DRUPAL/tools/jbrowse/bin.'
  56. ),
  57. '#type' => 'textfield',
  58. '#default_value' => $settings['bin_path'],
  59. '#maxlength' => 255,
  60. '#required' => TRUE,
  61. ];
  62. $form['menu_template'] = [
  63. '#title' => t('Default menuTemplate'),
  64. '#description' => t(
  65. 'Default menuTemplate in the trackList.json file. See
  66. the JBrowse documentation for more info.'
  67. ),
  68. '#type' => 'textarea',
  69. '#required' => FALSE,
  70. '#default_value' => json_encode(
  71. $settings['menu_template'],
  72. JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
  73. ),
  74. ];
  75. $form['submit'] = [
  76. '#type' => 'submit',
  77. '#value' => 'Save Settings',
  78. ];
  79. return $form;
  80. }
  81. /**
  82. * @param $form
  83. * @param $form_state
  84. */
  85. function tripal_jbrowse_mgmt_configure_form_validate($form, &$form_state) {
  86. $values = $form_state['values'];
  87. // DATA DIR
  88. if (!file_exists($values['data_dir'])) {
  89. form_set_error('data_dir', 'The data directory does not exist.');
  90. }
  91. elseif (!is_writable($values['data_dir']) || !is_dir($values['data_dir'])) {
  92. form_set_error('data_dir', 'The data directory is not writeable.');
  93. }
  94. // BIN PATH
  95. $bin_path = $values['bin_path'];
  96. if (!file_exists($bin_path)) {
  97. form_set_error(
  98. 'bin_path',
  99. 'The bin directory (' . $bin_path . ') does not exist'
  100. );
  101. }
  102. elseif (!file_exists($bin_path . '/prepare-refseqs.pl')) {
  103. form_set_error(
  104. 'bin_path',
  105. 'The bin directory does not contain the required scripts.
  106. Please make sure the prepare-refseqs.pl script exists in ' . $bin_path
  107. );
  108. }
  109. $link_path = DRUPAL_ROOT . '/' . trim($values['link'], '/') . '/index.html';
  110. if (!file_exists($link_path)) {
  111. form_set_error('link', 'index.html does not exist in ' . $link_path);
  112. }
  113. $menu_template = $values['menu_template'];
  114. if (!empty($menu_template) && !json_decode(
  115. '{"menuTemplate": ' . $menu_template . '}'
  116. )) {
  117. form_set_error(
  118. 'menu_template',
  119. 'Please provide valid JSON for menuTemplate.'
  120. );
  121. }
  122. }
  123. /**
  124. * @param $form
  125. * @param $form_state
  126. */
  127. function tripal_jbrowse_mgmt_configure_form_submit($form, &$form_state) {
  128. $values = $form_state['values'];
  129. $bin_path = rtrim($values['bin_path'], '/');
  130. if (!empty($values['menu_template'])) {
  131. $menu_template = json_decode(
  132. '{"menuTemplate": ' . $values['menu_template'] . '}',
  133. TRUE
  134. );
  135. $menu_template = $menu_template['menuTemplate'];
  136. }
  137. else {
  138. $menu_template = [];
  139. }
  140. $settings = [
  141. 'data_dir' => rtrim($values['data_dir'], '/'),
  142. 'data_path' => trim($values['data_path'], '/'),
  143. 'bin_path' => $bin_path,
  144. 'link' => $values['link'],
  145. 'menu_template' => $menu_template,
  146. ];
  147. tripal_jbrowse_mgmt_save_settings($settings);
  148. drupal_set_message('Settings saved successfully');
  149. }