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