bulk_loader.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. /*************************************************************************
  3. *
  4. */
  5. function tripal_core_bulk_loader_create (){
  6. return drupal_get_form('tripal_core_bulk_loader_create_form');
  7. }
  8. /*************************************************************************
  9. *
  10. */
  11. function tripal_core_bulk_loader_create_form (&$form_state = NULL){
  12. $form = array();
  13. // get the step used by this multistep form
  14. $step = 1;
  15. if(isset($form_state['storage'])){
  16. $step = (int)$form_state['storage']['step'];
  17. }
  18. $form = array();
  19. if($step == 1){
  20. tripal_core_bulk_loader_create_form_step1 ($form,$form_state);
  21. $form_state['storage']['step'] = $step + 1;
  22. }
  23. if($step == 2){
  24. tripal_core_bulk_loader_create_form_step2 ($form,$form_state);
  25. }
  26. return $form;
  27. }
  28. /*************************************************************************
  29. *
  30. */
  31. function tripal_core_bulk_loader_create_form_validate ($form,&$form_state) {
  32. }
  33. /*************************************************************************
  34. *
  35. */
  36. function tripal_core_bulk_loader_create_form_submit ($form,&$form_state) {
  37. if($form_State['storage']['step'] < 4){
  38. return;
  39. }
  40. }
  41. /*************************************************************************
  42. *
  43. */
  44. function tripal_core_bulk_loader_create_form_step2 (&$form,$form_state){
  45. if(isset($form_state['values']['columns'])){
  46. $form_state['storage']['columns'] = $form_state['values']['columns'];
  47. }
  48. $form['bulk_loader'] = array(
  49. '#type' => 'fieldset',
  50. '#title' => t('Step 2: Define the columns of the file'),
  51. );
  52. $columns = $form_state['storage']['columns'];
  53. // $form['debug']= array(
  54. // '#value' => "Columns: $columns",
  55. // '#weight' => -1
  56. // );
  57. $fields = array();
  58. $fields[''] = '';
  59. // these fields correspond with the columns of the feature table and
  60. // the foreign key contraints for the feature table.
  61. $fields = array(
  62. '' => '',
  63. 'ignore' => 'Ignore this column',
  64. 'Feature details' => array (
  65. 'name' => 'Feature Name (human readable)',
  66. 'uniquename' => 'Unique Feature Name',
  67. 'type' => 'Feature type (must be a valid SO term)',
  68. 'alt_type' => 'Additional ontology term',
  69. 'residues' => 'Residues',
  70. ),
  71. 'Organism specification' => array (
  72. 'organism_id' => 'Organism ID number',
  73. 'genus' => 'Genus',
  74. 'species' => 'Species',
  75. 'full_name' => 'Scientific Name (genus + species)',
  76. 'common_name' => 'Common Name',
  77. ),
  78. 'External Database Cross-Reference' => array(
  79. 'db_id' => 'Database ID number',
  80. 'db_name' => 'Database name (must exists in the database)',
  81. 'accession' => 'Accession',
  82. ),
  83. 'Feature Relationship' => array (
  84. 'rel_type' => 'Relationship Type (must be a valid relationship term)',
  85. 'parent' => 'Parent unique name',
  86. 'parent type' => 'Parent Type (must be a valid SO term)',
  87. ),
  88. 'Feature Location' => array (
  89. 'srcfeature' => 'Reference feature (must already exist in the database)',
  90. 'srcfeature_type' => 'Reference feature type (must be a valid SO term)',
  91. 'fmin' => 'Start position',
  92. 'fmax' => 'Stop position',
  93. 'strand' => 'Strand (valid values: 0,-1,+1)',
  94. 'phase' => 'Phase (valid values: (+,-,.)',
  95. ),
  96. 'Feature Property' => array (
  97. 'property' => 'Feature property value',
  98. ),
  99. 'Feature Synonym' => array (
  100. 'syn_name' => 'Synonym name',
  101. ),
  102. 'Library specification' => array (
  103. 'library_id' => 'Library ID number',
  104. 'library_name' => 'Library name',
  105. ),
  106. 'Analysis specification' => array (
  107. 'analysis_id' => 'Analysis ID number',
  108. 'analysis_source' => 'Analysis identifying name (sourcename column in Chado)',
  109. 'analysis_desc' => 'Analysis description',
  110. 'analysis_program' => 'Analysis program',
  111. 'analysis_program_version' => 'Analysis program version'
  112. ),
  113. );
  114. // organism foreign key identifies. These are used to find the organism
  115. // for which the feature belongs.
  116. $form['columns'] = array(
  117. '#type' => 'hidden',
  118. '#value' => $columns,
  119. );
  120. // get the list of organisms
  121. $sql = "SELECT * FROM {organism} ORDER BY genus, species";
  122. $previous_db = tripal_db_set_active('chado'); // use chado database
  123. $org_rset = db_query($sql);
  124. tripal_db_set_active($previous_db); // now use drupal database
  125. $organisms = array();
  126. $genus = array();
  127. $species = array();
  128. $common_names = array();
  129. $genera[''] = '';
  130. $species[''] = '';
  131. $common_names[''] = '';
  132. $full_names[''] = '';
  133. while($organism = db_fetch_object($org_rset)){
  134. $full_names["$organism->genus $organism->species"] = "$organism->genus $organism->species";
  135. $genera[$organism->genus] = "$organism->genus";
  136. $species[$organism->species] = "$organism->species";
  137. $common_names[$organism->common_name] = "$organism->common_name";
  138. }
  139. for($i = 1; $i <= $columns; $i++){
  140. $form['bulk_loader']["col_$i"] = array(
  141. '#type' => 'fieldset',
  142. '#title' => t("Column $i of the input file"),
  143. );
  144. $form['bulk_loader']["col_$i"]["col_type_$i"] = array(
  145. '#title' => t('Field Selection'),
  146. '#type' => 'select',
  147. '#options' => $fields,
  148. '#weight' => 0,
  149. // this field will use AJAX to populate and information needed
  150. // for specific types, such as for feature properties. It
  151. // calls the step2_get_type URL and passes the column ($i).
  152. // the return value is additional form items or text
  153. '#ahah' => array(
  154. 'event' => 'change',
  155. 'wrapper' => "type_cols_$i",
  156. 'path' => "/admin/tripal/bulk_load/step2_get_type/$i",
  157. 'effect' => 'fade',
  158. 'method' => 'replace'
  159. ),
  160. );
  161. // these next fields are hidden (access = false) and will be shown
  162. // if the user selects the feature property type in the drop down
  163. // above.
  164. //-------------------------------------------------------------------------
  165. // default text box for allowed values
  166. $form['bulk_loader']["col_$i"]["col_prop_valid_$i"] = array(
  167. '#title' => t('Allowed Values'),
  168. '#type' => 'textarea',
  169. '#weight' => 0,
  170. '#description' => 'Please provide a list of allowed values for this field. Separate these values with a space. Leave blank to allow any value',
  171. '#access' => FALSE
  172. );
  173. //-------------------------------------------------------------------------
  174. // Organism allowed values
  175. $form['bulk_loader']["col_$i"]["col_prop_genera_$i"] = array (
  176. '#title' => t('Allowed Values'),
  177. '#type' => t('select'),
  178. '#description' => t("Choose all allowed genera values for this column (ctrl+click to select multiple values). Select none to allow all"),
  179. '#required' => FALSE,
  180. '#options' => $genera,
  181. '#weight' => 2,
  182. '#multiple' => TRUE,
  183. '#size' => 10,
  184. '#access' => FALSE
  185. );
  186. $form['bulk_loader']["col_$i"]["col_prop_species_$i"] = array (
  187. '#title' => t('Allowed Values'),
  188. '#type' => t('select'),
  189. '#description' => t("Choose all allowed species values for this column (ctrl+click to select multiple values). Select none to allow all"),
  190. '#required' => FALSE,
  191. '#options' => $species,
  192. '#weight' => 2,
  193. '#multiple' => TRUE,
  194. '#size' => 10,
  195. '#access' => FALSE
  196. );
  197. $form['bulk_loader']["col_$i"]["col_prop_common_name_$i"] = array (
  198. '#title' => t('Allowed Values'),
  199. '#type' => t('select'),
  200. '#description' => t("Choose all allowed values for this column (ctrl+click to select multiple values). Select none to allow all"),
  201. '#required' => FALSE,
  202. '#options' => $common_names,
  203. '#weight' => 2,
  204. '#multiple' => TRUE,
  205. '#size' => 10,
  206. '#access' => FALSE
  207. );
  208. $form['bulk_loader']["col_$i"]["col_prop_full_name_$i"] = array (
  209. '#title' => t('Allowed Values'),
  210. '#type' => t('select'),
  211. '#description' => t("Choose all allowed values for this column (shift+click to select multiple values). Select none to allow all"),
  212. '#required' => FALSE,
  213. '#options' => $full_names,
  214. '#weight' => 2,
  215. '#multiple' => TRUE,
  216. '#size' => 10,
  217. '#access' => FALSE
  218. );
  219. //-------------------------------------------------------------------------
  220. // feature property fields
  221. $form['bulk_loader']["col_$i"]["col_prop_name_$i"] = array(
  222. '#title' => t('Property Name'),
  223. '#type' => 'textfield',
  224. '#weight' => 1,
  225. '#description' => 'Please provide a name for this property. It should be a single
  226. word with only alphanumeric characters and underscores. If this
  227. name exists as a CV term in Chado already it will be reused, otherwise
  228. a new CV term with this name will be added.',
  229. '#required' => TRUE,
  230. '#access' => FALSE
  231. );
  232. $form['bulk_loader']["col_$i"]["col_prop_full_name_$i"] = array(
  233. '#title' => t('Property Full Name'),
  234. '#type' => 'textfield',
  235. '#weight' => 3,
  236. '#description' => 'Please provide a human readable name for this property. This will be used when
  237. displaying the property title',
  238. '#access' => FALSE
  239. );
  240. $form['bulk_loader']["col_$i"]["col_prop_desc_$i"] = array(
  241. '#title' => t('Property Description'),
  242. '#type' => 'textarea',
  243. '#weight' => 4,
  244. '#description' => 'Please provide a description of this property.',
  245. '#access' => FALSE
  246. );
  247. // this is an empty div box that gets put on the form. This is the
  248. // box where the hidden form elements will be rendered when the
  249. // AJAX call returns
  250. $form['bulk_loader']["col_$i"]["type_wrap_$i"] = array(
  251. '#prefix' => "<div class=\"clear-block\" id=\"type_cols_$i\">",
  252. '#value' => ' ',
  253. '#suffix' => '</div>',
  254. );
  255. }
  256. $form['submit'] = array (
  257. '#type' => 'submit',
  258. '#value' => t('Next'),
  259. '#weight' => 5,
  260. '#executes_submit_callback' => TRUE,
  261. );
  262. }
  263. /*************************************************************************
  264. *
  265. */
  266. function tripal_core_bulk_loader_ahah_step2_feature_type($column){
  267. // create a form_state array to hold the form variables
  268. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  269. $form_state['storage']['step'] = 2;
  270. // retreive the form from the cache
  271. $form_build_id = $_POST['form_build_id'];
  272. $form = form_get_cache($form_build_id, $form_state);
  273. // get the form name
  274. $args = $form['#parameters'];
  275. $form_id = array_shift($args);
  276. // add the post variables to the form and set a few other items
  277. $form['#post'] = $_POST;
  278. $form['#redirect'] = FALSE;
  279. $form['#programmed'] = FALSE;
  280. $form_state['post'] = $_POST;
  281. drupal_process_form($form_id, $form, $form_state);
  282. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  283. // get the column
  284. $i = $column;
  285. // check to see what type of field has been selected and provide the
  286. // additional fields as appropriate
  287. $type = $form_state["post"]["col_type_$i"];
  288. if(strcmp($type,'genus')==0){
  289. $propform['bulk_loader']["col_$i"]["col_prop_genera_$i"] = $form['bulk_loader']["col_$i"]["col_prop_genera_$i"];
  290. $propform['bulk_loader']["col_$i"]["col_prop_genera_$i"]['#access'] = TRUE;
  291. }
  292. elseif(strcmp($type,'species')==0){
  293. $propform['bulk_loader']["col_$i"]["col_prop_species_$i"] = $form['bulk_loader']["col_$i"]["col_prop_species_$i"];
  294. $propform['bulk_loader']["col_$i"]["col_prop_species_$i"]['#access'] = TRUE;
  295. }
  296. elseif(strcmp($type,'common_name')==0){
  297. $propform['bulk_loader']["col_$i"]["col_prop_common_name_$i"] = $form['bulk_loader']["col_$i"]["col_prop_common_name_$i"];
  298. $propform['bulk_loader']["col_$i"]["col_prop_common_name_$i"]['#access'] = TRUE;
  299. }
  300. elseif(strcmp($type,'full_name')==0){
  301. $propform['bulk_loader']["col_$i"]["col_prop_full_name_$i"] = $form['bulk_loader']["col_$i"]["col_prop_full_name_$i"];
  302. $propform['bulk_loader']["col_$i"]["col_prop_full_name_$i"]['#access'] = TRUE;
  303. }
  304. elseif(strcmp($type,'property')==0){
  305. // we just want to render the property fields that were previously not visible
  306. $propform['bulk_loader']["col_$i"]["col_prop_name_$i"] = $form['bulk_loader']["col_$i"]["col_prop_name_$i"];
  307. $propform['bulk_loader']["col_$i"]["col_prop_full_name_$i"] = $form['bulk_loader']["col_$i"]["col_prop_full_name_$i"];
  308. $propform['bulk_loader']["col_$i"]["col_prop_desc_$i"] = $form['bulk_loader']["col_$i"]["col_prop_desc_$i"];
  309. $propform['bulk_loader']["col_$i"]["col_prop_name_$i"]['#access'] = TRUE;
  310. $propform['bulk_loader']["col_$i"]["col_prop_full_name_$i"]['#access'] = TRUE;
  311. $propform['bulk_loader']["col_$i"]["col_prop_desc_$i"]['#access'] = TRUE;
  312. }
  313. else {
  314. // use a default valid values textbox if we have no special needs for the type
  315. $propform['bulk_loader']["col_$i"]["col_prop_valid_$i"] = $form['bulk_loader']["col_$i"]["col_prop_valid_$i"];
  316. $propform['bulk_loader']["col_$i"]["col_prop_valid_$i"]['#access'] = TRUE;
  317. }
  318. $output = theme('status_messages') . drupal_render($propform);
  319. // Final rendering callback.
  320. drupal_json(array('status' => TRUE, 'data' => $output));
  321. }
  322. /*************************************************************************
  323. *
  324. */
  325. function tripal_core_bulk_loader_create_form_step1 (&$form,$form_state){
  326. $modules = array();
  327. $modules['feature'] = 'Feature';
  328. $modules['organism'] = 'Organism';
  329. $modules['library'] = 'Library';
  330. $modules['analysis'] = 'Analysis';
  331. // set the fieldset title to indicate the step
  332. $form['bulk_loader'] = array(
  333. '#type' => 'fieldset',
  334. '#title' => t('Step 1: Select the Chado module'),
  335. );
  336. $form['bulk_loader']['chado_module'] = array(
  337. '#title' => t('Chado Module'),
  338. '#description' => t('Please select the module for which you would like to create an importer'),
  339. '#type' => 'select',
  340. '#options' => $modules,
  341. '#weight' => 0,
  342. '#required' => TRUE
  343. );
  344. $form['bulk_loader']['columns']= array(
  345. '#type' => 'textfield',
  346. '#title' => t('Number of Columns'),
  347. '#description' => t('Please specify the number of columns in the input file.'),
  348. '#weight' => 2,
  349. '#required' => TRUE
  350. );
  351. $form['submit'] = array (
  352. '#type' => 'submit',
  353. '#value' => t('Next'),
  354. '#weight' => 5,
  355. '#executes_submit_callback' => TRUE,
  356. );
  357. }