tripal_bulk_loader.admin.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. $output = '';
  17. $output .= drupal_get_form('tripal_bulk_loader_create_template_base_form');
  18. return $output;
  19. }
  20. /**
  21. * Implements hook_tripal_bulk_loader_supported_modules
  22. *
  23. * @return
  24. * An array of <chado table> => <Human-readable Name> describing the modules supported
  25. */
  26. function tripal_bulk_loader_tripal_bulk_loader_supported_modules() {
  27. return array(
  28. 'feature' => 'Feature',
  29. 'organism' => 'Organism',
  30. 'library' => 'Library',
  31. 'analysis' => 'Analysis',
  32. );
  33. }
  34. function tripal_bulk_loader_tripal_bulk_loader_feature_related_tables () {
  35. return array(
  36. 'feature' => 'Feature',
  37. 'analysis' => 'Analysis',
  38. 'element' => 'Element',
  39. 'cvterm' => 'CV Terms',
  40. 'dbxref' => 'External Database References',
  41. 'genotype' => 'Genotypes',
  42. 'phenotype' => 'Phenotypes',
  43. 'pub' => 'Publications',
  44. 'feature_relationship' => 'Relationships between Features',
  45. 'synonym' => 'Synonyms',
  46. 'featureloc' => 'Feature Locations',
  47. 'featurepos' => 'Feature Positions',
  48. 'featureprop' => 'Feature Properties',
  49. 'featurerange' => 'Feature Ranges',
  50. 'library' => 'Library',
  51. 'phylonode' => 'Polynode'
  52. );
  53. }
  54. /*******************************************************************************
  55. * tripal_bulk_loader_admin_template_form
  56. */
  57. function tripal_bulk_loader_create_template_base_form (&$form_state = NULL) {
  58. // Basic Details--------------------------------------------------------------
  59. $form['bulk_loader'] = array(
  60. '#type' => 'fieldset',
  61. '#title' => t('Step 1: Basic Template Details'),
  62. );
  63. $modules = module_invoke_all('tripal_bulk_loader_supported_modules');
  64. $modules[''] = 'Select A Module';
  65. if ($form_state['storage']['base_table']) {
  66. $base_table = $form_state['storage']['base_table'];
  67. } else {
  68. $base_table = '';
  69. }
  70. $form['bulk_loader']['chado_module'] = array(
  71. '#title' => t('Chado Module'),
  72. '#description' => t('Please select the module for which you would like to create an importer'),
  73. '#type' => 'select',
  74. '#options' => $modules,
  75. '#default_value' => $base_table,
  76. '#weight' => 0,
  77. '#required' => TRUE
  78. );
  79. $form['bulk_loader']['template_name'] = array(
  80. '#type' => 'textfield',
  81. '#title' => t('Template Name'),
  82. '#default_value' => $form_state['storage']['template_name'],
  83. );
  84. if ($base_table) {
  85. // Add Field (Maps to column in spreadsheet)--------------------------------------
  86. $form['add_fields'] = array(
  87. '#type' => 'fieldset',
  88. '#title' => t('Step 2: Add Fields'),
  89. '#prefix' => '<div id="tripal-bulk-loader-field">',
  90. '#suffix' => '</div>',
  91. );
  92. $field_type = ($form_state['storage']['field_type'])? $form_state['storage']['field_type'] : 'column';
  93. $form['add_fields']['type'] = array(
  94. '#type' => 'radios',
  95. '#title' => t('Type of Field'),
  96. '#options' => array(
  97. 'column' => t('Fields which maps to a Spreadsheet Column'),
  98. 'constant' => t('Field which remains Constant throughout the Spreadsheet'),
  99. ),
  100. '#default_value' => $field_type,
  101. '#ahah' => array(
  102. 'path' => 'admin/tripal/tripal_bulk_loader_template/add/field_type_ahah',
  103. 'wrapper' => 'tripal-bulk-loader-field',
  104. 'effect' => 'fade'
  105. ),
  106. );
  107. $form['add_fields']['field_title'] = array(
  108. '#type' => 'textfield',
  109. '#title' => t('Human-readable Title for Field')
  110. );
  111. // Spreadsheet column
  112. $form['add_fields']['columns'] = array(
  113. '#type' => ($field_type == 'column')? 'fieldset' : 'hidden',
  114. '#title' => t('Spreadsheet Column'),
  115. '#prefix' => '<div id="tripal-bulk-loader-spreadsheet-column">',
  116. '#suffix' => '</div>',
  117. );
  118. $form['add_fields']['columns']['sheet'] = array(
  119. '#type' => 'textfield',
  120. '#title' => t('Worksheet'),
  121. '#description' => t('Specify the name of the worksheet.'),
  122. '#size' => 5,
  123. '#default_value' => ($form_state['storage']['sheet_name'])? $form_state['storage']['sheet_name'] : 'Sheet1',
  124. );
  125. $form['add_fields']['columns']['column'] = array(
  126. '#type' => 'textfield',
  127. '#title' => t('Column'),
  128. '#description' => t('Specify the column in the spreadsheet that this field maps to where the first column is 1.'),
  129. '#size' => 5,
  130. '#default_value' => $form_state['storage']['column_number'],
  131. );
  132. // Global Value
  133. $form['add_fields']['constant'] = array(
  134. '#type' => ($field_type == 'constant')? 'fieldset' : 'hidden',
  135. '#title' => t('Constant'),
  136. '#prefix' => '<div id="tripal-bulk-loader-constant-column">',
  137. '#suffix' => '</div>',
  138. );
  139. $form['add_fields']['constant']['constant_value'] = array(
  140. '#type' => 'textfield',
  141. '#title' => t('Constant Value'),
  142. '#description' => t('Specify the value you wish this field to have regardless of spreadsheet data.'),
  143. '#default_value' => $form_state['storage']['constant_value']
  144. );
  145. // Chado Field
  146. $form['add_fields']['chado'] = array(
  147. '#type' => 'fieldset',
  148. '#title' => t('Chado Field/Column Details'),
  149. '#prefix' => '<div id="tripal-bulk-loader-chado-column">',
  150. '#suffix' => '</div>',
  151. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  152. );
  153. $related_tables = module_invoke_all('tripal_bulk_loader_'.$base_table.'_related_tables');
  154. $table = ($form_state['storage']['chado_table'])? $form_state['storage']['chado_table'] : $base_table;
  155. $form['add_fields']['chado']['chado_table'] = array(
  156. '#type' => 'select',
  157. '#title' => t('Chado Table'),
  158. '#options' => $related_tables,
  159. '#default_value' => $table,
  160. '#ahah' => array(
  161. 'path' => 'admin/tripal/tripal_bulk_loader_template/add/chado_column_ahah',
  162. 'wrapper' => 'tripal-bulk-loader-chado-column',
  163. 'effect' => 'fade'
  164. ),
  165. );
  166. $table_description = module_invoke_all('chado_'.$table.'_schema');
  167. $chado_fields = array();
  168. foreach($table_description['fields'] as $field_name => $field_array) {
  169. $chado_fields[$field_name] = $field_name;
  170. }
  171. $form['add_fields']['chado']['chado_field'] = array(
  172. '#type' => 'select',
  173. '#title' => t('Chado Field/Column'),
  174. '#options' => $chado_fields
  175. );
  176. $form['add_fields']['submit-add_field'] = array(
  177. '#type' => 'submit',
  178. '#value' => 'Add Field'
  179. );
  180. // List Current Fields------------------------------------------------------------
  181. $form['current_fields'] = array(
  182. '#type' => 'fieldset',
  183. '#title' => t('Step 3: Confirm Fields')
  184. );
  185. $form['current_fields']['items'] = array(
  186. '#type' => 'item',
  187. '#value' => print_r($form_state['storage']['template'],TRUE),
  188. '#prefix' => '<pre>',
  189. '#suffix' => '</pre>',
  190. );
  191. // Submit-------------------------------------------------------------------------
  192. $form['submit-save'] = array(
  193. '#type' => 'submit',
  194. '#value' => 'Save Template'
  195. );
  196. } else {
  197. $form['submit-next_step'] = array(
  198. '#type' => 'submit',
  199. '#value' => 'Next Step'
  200. );
  201. } //end of if base details are set
  202. return $form;
  203. }
  204. /************************************************************************
  205. * tripal_bulk_loader_admin_template_form_validate
  206. */
  207. function tripal_bulk_loader_create_template_base_form_validate($form, &$form_state){
  208. if(!$form_state['ahah_submission']) {
  209. if ($form_state['values']['op'] == 'Add Field') {
  210. if ($form_state['values']['type'] == 'column') {
  211. if (!$form_state['values']['column']) {
  212. form_set_error('column', 'Column is Required!');
  213. }
  214. if (!$form_state['values']['sheet']) {
  215. form_set_error('sheet', 'Worksheet Name is Required!');
  216. }
  217. } elseif ($form_state['values']['type'] == 'constant') {
  218. if (!$form_state['values']['constant_value']) {
  219. form_set_error('constant_value', 'Value of Constant is Required!');
  220. }
  221. }
  222. } elseif ($form_state['values']['op'] == 'Save Template') {
  223. if (!$form_state['storage']['template']) {
  224. form_set_error('', 'Tempalte must contain at least one field');
  225. }
  226. }
  227. }
  228. }
  229. /************************************************************************
  230. * tripal_bulk_loader_admin_template_form_submit
  231. */
  232. function tripal_bulk_loader_create_template_base_form_submit($form, &$form_state){
  233. //dpm($form_state, 'form_state: submit');
  234. // AHAH Storage ----------------------------------------------------------
  235. $form_state['rebuild'] = TRUE;
  236. $form_state['storage']['base_table'] = $form_state['values']['chado_module'];
  237. $form_state['storage']['template_name'] = $form_state['values']['template_name'];
  238. $form_state['storage']['field_title'] = $form_state['values']['field_title'];
  239. $form_state['storage']['field_type'] = $form_state['values']['type'];
  240. $form_state['storage']['sheet_name'] = $form_state['values']['sheet'];
  241. $form_state['storage']['column_number'] = $form_state['values']['column'];
  242. $form_state['storage']['constant_value'] = $form_state['values']['constant_value'];
  243. $form_state['storage']['chado_table'] = $form_state['values']['chado_table'];
  244. $form_state['storage']['chado_field'] = $form_state['values']['chado_field'];
  245. if (!$form_state['ahah_submission']) {
  246. // Add Field to Template----------------------------------------------
  247. if ($form_state['values']['op'] == 'Add Field') {
  248. $template = $form_state['storage']['template'];
  249. if ($form_state['storage']['field_type'] == 'column') {
  250. $template[$form_state['storage']['chado_table']]['field'][] = array(
  251. 'type' => 'table field',
  252. 'title' => $form_state['storage']['field_title'],
  253. 'field' => $form_state['storage']['chado_field'],
  254. //'required' => <true|false>,
  255. //'allowed values' => empty by default,
  256. 'spreadsheet sheet' => $form_state['storage']['sheet_name'],
  257. 'spreadsheet column' => $form_state['storage']['column_number'],
  258. //'exposed' => 'true|false' If exposed, will give a select box first from allowed values if set, second from database if values not set.
  259. //'mapping' => array(
  260. // 'from' => 'to'
  261. // '/from re/' => 'to'
  262. //),
  263. );
  264. } elseif ($form_state['storage']['field_type'] == 'constant') {
  265. $template[$form_state['storage']['chado_table']]['field'][] = array(
  266. 'type' => 'constant',
  267. 'title' => $form_state['storage']['field_title'],
  268. 'field' => $form_state['storage']['chado_field'],
  269. //'required' => <true|false>,
  270. //'allowed values' => empty by default,
  271. 'constant value' => $form_state['storage']['constant_value'],
  272. //'exposed' => 'true|false' If exposed, will give a select box first from allowed values if set, second from database if values not set.
  273. );
  274. }
  275. dpm($template, 'template');
  276. $form_state['storage']['template'] = $template;
  277. // Save Template ----------------------------------------------------
  278. } elseif ($form_state['values']['op'] == 'Save Template') {
  279. $record = array(
  280. 'name' => $form_state['values']['template_name'],
  281. 'template_array' => print_r($form_state['storage']['template'],TRUE)
  282. );
  283. //Check if template exists
  284. $sql = "SELECT count(*) as count FROM tripal_bulk_loader_template WHERE name='%s'";
  285. if (db_result(db_query($sql, $form_state['values']['template_name']))) {
  286. // Update Previous
  287. drupal_write_record('tripal_bulk_loader_template', $record, array('name'));
  288. } else {
  289. // Insert New
  290. drupal_write_record('tripal_bulk_loader_template', $record);
  291. }
  292. }
  293. } //end of if not ahah submission
  294. }
  295. function tripal_bulk_loader_chado_column_ahah () {
  296. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  297. $form_build_id = $_POST['form_build_id'];
  298. $form = form_get_cache($form_build_id, $form_state);
  299. $args = $form['#parameters'];
  300. $form_id = array_shift($args);
  301. $form_state['post'] = $form['#post'] = $_POST;
  302. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  303. $form_state['ahah_submission'] = TRUE;
  304. $form['#programmed'] = $form['#redirect'] = FALSE;
  305. drupal_process_form($form_id, $form, $form_state);
  306. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  307. $form_element = $form['add_fields']['chado'];
  308. // Remove the wrapper so we don't double it up.
  309. unset($form_element['#prefix'], $form_element['#suffix']);
  310. $output = theme('status_messages');
  311. $output .= drupal_render($form_element);
  312. // Final rendering callback.
  313. print drupal_json(array('status' => TRUE, 'data' => $output));
  314. exit();
  315. }
  316. function tripal_bulk_loader_field_type_ahah () {
  317. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  318. $form_build_id = $_POST['form_build_id'];
  319. $form = form_get_cache($form_build_id, $form_state);
  320. $args = $form['#parameters'];
  321. $form_id = array_shift($args);
  322. $form_state['post'] = $form['#post'] = $_POST;
  323. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  324. $form_state['ahah_submission'] = TRUE;
  325. $form['#programmed'] = $form['#redirect'] = FALSE;
  326. drupal_process_form($form_id, $form, $form_state);
  327. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  328. $form_element = $form['add_fields'];
  329. // Remove the wrapper so we don't double it up.
  330. unset($form_element['#prefix'], $form_element['#suffix']);
  331. $output = theme('status_messages');
  332. $output .= drupal_render($form_element);
  333. // Final rendering callback.
  334. print drupal_json(array('status' => TRUE, 'data' => $output));
  335. exit();
  336. }