tripal_bulk_loader.admin.inc 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242
  1. <?php
  2. /**
  3. * @file
  4. * Handles Create/Edit/Delete Template Admin Forms
  5. */
  6. /**
  7. * Provides a description page and quick links for the bulk loader
  8. */
  9. function tripal_bulk_loader_admin_template() {
  10. $output = '';
  11. $output .= '<br><h3>Quick Links:</h3>';
  12. $output .= l('Create a new bulk loader template', 'admin/tripal/tripal_bulk_loader_template/create') . "<br>";
  13. $output .= l('Edit a bulk loader template', 'admin/tripal/tripal_bulk_loader_template/edit') . "<br>";
  14. $output .= l('Delete a bulk loader template', 'admin/tripal/tripal_bulk_loader_template/delete') . "<br>";
  15. $output .= l('Export a bulk loader template', 'admin/tripal/tripal_bulk_loader_template/export') . "<br>";
  16. $output .= l('Import a bulk loader template', 'admin/tripal/tripal_bulk_loader_template/import') . "<br>";
  17. $output .= '<br>';
  18. $output .= '<h3>Module Description:</h3>';
  19. $output .= '<p>This module provides the ability to create loading templates for any tab-delimited '
  20. . 'data file allowing it to be loaded into chado. The Loading Templates are a direct mapping '
  21. . 'between the columns in your file and the columns in chado tables. As such to use this tool '
  22. . 'you need to be very familar with the chado schema -See '
  23. . l('Chado -Getting Started', 'http://gmod.org/wiki/Chado_-_Getting_Started')
  24. . '. The ability to add constants and specify foreign key contraints is also provided '
  25. . 'in order for the loader to fill chado columns which may be required but are not specified '
  26. . 'in your input file.</p>';
  27. $output .= '<br>';
  28. $output .= '<h3>Setup Instructions</h3>';
  29. $output .= '<p>After intallation of the bulk loader module, the following tasks should be performed:</p>';
  30. $output .= '<ol>';
  31. $output .= '<li><b>Install Theme:</b> In order for Bulk Loading pages to be displayed correctly, '
  32. .'the contents of the Tripal Bulk Loader theme directory ([drupal root]/sites/all/modules/tripal/tripal_bulk_loader/theme) '
  33. .'should be moved to the base directory of the Tripal theme ([drupal root]/sites/all/themes/tripal). '
  34. .'Finally the drupal cache should be cleared for the new theme to take effect -navigate to admin/settings/performance '
  35. .'and click the Clear Cached Data button.</li>';
  36. $output .= '</ol>';
  37. return $output;
  38. }
  39. /**
  40. * A Configuration form for this module
  41. */
  42. function tripal_bulk_loader_configuration_form($form_state = NULL) {
  43. $form = array();
  44. $form['speed'] = array(
  45. '#type' => 'fieldset',
  46. '#title' => t('Possible Speed Improvements'),
  47. );
  48. $form['speed']['prepare'] = array(
  49. '#type' => 'checkbox',
  50. '#title' => t('Use Prepared Statements'),
  51. '#description' => t('SQL Prepared Statements allow for SQL queries which will be run '
  52. .'many times to be parsed, rewritten and planned only once rather then every time '
  53. .'the query is run. In the case of the bulk loader, this ensures that planning only '
  54. .'occurs once for each "record" in your bulk loading template.'),
  55. '#default_value' => variable_get('tripal_bulk_loader_prepare', TRUE),
  56. );
  57. $form['speed']['disable_triggers'] = array(
  58. '#type' => 'checkbox',
  59. '#title' => t('Delay Constraint Checking during loading job.'),
  60. '#description' => t('This delays the constraint checking until the end of the
  61. loading proccess.'),
  62. '#default_value' => variable_get('tripal_bulk_loader_disable_triggers', TRUE),
  63. );
  64. $form['speed']['transactions'] = array(
  65. '#type' => 'radios',
  66. '#title' => t('Transaction Rollback when an error is encountered'),
  67. '#options' => array(
  68. 'all' => t('Rollback the last constant set.'
  69. .'<div class="description"If you added more then one constant set then the
  70. successfully loaded constant sets will not be rolled back. However, once an error
  71. is encountered no further constant sets will be loaded either.</div>'),
  72. 'row' => t('Only Rollback the last line of the input file.'
  73. .'<div class="description">This option may allow you to restart the job after
  74. fixing the error (manual intervention needed).</div>'),
  75. 'none' => t('Do not use transactions<div class="description">This is not recommended.</div>')
  76. ),
  77. '#default_value' => variable_get('tripal_bulk_loader_transactions','row')
  78. );
  79. $form['submit1'] = array(
  80. '#type' => 'submit',
  81. '#value' => t('Save')
  82. );
  83. return $form;
  84. }
  85. /**
  86. * A Configuration form for this module (Submit)
  87. */
  88. function tripal_bulk_loader_configuration_form_submit($form, $form_state) {
  89. variable_set('tripal_bulk_loader_prepare', $form_state['values']['prepare']);
  90. variable_set('tripal_bulk_loader_disable_triggers', $form_state['values']['disable_triggers']);
  91. variable_set('tripal_bulk_loader_transactions',$form_state['values']['transactions']);
  92. }
  93. //////////////////////////////////////////////////////////////////////////////////////
  94. // Modify Template
  95. //////////////////////////////////////////////////////////////////////////////////////
  96. /**
  97. * The main form reached at admin/tripal/tripal_bulk_loader/create and /edit
  98. */
  99. function tripal_bulk_loader_modify_template_base_form($form_state = NULL, $mode) {
  100. $form = array();
  101. // get template id from path and rebuild form
  102. if ($_GET['template_id']) {
  103. if (preg_match('/^\d+$/', $_GET['template_id'])) {
  104. $form_state['storage']['template_id'] = $_GET['template_id'];
  105. }
  106. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  107. $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
  108. $form_state['storage']['template'] = unserialize($result->template_array);
  109. $form_state['storage']['template_name'] = $result->name;
  110. $form_state['storage']['record2priority'] = array();
  111. foreach ($form_state['storage']['template'] as $priority => $record_array) {
  112. if (!is_array($record_array)) {
  113. continue; }
  114. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  115. }
  116. }
  117. $form['mode'] = array(
  118. '#type' => 'hidden',
  119. '#value' => $mode,
  120. );
  121. if ($form_state['storage']['template_id']) {
  122. $form['template_name'] = array(
  123. '#type' => 'item',
  124. '#title' => 'Template',
  125. '#value' => $form_state['storage']['template_name'],
  126. '#weight' => 1,
  127. );
  128. }
  129. else {
  130. if (preg_match('/create/', $mode)) {
  131. $form['new_template_name'] = array(
  132. '#type' => 'textfield',
  133. '#title' => 'Template Name',
  134. '#weight' => 1,
  135. );
  136. }
  137. elseif (preg_match('/edit/', $mode)) {
  138. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  139. $resource = db_query($sql);
  140. $templates = array();
  141. $templates[''] = 'Select a Template';
  142. while ($r = db_fetch_object($resource)) {
  143. $templates[$r->template_id] = $r->name;
  144. }
  145. $form['template_id'] = array(
  146. '#title' => t('Template'),
  147. '#description' => t('Please select the template you would like to edit.'),
  148. '#type' => 'select',
  149. '#options' => $templates,
  150. '#default_value' => $form_state['storage']['template_id'],
  151. '#weight' => 0,
  152. '#required' => TRUE,
  153. '#weight' => 1,
  154. );
  155. }
  156. }
  157. $form['records'] = array(
  158. '#type' => ($form_state['storage']['template_id'])? 'fieldset' : 'hidden',
  159. '#title' => t('Current Records'),
  160. '#collapsible' => TRUE,
  161. '#weight' => 2,
  162. );
  163. $form['records']['description'] = array(
  164. '#type' => 'item',
  165. '#value' => 'Records will be inserted into the chado database in the order listed below. To '
  166. .'change this order: <ul><li>Drag the rows into the correct order OR</li><li>Enter '
  167. .'the numbers 1 and up in the Order textboxes to indicate the correct order.</li></ul>',
  168. );
  169. $form['records']['records-data'] = array(
  170. '#tree' => TRUE,
  171. );
  172. $form['records']['no_records'] = array(
  173. '#type' => 'hidden',
  174. '#value' => TRUE,
  175. );
  176. $form['records']['submit-new_record'] = array(
  177. '#type' => 'submit',
  178. '#value' => 'New Record/Field',
  179. );
  180. $form['records']['submit-reorder'] = array(
  181. '#type' => 'submit',
  182. '#value' => 'Save Order',
  183. );
  184. $form['fields'] = array(
  185. '#type' => ($form_state['storage']['template_id'])? 'fieldset' : 'hidden',
  186. '#title' => t('Current Fields'),
  187. '#collapsible' => TRUE,
  188. '#weight' => 3,
  189. );
  190. $form['fields']['fields-data'] = array(
  191. '#tree' => TRUE,
  192. );
  193. if ($form_state['storage']['template']) {
  194. // List Current Fields -------------------------------------------------------------
  195. $i=1;
  196. foreach ($form_state['storage']['template'] as $priority => $table_array) {
  197. if (!is_array($table_array)) {
  198. continue; }
  199. $form['records']['no_records']['#value'] = FALSE;
  200. $form['records']['records-data'][$priority] = array(
  201. 'title' => array(
  202. '#type' => 'markup',
  203. '#value' => $table_array['record_id'],
  204. ),
  205. 'chado_table' => array(
  206. '#type' => 'markup',
  207. '#value' => $table_array['table'],
  208. ),
  209. 'mode' => array(
  210. '#type' => 'item',
  211. '#value' => ($table_array['mode']) ? $table_array['mode'] : 'insert_unique',
  212. ),
  213. 'new_priority' => array(
  214. '#type' => 'select',
  215. '#options' => range(1, sizeof($form_state['storage']['template'])),
  216. '#default_value' => $priority,
  217. ),
  218. 'old_priority' => array(
  219. '#type' => 'hidden',
  220. '#value' => $priority,
  221. ),
  222. 'id' => array(
  223. '#type' => 'hidden',
  224. '#value' => $priority,
  225. ),
  226. 'submit-edit_record' => array(
  227. '#type' => 'submit',
  228. '#name' => (string)$priority,
  229. '#value' => 'Edit Record',
  230. ),
  231. 'submit-add_field' => array(
  232. '#type' => 'submit',
  233. '#name' => (string)$priority,
  234. '#value' => 'Add Field',
  235. ),
  236. 'submit-duplicate_record' => array(
  237. '#type' => 'submit',
  238. '#name' => (string)$priority,
  239. '#value' => 'Duplicate Record'
  240. ),
  241. );
  242. foreach ($table_array['fields'] as $field_index => $field) {
  243. $form['fields']['fields-data'][$i] = array(
  244. 'record_id' => array(
  245. '#type' => 'item',
  246. '#value' => $table_array['record_id'],
  247. ),
  248. 'priority_hidden' => array(
  249. '#type' => 'hidden',
  250. '#value' => $priority,
  251. ),
  252. 'field_name' => array(
  253. '#type' => 'item',
  254. '#value' => $field['title'],
  255. ),
  256. 'chado_table_name' => array(
  257. '#type' => 'item',
  258. '#value' => $table_array['table'],
  259. ),
  260. 'chado_table_hidden' => array(
  261. '#type' => 'hidden',
  262. '#value' => $table_array['table'],
  263. ),
  264. 'chado_field_name' => array(
  265. '#type' => 'item',
  266. '#value' => $field['field'],
  267. ),
  268. 'sheet_name' => array(
  269. '#type' => 'item',
  270. '#value' => $field['spreadsheet sheet'],
  271. ),
  272. 'column_num' => array(
  273. '#type' => 'item',
  274. '#value' => $field['spreadsheet column'],
  275. ),
  276. 'constant_value' => array(
  277. '#type' => 'item',
  278. '#value' => $field['constant value'],
  279. ),
  280. 'field_index' => array(
  281. '#type' => 'hidden',
  282. '#value' => $field_index
  283. ),
  284. 'foreign_record_id' => array(
  285. '#type' => 'item',
  286. '#value' => $field['foreign key'],
  287. ),
  288. 'edit_submit' => array(
  289. '#type' => 'submit',
  290. '#name' => (string)$i,
  291. '#value' => "Edit Field",
  292. ),
  293. 'delete_submit' => array(
  294. '#type' => 'submit',
  295. '#name' => (string)$i,
  296. '#value' => "Delete Field",
  297. ),
  298. );
  299. $i++;
  300. }
  301. }
  302. $form['fields']['total_fields'] = array(
  303. '#type' => 'item',
  304. '#value' => $i,
  305. );
  306. }
  307. if ($form['records']['no_records']['#value']) {
  308. $form['records']['description'] = array(
  309. '#type' => 'item',
  310. '#value' => 'There are currently no records.',
  311. );
  312. unset($form['records']['submit-reorder']);
  313. $form['fields']['description'] = array(
  314. '#type' => 'item',
  315. '#value' => 'There are currently no fields.',
  316. );
  317. }
  318. $mode_title = (preg_match('/create/', $mode)) ? 'Create Template' : 'Edit Template';
  319. $value = ($form_state['storage']['template_id'])? 'Save Template' : $mode_title;
  320. $form['submit'] = array(
  321. '#type' => 'submit',
  322. '#value' => $value,
  323. '#weight' => 4,
  324. );
  325. return $form;
  326. }
  327. /**
  328. * Submit for tripal_bulk_loader_modify_template_base_form
  329. */
  330. function tripal_bulk_loader_modify_template_base_form_submit($form, &$form_state) {
  331. $form_state['rebuild'] = TRUE;
  332. if ($form_state['storage']['template_id']) {
  333. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  334. $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
  335. $form_state['storage']['template'] = unserialize($result->template_array);
  336. }
  337. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  338. switch ($op) {
  339. // Initialize after template is chosen ----------------------------------------
  340. case 'Edit Template':
  341. $form_state['storage']['template_id'] = $form_state['values']['template_id'];
  342. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  343. $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
  344. $form_state['storage']['template'] = unserialize($result->template_array);
  345. $form_state['storage']['template_name'] = $result->name;
  346. $form_state['storage']['record2priority'] = array();
  347. foreach ($form_state['storage']['template'] as $priority => $record_array) {
  348. if (!is_array($record_array)) {
  349. continue;
  350. }
  351. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  352. }
  353. break;
  354. case 'Create Template':
  355. $record = array(
  356. 'name' => $form_state['values']['new_template_name'],
  357. 'template_array' => array(),
  358. );
  359. drupal_write_record('tripal_bulk_loader_template', $record);
  360. $form_state['storage']['template_id'] = $record['template_id'];
  361. $form_state['storage']['template_name'] = $record['name'];
  362. $form_state['storage']['template'] = array();
  363. break;
  364. // Save Reordered Records -----------------------------------------------------
  365. case 'Save Order':
  366. $new_template = $form_state['storage']['template'];
  367. // unset old elements
  368. $form_state['storage']['record2priority'] = array();
  369. foreach ($new_template as $priority => $record_array) {
  370. if (preg_match('/\d+/', $priority)) {
  371. unset($new_template[$priority]);
  372. }
  373. }
  374. //set elements in new order
  375. foreach ($form_state['values']['records-data'] as $item) {
  376. $new_template[$item['new_priority']] = $form_state['storage']['template'][$item['old_priority']];
  377. $record_name = $new_template[$item['new_priority']]['record_id'];
  378. $form_state['storage']['record2priority'][$record_name] = $item['new_priority'];
  379. }
  380. ksort($new_template);
  381. $form_state['storage']['template'] = $new_template;
  382. break;
  383. case 'New Record/Field':
  384. $query = array(
  385. 'template_id' => $form_state['storage']['template_id'],
  386. 'record_id' => 'NEW',
  387. );
  388. drupal_goto('admin/tripal/tripal_bulk_loader_template/add_field', $query);
  389. break;
  390. case 'Edit Record':
  391. $query = array(
  392. 'template_id' => $form_state['storage']['template_id'],
  393. 'record_id' => $form_state['clicked_button']['#name'],
  394. );
  395. drupal_goto('admin/tripal/tripal_bulk_loader_template/edit_record', $query);
  396. break;
  397. case 'Add Field':
  398. $query = array(
  399. 'template_id' => $form_state['storage']['template_id'],
  400. 'record_id' => $form_state['clicked_button']['#name'],
  401. );
  402. drupal_goto('admin/tripal/tripal_bulk_loader_template/add_field', $query);
  403. break;
  404. case 'Duplicate Record':
  405. // original record (one to be duplicated)
  406. $orig_priority = $form_state['clicked_button']['#name'];
  407. $record = $form_state['storage']['template'][ $orig_priority ];
  408. // new record
  409. $new_priority = sizeof($form_state['storage']['template']) + 1;
  410. $record['record_id'] = $record['record_id'] . '_' . date('YzHi');
  411. $form_state['storage']['template'][ $new_priority ] = $record;
  412. break;
  413. case 'Edit Field':
  414. $field_data_index = $form_state['clicked_button']['#name'];
  415. $query = array(
  416. 'template_id' => $form_state['storage']['template_id'],
  417. 'record_id' => $form_state['values']['fields-data'][$field_data_index]['priority_hidden'],
  418. 'field_index' => $form_state['values']['fields-data'][$field_data_index]['field_index'],
  419. );
  420. drupal_goto('admin/tripal/tripal_bulk_loader_template/edit_field', $query);
  421. break;
  422. case 'Delete Field':
  423. $field_data = $form_state['values']['fields-data'][$form_state['clicked_button']['#name']];
  424. $priority = $field_data['priority_hidden'];
  425. $field_key = $field_data['field_index'];
  426. unset($form_state['storage']['template'][$priority]['fields'][$field_key]);
  427. if (!$form_state['storage']['template'][$priority]['fields']) {
  428. unset($form_state['storage']['record2priority'][$form_state['storage']['template'][$priority]['record_id']]);
  429. unset($form_state['storage']['template'][$priority]);
  430. }
  431. drupal_set_message('Deleted Field from Template.');
  432. break;
  433. } //end of switch
  434. // Save Template
  435. $record = array(
  436. 'template_id' => $form_state['storage']['template_id'],
  437. 'template_array' => serialize($form_state['storage']['template'])
  438. );
  439. drupal_write_record('tripal_bulk_loader_template', $record, array('template_id'));
  440. drupal_set_message('Template Saved.');
  441. }
  442. //////////////////////////////////////////////////////////////////////////////////////
  443. // Delete Template
  444. //////////////////////////////////////////////////////////////////////////////////////
  445. /**
  446. * Delete Template Form
  447. * This form allows admin to delete already existing templates
  448. */
  449. function tripal_bulk_loader_delete_template_base_form() {
  450. $form = array();
  451. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  452. $resource = db_query($sql);
  453. $templates = array();
  454. $templates[''] = 'Select a Template';
  455. while ($r = db_fetch_object($resource)) {
  456. $templates[$r->template_id] = $r->name;
  457. }
  458. $form['template_name'] = array(
  459. '#title' => t('Template'),
  460. '#description' => t('Please select the template you would like to delete.'),
  461. '#type' => 'select',
  462. '#options' => $templates,
  463. '#weight' => 0,
  464. '#required' => TRUE,
  465. );
  466. $form['submit'] = array(
  467. '#type' => 'submit',
  468. '#value' => 'Delete Template',
  469. );
  470. return $form;
  471. }
  472. /**
  473. * Delete Template Form Submit
  474. *
  475. * @param $form
  476. * The form that was submitted
  477. * @param $form_state
  478. * The values and storage that were submitted
  479. */
  480. function tripal_bulk_loader_delete_template_base_form_submit($form, &$form_state) {
  481. $sql = "DELETE FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  482. db_query($sql, $form_state['values']['template_name']);
  483. }
  484. //////////////////////////////////////////////////////////////////////////////////////
  485. // Import/Export Template
  486. //////////////////////////////////////////////////////////////////////////////////////
  487. /**
  488. * Import/Export Template Form
  489. *
  490. * On export, simply selects the serialized array from the db for a given template
  491. * and presents it to the user. On import, a serialized template array and a name is
  492. * supplied and a template record is created.
  493. *
  494. * @todo Make array presented to the user more readable. (ie: unserialize and print to the screen)
  495. *
  496. * @param $form_state
  497. * The values and storage for the form
  498. * @param $mode
  499. * Either 'import' or 'export' to indicate which function is being performed
  500. * @return
  501. * A form array to be rendered by drupal_get_form
  502. */
  503. function tripal_bulk_loader_import_export_template_form($form_state = NULL, $mode) {
  504. $form = array();
  505. $form['mode'] = array(
  506. '#type' => 'hidden',
  507. '#value' => $mode,
  508. );
  509. if (preg_match('/import/', $mode)) {
  510. $form['new_template_name'] = array(
  511. '#type' => 'textfield',
  512. '#title' => 'Template Name',
  513. '#weight' => 1,
  514. );
  515. }
  516. elseif (preg_match('/export/', $mode)) {
  517. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  518. $resource = db_query($sql);
  519. $templates = array();
  520. $templates[''] = 'Select a Template';
  521. while ($r = db_fetch_object($resource)) {
  522. $templates[$r->template_id] = $r->name;
  523. }
  524. $form['template_id'] = array(
  525. '#title' => t('Template'),
  526. '#description' => t('Please select the template you would like to edit.'),
  527. '#type' => 'select',
  528. '#options' => $templates,
  529. '#default_value' => $form_state['storage']['template_id'],
  530. '#weight' => 0,
  531. '#required' => TRUE,
  532. '#weight' => 1,
  533. );
  534. }
  535. $form['template_array'] = array(
  536. '#type' => 'textarea',
  537. '#title' => 'Template Array',
  538. '#default_value' => $form_state['storage']['template_array'],
  539. '#weight' => 2,
  540. );
  541. $form['submit'] = array(
  542. '#type' => 'submit',
  543. '#value' => 'Submit',
  544. '#weight' => 10,
  545. );
  546. return $form;
  547. }
  548. /**
  549. * Import/Export Template Form Submit
  550. *
  551. * @param $form
  552. * The form that was submitted
  553. * @param $form_state
  554. * The values and storage that were submitted
  555. */
  556. function tripal_bulk_loader_import_export_template_form_submit($form, &$form_state) {
  557. switch ($form_state['values']['mode']) {
  558. case 'export':
  559. $record = db_fetch_object(db_query("SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d", $form_state['values']['template_id']));
  560. $form_state['storage']['template_array'] = $record->template_array;
  561. $form_state['storage']['template_id'] = $form_state['values']['template_id'];
  562. break;
  563. case 'import':
  564. $record = array(
  565. 'name' => $form_state['values']['new_template_name'],
  566. 'template_array' => $form_state['values']['template_array'],
  567. );
  568. drupal_write_record('tripal_bulk_loader_template', $record);
  569. if ($record->template_id) {
  570. drupal_set_message('Successfully imported Tripal Bulk Loader Template.');
  571. }
  572. break;
  573. }
  574. }
  575. //////////////////////////////////////////////////////////////////////////////////////
  576. // Edit Record Form
  577. //////////////////////////////////////////////////////////////////////////////////////
  578. /**
  579. * Edit Record Form
  580. *
  581. * This form is meant to be called from a bulk loader form. The following should be set
  582. * in the query section of the path:
  583. * - template_id=\d+: the template which the edited record is part of
  584. * - record_id=\d+: the priority or key in the template array of the record to be edited
  585. *
  586. * @param $form_state
  587. * Contains the values and storage for the form
  588. * @return
  589. * A form array to be rendered by drupal_get_form
  590. */
  591. function tripal_bulk_loader_edit_template_record_form(&$form_state = NULL) {
  592. $form['#cache'] = TRUE; // Make sure the form is cached.
  593. // get template id from path
  594. $template_id = ($_GET['template_id'] !== NULL) ? $_GET['template_id'] : $form_state['values']['template_id'];
  595. // if there is no template supplied don't return rest of form
  596. if (!$template_id) {
  597. return $form;
  598. }
  599. // Pre-process values/defaults ---------------------------
  600. // If this is the first load of the form (no form state) we need to initialize some variables
  601. if (!$form_state['storage']['template']) {
  602. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  603. $template = db_fetch_object(db_query($sql, $template_id));
  604. $form_state['storage']['template_array'] = unserialize($template->template_array);
  605. $form_state['storage']['template'] = $template;
  606. $form_state['storage']['record2priority'] = array();
  607. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  608. if (!is_array($record_array)) {
  609. continue;
  610. }
  611. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  612. }
  613. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  614. }
  615. else {
  616. $template = $form_state['storage']['template'];
  617. }
  618. // get the record_id from the path
  619. if ($_GET['record_id'] !== NULL) {
  620. $form_state['values']['field_group'] = $_GET['record_id'];
  621. $form_state['storage']['original_priority'] = $_GET['record_id'];
  622. }
  623. // Tables and default table
  624. $tables = tripal_core_get_chado_tables();
  625. if ($form_state['values']['chado_table']) {
  626. $table = $form_state['values']['chado_table'];
  627. }
  628. else {
  629. $table = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['table'];
  630. }
  631. //dpm($form_state, 'form state');
  632. // Form Proper -------------------------------------------
  633. $form['template_name'] = array(
  634. '#type' => 'item',
  635. '#title' => 'Template',
  636. '#value' => $template->name,
  637. );
  638. $form['template_id'] = array(
  639. '#type' => 'hidden',
  640. '#value' => $template_id,
  641. );
  642. $form['edit_record'] = array(
  643. '#type' => 'fieldset',
  644. );
  645. // check template array for records then add one more
  646. if (!$form_state['storage']['record2priority']) {
  647. $groups = array();
  648. }
  649. else {
  650. $groups = array_flip($form_state['storage']['record2priority']);
  651. }
  652. $priority_default = $form_state['values']['field_group'];
  653. $form['edit_record']['field_group'] = array(
  654. '#type' => 'select',
  655. '#title' => 'Record',
  656. '#description' => 'By Changing the record here, you can move all the fields from the current record into the selected record.',
  657. '#options' => $groups,
  658. '#default_value' => $priority_default,
  659. '#required' => TRUE,
  660. );
  661. $form['edit_record']['record_name'] = array(
  662. '#type' => 'textfield',
  663. '#title' => 'Unique Record Name',
  664. '#default_value' => $groups[$priority_default],
  665. );
  666. $form['edit_record']['chado_table'] = array(
  667. '#type' => 'select',
  668. '#title' => t('Chado Table'),
  669. '#description' => 'This changes the chado table for all fields in this record.',
  670. '#options' => $tables,
  671. '#default_value' => $table,
  672. );
  673. $form['edit_record']['mode'] = array(
  674. '#type' => 'radios',
  675. '#title' => 'Action to take when Loading Record',
  676. '#options' => array(
  677. 'select' => 'SELECT: Don\'t insert this record: it\'s used to define a foreign key in another record',
  678. 'insert' => 'INSERT: Insert the record',
  679. 'optional' => 'OPTIONAL: Record will only be inserted if all required data is filled in',
  680. 'insert_once' => 'INSERT ONCE: Record will be inserted once for the entire spreadsheet',
  681. 'insert_unique' => 'INSERT UNIQUE: Only insert record if there isn\'t a record with the same values',
  682. ),
  683. '#default_value' => 'insert_unique'
  684. );
  685. $form['edit_record']['submit-edit_record'] = array(
  686. '#type' => 'submit',
  687. '#value' => 'Edit Record'
  688. );
  689. $form['edit_record']['submit-cancel'] = array(
  690. '#type' => 'submit',
  691. '#value' => 'Cancel'
  692. );
  693. return $form;
  694. }
  695. /**
  696. * Edit Record Form Submit
  697. *
  698. * @param $form
  699. * The form that was submitted
  700. * @param $form_state
  701. * Contains the values and storage for the form
  702. */
  703. function tripal_bulk_loader_edit_template_record_form_submit($form, &$form_state) {
  704. //dpm($form_state, 'form state -start submit');
  705. if (!$form_state['ahah_submission']) {
  706. if ($form_state['values']['op'] == 'Edit Record') {
  707. $template = $form_state['storage']['template_array'];
  708. // Edit Record
  709. $record = $template[ $form_state['storage']['original_priority'] ];
  710. $record['record_id'] = $form_state['values']['record_name'];
  711. $record['mode'] = $form_state['values']['mode'];
  712. $record['table'] = $form_state['values']['chado_table'];
  713. if ($form_state['storage']['original_priority'] != $form_state['values']['field_group']) {
  714. $record['fields'] = array_merge($record['fields'], $template[ $form_state['values']['field_group'] ]['fields']);
  715. $template[ $form_state['values']['field_group'] ] = $record;
  716. unset($template[ $form_state['storage']['original_priority'] ]);
  717. }
  718. else {
  719. $template[ $form_state['storage']['original_priority'] ] = $record;
  720. }
  721. // Save Template
  722. $form_state['storage']['template']->template_array = serialize($template);
  723. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  724. if ($success) {
  725. drupal_set_message('Successfully Updated Template Record');
  726. drupal_set_message('Template Saved.');
  727. $path = explode('?', $form_state['storage']['referring URL']);
  728. parse_str($path[1], $query);
  729. $query['template_id'] = $form_state['storage']['template']->template_id;
  730. drupal_goto($path[0], $query);
  731. }
  732. else {
  733. drupal_set_message('Unable to Save Template!', 'error');
  734. watchdog('T_bulk_loader',
  735. 'Unable to save bulk loader template: %template',
  736. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  737. WATCHDOG_ERROR
  738. );
  739. }
  740. }
  741. elseif ($form_state['values']['op'] == 'Cancel') {
  742. $path = explode('?', $form_state['storage']['referring URL']);
  743. parse_str($path[1], $query);
  744. $query['template_id'] = $form_state['storage']['template']->template_id;
  745. //dpm('Redirecting to: '.$path[0].'?'.print_r($query,TRUE).' where the referring URL:'.$form_state['storage']['referring URL']);
  746. drupal_goto($path[0], $query);
  747. }
  748. }
  749. }
  750. //////////////////////////////////////////////////////////////////////////////////////
  751. // Add/Edit Field Forms
  752. //////////////////////////////////////////////////////////////////////////////////////
  753. /**
  754. * Add Field Form
  755. *
  756. * This form is meant to be called from a bulk loader form. Blank Defaults are in place but you
  757. * can use the following in the query of the path to set defaults for a given template:
  758. * - template_id=\d+: the template to add the field to
  759. * - record_id=\d+: the priority or key in the template array of the record to add the field to
  760. *
  761. * @param $form_state
  762. * Contains the values and storage for the form
  763. * @return
  764. * A form array to be rendered by drupal_get_form
  765. */
  766. function tripal_bulk_loader_add_template_field_form(&$form_state = NULL) {
  767. $form = array();
  768. $form['#cache'] = TRUE; // Make sure the form is cached.
  769. // get template id from path
  770. $template_id = ($_GET['template_id']) ? $_GET['template_id'] : $form_state['values']['template_id'];
  771. // if there is no template supplied don't return rest of form
  772. if (!$template_id) {
  773. return $form;
  774. }
  775. // Pre-set Variables needed for form proper------------------------------------------
  776. // If this is the first load of the form (no form state) we need to initialize some variables
  777. if (!$form_state['storage']['template']) {
  778. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  779. $template = db_fetch_object(db_query($sql, $template_id));
  780. $form_state['storage']['template_array'] = unserialize($template->template_array);
  781. $form_state['storage']['template'] = $template;
  782. $form_state['storage']['record2priority'] = array();
  783. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  784. if (!is_array($record_array)) {
  785. continue;
  786. }
  787. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  788. }
  789. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  790. }
  791. else {
  792. $template = $form_state['storage']['template'];
  793. }
  794. $field_type = ($form_state['values']['field_type'])? $form_state['values']['field_type'] : 'table field';
  795. // Tables and default table
  796. $tables = tripal_core_get_chado_tables();
  797. if ($form_state['values']) {
  798. if (!preg_match('/^' . current($tables) . '$/', $form_state['values']['chado_table'])) {
  799. $table = $form_state['values']['chado_table'];
  800. }
  801. elseif ($form_state['values']['record_name']) {
  802. $record_name = $form_state['values']['record_name'];
  803. $priority = $form_state['storage']['record2priority'][$record_name];
  804. $table = $form_state['storage']['template_array'][$priority]['table'];
  805. }
  806. else {
  807. $priority = $form_state['values']['field_group'];
  808. $table = $form_state['storage']['template_array'][$priority]['table'];
  809. }
  810. }
  811. if (!$table) {
  812. $table = reset($tables);
  813. }
  814. // get the record_id from the path
  815. if ($_GET['record_id'] !== NULL) {
  816. $form_state['values']['field_group'] = $_GET['record_id'];
  817. if (preg_match('/\d+/', $_GET['record_id'])) {
  818. $priority = $form_state['values']['field_group'];
  819. $table = $form_state['storage']['template_array'][$priority]['table'];
  820. }
  821. }
  822. // Fields and foreign key mappings
  823. $chado_fields = array();
  824. $fk_options = array();
  825. $fk_options['NULL'] = 'None';
  826. $table_description = module_invoke_all('chado_' . $table . '_schema');
  827. //dpm($table_description, 'table description for |'.$table.'|');
  828. if ($field_type == 'foreign key') {
  829. $foreign_field2table = array();
  830. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  831. foreach ($key_array['columns'] as $left_field => $right_field) {
  832. $chado_fields[$left_field] = $left_field;
  833. $foreign_field2table[$left_field] = $key_table;
  834. }
  835. }
  836. reset($chado_fields);
  837. // set default field
  838. if (empty($chado_fields)) {
  839. $field = NULL;
  840. }
  841. elseif ($chado_fields[$form_state['values']['chado_field']]) {
  842. $field = $form_state['values']['chado_field'];
  843. }
  844. else {
  845. $field = current($chado_fields);
  846. }
  847. // Foreign key options
  848. $foreign_table = $foreign_field2table[$field];
  849. if ($foreign_table) {
  850. foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) {
  851. if (preg_match('/^' . $foreign_table . '$/', $form_state['storage']['template_array'][$priority]['table'])) {
  852. $fk_options[$record_name] = $record_name;
  853. }
  854. }
  855. }
  856. }
  857. else {
  858. foreach ($table_description['fields'] as $field_name => $field_array) {
  859. $chado_fields[$field_name] = $field_name;
  860. }
  861. }
  862. $variables = array(
  863. 'form_state' => $form_state,
  864. 'tables' => $tables,
  865. 'default table' => $table,
  866. 'fields' => $chado_fields,
  867. 'default_field' => $field,
  868. 'priority' => $priority,
  869. 'record_name' => $record_name,
  870. 'table description' => $table_description,
  871. 'foreign key options' => $fk_options,
  872. 'foreign field=>table' => $foreign_field2table,
  873. 'foreign table' => $foreign_table,
  874. );
  875. //dpm($variables, 'variables');
  876. // Start of Form Proper--------------------------------------------------------------
  877. //dpm($form_state, 'Form State');
  878. $form['template_name'] = array(
  879. '#type' => 'item',
  880. '#title' => 'Template',
  881. '#value' => $template->name,
  882. );
  883. $form['template_id'] = array(
  884. '#type' => 'hidden',
  885. '#value' => $template_id,
  886. );
  887. $form['add_fields'] = array(
  888. '#type' => 'fieldset',
  889. '#prefix' => '<div id="tripal_bulk_loader_template-add_field">',
  890. '#suffix' => '</div>',
  891. );
  892. $form['add_fields']['field_type'] = array(
  893. '#type' => 'radios',
  894. '#title' => t('Type of Field'),
  895. '#options' => array(
  896. 'table field' => t('Spreadsheet: Fields which maps to a Spreadsheet Column'),
  897. 'constant' => t('Constant: Field which remains Constant throughout the Spreadsheet'),
  898. 'foreign key' => t('Foreign Key: Fields which map to a record in another table'),
  899. ),
  900. '#required' => TRUE,
  901. '#default_value' => $field_type,
  902. '#ahah' => array(
  903. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  904. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  905. 'effect' => 'fade'
  906. ),
  907. );
  908. // check template array for records then add one more
  909. if (!$form_state['storage']['record2priority']) {
  910. $groups = array();
  911. }
  912. else {
  913. $groups = array_flip($form_state['storage']['record2priority']);
  914. }
  915. $groups['NONE'] = 'Select a Record';
  916. $groups['NEW'] = 'New Record';
  917. $form['add_fields']['field_group'] = array(
  918. '#type' => 'select',
  919. '#title' => 'Record',
  920. '#description' => 'This is used to group a set of fields together allowing '
  921. .'multiple records to be inserted into the same table per line of the spreadsheet',
  922. '#options' => $groups,
  923. '#default_value' => (preg_match('/\w+.*/', $form_state['values']['field_group'])) ? $form_state['values']['field_group'] : 'NONE',
  924. '#ahah' => array(
  925. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  926. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  927. 'effect' => 'fade'
  928. ),
  929. '#required' => TRUE,
  930. );
  931. $form['add_fields']['record_name'] = array(
  932. '#type' => (preg_match('/NEW/', $form_state['values']['field_group'])) ? 'textfield' : 'hidden',
  933. '#title' => 'Unique Record Name',
  934. '#prefix' => '<div id="tripal_bulk_loader_template-add_record">',
  935. '#suffix' => '</div>',
  936. '#default_value' => $form_state['values']['record_name'],
  937. );
  938. $form['add_fields']['field_title'] = array(
  939. '#type' => 'textfield',
  940. '#title' => t('Human-readable Title for Field'),
  941. '#default_value' => $form_state['values']['field_title'],
  942. );
  943. // Spreadsheet column
  944. $form['add_fields']['columns'] = array(
  945. '#type' => 'fieldset',
  946. '#title' => t('Spreadsheet Column'),
  947. '#collapsible' => TRUE,
  948. '#collapsed' => ($field_type == 'table field')? FALSE : TRUE,
  949. );
  950. $form['add_fields']['columns']['sheet_name'] = array(
  951. '#type' => 'textfield',
  952. '#title' => t('Worksheet'),
  953. '#description' => t('Specify the name of the worksheet.'),
  954. '#size' => 5,
  955. '#default_value' => ($form_state['values']['sheet_name'])? $form_state['values']['sheet_name'] : 'Sheet1',
  956. );
  957. $form['add_fields']['columns']['column_number'] = array(
  958. '#type' => 'textfield',
  959. '#title' => t('Column'),
  960. '#description' => t('Specify the column in the spreadsheet that this field maps to where the first column is 1.'),
  961. '#size' => 5,
  962. '#default_value' => $form_state['values']['column_number'],
  963. );
  964. $form['add_fields']['columns']['column_exposed'] = array(
  965. '#type' => 'checkbox',
  966. '#title' => t('Allow Column to be set for each Bulk Loading Job'),
  967. '#description' => t('Adds a textbox field to the Bulk Loader Page to allow users to set this value.'),
  968. '#default_value' => ($form_state['values']['column_exposed']) ? $form_state['values']['column_exposed'] : $template_field['exposed'],
  969. );
  970. $form['add_fields']['columns']['column_exposed_desc'] = array(
  971. '#type' => 'textfield',
  972. '#title' => t('Description for exposed field on bulk loading job'),
  973. '#description' => t('This description should tell the user what column should be entered here.'),
  974. '#default_value' => ($form_state['values']['column_exposed_desc']) ? $form_state['values']['column_exposed_desc'] : $template_field['exposed_description'],
  975. );
  976. // Global Value
  977. $form['add_fields']['constant'] = array(
  978. '#type' => 'fieldset',
  979. '#title' => t('Constant'),
  980. '#collapsible' => TRUE,
  981. '#collapsed' => ($field_type == 'constant')? FALSE : TRUE,
  982. );
  983. $form['add_fields']['constant']['constant_value'] = array(
  984. '#type' => 'textfield',
  985. '#title' => t('Constant Value'),
  986. '#description' => t('Specify the value you wish this field to have regardless of spreadsheet data.'),
  987. '#default_value' => $form_state['values']['constant_value']
  988. );
  989. $form['add_fields']['constant']['constant_exposed'] = array(
  990. '#type' => 'checkbox',
  991. '#title' => t('Allow Constant to be set for each Bulk Loading Job'),
  992. '#description' => t('Adds a textbox field to the Create Bulk Loader Form to allow users to set this value.')
  993. );
  994. $form['add_fields']['constant']['constant_validate'] = array(
  995. '#type' => 'checkbox',
  996. '#title' => t('Ensure value is in table'),
  997. '#description' => t('Checks the database when a bulk loading job is created to ensure the value entered already exists in the database.'),
  998. );
  999. // Foreign Key
  1000. $form['add_fields']['foreign_key'] = array(
  1001. '#type' => 'fieldset',
  1002. '#title' => 'Foreign Key',
  1003. '#collapsible' => TRUE,
  1004. '#collapsed' => ($field_type == 'foreign key')? FALSE : TRUE,
  1005. );
  1006. $form['add_fields']['foreign_key']['foreign_record'] = array(
  1007. '#type' => 'select',
  1008. '#title' => 'Record to refer to',
  1009. '#descripion' => 'Select the record that this foreign key shouold refer to. The record needs to already exist and be of the correct table.',
  1010. '#options' => $fk_options,
  1011. );
  1012. // Chado Field
  1013. $form['add_fields']['chado'] = array(
  1014. '#type' => 'fieldset',
  1015. '#title' => t('Chado Field/Column Details'),
  1016. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  1017. );
  1018. $form['add_fields']['chado']['chado_table'] = array(
  1019. '#type' => 'select',
  1020. '#title' => t('Chado Table'),
  1021. '#options' => $tables,
  1022. '#default_value' => $table,
  1023. '#ahah' => array(
  1024. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  1025. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  1026. 'effect' => 'fade'
  1027. ),
  1028. );
  1029. $form['add_fields']['chado']['chado_field'] = array(
  1030. '#type' => 'select',
  1031. '#title' => t('Chado Field/Column'),
  1032. '#options' => $chado_fields,
  1033. '#default_value' => $form_state['values']['chado_field'],
  1034. '#ahah' => array(
  1035. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  1036. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  1037. 'effect' => 'fade'
  1038. ),
  1039. );
  1040. $form['add_fields']['additional'] = array(
  1041. '#type' => 'fieldset',
  1042. '#title' => 'Additional Options',
  1043. );
  1044. $form['add_fields']['additional']['required'] = array(
  1045. '#type' => 'checkbox',
  1046. '#title' => 'Make this file required',
  1047. );
  1048. $form['add_fields']['additional']['regex_transform'] = array(
  1049. '#type' => 'fieldset',
  1050. '#title' => 'Transform Spreadsheet Value Rules',
  1051. '#collapsible' => TRUE,
  1052. '#collapsed' => (!$form_state['storage']['regex']['pattern']) ? TRUE : FALSE,
  1053. );
  1054. $form['add_fields']['additional']['regex_transform']['regex_description'] = array(
  1055. '#type' => 'item',
  1056. '#value' => 'A transformation rule allows you to transform the original value '
  1057. .'(usually from a user submitted spreadsheet) into the form you would like it stored '
  1058. .'in the chado database. Each rule consists of a match pattern (a php regular expression '
  1059. .'which determines which replacement patterns are applied and captures regions of the '
  1060. .'original value) and a replacement pattern (a string which may contain capture references '
  1061. .'that describes what the new value should be). Each rule is applied to the result of the '
  1062. .'previous rule.'
  1063. );
  1064. $form['add_fields']['additional']['regex_transform']['regex-data'] = array(
  1065. '#tree' => TRUE,
  1066. );
  1067. if (!$form_state['storage']['regex']['pattern']) {
  1068. $form_state['storage']['regex']['pattern'] = array();
  1069. }
  1070. foreach ($form_state['storage']['regex']['pattern'] as $index => $pattern) {
  1071. $data_element = array(
  1072. 'pattern' => array(
  1073. '#type' => 'item',
  1074. '#value' => $pattern,
  1075. ),
  1076. 'replace' => array(
  1077. '#type' => 'item',
  1078. '#value' => $form_state['storage']['regex']['replace'][$index],
  1079. ),
  1080. 'old_index' => array(
  1081. '#type' => 'hidden',
  1082. '#value' => $index,
  1083. ),
  1084. 'new_index' => array(
  1085. '#type' => 'select',
  1086. '#options' => range(0, sizeof($form_state['storage']['regex']['pattern'])-1),
  1087. '#default_value' => $index,
  1088. ),
  1089. 'id' => array(
  1090. '#type' => 'hidden',
  1091. '#value' => $index,
  1092. ),
  1093. 'submit-delete' => array(
  1094. '#type' => 'submit',
  1095. '#value' => 'Delete Transformation',
  1096. '#name' => $index,
  1097. ),
  1098. );
  1099. $form['add_fields']['additional']['regex_transform']['regex-data'][$index] = $data_element;
  1100. }
  1101. $form['add_fields']['additional']['regex_transform']['submit-reorder_regex'] = array(
  1102. '#type' => ($form_state['storage']['regex']['pattern']) ? 'submit' : 'hidden',
  1103. '#value' => 'Save Transformation Rule Order'
  1104. );
  1105. $form['add_fields']['additional']['regex_transform']['new_regex'] = array(
  1106. '#type' => 'fieldset',
  1107. '#title' => 'Add a new Transformation Rule',
  1108. );
  1109. $form['add_fields']['additional']['regex_transform']['new_regex']['pattern'] = array(
  1110. '#type' => 'textfield',
  1111. '#title' => 'Match Pattern',
  1112. '#description' => 'You can use standard php regular expressions in this field to specify a '
  1113. .'pattern. Only if this pattern matches the value in the spreadsheet does the replacement '
  1114. .'pattern get applied to the value. To capture a section of your value for use in the '
  1115. .'replacement patten surround with round brackets. For example, <i>GI:(\d+)</i> will match '
  1116. .' NCBI gi numbers and will capture the numerical digits for use in the replacement pattern. '
  1117. .' To match and capture any value use <i>.*</i>',
  1118. );
  1119. $form['add_fields']['additional']['regex_transform']['new_regex']['replace'] = array(
  1120. '#type' => 'textfield',
  1121. '#title' => 'Replacement Pattern',
  1122. '#description' => 'This pattern should contain the text you want to replace the match pattern '
  1123. .'mentioned above. It can include references of the form \n where n is the number of the '
  1124. .'capture in the match pattern. For example, \1 will be replaced with the text matched in your '
  1125. .'first set of round brackets.',
  1126. );
  1127. if ($field_type == 'table field') {
  1128. $tab = '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
  1129. $form['add_fields']['additional']['regex_transform']['new_regex']['replace']['#description'] .= '<p>'
  1130. .'The following references are also available for spreadsheet fields: <b><#column:<i>number</i>#></b>. '
  1131. .'This allows you to substitute other spreadsheet values into the current field. For example, '
  1132. .'if you had the following line:<br>'
  1133. . $tab . 'SNP' . $tab . '15-Jan-2011' . $tab . '1' . $tab . '54' . $tab . 'Contig34355'
  1134. .'<br> and your current field is for column #1 and you\'re inserting into the chado field '
  1135. .'feature.uniquename then you might want to add in the data to ensure your uniquename is '
  1136. .'unique. The Match Pattern is (.*) to select all the first column and the Replacement '
  1137. .'Pattern could be \1_<#column:2#> which would insert SNP_15-Jan-2011 into the database.</p>';
  1138. }
  1139. $form['add_fields']['additional']['regex_transform']['new_regex']['submit-add_transform'] = array(
  1140. '#type' => 'submit',
  1141. '#value' => 'Add Transformation',
  1142. );
  1143. $form['add_fields']['additional']['regex_transform']['test_regex'] = array(
  1144. '#type' => 'fieldset',
  1145. '#title' => 'Test Transformation Rules',
  1146. );
  1147. $form['add_fields']['additional']['regex_transform']['test_regex']['test_string'] = array(
  1148. '#type' => 'textfield',
  1149. '#title' => 'Test Value',
  1150. '#description' => 'This should be a value that you expect the above transformation rules '
  1151. .'to be applied to.',
  1152. '#default_value' => $form_state['storage']['test_regex_test'],
  1153. );
  1154. $form['add_fields']['additional']['regex_transform']['test_regex']['test_result'] = array(
  1155. '#type' => 'textfield',
  1156. '#title' => 'Test Result',
  1157. '#description' => 'This is the value that would be saved to the database after the above transformation '
  1158. .'riles were applied to the Test Value.',
  1159. '#default_value' => $form_state['storage']['test_regex_result'],
  1160. );
  1161. $form['add_fields']['additional']['regex_transform']['test_regex']['submit-test'] = array(
  1162. '#type' => 'submit',
  1163. '#value' => 'Test Transformation Rules'
  1164. );
  1165. $form['add_fields']['submit-add_field'] = array(
  1166. '#type' => 'submit',
  1167. '#value' => 'Add Field'
  1168. );
  1169. $form['add_fields']['submit-cancel'] = array(
  1170. '#type' => 'submit',
  1171. '#value' => 'Cancel'
  1172. );
  1173. return $form;
  1174. }
  1175. /**
  1176. * Add Field Submit
  1177. *
  1178. * @param $form
  1179. * The form that was submitted
  1180. * @param $form_state
  1181. * The values and storage for the form
  1182. */
  1183. function tripal_bulk_loader_add_template_field_form_submit($form, &$form_state) {
  1184. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  1185. if (!$form_state['ahah_submission']) {
  1186. if ($op == 'Add Field') {
  1187. $template = $form_state['storage']['template_array'];
  1188. // If new record
  1189. if (preg_match('/NEW/', $form_state['values']['field_group'])) {
  1190. $record_name = $form_state['values']['record_name'];
  1191. $priority = sizeof($form_state['storage']['template_array']) + 1;
  1192. $record2priority[$record_name] = $priority;
  1193. $template[$priority]['table'] = $form_state['values']['chado_table'];
  1194. $template[$priority]['record_id'] = $record_name;
  1195. }
  1196. else {
  1197. $priority = $form_state['values']['field_group'];
  1198. $record_name = $record2priority[$priority];
  1199. }
  1200. // Add field to template array
  1201. if ($form_state['values']['field_type'] == 'table field') {
  1202. $field = array(
  1203. 'type' => 'table field',
  1204. 'title' => $form_state['values']['field_title'],
  1205. 'field' => $form_state['values']['chado_field'],
  1206. 'required' => $form_state['values']['required'],
  1207. //'allowed values' => empty by default,
  1208. 'spreadsheet sheet' => $form_state['values']['sheet_name'],
  1209. 'spreadsheet column' => $form_state['values']['column_number'],
  1210. 'exposed' => $form_state['values']['column_exposed'],
  1211. 'exposed_description' => $form_state['values']['column_exposed_desc'],
  1212. );
  1213. }
  1214. elseif ($form_state['values']['field_type'] == 'constant') {
  1215. $field = array(
  1216. 'type' => 'constant',
  1217. 'title' => $form_state['values']['field_title'],
  1218. 'field' => $form_state['values']['chado_field'],
  1219. 'required' => $form_state['values']['required'],
  1220. //'allowed values' => empty by default,
  1221. 'constant value' => $form_state['values']['constant_value'],
  1222. 'exposed' => $form_state['values']['constant_exposed'],
  1223. 'exposed_validate' => $form_state['values']['constant_validate'],
  1224. );
  1225. }
  1226. elseif ($form_state['values']['field_type'] == 'foreign key') {
  1227. $field = array(
  1228. 'type' => 'foreign key',
  1229. 'title' => $form_state['values']['field_title'],
  1230. 'field' => $form_state['values']['chado_field'],
  1231. 'foreign key' => $form_state['values']['foreign_record'],
  1232. 'required' => $form_state['values']['required'],
  1233. );
  1234. }
  1235. // Deal with any additional options
  1236. if ($form_state['storage']['regex']) {
  1237. $field['regex'] = $form_state['storage']['regex'];
  1238. }
  1239. // Save Template
  1240. $template[$priority]['fields'][] = $field;
  1241. $form_state['storage']['template']->template_array = serialize($template);
  1242. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1243. if ($success) {
  1244. drupal_set_message('Successfully Added Field to Template');
  1245. drupal_set_message('Template Saved.');
  1246. $path = explode('?', $form_state['storage']['referring URL']);
  1247. parse_str($path[1], $query);
  1248. $query['template_id'] = $form_state['storage']['template']->template_id;
  1249. drupal_goto($path[0], $query);
  1250. }
  1251. else {
  1252. drupal_set_message('Unable to Save Template!', 'error');
  1253. watchdog('T_bulk_loader',
  1254. 'Unable to save bulk loader template: %template',
  1255. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1256. WATCHDOG_ERROR
  1257. );
  1258. }
  1259. }
  1260. elseif ($op == 'Cancel') {
  1261. $path = explode('?', $form_state['storage']['referring URL']);
  1262. parse_str($path[1], $query);
  1263. $query['template_id'] = $form_state['storage']['template']->template_id;
  1264. drupal_goto($path[0], $query);
  1265. }
  1266. elseif ($op == 'Add Transformation') {
  1267. // Add transformation rule to original field
  1268. $form_state['storage']['regex']['pattern'][] = '/' . $form_state['values']['pattern'] . '/';
  1269. $form_state['storage']['regex']['replace'][] = $form_state['values']['replace'];
  1270. drupal_set_message('Successfully Added Transformation Rule');
  1271. }
  1272. elseif ($op == 'Save Transformation Rule Order') {
  1273. // Generate new regex array
  1274. $new_regex = array();
  1275. $old_regex = $form_state['storage']['regex'];
  1276. foreach ($form_state['values']['regex-data'] as $key => $element) {
  1277. $new_regex['pattern'][ $element['new_index'] ] = $old_regex['pattern'][ $element['old_index'] ];
  1278. $new_regex['replace'][ $element['new_index'] ] = $old_regex['replace'][ $element['old_index'] ];
  1279. }
  1280. // sort new regex arrays
  1281. asort($new_regex['pattern']);
  1282. asort($new_regex['replace']);
  1283. $form_state['storage']['regex'] = $new_regex;
  1284. }
  1285. elseif ($op == 'Delete Transformation') {
  1286. // Unset regex rule
  1287. $index = $form_state['clicked_button']['#name'];
  1288. unset($form_state['storage']['regex']['pattern'][$index]);
  1289. unset($form_state['storage']['regex']['replace'][$index]);
  1290. }
  1291. elseif ($op == 'Test Transformation Rules') {
  1292. $patterns = $form_state['storage']['regex']['pattern'];
  1293. $replaces = $form_state['storage']['regex']['replace'];
  1294. $test_string = $form_state['values']['test_string'];
  1295. $form_state['storage']['test_regex_result'] = preg_replace($patterns, $replaces, $test_string);
  1296. $form_state['storage']['test_regex_test'] = $test_string;
  1297. }
  1298. }
  1299. }
  1300. /**
  1301. * Edit Field Form
  1302. *
  1303. * This form is meant to be called from a bulk loader form. The following should be set
  1304. * in the query section of the path:
  1305. * - template_id=\d+: the template which the edited field is part of
  1306. * - record_id=\d+: the priority or key in the template array of the record the field
  1307. * is currently part of
  1308. * - field_index=\d+: the key of the field in the fields array of the previously
  1309. * specified record
  1310. *
  1311. * @param $form_state
  1312. * Contains the values and storage for the form
  1313. * @return
  1314. * A form array to be rendered by drupal_get_form
  1315. */
  1316. function tripal_bulk_loader_edit_template_field_form(&$form_state = NULL) {
  1317. $form = array();
  1318. $form['#cache'] = TRUE; // Make sure the form is cached.
  1319. // get template id from path
  1320. $template_id = ($_GET['template_id']) ? $_GET['template_id'] : $form_state['values']['template_id'];
  1321. // if there is no template supplied don't return rest of form
  1322. if (!$template_id) {
  1323. return $form;
  1324. }
  1325. // Pre-set Variables needed for form proper------------------------------------------
  1326. // If this is the first load of the form (no form state) we need to initialize some variables
  1327. if (!$form_state['storage']['template']) {
  1328. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  1329. $template = db_fetch_object(db_query($sql, $template_id));
  1330. $form_state['storage']['template_array'] = unserialize($template->template_array);
  1331. $form_state['storage']['template'] = $template;
  1332. $form_state['storage']['record2priority'] = array();
  1333. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  1334. if (!is_array($record_array)) {
  1335. continue;
  1336. }
  1337. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  1338. }
  1339. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  1340. }
  1341. else {
  1342. $template = $form_state['storage']['template'];
  1343. }
  1344. // get the field from the path
  1345. if (!($_GET['record_id']===NULL || $_GET['field_index']===NULL)) {
  1346. $priority = $_GET['record_id'];
  1347. $field_index = $_GET['field_index'];
  1348. $template_field = $form_state['storage']['template_array'][$priority]['fields'][$field_index];
  1349. $form_state['storage']['original_field'] = $template_field;
  1350. $form_state['storage']['original_field']['priority'] = $priority;
  1351. $form_state['storage']['original_field']['field_index'] = $field_index;
  1352. }
  1353. $field_type = ($form_state['values']['field_type'])? $form_state['values']['field_type'] : $template_field['type'];
  1354. // Tables and default table
  1355. $tables = tripal_core_get_chado_tables();
  1356. if ($form_state['values']) {
  1357. $table = $form_state['values']['chado_table'];
  1358. }
  1359. else {
  1360. $table = $form_state['storage']['template_array'][$priority]['table'];
  1361. }
  1362. // Fields and foreign key mappings
  1363. $chado_fields = array();
  1364. $fk_options = array();
  1365. $fk_options['NULL'] = 'None';
  1366. $table_description = module_invoke_all('chado_' . $table . '_schema');
  1367. if ($field_type == 'foreign key') {
  1368. $foreign_field2table = array();
  1369. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  1370. foreach ($key_array['columns'] as $left_field => $right_field) {
  1371. $chado_fields[$left_field] = $left_field;
  1372. $foreign_field2table[$left_field] = $key_table;
  1373. }
  1374. }
  1375. reset($chado_fields);
  1376. // set default field
  1377. if (empty($chado_fields)) {
  1378. $field = NULL;
  1379. }
  1380. elseif ($chado_fields[$form_state['values']['chado_field']]) {
  1381. $field = $form_state['values']['chado_field'];
  1382. }
  1383. elseif ($template_field['field']) {
  1384. $field = $template_field['field'];
  1385. }
  1386. else {
  1387. $field = current($chado_fields);
  1388. }
  1389. //dpm($field, 'field');
  1390. // Foreign key options
  1391. $foreign_table = $foreign_field2table[$field];
  1392. if ($foreign_table) {
  1393. foreach ($form_state['storage']['record2priority'] as $record_name_ => $priority_ ) {
  1394. if (preg_match('/^' . $foreign_table . '$/', $form_state['storage']['template_array'][$priority_]['table'])) {
  1395. $fk_options[$record_name_] = $record_name_;
  1396. }
  1397. }
  1398. }
  1399. }
  1400. else {
  1401. foreach ($table_description['fields'] as $field_name => $field_array) {
  1402. $chado_fields[$field_name] = $field_name;
  1403. }
  1404. }
  1405. // dpm(array( 'tables' => $tables, 'default table' => $table, 'record name' => $record_name, 'priority' => $priority,
  1406. // 'fields' => $chad_fields, 'default field' => $field, 'foreign field=>table' => $foreign_field2table,
  1407. // 'table desc' => $table_description, 'foreign record options' => $fk_options, 'foreign table' => $foreign_table
  1408. // ), 'Variables');
  1409. // Start of Form Proper--------------------------------------------------------------
  1410. $form['template_name'] = array(
  1411. '#type' => 'item',
  1412. '#title' => 'Template',
  1413. '#value' => $template->name,
  1414. );
  1415. $form['template_id'] = array(
  1416. '#type' => 'hidden',
  1417. '#value' => $template_id,
  1418. );
  1419. $form['edit_fields'] = array(
  1420. '#type' => 'fieldset',
  1421. '#prefix' => '<div id="tripal_bulk_loader_template-edit_field">',
  1422. '#suffix' => '</div>',
  1423. );
  1424. $form['edit_fields']['field_type'] = array(
  1425. '#type' => 'radios',
  1426. '#title' => t('Type of Field'),
  1427. '#options' => array(
  1428. 'table field' => t('Spreadsheet: Fields which maps to a Spreadsheet Column'),
  1429. 'constant' => t('Constant: Field which remains Constant throughout the Spreadsheet'),
  1430. 'foreign key' => t('Foreign Key: Fields which map to a record in another table'),
  1431. ),
  1432. '#required' => TRUE,
  1433. '#default_value' => $field_type,
  1434. '#ahah' => array(
  1435. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1436. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1437. 'effect' => 'fade'
  1438. ),
  1439. );
  1440. // check template array for records then edit one more
  1441. if (!$form_state['storage']['record2priority']) {
  1442. $groups = array();
  1443. }
  1444. else {
  1445. $groups = array_flip($form_state['storage']['record2priority']);
  1446. }
  1447. $groups['NONE'] = 'Select a Record';
  1448. $groups['NEW'] = 'New Record';
  1449. $form['edit_fields']['field_group'] = array(
  1450. '#type' => 'select',
  1451. '#title' => 'Record',
  1452. '#description' => 'This is used to group a set of fields together allowing '
  1453. .'multiple records to be inserted into the same table per line of the spreadsheet',
  1454. '#options' => $groups,
  1455. '#default_value' => (preg_match('/(\d+|\w+)/', $form_state['values']['field_group'])) ? $form_state['values']['field_group'] : $priority,
  1456. '#ahah' => array(
  1457. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1458. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1459. 'effect' => 'fade'
  1460. ),
  1461. '#required' => TRUE,
  1462. );
  1463. $form['edit_fields']['record_name'] = array(
  1464. '#type' => (preg_match('/NEW/', $form_state['values']['field_group'])) ? 'textfield' : 'hidden',
  1465. '#title' => 'Unique Record Name',
  1466. '#prefix' => '<div id="tripal_bulk_loader_template-edit_record">',
  1467. '#suffix' => '</div>',
  1468. '#default_value' => $form_state['values']['record_name'],
  1469. );
  1470. $form['edit_fields']['field_title'] = array(
  1471. '#type' => 'textfield',
  1472. '#title' => t('Human-readable Title for Field'),
  1473. '#default_value' => ($form_state['values']['field_title']) ? $form_state['values']['field_title'] : $template_field['title'],
  1474. );
  1475. // Spreadsheet column
  1476. $form['edit_fields']['columns'] = array(
  1477. '#type' => 'fieldset',
  1478. '#title' => t('Spreadsheet Column'),
  1479. '#collapsible' => TRUE,
  1480. '#collapsed' => ($field_type == 'table field')? FALSE : TRUE,
  1481. );
  1482. $form['edit_fields']['columns']['sheet_name'] = array(
  1483. '#type' => 'textfield',
  1484. '#title' => t('Worksheet'),
  1485. '#description' => t('Specify the name of the worksheet.'),
  1486. '#size' => 5,
  1487. '#default_value' => ($form_state['values']['sheet_name'])? $form_state['values']['sheet_name'] : $template_field['spreadsheet sheet'],
  1488. );
  1489. $form['edit_fields']['columns']['column_number'] = array(
  1490. '#type' => 'textfield',
  1491. '#title' => t('Column'),
  1492. '#description' => t('Specify the column in the spreadsheet that this field maps to where the first column is 1.'),
  1493. '#size' => 5,
  1494. '#default_value' => ($form_state['values']['column_number']) ? $form_state['values']['column_number'] : $template_field['spreadsheet column'],
  1495. );
  1496. $form['edit_fields']['columns']['column_exposed'] = array(
  1497. '#type' => 'checkbox',
  1498. '#title' => t('Allow Column to be set for each Bulk Loading Job'),
  1499. '#description' => t('Adds a textbox field to the Bulk Loader Page to allow users to set this value.'),
  1500. '#default_value' => ($form_state['values']['column_exposed']) ? $form_state['values']['column_exposed'] : $template_field['exposed'],
  1501. );
  1502. $form['edit_fields']['columns']['column_exposed_desc'] = array(
  1503. '#type' => 'textfield',
  1504. '#title' => t('Description for exposed field on bulk loading job'),
  1505. '#description' => t('This description should tell the user what column should be entered here.'),
  1506. '#default_value' => ($form_state['values']['column_exposed_desc']) ? $form_state['values']['column_exposed_desc'] : $template_field['exposed_description'],
  1507. );
  1508. // Global Value
  1509. $form['edit_fields']['constant'] = array(
  1510. '#type' => 'fieldset',
  1511. '#title' => t('Constant'),
  1512. '#collapsible' => TRUE,
  1513. '#collapsed' => ($field_type == 'constant')? FALSE : TRUE,
  1514. );
  1515. $form['edit_fields']['constant']['constant_value'] = array(
  1516. '#type' => 'textfield',
  1517. '#title' => t('Constant Value'),
  1518. '#description' => t('Specify the value you wish this field to have regardless of spreadsheet data.'),
  1519. '#default_value' => ($form_state['values']['constant_value']) ? $form_state['values']['constant_value'] : $template_field['constant value'],
  1520. );
  1521. $form['edit_fields']['constant']['constant_exposed'] = array(
  1522. '#type' => 'checkbox',
  1523. '#title' => t('Allow Constant to be set for each Bulk Loading Job'),
  1524. '#description' => t('Adds a textbox field to the Create Bulk Loader Form to allow users to set this value.'),
  1525. '#default_value' => ($form_state['values']['constant_exposed']) ? $form_state['values']['constant_exposed'] : $template_field['exposed'],
  1526. );
  1527. $form['edit_fields']['constant']['constant_validate'] = array(
  1528. '#type' => 'checkbox',
  1529. '#title' => t('Ensure value is in table'),
  1530. '#description' => t('Checks the database when a bulk loading job is created to ensure the value entered already exists in the database.'),
  1531. '#default_value' => ($form_state['values']['constant_validate']) ? $form_state['values']['constant_validate'] : $template_field['exposed_validate'],
  1532. );
  1533. // Foreign Key
  1534. $form['edit_fields']['foreign_key'] = array(
  1535. '#type' => 'fieldset',
  1536. '#title' => 'Foreign Key',
  1537. '#collapsible' => TRUE,
  1538. '#collapsed' => ($field_type == 'foreign key')? FALSE : TRUE,
  1539. );
  1540. $form['edit_fields']['foreign_key']['foreign_record'] = array(
  1541. '#type' => 'select',
  1542. '#title' => 'Record to refer to',
  1543. '#descripion' => 'Select the record that this foreign key shouold refer to. The record needs to already exist and be of the correct table.',
  1544. '#options' => $fk_options,
  1545. );
  1546. // Chado Field
  1547. $form['edit_fields']['chado'] = array(
  1548. '#type' => 'fieldset',
  1549. '#title' => t('Chado Field/Column Details'),
  1550. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  1551. );
  1552. $form['edit_fields']['chado']['chado_table'] = array(
  1553. '#type' => 'select',
  1554. '#title' => t('Chado Table'),
  1555. '#options' => $tables,
  1556. '#default_value' => $table,
  1557. '#ahah' => array(
  1558. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1559. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1560. 'effect' => 'fade'
  1561. ),
  1562. );
  1563. $form['edit_fields']['chado']['chado_field'] = array(
  1564. '#type' => 'select',
  1565. '#title' => t('Chado Field/Column'),
  1566. '#options' => $chado_fields,
  1567. '#default_value' => ($form_state['values']['chado_field']) ? $form_state['values']['chado_field'] : $template_field['field'],
  1568. '#ahah' => array(
  1569. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1570. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1571. 'effect' => 'fade'
  1572. ),
  1573. );
  1574. $form['edit_fields']['additional'] = array(
  1575. '#type' => 'fieldset',
  1576. '#title' => 'Additional Options',
  1577. );
  1578. $form['edit_fields']['additional']['required'] = array(
  1579. '#type' => 'checkbox',
  1580. '#title' => 'Make this file required',
  1581. '#default_value' => $template_field['required'],
  1582. );
  1583. $form['edit_fields']['additional']['regex_transform'] = array(
  1584. '#type' => 'fieldset',
  1585. '#title' => 'Transform Spreadsheet Value Rules',
  1586. '#collapsible' => TRUE,
  1587. '#collapsed' => (!$template_field['regex']['pattern']) ? TRUE : FALSE,
  1588. );
  1589. $transformation_msg = '<p>A transformation rule allows you to transform the original value '
  1590. .'(usually from a user submitted spreadsheet) into the form you would like it stored '
  1591. .'in the chado database. Each rule consists of a match pattern (a php regular expression '
  1592. .'which determines which replacement patterns are applied and captures regions of the '
  1593. .'original value) and a replacement pattern (a string which may contain capture references '
  1594. .'that describes what the new value should be). Each rule is applied to the result of the '
  1595. .'previous rule.<p>';
  1596. $form['edit_fields']['additional']['regex_transform']['regex_description'] = array(
  1597. '#type' => 'item',
  1598. '#value' => $transformation_msg,
  1599. );
  1600. $form['edit_fields']['additional']['regex_transform']['regex-data'] = array(
  1601. '#tree' => TRUE,
  1602. );
  1603. foreach ($template_field['regex']['pattern'] as $index => $pattern) {
  1604. $data_element = array(
  1605. 'pattern' => array(
  1606. '#type' => 'item',
  1607. '#value' => $pattern,
  1608. ),
  1609. 'replace' => array(
  1610. '#type' => 'item',
  1611. '#value' => $template_field['regex']['replace'][$index],
  1612. ),
  1613. 'old_index' => array(
  1614. '#type' => 'hidden',
  1615. '#value' => $index,
  1616. ),
  1617. 'new_index' => array(
  1618. '#type' => 'select',
  1619. '#options' => range(0, sizeof($template_field['regex']['pattern'])-1),
  1620. '#default_value' => $index,
  1621. ),
  1622. 'id' => array(
  1623. '#type' => 'hidden',
  1624. '#value' => $index,
  1625. ),
  1626. 'submit-delete' => array(
  1627. '#type' => 'submit',
  1628. '#value' => 'Delete Transformation',
  1629. '#name' => $index,
  1630. ),
  1631. );
  1632. $form['edit_fields']['additional']['regex_transform']['regex-data'][$index] = $data_element;
  1633. }
  1634. $form['edit_fields']['additional']['regex_transform']['submit-reorder_regex'] = array(
  1635. '#type' => 'submit',
  1636. '#value' => 'Save Transformation Rule Order'
  1637. );
  1638. $form['edit_fields']['additional']['regex_transform']['new_regex'] = array(
  1639. '#type' => 'fieldset',
  1640. '#title' => 'Add a new Transformation Rule',
  1641. );
  1642. $form['edit_fields']['additional']['regex_transform']['new_regex']['pattern'] = array(
  1643. '#type' => 'textfield',
  1644. '#title' => 'Match Pattern',
  1645. '#description' => 'You can use standard <b>php regular expressions</b> in this field to specify a '
  1646. .'pattern. Only if this pattern matches the value in the spreadsheet does the replacement '
  1647. .'pattern get applied to the value. To capture a section of your value for use in the '
  1648. .'replacement patten surround with round brackets. For example, <i>GI:(\d+)</i> will match '
  1649. .' NCBI gi numbers and will capture the numerical digits for use in the replacement pattern. '
  1650. .' To match and capture any value use <i>.*</i>',
  1651. );
  1652. $form['edit_fields']['additional']['regex_transform']['new_regex']['replace'] = array(
  1653. '#type' => 'textfield',
  1654. '#title' => 'Replacement Pattern',
  1655. '#description' => '<p>This pattern should contain the text you want to replace the match pattern '
  1656. .'mentioned above. It can include references of the form <b>\n</b> where n is the number of the '
  1657. .'capture in the match pattern. For example, \1 will be replaced with the text matched in your '
  1658. .'first set of round brackets.</p>',
  1659. );
  1660. if ($field_type == 'table field') {
  1661. $tab = '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
  1662. $form['edit_fields']['additional']['regex_transform']['new_regex']['replace']['#description'] .= '<p>'
  1663. .'The following references are also available for spreadsheet fields: <b><#column:<i>number</i>#></b>. '
  1664. .'This allows you to substitute other spreadsheet values into the current field. For example, '
  1665. .'if you had the following line:<br>'
  1666. . $tab . 'SNP' . $tab . '15-Jan-2011' . $tab . '1' . $tab . '54' . $tab . 'Contig34355'
  1667. .'<br> and your current field is for column #1 and you\'re inserting into the chado field '
  1668. .'feature.uniquename then you might want to add in the data to ensure your uniquename is '
  1669. .'unique. The Match Pattern is (.*) to select all the first column and the Replacement '
  1670. .'Pattern could be \1_<#column:2#> which would insert SNP_15-Jan-2011 into the database.</p>';
  1671. }
  1672. $form['edit_fields']['additional']['regex_transform']['new_regex']['submit-add_transform'] = array(
  1673. '#type' => 'submit',
  1674. '#value' => 'Add Transformation',
  1675. );
  1676. $form['edit_fields']['additional']['regex_transform']['test_regex'] = array(
  1677. '#type' => 'fieldset',
  1678. '#title' => 'Test Transformation Rules',
  1679. );
  1680. $form['edit_fields']['additional']['regex_transform']['test_regex']['test_string'] = array(
  1681. '#type' => 'textfield',
  1682. '#title' => 'Test Value',
  1683. '#description' => 'This should be a value that you expect the above transformation rules '
  1684. .'to be applied to.',
  1685. '#default_value' => $form_state['storage']['test_regex_test'],
  1686. );
  1687. $form['edit_fields']['additional']['regex_transform']['test_regex']['test_result'] = array(
  1688. '#type' => 'textfield',
  1689. '#title' => 'Test Result',
  1690. '#description' => 'This is the value that would be saved to the database after the above transformation '
  1691. .'riles were applied to the Test Value.',
  1692. '#default_value' => $form_state['storage']['test_regex_result'],
  1693. );
  1694. $form['edit_fields']['additional']['regex_transform']['test_regex']['submit-test'] = array(
  1695. '#type' => 'submit',
  1696. '#value' => 'Test Transformation Rules'
  1697. );
  1698. $form['edit_fields']['submit-edit_field'] = array(
  1699. '#type' => 'submit',
  1700. '#value' => 'Edit Field'
  1701. );
  1702. $form['edit_fields']['submit-cancel'] = array(
  1703. '#type' => 'submit',
  1704. '#value' => 'Cancel'
  1705. );
  1706. return $form;
  1707. }
  1708. /**
  1709. * Edit Field Form Submit
  1710. *
  1711. * @param $form
  1712. * The form that was submitted
  1713. * @param $form_state
  1714. * The values and storage for the form
  1715. */
  1716. function tripal_bulk_loader_edit_template_field_form_submit($form, &$form_state) {
  1717. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  1718. //dpm($op, 'Operation Submitted');
  1719. //Clear Test
  1720. $form_state['storage']['test_regex_result'] = NULL;
  1721. $form_state['storage']['test_regex_test'] = NULL;
  1722. if (!$form_state['ahah_submission']) {
  1723. if ($op == 'Edit Field') {
  1724. // If new record
  1725. if (preg_match('/NEW/', $form_state['values']['field_group'])) {
  1726. // add new record
  1727. $record_name = $form_state['values']['record_name'];
  1728. $priority = sizeof($form_state['storage']['template_array']) + 1;
  1729. $old_priority = $form_state['storage']['original_field']['priority'];
  1730. $field_index = $form_state['storage']['original_field']['field_index'];
  1731. $form_state['storage']['record2priority'][$record_name] = $priority;
  1732. $form_state['storage']['template_array'][$priority]['table'] = $form_state['values']['chado_table'];
  1733. $form_state['storage']['template_array'][$priority]['record_id'] = $record_name;
  1734. }
  1735. else {
  1736. $priority = $form_state['values']['field_group'];
  1737. $old_priority = $form_state['storage']['original_field']['priority'];
  1738. $field_index = $form_state['storage']['original_field']['field_index'];
  1739. $record_name = $form_state['storage']['record2priority'][$priority];
  1740. }
  1741. $field = $form_state['storage']['original_field'];
  1742. if ($form_state['values']['field_type'] == 'table field') {
  1743. $field['type'] = 'table field';
  1744. $field['title'] = $form_state['values']['field_title'];
  1745. $field['field'] = $form_state['values']['chado_field'];
  1746. $field['required'] = $form_state['values']['required'];
  1747. //$field['allowed values'] = empty by default;
  1748. $field['spreadsheet sheet'] = $form_state['values']['sheet_name'];
  1749. $field['spreadsheet column'] = $form_state['values']['column_number'];
  1750. $field['exposed'] = $form_state['values']['column_exposed'];
  1751. $field['exposed_description'] = $form_state['values']['column_exposed_desc'];
  1752. }
  1753. elseif ($form_state['values']['field_type'] == 'constant') {
  1754. $field['type'] = 'constant';
  1755. $field['title'] = $form_state['values']['field_title'];
  1756. $field['field'] = $form_state['values']['chado_field'];
  1757. $field['required'] = $form_state['values']['required'];
  1758. //$field['allowed values'] = empty by default;
  1759. $field['constant value'] = $form_state['values']['constant_value'];
  1760. $field['exposed_validate'] = $form_state['values']['constant_validate'];
  1761. $field['exposed'] = $form_state['values']['constant_exposed'];
  1762. }
  1763. elseif ($form_state['values']['field_type'] == 'foreign key') {
  1764. $field['type'] = 'foreign key';
  1765. $field['title'] = $form_state['values']['field_title'];
  1766. $field['field'] = $form_state['values']['chado_field'];
  1767. $field['foreign key'] = $form_state['values']['foreign_record'];
  1768. $field['required'] = $form_state['values']['required'];
  1769. }
  1770. // Deal with any additional options
  1771. // if the record has changed...
  1772. $form_state['storage']['template_array'][$priority]['table'] = $form_state['values']['chado_table'];
  1773. if ($old_priority != $priority) {
  1774. $form_state['storage']['template_array'][$priority]['fields'][] = $field;
  1775. unset($form_state['storage']['template_array'][$old_priority]['fields'][$field_index]);
  1776. // if there are no fields left delete the old record
  1777. if (!$form_state['storage']['template_array'][$old_priority]['fields']) {
  1778. unset($form_state['storage']['template_array'][$old_priority]);
  1779. }
  1780. }
  1781. else {
  1782. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $field;
  1783. }
  1784. // Save Template
  1785. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  1786. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1787. if ($success) {
  1788. drupal_set_message('Successfully Updated Field');
  1789. drupal_set_message('Template Saved.');
  1790. $path = explode('?', $form_state['storage']['referring URL']);
  1791. parse_str($path[1], $query);
  1792. $query['template_id'] = $form_state['storage']['template']->template_id;
  1793. drupal_goto($path[0], $query);
  1794. }
  1795. else {
  1796. drupal_set_message('Unable to Save Template!', 'error');
  1797. watchdog('T_bulk_loader',
  1798. 'Unable to save bulk loader template: %template',
  1799. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1800. WATCHDOG_ERROR
  1801. );
  1802. }
  1803. }
  1804. elseif ($op == 'Cancel') {
  1805. $path = explode('?', $form_state['storage']['referring URL']);
  1806. parse_str($path[1], $query);
  1807. $query['template_id'] = $form_state['storage']['template']->template_id;
  1808. drupal_goto($path[0], $query);
  1809. }
  1810. elseif ($op == 'Add Transformation') {
  1811. // Add transformation rule to original field
  1812. $form_state['storage']['original_field']['regex']['pattern'][] = '/' . $form_state['values']['pattern'] . '/';
  1813. $form_state['storage']['original_field']['regex']['replace'][] = $form_state['values']['replace'];
  1814. // Add original field back into template
  1815. $priority = $form_state['storage']['original_field']['priority'];
  1816. $field_index = $form_state['storage']['original_field']['field_index'];
  1817. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field'];
  1818. // Save Template
  1819. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  1820. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1821. if ($success) {
  1822. drupal_set_message('Successfully Added Transformation Rule');
  1823. drupal_set_message('Template Saved.');
  1824. }
  1825. else {
  1826. drupal_set_message('Unable to Save Template!', 'error');
  1827. watchdog('T_bulk_loader',
  1828. 'Unable to save bulk loader template: %template',
  1829. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1830. WATCHDOG_ERROR
  1831. );
  1832. }
  1833. }
  1834. elseif ($op == 'Save Transformation Rule Order') {
  1835. // Generate new regex array
  1836. $new_regex = array();
  1837. $old_regex = $form_state['storage']['original_field']['regex'];
  1838. foreach ($form_state['values']['regex-data'] as $key => $element) {
  1839. $new_regex['pattern'][ $element['new_index'] ] = $old_regex['pattern'][ $element['old_index'] ];
  1840. $new_regex['replace'][ $element['new_index'] ] = $old_regex['replace'][ $element['old_index'] ];
  1841. }
  1842. // sort new regex arrays
  1843. asort($new_regex['pattern']);
  1844. asort($new_regex['replace']);
  1845. // Add back to original field
  1846. $form_state['storage']['original_field']['regex'] = $new_regex;
  1847. $priority = $form_state['storage']['original_field']['priority'];
  1848. $field_index = $form_state['storage']['original_field']['field_index'];
  1849. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field'];
  1850. // Save Template
  1851. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  1852. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1853. if ($success) {
  1854. drupal_set_message('Successfully Reordered Transformation Rules');
  1855. drupal_set_message('Template Saved.');
  1856. }
  1857. else {
  1858. drupal_set_message('Unable to Save Template!', 'error');
  1859. watchdog('T_bulk_loader',
  1860. 'Unable to save bulk loader template: %template',
  1861. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1862. WATCHDOG_ERROR
  1863. );
  1864. }
  1865. }
  1866. elseif ($op == 'Delete Transformation') {
  1867. // Unset regex rule
  1868. $index = $form_state['clicked_button']['#name'];
  1869. unset($form_state['storage']['original_field']['regex']['pattern'][$index]);
  1870. unset($form_state['storage']['original_field']['regex']['replace'][$index]);
  1871. $priority = $form_state['storage']['original_field']['priority'];
  1872. $field_index = $form_state['storage']['original_field']['field_index'];
  1873. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field'];
  1874. // Save Template
  1875. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  1876. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1877. if ($success) {
  1878. drupal_set_message('Successfully Reordered Transformation Rules');
  1879. drupal_set_message('Template Saved.');
  1880. }
  1881. else {
  1882. drupal_set_message('Unable to Save Template!', 'error');
  1883. watchdog('T_bulk_loader',
  1884. 'Unable to save bulk loader template: %template',
  1885. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1886. WATCHDOG_ERROR
  1887. );
  1888. }
  1889. }
  1890. elseif ($op == 'Test Transformation Rules') {
  1891. $patterns = $form_state['storage']['original_field']['regex']['pattern'];
  1892. $replaces = $form_state['storage']['original_field']['regex']['replace'];
  1893. $test_string = $form_state['values']['test_string'];
  1894. $form_state['storage']['test_regex_result'] = preg_replace($patterns, $replaces, $test_string);
  1895. $form_state['storage']['test_regex_test'] = $test_string;
  1896. }
  1897. }
  1898. }
  1899. //////////////////////////////////////////////////////////////////////////////////////
  1900. // AHAH Callbacks
  1901. //////////////////////////////////////////////////////////////////////////////////////
  1902. /**
  1903. * AHAH Function: Replace $form['add_fields'] in tripal_bulk_loader_add_template_field_form
  1904. *
  1905. * @return
  1906. * JSON Data printed to the screen
  1907. */
  1908. function tripal_bulk_loader_add_field_ahah() {
  1909. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  1910. $form_build_id = $_POST['form_build_id'];
  1911. $form = form_get_cache($form_build_id, $form_state);
  1912. $args = $form['#parameters'];
  1913. $form_id = array_shift($args);
  1914. $form_state['post'] = $form['#post'] = $_POST;
  1915. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  1916. $form_state['ahah_submission'] = TRUE;
  1917. $form['#programmed'] = $form['#redirect'] = FALSE;
  1918. drupal_process_form($form_id, $form, $form_state);
  1919. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  1920. $form_element = $form['add_fields'];
  1921. // Remove the wrapper so we don't double it up.
  1922. //unset($form_element['#prefix'], $form_element['#suffix']);
  1923. $output = theme('status_messages');
  1924. $output .= drupal_render($form_element);
  1925. // Final rendering callback.
  1926. print drupal_json(array('status' => TRUE, 'data' => $output));
  1927. exit();
  1928. }
  1929. /**
  1930. * AHAH Function: Replace $form['edit_fields'] in tripal_bulk_loader_edit_template_field_form
  1931. *
  1932. * @return
  1933. * JSON Data printed to the screen
  1934. */
  1935. function tripal_bulk_loader_edit_field_ahah() {
  1936. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  1937. $form_build_id = $_POST['form_build_id'];
  1938. $form = form_get_cache($form_build_id, $form_state);
  1939. $args = $form['#parameters'];
  1940. $form_id = array_shift($args);
  1941. $form_state['post'] = $form['#post'] = $_POST;
  1942. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  1943. $form_state['ahah_submission'] = TRUE;
  1944. $form['#programmed'] = $form['#redirect'] = FALSE;
  1945. drupal_process_form($form_id, $form, $form_state);
  1946. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  1947. $form_element = $form['edit_fields'];
  1948. // Remove the wrapper so we don't double it up.
  1949. unset($form_element['#prefix'], $form_element['#suffix']);
  1950. $output = theme('status_messages');
  1951. $output .= drupal_render($form_element);
  1952. // Final rendering callback.
  1953. print drupal_json(array('status' => TRUE, 'data' => $output));
  1954. exit();
  1955. }