tripal_bulk_loader.admin.inc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /*******************************************************************************
  3. * tripal_bulk_loader_admin_template
  4. */
  5. function tripal_bulk_loader_admin_template () {
  6. $add_url = url("admin/tripal/tripal_bulk_loader_template/add");
  7. $output = "<a href=\"$add_url\">Create a new bulk loader template</a><br>";
  8. $del_url = url("admin/tripal/tripal_bulk_loader_template/delete");
  9. $output .= "<a href=\"$del_url\">Delete a bulk loader template</a>";
  10. return $output;
  11. }
  12. /*******************************************************************************
  13. * tripal_bulk_loader_admin_template_add
  14. */
  15. function tripal_bulk_loader_admin_template_add () {
  16. return drupal_get_form('tripal_bulk_loader_admin_template_form');
  17. }
  18. /*******************************************************************************
  19. * tripal_bulk_loader_admin_template_form
  20. */
  21. function tripal_bulk_loader_admin_template_form (&$form_state = NULL) {
  22. $no_col = variable_get('tripal_bulk_loader_template_number_of_column', 0);
  23. $name = variable_get('tripal_bulk_loader_template_name', NULL);
  24. $form = array();
  25. if ($no_col == 0) {
  26. $form['template_name'] = array(
  27. '#type' => 'textfield',
  28. '#title' => t('Template Name'),
  29. '#weight' => 0,
  30. '#required' => TRUE,
  31. '#default_value' => $template_name,
  32. );
  33. $form['no_cols'] = array(
  34. '#type' => 'textfield',
  35. '#title' => t('Number of Column'),
  36. '#weight' => 1,
  37. '#required' => TRUE,
  38. '#maxlength' => 3,
  39. '#size' =>3,
  40. );
  41. $form['addsheet'] = array (
  42. '#type' => 'button',
  43. '#value' => t('Add a sheet'),
  44. '#weight' => 1,
  45. '#executes_submit_callback' => TRUE,
  46. );
  47. } else {
  48. $form['template_setting'] = array(
  49. '#suffix' => '<table id="tripal_bulk_loader_template_add_table"><tr><th>Column#</th><th>Chado Table</th><th>Chado Column</th><th>Associated CVterm</th><th>Translate Regex</th></tr><tr>',
  50. '#type' => 'item',
  51. '#title' => t("Template '$name'"),
  52. );
  53. }
  54. $shema = "";
  55. global $db_url;
  56. if(is_array($db_url) and array_key_exists('chado',$db_url)){
  57. $shema = 'public';
  58. } else {
  59. $shema = 'chado';
  60. }
  61. $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = '%s' ORDER BY table_name";
  62. $previous_db = tripal_db_set_active('chado');
  63. $result = db_query($sql, $shema);
  64. $allowed_tables = array ();
  65. while ($tb = db_fetch_object($result)) {
  66. $allowed_tables[$tb->table_name] = $tb->table_name;
  67. }
  68. $first_table = db_result(db_query($sql,$shema));
  69. $sql = "SELECT column_name FROM information_schema.columns WHERE table_name ='%s'";
  70. $result = db_query($sql, $first_table);
  71. $allowed_columns = array();
  72. while ($col = db_fetch_object($result)) {
  73. $allowed_columns[$col->column_name] = $col->column_name;
  74. }
  75. tripal_db_set_active($previous_db);
  76. for ($i = 0; $i < $no_col; $i ++) {
  77. $j = $i + 1;
  78. $form[$i]['col'] = array(
  79. '#prefix' => '<td class="tripal_bulk_loader_template_col">',
  80. '#suffix' => '</td>',
  81. '#type' => 'textfield',
  82. '#maxlength' => 3,
  83. '#size' => 3,
  84. '#required' => TRUE
  85. );
  86. global $base_url;
  87. $form[$i]['chado_table'] = array(
  88. '#prefix' => '<td class="tripal_bulk_loader_template_chado_table">',
  89. '#suffix' => '</td>',
  90. '#type' => 'select',
  91. '#options' => $allowed_tables,
  92. '#attributes' => array(
  93. 'onChange' => "return tripal_update_chado_columns(this,'$base_url')",
  94. ),
  95. '#required' => TRUE
  96. );
  97. $form[$i]['chado_column'] = array(
  98. '#prefix' => '<td class="tripal_bulk_loader_template_chado_column">',
  99. '#suffix' => '</td>',
  100. '#type' => 'select',
  101. '#options' => $allowed_columns,
  102. );
  103. $form[$i]['chado_cvterm'] = array(
  104. '#prefix' => '<td class="tripal_bulk_loader_template_chado_cvterm">',
  105. '#suffix' => '</td>',
  106. '#type' => 'textfield',
  107. '#size' => 10,
  108. );
  109. $form[$i]['chado_regex'] = array(
  110. '#prefix' => '<td class="tripal_bulk_loader_template_chado_regex">',
  111. '#suffix' => '</td></tr><tr>',
  112. '#type' => 'textfield',
  113. '#size' => 10,
  114. );
  115. }
  116. if ($no_col != 0) {
  117. $form['removesheet'] = array (
  118. '#type' => 'button',
  119. '#value' => t('Remove this sheet'),
  120. '#weight' => 49,
  121. '#executes_submit_callback' => TRUE,
  122. '#prefix' => '</tr></table>',
  123. );
  124. $form['submit'] = array (
  125. '#type' => 'submit',
  126. '#value' => t('Save'),
  127. '#weight' => 50,
  128. '#executes_submit_callback' => TRUE,
  129. );
  130. }
  131. return $form;
  132. }
  133. /************************************************************************
  134. * tripal_bulk_loader_admin_template_form_submit
  135. */
  136. function tripal_bulk_loader_admin_template_form_submit($form, &$form_state){
  137. $op = $form_state['values']['op'];
  138. if ($op == 'Add a sheet') {
  139. $name = $form_state['values']['template_name'];
  140. $sql = "SELECT template_id FROM {tripal_bulk_loader_template} WHERE name = '%s'";
  141. $template_id = db_result(db_query($sql, $name));
  142. if ($template_id) {
  143. form_set_error('template_name',t("The template '$name' exists. Please use another name."));
  144. variable_set('tripal_bulk_loader_template_name', NULL);
  145. variable_set('tripal_bulk_loader_template_number_of_column', NULL);
  146. } else {
  147. variable_set('tripal_bulk_loader_template_name', $form_state['values']['template_name']);
  148. variable_set('tripal_bulk_loader_template_number_of_column', $form_state['values']['no_cols']);
  149. }
  150. return;
  151. } elseif ($op == 'Remove this sheet') {
  152. variable_set('tripal_bulk_loader_template_name', NULL);
  153. variable_set('tripal_bulk_loader_template_number_of_column', NULL);
  154. return;
  155. } elseif ($op == 'Save') {
  156. $name = variable_get('tripal_bulk_loader_template_name', NULL);
  157. $template_array = "array('DUMMY' => 'TEMPLATE ARRAY')";
  158. $sql = "INSERT INTO {tripal_bulk_loader_template} (name, template_array) VALUES ('%s', '%s')";
  159. if (db_query($sql, $name, $template_array)) {
  160. drupal_set_message("Bulk loader template '$name' added.");
  161. variable_set('tripal_bulk_loader_template_name', NULL);
  162. variable_set('tripal_bulk_loader_template_number_of_column', NULL);
  163. }
  164. }
  165. }
  166. /************************************************************************
  167. * tripal_bulk_loader_admin_template_delete
  168. */
  169. function tripal_bulk_loader_admin_template_delete () {
  170. return drupal_get_form('tripal_bulk_loader_admin_template_del_form');
  171. }
  172. /************************************************************************
  173. * tripal_bulk_loader_admin_template_del_from
  174. */
  175. function tripal_bulk_loader_admin_template_del_form (&$form_state = NULL) {
  176. $form = array();
  177. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  178. $results = db_query($sql);
  179. $templates = array();
  180. while ($template = db_fetch_object($results)) {
  181. $templates [$template->template_id] = $template->name;
  182. }
  183. if ($templates) {
  184. $form['label'] = array(
  185. '#type' => 'item',
  186. '#title' => t('Select a template to delete'),
  187. '#weight' => 0,
  188. );
  189. $form['template_name'] = array(
  190. '#type' => 'select',
  191. '#title' => t('Template Name'),
  192. '#options' => $templates,
  193. '#weight' => 1,
  194. '#required' => TRUE
  195. );
  196. $form['submit'] = array (
  197. '#type' => 'submit',
  198. '#value' => t('Delete'),
  199. '#weight' => 2,
  200. '#executes_submit_callback' => TRUE,
  201. );
  202. } else {
  203. $form['label'] = array(
  204. '#type' => 'item',
  205. '#description' => t('No template available'),
  206. '#weight' => 0,
  207. );
  208. }
  209. return $form;
  210. }
  211. /************************************************************************
  212. * tripal_bulk_loader_admin_template_del_form_submit
  213. */
  214. function tripal_bulk_loader_admin_template_del_form_submit($form, &$form_state){
  215. $template = $form_state['values']['template_name'];
  216. $name = db_result(db_query("SELECT name FROM {tripal_bulk_loader_template} WHERE template_id = $template"));
  217. $sql = "DELETE FROM {tripal_bulk_loader_template} WHERE template_id = %d";
  218. if (db_query($sql, $template)) {
  219. drupal_set_message("Bulk loader template '$name' deleted.");
  220. }
  221. }
  222. /************************************************************************
  223. * tripal_bulk_loader_chado_column_ajax
  224. */
  225. function tripal_bulk_loader_chado_column_ajax ($table) {
  226. $sql = "SELECT column_name FROM information_schema.columns WHERE table_name ='%s'";
  227. $previous_db = tripal_db_set_active('chado');
  228. $result = db_query($sql, $table);
  229. tripal_db_set_active($previous_db);
  230. $cols = array();
  231. while ($col = db_fetch_object($result)) {
  232. $cols[$col->column_name] = $col->column_name;
  233. }
  234. drupal_json($cols);
  235. }