tripal_bulk_loader.admin.inc 84 KB

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