tripal_bulk_loader.admin.inc 83 KB

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