tripal_bulk_loader.admin.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. );
  90. $field_type = ($form_state['storage']['field_type'])? $form_state['storage']['field_type'] : 'column';
  91. $form['add_fields']['type'] = array(
  92. '#type' => 'radios',
  93. '#title' => t('Type of Field'),
  94. '#options' => array(
  95. 'column' => t('Fields which maps to a Spreadsheet Column'),
  96. 'constant' => t('Field which remains Constant throughout the Spreadsheet'),
  97. ),
  98. '#default_value' => $field_type,
  99. );
  100. $form['add_fields']['field_title'] = array(
  101. '#type' => 'textfield',
  102. '#title' => t('Human-readable Title for Field')
  103. );
  104. // Spreadsheet column
  105. $form['add_fields']['columns'] = array(
  106. '#type' => ($field_type == 'column')? 'fieldset' : 'hidden',
  107. '#title' => t('Spreadsheet Column'),
  108. '#prefix' => '<div id="tripal-bulk-loader-spreadsheet-column">',
  109. '#suffix' => '</div>',
  110. );
  111. $form['add_fields']['columns']['sheet'] = array(
  112. '#type' => 'textfield',
  113. '#title' => t('Worksheet'),
  114. '#description' => t('Specify the name of the worksheet.'),
  115. '#size' => 5,
  116. '#default_value' => ($form_state['storage']['sheet_name'])? $form_state['storage']['sheet_name'] : 'Sheet1',
  117. );
  118. $form['add_fields']['columns']['column'] = array(
  119. '#type' => 'textfield',
  120. '#title' => t('Column'),
  121. '#description' => t('Specify the column in the spreadsheet that this field maps to where the first column is 1.'),
  122. '#size' => 5,
  123. '#default_value' => $form_state['storage']['column_number'],
  124. );
  125. // Global Value
  126. $form['add_fields']['constant'] = array(
  127. '#type' => ($field_type == 'constant')? 'fieldset' : 'hidden',
  128. '#title' => t('Constant'),
  129. '#prefix' => '<div id="tripal-bulk-loader-constant-column">',
  130. '#suffix' => '</div>',
  131. );
  132. $form['add_fields']['constant']['constant_value'] = array(
  133. '#type' => 'textfield',
  134. '#title' => t('Constant Value'),
  135. '#description' => t('Specify the value you wish this field to have regardless of spreadsheet data.'),
  136. '#default_value' => $form_state['storage']['constant_value']
  137. );
  138. // Chado Field
  139. $form['add_fields']['chado'] = array(
  140. '#type' => 'fieldset',
  141. '#title' => t('Chado Field/Column Details'),
  142. '#prefix' => '<div id="tripal-bulk-loader-chado-column">',
  143. '#suffix' => '</div>',
  144. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  145. );
  146. $related_tables = module_invoke_all('tripal_bulk_loader_'.$base_table.'_related_tables');
  147. $table = ($form_state['storage']['chado_table'])? $form_state['storage']['chado_table'] : $base_table;
  148. $form['add_fields']['chado']['chado_table'] = array(
  149. '#type' => 'select',
  150. '#title' => t('Chado Table'),
  151. '#options' => $related_tables,
  152. '#default_value' => $table,
  153. '#ahah' => array(
  154. 'path' => 'admin/tripal/tripal_bulk_loader_template/add/chado_column_ahah',
  155. 'wrapper' => 'tripal-bulk-loader-chado-column',
  156. 'effect' => 'fade'
  157. ),
  158. );
  159. $table_description = module_invoke_all('chado_'.$table.'_schema');
  160. $chado_fields = array();
  161. foreach($table_description['fields'] as $field_name => $field_array) {
  162. $chado_fields[$field_name] = $field_name;
  163. }
  164. $form['add_fields']['chado']['chado_field'] = array(
  165. '#type' => 'select',
  166. '#title' => t('Chado Field/Column'),
  167. '#options' => $chado_fields
  168. );
  169. $form['add_fields']['submit-add_field'] = array(
  170. '#type' => 'submit',
  171. '#value' => 'Add Field'
  172. );
  173. // List Current Fields------------------------------------------------------------
  174. $form['current_fields'] = array(
  175. '#type' => 'fieldset',
  176. '#title' => t('Step 3: Confirm Fields')
  177. );
  178. $form['current_fields']['items'] = array(
  179. '#type' => 'item',
  180. '#value' => print_r($form_state['storage']['template'],TRUE),
  181. '#prefix' => '<pre>',
  182. '#suffix' => '</pre>',
  183. );
  184. // Submit-------------------------------------------------------------------------
  185. $form['submit-save'] = array(
  186. '#type' => 'submit',
  187. '#value' => 'Save Template'
  188. );
  189. } else {
  190. $form['submit-next_step'] = array(
  191. '#type' => 'submit',
  192. '#value' => 'Next Step'
  193. );
  194. } //end of if base details are set
  195. return $form;
  196. }
  197. /************************************************************************
  198. * tripal_bulk_loader_admin_template_form_validate
  199. */
  200. function tripal_bulk_loader_create_template_base_form_validate($form, &$form_state){
  201. if(!$form_state['ahah_submission']) {
  202. if ($form_state['values']['op'] == 'Add Field') {
  203. if ($form_state['values']['type'] == 'column') {
  204. if (!$form_state['values']['column']) {
  205. form_set_error('column', 'Column is Required!');
  206. }
  207. if (!$form_state['values']['sheet']) {
  208. form_set_error('sheet', 'Worksheet Name is Required!');
  209. }
  210. } elseif ($form_state['values']['type'] == 'constant') {
  211. if (!$form_state['values']['constant_value']) {
  212. form_set_error('constant_value', 'Value of Constant is Required!');
  213. }
  214. }
  215. } elseif ($form_state['values']['op'] == 'Save Template') {
  216. if (!$form_state['storage']['template']) {
  217. form_set_error('', 'Tempalte must contain at least one field');
  218. }
  219. }
  220. }
  221. }
  222. /************************************************************************
  223. * tripal_bulk_loader_admin_template_form_submit
  224. */
  225. function tripal_bulk_loader_create_template_base_form_submit($form, &$form_state){
  226. //dpm($form_state, 'form_state: submit');
  227. // AHAH Storage ----------------------------------------------------------
  228. $form_state['rebuild'] = TRUE;
  229. $form_state['storage']['base_table'] = $form_state['values']['chado_module'];
  230. $form_state['storage']['template_name'] = $form_state['values']['template_name'];
  231. $form_state['storage']['field_title'] = $form_state['values']['field_title'];
  232. $form_state['storage']['field_type'] = $form_state['values']['type'];
  233. $form_state['storage']['sheet_name'] = $form_state['values']['sheet'];
  234. $form_state['storage']['column_number'] = $form_state['values']['column'];
  235. $form_state['storage']['constant_value'] = $form_state['values']['constant_value'];
  236. $form_state['storage']['chado_table'] = $form_state['values']['chado_table'];
  237. $form_state['storage']['chado_field'] = $form_state['values']['chado_field'];
  238. if (!$form_state['ahah_submission']) {
  239. // Add Field to Template----------------------------------------------
  240. if ($form_state['values']['op'] == 'Add Field') {
  241. $template = $form_state['storage']['template'];
  242. if ($form_state['storage']['field_type'] == 'column') {
  243. $template[$form_state['storage']['chado_table']]['field'][] = array(
  244. 'type' => 'table field',
  245. 'title' => $form_state['storage']['field_title'],
  246. 'field' => $form_state['storage']['chado_field'],
  247. //'required' => <true|false>,
  248. //'allowed values' => empty by default,
  249. 'spreadsheet sheet' => $form_state['storage']['sheet_name'],
  250. 'spreadsheet column' => $form_state['storage']['column_number'],
  251. //'exposed' => 'true|false' If exposed, will give a select box first from allowed values if set, second from database if values not set.
  252. //'mapping' => array(
  253. // 'from' => 'to'
  254. // '/from re/' => 'to'
  255. //),
  256. );
  257. } elseif ($form_state['storage']['field_type'] == 'constant') {
  258. $template[$form_state['storage']['chado_table']]['field'][] = array(
  259. 'type' => 'constant',
  260. 'title' => $form_state['storage']['field_title'],
  261. 'field' => $form_state['storage']['chado_field'],
  262. //'required' => <true|false>,
  263. //'allowed values' => empty by default,
  264. 'constant value' => $form_state['storage']['constant_value'],
  265. //'exposed' => 'true|false' If exposed, will give a select box first from allowed values if set, second from database if values not set.
  266. );
  267. }
  268. dpm($template, 'template');
  269. $form_state['storage']['template'] = $template;
  270. // Save Template ----------------------------------------------------
  271. } elseif ($form_state['values']['op'] == 'Save Template') {
  272. $record = array(
  273. 'name' => $form_state['values']['template_name'],
  274. 'template_array' => print_r($form_state['storage']['template'],TRUE)
  275. );
  276. //Check if template exists
  277. $sql = "SELECT count(*) as count FROM tripal_bulk_loader_template WHERE name='%s'";
  278. if (db_result(db_query($sql, $form_state['values']['template_name']))) {
  279. // Update Previous
  280. drupal_write_record('tripal_bulk_loader_template', $record, array('name'));
  281. } else {
  282. // Insert New
  283. drupal_write_record('tripal_bulk_loader_template', $record);
  284. }
  285. }
  286. } //end of if not ahah submission
  287. }
  288. function tripal_bulk_loader_chado_column_ahah () {
  289. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  290. $form_build_id = $_POST['form_build_id'];
  291. $form = form_get_cache($form_build_id, $form_state);
  292. $args = $form['#parameters'];
  293. $form_id = array_shift($args);
  294. $form_state['post'] = $form['#post'] = $_POST;
  295. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  296. $form_state['ahah_submission'] = TRUE;
  297. $form['#programmed'] = $form['#redirect'] = FALSE;
  298. drupal_process_form($form_id, $form, $form_state);
  299. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  300. $form_element = $form['add_fields']['chado'];
  301. // Remove the wrapper so we don't double it up.
  302. unset($form_element['#prefix'], $form_element['#suffix']);
  303. $output = theme('status_messages');
  304. $output .= drupal_render($form_element);
  305. // Final rendering callback.
  306. print drupal_json(array('status' => TRUE, 'data' => $output));
  307. exit();
  308. }