tripal_bulk_loader.admin.templates.inc 80 KB

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