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