tripal_bulk_loader.admin.inc 85 KB

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