tripal_bulk_loader.admin.inc 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678
  1. <?php
  2. /**
  3. * @file
  4. * Handles Create/Edit/Delete Template Admin Forms
  5. */
  6. /**
  7. * tripal_bulk_loader_admin_template
  8. */
  9. function tripal_bulk_loader_admin_template () {
  10. $add_url = url("admin/tripal/tripal_bulk_loader_template/add");
  11. $output = "<a href=\"$add_url\">Create a new bulk loader template</a><br>";
  12. $del_url = url("admin/tripal/tripal_bulk_loader_template/delete");
  13. $output .= "<a href=\"$del_url\">Delete a bulk loader template</a>";
  14. return $output;
  15. }
  16. //////////////////////////////////////////////////////////////////////////////////////
  17. // Modify Template
  18. //////////////////////////////////////////////////////////////////////////////////////
  19. /**
  20. *
  21. */
  22. function tripal_bulk_loader_modify_template_base_form ($form_state = NULL, $mode){
  23. $form = array();
  24. // get template id from path and rebuild form
  25. if ($_GET['template_id']) {
  26. if (preg_match('/^\d+$/', $_GET['template_id'])) {
  27. $form_state['storage']['template_id'] = $_GET['template_id'];
  28. }
  29. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  30. $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
  31. $form_state['storage']['template'] = unserialize($result->template_array);
  32. $form_state['storage']['template_name'] = $result->name;
  33. $form_state['storage']['record2priority'] = array();
  34. foreach ($form_state['storage']['template'] as $priority => $record_array) {
  35. if (!is_array($record_array)) { continue; }
  36. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  37. }
  38. }
  39. $form['mode'] = array(
  40. '#type' => 'hidden',
  41. '#value' => $mode,
  42. );
  43. if ($form_state['storage']['template_id']) {
  44. $form['template_name'] = array(
  45. '#type' => 'item',
  46. '#title' => 'Template',
  47. '#value' => $form_state['storage']['template_name'],
  48. '#weight' => 1,
  49. );
  50. } else {
  51. if (preg_match('/create/', $mode)) {
  52. $form['new_template_name'] = array(
  53. '#type' => 'textfield',
  54. '#title' => 'Template Name',
  55. '#weight' => 1,
  56. );
  57. } elseif (preg_match('/edit/', $mode)) {
  58. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  59. $resource = db_query($sql);
  60. $templates = array();
  61. $templates[''] = 'Select a Template';
  62. while ($r = db_fetch_object($resource)) {
  63. $templates[$r->template_id] = $r->name;
  64. }
  65. $form['template_id'] = array(
  66. '#title' => t('Template'),
  67. '#description' => t('Please select the template you would like to edit.'),
  68. '#type' => 'select',
  69. '#options' => $templates,
  70. '#default_value' => $form_state['storage']['template_id'],
  71. '#weight' => 0,
  72. '#required' => TRUE,
  73. '#weight' => 1,
  74. );
  75. }
  76. }
  77. $form['records'] = array(
  78. '#type' => ($form_state['storage']['template_id'])? 'fieldset' : 'hidden',
  79. '#title' => t('Current Records'),
  80. '#weight' => 2,
  81. );
  82. $form['records']['description'] = array(
  83. '#type' => 'item',
  84. '#value' => 'Records will be inserted into the chado database in the order listed below. To '
  85. .'change this order: <ul><li>Drag the rows into the correct order OR</li><li>Enter '
  86. .'the numbers 1 and up in the Order textboxes to indicate the correct order.</li></ul>',
  87. );
  88. $form['records']['records-data'] = array(
  89. '#tree' => TRUE,
  90. );
  91. $form['records']['no_records'] = array(
  92. '#type' => 'hidden',
  93. '#value' => TRUE,
  94. );
  95. $form['records']['submit-new_record'] = array(
  96. '#type' => 'submit',
  97. '#value' => 'New Record/Field',
  98. );
  99. $form['records']['submit-reorder'] = array(
  100. '#type' => 'submit',
  101. '#value' => 'Save Order',
  102. );
  103. $form['fields'] = array(
  104. '#type' => ($form_state['storage']['template_id'])? 'fieldset' : 'hidden',
  105. '#title' => t('Current Fields'),
  106. '#weight' => 3,
  107. );
  108. $form['fields']['fields-data'] = array(
  109. '#tree' => TRUE,
  110. );
  111. if ($form_state['storage']['template']) {
  112. // List Current Fields -------------------------------------------------------------
  113. $i=1;
  114. foreach ($form_state['storage']['template'] as $priority => $table_array) {
  115. if (!is_array($table_array)) { continue; }
  116. $form['records']['no_records']['#value'] = FALSE;
  117. $form['records']['records-data'][$priority] = array(
  118. 'title' => array(
  119. '#type' => 'markup',
  120. '#value' => $table_array['record_id'],
  121. ),
  122. 'chado_table' => array(
  123. '#type' => 'markup',
  124. '#value' => $table_array['table'],
  125. ),
  126. 'mode' => array(
  127. '#type' => 'item',
  128. '#value' => ($table_array['mode']) ? $table_array['mode'] : 'insert',
  129. ),
  130. 'new_priority' => array(
  131. '#type' => 'select',
  132. '#options' => range(1, sizeof($form_state['storage']['template'])),
  133. '#default_value' => $priority,
  134. ),
  135. 'old_priority' => array(
  136. '#type' => 'hidden',
  137. '#value' => $priority,
  138. ),
  139. 'id' => array(
  140. '#type' => 'hidden',
  141. '#value' => $priority,
  142. ),
  143. 'submit-edit_record' => array(
  144. '#type' => 'submit',
  145. '#name' => (string)$priority,
  146. '#value' => 'Edit Record',
  147. ),
  148. 'submit-add_field' => array(
  149. '#type' => 'submit',
  150. '#name' => (string)$priority,
  151. '#value' => 'Add Field',
  152. ),
  153. );
  154. foreach ($table_array['fields'] as $field_index => $field) {
  155. $form['fields']['fields-data'][$i] = array(
  156. 'record_id' => array(
  157. '#type' => 'item',
  158. '#value' => $table_array['record_id'],
  159. ),
  160. 'priority_hidden' => array(
  161. '#type' => 'hidden',
  162. '#value' => $priority,
  163. ),
  164. 'field_name' => array(
  165. '#type' => 'item',
  166. '#value' => $field['title'],
  167. ),
  168. 'chado_table_name' => array(
  169. '#type' => 'item',
  170. '#value' => $table_array['table'],
  171. ),
  172. 'chado_table_hidden' => array(
  173. '#type' => 'hidden',
  174. '#value' => $table_array['table'],
  175. ),
  176. 'chado_field_name' => array(
  177. '#type' => 'item',
  178. '#value' => $field['field'],
  179. ),
  180. 'sheet_name' => array(
  181. '#type' => 'item',
  182. '#value' => $field['spreadsheet sheet'],
  183. ),
  184. 'column_num' => array(
  185. '#type' => 'item',
  186. '#value' => $field['spreadsheet column'],
  187. ),
  188. 'constant_value' => array(
  189. '#type' => 'item',
  190. '#value' => $field['constant value'],
  191. ),
  192. 'field_index' => array(
  193. '#type' => 'hidden',
  194. '#value' => $field_index
  195. ),
  196. 'foreign_record_id' => array(
  197. '#type' => 'item',
  198. '#value' => $field['foreign key'],
  199. ),
  200. 'edit_submit' => array(
  201. '#type' => 'submit',
  202. '#name' => (string)$i,
  203. '#value' => "Edit Field",
  204. ),
  205. 'delete_submit' => array(
  206. '#type' => 'submit',
  207. '#name' => (string)$i,
  208. '#value' => "Delete Field",
  209. ),
  210. );
  211. $i++;
  212. }
  213. }
  214. $form['fields']['total_fields'] = array(
  215. '#type' => 'item',
  216. '#value' => $i,
  217. );
  218. }
  219. if ($form['records']['no_records']['#value']) {
  220. $form['records']['description'] = array(
  221. '#type' => 'item',
  222. '#value' => 'There are currently no records.',
  223. );
  224. unset($form['records']['submit-reorder']);
  225. $form['fields']['description'] = array(
  226. '#type' => 'item',
  227. '#value' => 'There are currently no fields.',
  228. );
  229. }
  230. $mode_title = (preg_match('/create/',$mode)) ? 'Create Template' : 'Edit Template';
  231. $value = ($form_state['storage']['template_id'])? 'Save Template' : $mode_title;
  232. $form['submit'] = array(
  233. '#type' => 'submit',
  234. '#value' => $value,
  235. '#weight' => 4,
  236. );
  237. return $form;
  238. }
  239. /**
  240. *
  241. */
  242. function tripal_bulk_loader_modify_template_base_form_submit($form, &$form_state){
  243. $form_state['rebuild'] = TRUE;
  244. if ($form_state['storage']['template_id']) {
  245. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  246. $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
  247. $form_state['storage']['template'] = unserialize($result->template_array);
  248. }
  249. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  250. switch ($op) {
  251. // Initialize after template is chosen ----------------------------------------
  252. case 'Edit Template':
  253. $form_state['storage']['template_id'] = $form_state['values']['template_id'];
  254. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  255. $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
  256. $form_state['storage']['template'] = unserialize($result->template_array);
  257. $form_state['storage']['template_name'] = $result->name;
  258. $form_state['storage']['record2priority'] = array();
  259. foreach ($form_state['storage']['template'] as $priority => $record_array) {
  260. if (!is_array($record_array)) { continue; }
  261. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  262. }
  263. break;
  264. case 'Create Template':
  265. $record = array(
  266. 'name' => $form_state['values']['new_template_name'],
  267. 'template_array' => array(),
  268. );
  269. drupal_write_record('tripal_bulk_loader_template', $record);
  270. $form_state['storage']['template_id'] = $record['template_id'];
  271. $form_state['storage']['template_name'] = $record['name'];
  272. $form_state['storage']['template'] = array();
  273. break;
  274. // Save Reordered Records -----------------------------------------------------
  275. case 'Save Order':
  276. $new_template = $form_state['storage']['template'];
  277. // unset old elements
  278. $form_state['storage']['record2priority'] = array();
  279. foreach ($new_template as $priority => $record_array) {
  280. if (preg_match('/\d+/', $priority)) { unset($new_template[$priority]); }
  281. }
  282. //set elements in new order
  283. foreach ($form_state['values']['records-data'] as $item) {
  284. $new_template[$item['new_priority']] = $form_state['storage']['template'][$item['old_priority']];
  285. $record_name = $new_template[$item['new_priority']]['record_id'];
  286. $form_state['storage']['record2priority'][$record_name] = $item['new_priority'];
  287. }
  288. ksort($new_template);
  289. $form_state['storage']['template'] = $new_template;
  290. break;
  291. case 'New Record/Field':
  292. $query = array(
  293. 'template_id'=> $form_state['storage']['template_id'],
  294. 'record_id' => 'NEW',
  295. );
  296. drupal_goto('admin/tripal/tripal_bulk_loader_template/add_field', $query);
  297. break;
  298. case 'Edit Record':
  299. $query = array(
  300. 'template_id'=> $form_state['storage']['template_id'],
  301. 'record_id' => $form_state['clicked_button']['#name'],
  302. );
  303. drupal_goto('admin/tripal/tripal_bulk_loader_template/edit_record', $query);
  304. break;
  305. case 'Add Field':
  306. $query = array(
  307. 'template_id'=> $form_state['storage']['template_id'],
  308. 'record_id' => $form_state['clicked_button']['#name'],
  309. );
  310. drupal_goto('admin/tripal/tripal_bulk_loader_template/add_field', $query);
  311. break;
  312. case 'Edit Field':
  313. $field_data_index = $form_state['clicked_button']['#name'];
  314. $query = array(
  315. 'template_id'=> $form_state['storage']['template_id'],
  316. 'record_id' => $form_state['values']['fields-data'][$field_data_index]['priority_hidden'],
  317. 'field_index' => $form_state['values']['fields-data'][$field_data_index]['field_index'],
  318. );
  319. drupal_goto('admin/tripal/tripal_bulk_loader_template/edit_field', $query);
  320. break;
  321. case 'Delete Field':
  322. $field_data = $form_state['values']['fields-data'][$form_state['clicked_button']['#name']];
  323. $priority = $field_data['priority_hidden'];
  324. $field_key = $field_data['field_index'];
  325. unset($form_state['storage']['template'][$priority]['fields'][$field_key]);
  326. if (!$form_state['storage']['template'][$priority]['fields']) {
  327. unset($form_state['storage']['record2priority'][$form_state['storage']['template'][$priority]['record_id']]);
  328. unset($form_state['storage']['template'][$priority]);
  329. }
  330. drupal_set_message('Deleted Field from Template.');
  331. break;
  332. } //end of switch
  333. // Save Template
  334. $record = array(
  335. 'template_id' => $form_state['storage']['template_id'],
  336. 'template_array' => serialize($form_state['storage']['template'])
  337. );
  338. drupal_write_record('tripal_bulk_loader_template', $record, array('template_id'));
  339. drupal_set_message('Template Saved.');
  340. }
  341. //////////////////////////////////////////////////////////////////////////////////////
  342. // Delete Template
  343. //////////////////////////////////////////////////////////////////////////////////////
  344. /**
  345. * Delete Template Form
  346. * This form allows admin to delete already existing templates
  347. */
  348. function tripal_bulk_loader_delete_template_base_form () {
  349. $form = array();
  350. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  351. $resource = db_query($sql);
  352. $templates = array();
  353. $templates[''] = 'Select a Template';
  354. while ($r = db_fetch_object($resource)) {
  355. $templates[$r->template_id] = $r->name;
  356. }
  357. $form['template_name'] = array(
  358. '#title' => t('Template'),
  359. '#description' => t('Please select the template you would like to delete.'),
  360. '#type' => 'select',
  361. '#options' => $templates,
  362. '#weight' => 0,
  363. '#required' => TRUE,
  364. );
  365. $form['submit'] = array(
  366. '#type' => 'submit',
  367. '#value' => 'Delete Template',
  368. );
  369. return $form;
  370. }
  371. /**
  372. * Delete Template Form Submit
  373. *
  374. * @param $form
  375. * The form that was submitted
  376. * @param $form_state
  377. * The values and storage that were submitted
  378. */
  379. function tripal_bulk_loader_delete_template_base_form_submit ($form, &$form_state) {
  380. $sql = "DELETE FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  381. db_query($sql, $form_state['values']['template_name']);
  382. }
  383. /**
  384. * Import/Export Template Form
  385. *
  386. * On export, simply selects the serialized array from the db for a given template
  387. * and presents it to the user. On import, a serialized template array and a name is
  388. * supplied and a template record is created.
  389. *
  390. * @todo Make array presented to the user more readable. (ie: unserialize and print to the screen)
  391. *
  392. * @param $form_state
  393. * The values and storage for the form
  394. * @param $mode
  395. * Either 'import' or 'export' to indicate which function is being performed
  396. * @return
  397. * A form array to be rendered by drupal_get_form
  398. */
  399. function tripal_bulk_loader_import_export_template_form ($form_state = NULL, $mode) {
  400. $form = array();
  401. $form['mode'] = array(
  402. '#type' => 'hidden',
  403. '#value' => $mode,
  404. );
  405. if (preg_match('/import/', $mode)) {
  406. $form['new_template_name'] = array(
  407. '#type' => 'textfield',
  408. '#title' => 'Template Name',
  409. '#weight' => 1,
  410. );
  411. } elseif (preg_match('/export/', $mode)) {
  412. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  413. $resource = db_query($sql);
  414. $templates = array();
  415. $templates[''] = 'Select a Template';
  416. while ($r = db_fetch_object($resource)) {
  417. $templates[$r->template_id] = $r->name;
  418. }
  419. $form['template_id'] = array(
  420. '#title' => t('Template'),
  421. '#description' => t('Please select the template you would like to edit.'),
  422. '#type' => 'select',
  423. '#options' => $templates,
  424. '#default_value' => $form_state['storage']['template_id'],
  425. '#weight' => 0,
  426. '#required' => TRUE,
  427. '#weight' => 1,
  428. );
  429. }
  430. $form['template_array'] = array(
  431. '#type' => 'textarea',
  432. '#title' => 'Template Array',
  433. '#default_value' => $form_state['storage']['template_array'],
  434. '#weight' => 2,
  435. );
  436. $form['submit'] = array(
  437. '#type' => 'submit',
  438. '#value' => 'Submit',
  439. '#weight' => 10,
  440. );
  441. return $form;
  442. }
  443. /**
  444. * Import/Export Template Form Submit
  445. *
  446. * @param $form
  447. * The form that was submitted
  448. * @param $form_state
  449. * The values and storage that were submitted
  450. */
  451. function tripal_bulk_loader_import_export_template_form_submit ($form, &$form_state) {
  452. switch ($form_state['values']['mode']) {
  453. case 'export':
  454. $record = db_fetch_object(db_query("SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d", $form_state['values']['template_id']));
  455. $form_state['storage']['template_array'] = $record->template_array;
  456. $form_state['storage']['template_id'] = $form_state['values']['template_id'];
  457. break;
  458. case 'import':
  459. $record = array(
  460. 'name' => $form_state['values']['new_template_name'],
  461. 'template_array' => $form_state['values']['template_array'],
  462. );
  463. drupal_write_record('tripal_bulk_loader_template', $record);
  464. if ($record->template_id) {
  465. drupal_set_message('Successfully imported Tripal Bulk Loader Template.');
  466. }
  467. break;
  468. }
  469. }
  470. //////////////////////////////////////////////////////////////////////////////////////
  471. // Add/Edit Field/Record Forms
  472. //////////////////////////////////////////////////////////////////////////////////////
  473. /**
  474. * Edit Record Form
  475. *
  476. * This form is meant to be called from a bulk loader form. The following should be set
  477. * in the query section of the path:
  478. * - template_id=\d+: the template which the edited record is part of
  479. * - record_id=\d+: the priority or key in the template array of the record to be edited
  480. *
  481. * @param $form_state
  482. * Contains the values and storage for the form
  483. * @return
  484. * A form array to be rendered by drupal_get_form
  485. */
  486. function tripal_bulk_loader_edit_template_record_form (&$form_state = NULL) {
  487. $form['#cache'] = TRUE; // Make sure the form is cached.
  488. // get template id from path
  489. $template_id = ($_GET['template_id'] !== NULL) ? $_GET['template_id'] : $form_state['values']['template_id'];
  490. // if there is no template supplied don't return rest of form
  491. if (!$template_id) {
  492. return $form;
  493. }
  494. // Pre-process values/defaults ---------------------------
  495. // If this is the first load of the form (no form state) we need to initialize some variables
  496. if (!$form_state['storage']['template']) {
  497. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  498. $template = db_fetch_object(db_query($sql, $template_id));
  499. $form_state['storage']['template_array'] = unserialize($template->template_array);
  500. $form_state['storage']['template'] = $template;
  501. $form_state['storage']['record2priority'] = array();
  502. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  503. if (!is_array($record_array)) { continue; }
  504. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  505. }
  506. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  507. } else {
  508. $template = $form_state['storage']['template'];
  509. }
  510. // get the record_id from the path
  511. if ($_GET['record_id'] !== NULL) {
  512. $form_state['values']['field_group'] = $_GET['record_id'];
  513. $form_state['storage']['original_priority'] = $_GET['record_id'];
  514. }
  515. // Tables and default table
  516. $tables = tripal_core_get_chado_tables();
  517. if ($form_state['values']['chado_table']) {
  518. $table = $form_state['values']['chado_table'];
  519. } else {
  520. $table = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['table'];
  521. }
  522. //dpm($form_state, 'form state');
  523. // Form Proper -------------------------------------------
  524. $form['template_name'] = array(
  525. '#type' => 'item',
  526. '#title' => 'Template',
  527. '#value' => $template->name,
  528. );
  529. $form['template_id'] = array(
  530. '#type' => 'hidden',
  531. '#value' => $template_id,
  532. );
  533. $form['edit_record'] = array(
  534. '#type' => 'fieldset',
  535. );
  536. // check template array for records then add one more
  537. if (!$form_state['storage']['record2priority']) {
  538. $groups = array();
  539. } else {
  540. $groups = array_flip($form_state['storage']['record2priority']);
  541. }
  542. $priority_default = $form_state['values']['field_group'];
  543. $form['edit_record']['field_group'] = array(
  544. '#type' => 'select',
  545. '#title' => 'Record',
  546. '#description' => 'By Changing the record here, you can move all the fields from the current record into the selected record.',
  547. '#options' => $groups,
  548. '#default_value' => $priority_default,
  549. '#required' => TRUE,
  550. );
  551. $form['edit_record']['record_name'] = array(
  552. '#type' => 'textfield',
  553. '#title' => 'Unique Record Name',
  554. '#default_value' => $groups[$priority_default],
  555. );
  556. $form['edit_record']['chado_table'] = array(
  557. '#type' => 'select',
  558. '#title' => t('Chado Table'),
  559. '#description' => 'This changes the chado table for all fields in this record.',
  560. '#options' => $tables,
  561. '#default_value' => $table,
  562. );
  563. $form['edit_record']['mode'] = array(
  564. '#type' => 'radios',
  565. '#title' => 'Action to take when Loading Record',
  566. '#options' => array(
  567. 'insert' => 'Insert the record if it doesn\'t already exist',
  568. 'select' => 'Don\'t insert this record: it\'s used to define a foreign key in another record',
  569. 'optional' => 'Record will only be inserted if all required data is filled in'
  570. ),
  571. '#default_value' => 'insert'
  572. );
  573. $form['edit_record']['submit-edit_record'] = array(
  574. '#type' => 'submit',
  575. '#value' => 'Edit Record'
  576. );
  577. $form['edit_record']['submit-cancel'] = array(
  578. '#type' => 'submit',
  579. '#value' => 'Cancel'
  580. );
  581. return $form;
  582. }
  583. /**
  584. * Edit Record Form Submit
  585. *
  586. * @param $form
  587. * The form that was submitted
  588. * @param $form_state
  589. * Contains the values and storage for the form
  590. */
  591. function tripal_bulk_loader_edit_template_record_form_submit ($form, &$form_state) {
  592. //dpm($form_state, 'form state -start submit');
  593. if (!$form_state['ahah_submission']) {
  594. if ($form_state['values']['op'] == 'Edit Record') {
  595. $template = $form_state['storage']['template_array'];
  596. // Edit Record
  597. $record = $template[ $form_state['storage']['original_priority'] ];
  598. $record['record_id'] = $form_state['values']['record_name'];
  599. $record['mode'] = $form_state['values']['mode'];
  600. $record['table'] = $form_state['values']['chado_table'];
  601. if ($form_state['storage']['original_priority'] != $form_state['values']['field_group']) {
  602. $record['fields'] = array_merge($record['fields'], $template[ $form_state['values']['field_group'] ]['fields']);
  603. $template[ $form_state['values']['field_group'] ] = $record;
  604. unset($template[ $form_state['storage']['original_priority'] ]);
  605. } else {
  606. $template[ $form_state['storage']['original_priority'] ] = $record;
  607. }
  608. // Save Template
  609. $form_state['storage']['template']->template_array = serialize($template);
  610. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  611. if ($success) {
  612. drupal_set_message('Successfully Updated Template Record');
  613. drupal_set_message('Template Saved.');
  614. $path = explode('?',$form_state['storage']['referring URL']);
  615. parse_str($path[1], $query);
  616. $query['template_id'] = $form_state['storage']['template']->template_id;
  617. drupal_goto($path[0], $query);
  618. } else {
  619. drupal_set_message('Unable to Save Template!', 'error');
  620. watchdog('T_bulk_loader',
  621. 'Unable to save bulk loader template: %template',
  622. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  623. WATCHDOG_ERROR
  624. );
  625. }
  626. } elseif ($form_state['values']['op'] == 'Cancel') {
  627. $path = explode('?',$form_state['storage']['referring URL']);
  628. parse_str($path[1], $query);
  629. $query['template_id'] = $form_state['storage']['template']->template_id;
  630. //dpm('Redirecting to: '.$path[0].'?'.print_r($query,TRUE).' where the referring URL:'.$form_state['storage']['referring URL']);
  631. drupal_goto($path[0], $query);
  632. }
  633. }
  634. }
  635. /**
  636. * Add Field Form
  637. *
  638. * This form is meant to be called from a bulk loader form. Blank Defaults are in place but you
  639. * can use the following in the query of the path to set defaults for a given template:
  640. * - template_id=\d+: the template to add the field to
  641. * - record_id=\d+: the priority or key in the template array of the record to add the field to
  642. *
  643. * @param $form_state
  644. * Contains the values and storage for the form
  645. * @return
  646. * A form array to be rendered by drupal_get_form
  647. */
  648. function tripal_bulk_loader_add_template_field_form (&$form_state = NULL) {
  649. $form = array();
  650. $form['#cache'] = TRUE; // Make sure the form is cached.
  651. // get template id from path
  652. $template_id = ($_GET['template_id']) ? $_GET['template_id'] : $form_state['values']['template_id'];
  653. // if there is no template supplied don't return rest of form
  654. if (!$template_id) {
  655. return $form;
  656. }
  657. // Pre-set Variables needed for form proper------------------------------------------
  658. // If this is the first load of the form (no form state) we need to initialize some variables
  659. if (!$form_state['storage']['template']) {
  660. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  661. $template = db_fetch_object(db_query($sql, $template_id));
  662. $form_state['storage']['template_array'] = unserialize($template->template_array);
  663. $form_state['storage']['template'] = $template;
  664. $form_state['storage']['record2priority'] = array();
  665. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  666. if (!is_array($record_array)) { continue; }
  667. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  668. }
  669. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  670. } else {
  671. $template = $form_state['storage']['template'];
  672. }
  673. $field_type = ($form_state['values']['field_type'])? $form_state['values']['field_type'] : 'table field';
  674. // Tables and default table
  675. $tables = tripal_core_get_chado_tables();
  676. if ($form_state['values']) {
  677. if (!preg_match('/^'.current($tables).'$/', $form_state['values']['chado_table'])) {
  678. $table = $form_state['values']['chado_table'];
  679. } elseif ($form_state['values']['record_name']) {
  680. $record_name = $form_state['values']['record_name'];
  681. $priority = $form_state['storage']['record2priority'][$record_name];
  682. $table = $form_state['storage']['template_array'][$priority]['table'];
  683. } else {
  684. $priority = $form_state['values']['field_group'];
  685. $table = $form_state['storage']['template_array'][$priority]['table'];
  686. }
  687. }
  688. if (!$table) {
  689. $table = reset($tables);
  690. }
  691. // get the record_id from the path
  692. if ($_GET['record_id'] !== NULL) {
  693. $form_state['values']['field_group'] = $_GET['record_id'];
  694. if (preg_match('/\d+/', $_GET['record_id'])) {
  695. $priority = $form_state['values']['field_group'];
  696. $table = $form_state['storage']['template_array'][$priority]['table'];
  697. }
  698. }
  699. // Fields and foreign key mappings
  700. $chado_fields = array();
  701. $fk_options = array();
  702. $fk_options['NULL'] = 'None';
  703. $table_description = module_invoke_all('chado_'.$table.'_schema');
  704. if ($field_type == 'foreign key') {
  705. $foreign_field2table = array();
  706. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  707. foreach ($key_array['columns'] as $left_field => $right_field) {
  708. $chado_fields[$left_field] = $left_field;
  709. $foreign_field2table[$left_field] = $key_table;
  710. }
  711. }
  712. reset($chado_fields);
  713. // set default field
  714. if (empty($chado_fields)) {
  715. $field = NULL;
  716. } elseif ($chado_fields[$form_state['values']['chado_field']]) {
  717. $field = $form_state['values']['chado_field'];
  718. } else {
  719. $field = current($chado_fields);
  720. }
  721. // Foreign key options
  722. $foreign_table = $foreign_field2table[$field];
  723. if ($foreign_table) {
  724. foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) {
  725. if (preg_match('/^'.$foreign_table.'$/', $form_state['storage']['template_array'][$priority]['table'])) {
  726. $fk_options[$record_name] = $record_name;
  727. }
  728. }
  729. }
  730. } else {
  731. foreach($table_description['fields'] as $field_name => $field_array) {
  732. $chado_fields[$field_name] = $field_name;
  733. }
  734. }
  735. $variables = array(
  736. 'form_state' => $form_state,
  737. 'tables' => $tables,
  738. 'default table' => $table,
  739. 'fields' => $chado_fields,
  740. 'default_field' => $field,
  741. 'priority' => $priority,
  742. 'record_name' => $record_name,
  743. 'table description' => $table_description,
  744. 'foreign key options' => $fk_options,
  745. 'foreign field=>table' => $foreign_field2table,
  746. 'foreign table' => $foreign_table,
  747. );
  748. //dpm($variables, 'variables');
  749. // Start of Form Proper--------------------------------------------------------------
  750. $form['template_name'] = array(
  751. '#type' => 'item',
  752. '#title' => 'Template',
  753. '#value' => $template->name,
  754. );
  755. $form['template_id'] = array(
  756. '#type' => 'hidden',
  757. '#value' => $template_id,
  758. );
  759. $form['add_fields'] = array(
  760. '#type' => 'fieldset',
  761. '#prefix' => '<div id="tripal_bulk_loader_template-add_field">',
  762. '#suffix' => '</div>',
  763. );
  764. $form['add_fields']['field_type'] = array(
  765. '#type' => 'radios',
  766. '#title' => t('Type of Field'),
  767. '#options' => array(
  768. 'table field' => t('Spreadsheet: Fields which maps to a Spreadsheet Column'),
  769. 'constant' => t('Constant: Field which remains Constant throughout the Spreadsheet'),
  770. 'foreign key' => t('Foreign Key: Fields which map to a record in another table'),
  771. ),
  772. '#required' => TRUE,
  773. '#default_value' => $field_type,
  774. '#ahah' => array(
  775. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  776. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  777. 'effect' => 'fade'
  778. ),
  779. );
  780. // check template array for records then add one more
  781. if (!$form_state['storage']['record2priority']) {
  782. $groups = array();
  783. } else {
  784. $groups = array_flip($form_state['storage']['record2priority']);
  785. }
  786. $groups['NONE'] = 'Select a Record';
  787. $groups['NEW'] = 'New Record';
  788. $form['add_fields']['field_group'] = array(
  789. '#type' => 'select',
  790. '#title' => 'Record',
  791. '#description' => 'This is used to group a set of fields together allowing '
  792. .'multiple records to be inserted into the same table per line of the spreadsheet',
  793. '#options' => $groups,
  794. '#default_value' => (preg_match('/\w+.*/',$form_state['values']['field_group'])) ? $form_state['values']['field_group'] : 'NONE',
  795. '#ahah' => array(
  796. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  797. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  798. 'effect' => 'fade'
  799. ),
  800. '#required' => TRUE,
  801. );
  802. $form['add_fields']['record_name'] = array(
  803. '#type' => (preg_match('/NEW/', $form_state['values']['field_group'])) ? 'textfield' : 'hidden',
  804. '#title' => 'Unique Record Name',
  805. '#prefix' => '<div id="tripal_bulk_loader_template-add_record">',
  806. '#suffix' => '</div>',
  807. '#default_value' => $form_state['values']['record_name'],
  808. );
  809. $form['add_fields']['field_title'] = array(
  810. '#type' => 'textfield',
  811. '#title' => t('Human-readable Title for Field'),
  812. '#default_value' => $form_state['values']['field_title'],
  813. );
  814. // Spreadsheet column
  815. $form['add_fields']['columns'] = array(
  816. '#type' => 'fieldset',
  817. '#title' => t('Spreadsheet Column'),
  818. '#collapsible' => TRUE,
  819. '#collapsed' => ($field_type == 'table field')? FALSE : TRUE,
  820. );
  821. $form['add_fields']['columns']['sheet_name'] = array(
  822. '#type' => 'textfield',
  823. '#title' => t('Worksheet'),
  824. '#description' => t('Specify the name of the worksheet.'),
  825. '#size' => 5,
  826. '#default_value' => ($form_state['values']['sheet_name'])? $form_state['values']['sheet_name'] : 'Sheet1',
  827. );
  828. $form['add_fields']['columns']['column_number'] = array(
  829. '#type' => 'textfield',
  830. '#title' => t('Column'),
  831. '#description' => t('Specify the column in the spreadsheet that this field maps to where the first column is 1.'),
  832. '#size' => 5,
  833. '#default_value' => $form_state['values']['column_number'],
  834. );
  835. // Global Value
  836. $form['add_fields']['constant'] = array(
  837. '#type' => 'fieldset',
  838. '#title' => t('Constant'),
  839. '#collapsible' => TRUE,
  840. '#collapsed' => ($field_type == 'constant')? FALSE : TRUE,
  841. );
  842. $form['add_fields']['constant']['constant_value'] = array(
  843. '#type' => 'textfield',
  844. '#title' => t('Constant Value'),
  845. '#description' => t('Specify the value you wish this field to have regardless of spreadsheet data.'),
  846. '#default_value' => $form_state['values']['constant_value']
  847. );
  848. // Foreign Key
  849. $form['add_fields']['foreign_key'] = array(
  850. '#type' => 'fieldset',
  851. '#title' => 'Foreign Key',
  852. '#collapsible' => TRUE,
  853. '#collapsed' => ($field_type == 'foreign key')? FALSE : TRUE,
  854. );
  855. $form['add_fields']['foreign_key']['foreign_record'] = array(
  856. '#type' => 'select',
  857. '#title' => 'Record to refer to',
  858. '#descripion' => 'Select the record that this foreign key shouold refer to. The record needs to already exist and be of the correct table.',
  859. '#options' => $fk_options,
  860. );
  861. // Chado Field
  862. $form['add_fields']['chado'] = array(
  863. '#type' => 'fieldset',
  864. '#title' => t('Chado Field/Column Details'),
  865. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  866. );
  867. $form['add_fields']['chado']['chado_table'] = array(
  868. '#type' => 'select',
  869. '#title' => t('Chado Table'),
  870. '#options' => $tables,
  871. '#default_value' => $table,
  872. '#ahah' => array(
  873. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  874. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  875. 'effect' => 'fade'
  876. ),
  877. );
  878. $form['add_fields']['chado']['chado_field'] = array(
  879. '#type' => 'select',
  880. '#title' => t('Chado Field/Column'),
  881. '#options' => $chado_fields,
  882. '#default_value' => $form_state['values']['chado_field'],
  883. '#ahah' => array(
  884. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  885. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  886. 'effect' => 'fade'
  887. ),
  888. );
  889. $form['add_fields']['additional'] = array(
  890. '#type' => 'fieldset',
  891. '#title' => 'Additional Options',
  892. );
  893. $form['add_fields']['additional']['required'] = array(
  894. '#type' => 'checkbox',
  895. '#title' => 'Make this file required',
  896. '#default_value' => $template_field['required'],
  897. );
  898. $form['add_fields']['additional']['regex_transform'] = array(
  899. '#type' => 'fieldset',
  900. '#title' => 'Transform Spreadsheet Value Rule'
  901. );
  902. $form['add_fields']['additional']['regex_transform']['transform'] = array(
  903. '#type' => 'checkbox',
  904. '#title' => 'Apply transformation to this field'
  905. );
  906. $form['add_fields']['additional']['regex_transform']['pattern'] = array(
  907. '#type' => 'textfield',
  908. '#title' => 'Match Pattern',
  909. '#description' => 'You can use standard php regular expressions in this field to specify a '
  910. .'pattern. Only if this pattern matches the value in the spreadsheet does the replacement '
  911. .'pattern get applied to the value. To capture a section of your value for use in the '
  912. .'replacement patten surround with round brackets. For example, <i>GI:(\d+)</i> will match '
  913. .' NCBI gi numbers and will capture the numerical digits for use in the replacement pattern. '
  914. .' To match and capture any value use <i>.*</i>',
  915. '#default_value' => ($template_field['regex']['pattern']) ? $template_field['regex']['pattern'] : '^(.*)$',
  916. );
  917. $form['add_fields']['additional']['regex_transform']['replace'] = array(
  918. '#type' => 'textfield',
  919. '#title' => 'Replacement Pattern',
  920. '#description' => 'This pattern should contain the text you want to replace the match pattern '
  921. .'mentioned above. It can include references of the form \n where n is the number of the '
  922. .'capture in the match pattern. For example, \1 will be replaced with the text matched in your '
  923. .'first set of round brackets.',
  924. '#default_value' => ($template_field['regex']['replace']) ? $template_field['regex']['replace'] : '\1'
  925. );
  926. $form['add_fields']['submit-add_field'] = array(
  927. '#type' => 'submit',
  928. '#value' => 'Add Field'
  929. );
  930. $form['add_fields']['submit-cancel'] = array(
  931. '#type' => 'submit',
  932. '#value' => 'Cancel'
  933. );
  934. return $form;
  935. }
  936. /**
  937. * Add Field Submit
  938. *
  939. * @param $form
  940. * The form that was submitted
  941. * @param $form_state
  942. * The values and storage for the form
  943. */
  944. function tripal_bulk_loader_add_template_field_form_submit ($form, &$form_state) {
  945. if (!$form_state['ahah_submission']) {
  946. if ($form_state['values']['op'] == 'Add Field') {
  947. $template = $form_state['storage']['template_array'];
  948. // If new record
  949. if (preg_match('/NEW/',$form_state['values']['field_group'])) {
  950. $record_name = $form_state['values']['record_name'];
  951. $priority = sizeof($form_state['storage']['template_array']) + 1;
  952. $record2priority[$record_name] = $priority;
  953. $template[$priority]['table'] = $form_state['values']['chado_table'];
  954. $template[$priority]['record_id'] = $record_name;
  955. } else {
  956. $priority = $form_state['values']['field_group'];
  957. $record_name = $record2priority[$priority];
  958. }
  959. // Add field to template array
  960. if ($form_state['values']['field_type'] == 'table field') {
  961. $field = array(
  962. 'type' => 'table field',
  963. 'title' => $form_state['values']['field_title'],
  964. 'field' => $form_state['values']['chado_field'],
  965. 'required' => $form_state['values']['required'],
  966. //'allowed values' => empty by default,
  967. 'spreadsheet sheet' => $form_state['values']['sheet_name'],
  968. 'spreadsheet column' => $form_state['values']['column_number'],
  969. //'exposed' => 'true|false' If exposed, will give a select box first from allowed values if set, second from database if values not set.
  970. );
  971. } elseif ($form_state['values']['field_type'] == 'constant') {
  972. $field = array(
  973. 'type' => 'constant',
  974. 'title' => $form_state['values']['field_title'],
  975. 'field' => $form_state['values']['chado_field'],
  976. 'required' => $form_state['values']['required'],
  977. //'allowed values' => empty by default,
  978. 'constant value' => $form_state['values']['constant_value'],
  979. //'exposed' => 'true|false' If exposed, will give a select box first from allowed values if set, second from database if values not set.
  980. );
  981. } elseif ($form_state['values']['field_type'] == 'foreign key') {
  982. $field = array(
  983. 'type' => 'foreign key',
  984. 'title' => $form_state['values']['field_title'],
  985. 'field' => $form_state['values']['chado_field'],
  986. 'foreign key' => $form_state['values']['foreign_record'],
  987. 'required' => $form_state['values']['required'],
  988. );
  989. }
  990. // Deal with any additional options
  991. if ($form_state['values']['transform']) {
  992. $field['regex']['pattern'] = $form_state['values']['pattern'];
  993. $field['regex']['replace'] = $form_state['values']['replace'];
  994. }
  995. // Save Template
  996. $template[$priority]['fields'][] = $field;
  997. $form_state['storage']['template']->template_array = serialize($template);
  998. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  999. if ($success) {
  1000. drupal_set_message('Successfully Added Field to Template');
  1001. drupal_set_message('Template Saved.');
  1002. $path = explode('?',$form_state['storage']['referring URL']);
  1003. parse_str($path[1], $query);
  1004. $query['template_id'] = $form_state['storage']['template']->template_id;
  1005. drupal_goto($path[0], $query);
  1006. } else {
  1007. drupal_set_message('Unable to Save Template!', 'error');
  1008. watchdog('T_bulk_loader',
  1009. 'Unable to save bulk loader template: %template',
  1010. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1011. WATCHDOG_ERROR
  1012. );
  1013. }
  1014. } elseif ($form_state['values']['op'] == 'Cancel') {
  1015. $path = explode('?',$form_state['storage']['referring URL']);
  1016. parse_str($path[1], $query);
  1017. $query['template_id'] = $form_state['storage']['template']->template_id;
  1018. drupal_goto($path[0], $query);
  1019. }
  1020. }
  1021. }
  1022. /**
  1023. * Edit Field Form
  1024. *
  1025. * This form is meant to be called from a bulk loader form. The following should be set
  1026. * in the query section of the path:
  1027. * - template_id=\d+: the template which the edited field is part of
  1028. * - record_id=\d+: the priority or key in the template array of the record the field
  1029. * is currently part of
  1030. * - field_index=\d+: the key of the field in the fields array of the previously
  1031. * specified record
  1032. *
  1033. * @param $form_state
  1034. * Contains the values and storage for the form
  1035. * @return
  1036. * A form array to be rendered by drupal_get_form
  1037. */
  1038. function tripal_bulk_loader_edit_template_field_form (&$form_state = NULL) {
  1039. $form = array();
  1040. $form['#cache'] = TRUE; // Make sure the form is cached.
  1041. // get template id from path
  1042. $template_id = ($_GET['template_id']) ? $_GET['template_id'] : $form_state['values']['template_id'];
  1043. // if there is no template supplied don't return rest of form
  1044. if (!$template_id) {
  1045. return $form;
  1046. }
  1047. // Pre-set Variables needed for form proper------------------------------------------
  1048. // If this is the first load of the form (no form state) we need to initialize some variables
  1049. if (!$form_state['storage']['template']) {
  1050. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  1051. $template = db_fetch_object(db_query($sql, $template_id));
  1052. $form_state['storage']['template_array'] = unserialize($template->template_array);
  1053. $form_state['storage']['template'] = $template;
  1054. $form_state['storage']['record2priority'] = array();
  1055. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  1056. if (!is_array($record_array)) { continue; }
  1057. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  1058. }
  1059. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  1060. } else {
  1061. $template = $form_state['storage']['template'];
  1062. }
  1063. // get the field from the path
  1064. if (!($_GET['record_id']===NULL || $_GET['field_index']===NULL)) {
  1065. $priority = $_GET['record_id'];
  1066. $field_index = $_GET['field_index'];
  1067. $template_field = $form_state['storage']['template_array'][$priority]['fields'][$field_index];
  1068. $form_state['storage']['original_field'] = $template_field;
  1069. $form_state['storage']['original_field']['priority'] = $priority;
  1070. $form_state['storage']['original_field']['field_index'] = $field_index;
  1071. }
  1072. $field_type = ($form_state['values']['field_type'])? $form_state['values']['field_type'] : $template_field['type'];
  1073. // Tables and default table
  1074. $tables = tripal_core_get_chado_tables();
  1075. if ($form_state['values']) {
  1076. $table = $form_state['values']['chado_table'];
  1077. } else {
  1078. $table = $form_state['storage']['template_array'][$priority]['table'];
  1079. }
  1080. // Fields and foreign key mappings
  1081. $chado_fields = array();
  1082. $fk_options = array();
  1083. $fk_options['NULL'] = 'None';
  1084. $table_description = module_invoke_all('chado_'.$table.'_schema');
  1085. if ($field_type == 'foreign key') {
  1086. $foreign_field2table = array();
  1087. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  1088. foreach ($key_array['columns'] as $left_field => $right_field) {
  1089. $chado_fields[$left_field] = $left_field;
  1090. $foreign_field2table[$left_field] = $key_table;
  1091. }
  1092. }
  1093. reset($chado_fields);
  1094. // set default field
  1095. if (empty($chado_fields)) {
  1096. $field = NULL;
  1097. } elseif ($chado_fields[$form_state['values']['chado_field']]) {
  1098. $field = $form_state['values']['chado_field'];
  1099. } else {
  1100. $field = current($chado_fields);
  1101. }
  1102. // Foreign key options
  1103. $foreign_table = $foreign_field2table[$field];
  1104. if ($foreign_table) {
  1105. foreach ($form_state['storage']['record2priority'] as $record_nameT => $priorityT ) {
  1106. if (preg_match('/^'.$foreign_table.'$/', $form_state['storage']['template_array'][$priorityT]['table'])) {
  1107. $fk_options[$record_nameT] = $record_nameT;
  1108. }
  1109. }
  1110. }
  1111. } else {
  1112. foreach($table_description['fields'] as $field_name => $field_array) {
  1113. $chado_fields[$field_name] = $field_name;
  1114. }
  1115. }
  1116. // dpm(array( 'tables' => $tables, 'default table' => $table, 'record name' => $record_name, 'priority' => $priority,
  1117. // 'fields' => $chad_fields, 'default field' => $field, 'foreign field=>table' => $foreign_field2table,
  1118. // 'table desc' => $table_description, 'foreign record options' => $fk_options, 'foreign table' => $foreign_table
  1119. // ), 'Variables');
  1120. // Start of Form Proper--------------------------------------------------------------
  1121. $form['template_name'] = array(
  1122. '#type' => 'item',
  1123. '#title' => 'Template',
  1124. '#value' => $template->name,
  1125. );
  1126. $form['template_id'] = array(
  1127. '#type' => 'hidden',
  1128. '#value' => $template_id,
  1129. );
  1130. $form['edit_fields'] = array(
  1131. '#type' => 'fieldset',
  1132. '#prefix' => '<div id="tripal_bulk_loader_template-edit_field">',
  1133. '#suffix' => '</div>',
  1134. );
  1135. $form['edit_fields']['field_type'] = array(
  1136. '#type' => 'radios',
  1137. '#title' => t('Type of Field'),
  1138. '#options' => array(
  1139. 'table field' => t('Spreadsheet: Fields which maps to a Spreadsheet Column'),
  1140. 'constant' => t('Constant: Field which remains Constant throughout the Spreadsheet'),
  1141. 'foreign key' => t('Foreign Key: Fields which map to a record in another table'),
  1142. ),
  1143. '#required' => TRUE,
  1144. '#default_value' => $field_type,
  1145. '#ahah' => array(
  1146. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1147. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1148. 'effect' => 'fade'
  1149. ),
  1150. );
  1151. // check template array for records then edit one more
  1152. if (!$form_state['storage']['record2priority']) {
  1153. $groups = array();
  1154. } else {
  1155. $groups = array_flip($form_state['storage']['record2priority']);
  1156. }
  1157. $groups['NONE'] = 'Select a Record';
  1158. $groups['NEW'] = 'New Record';
  1159. $form['edit_fields']['field_group'] = array(
  1160. '#type' => 'select',
  1161. '#title' => 'Record',
  1162. '#description' => 'This is used to group a set of fields together allowing '
  1163. .'multiple records to be inserted into the same table per line of the spreadsheet',
  1164. '#options' => $groups,
  1165. '#default_value' => (preg_match('/(\d+|\w+)/',$form_state['values']['field_group'])) ? $form_state['values']['field_group'] : $priority,
  1166. '#ahah' => array(
  1167. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1168. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1169. 'effect' => 'fade'
  1170. ),
  1171. '#required' => TRUE,
  1172. );
  1173. $form['edit_fields']['record_name'] = array(
  1174. '#type' => (preg_match('/NEW/', $form_state['values']['field_group'])) ? 'textfield' : 'hidden',
  1175. '#title' => 'Unique Record Name',
  1176. '#prefix' => '<div id="tripal_bulk_loader_template-edit_record">',
  1177. '#suffix' => '</div>',
  1178. '#default_value' => $form_state['values']['record_name'],
  1179. );
  1180. $form['edit_fields']['field_title'] = array(
  1181. '#type' => 'textfield',
  1182. '#title' => t('Human-readable Title for Field'),
  1183. '#default_value' => ($form_state['values']['field_title']) ? $form_state['values']['field_title'] : $template_field['title'],
  1184. );
  1185. // Spreadsheet column
  1186. $form['edit_fields']['columns'] = array(
  1187. '#type' => 'fieldset',
  1188. '#title' => t('Spreadsheet Column'),
  1189. '#collapsible' => TRUE,
  1190. '#collapsed' => ($field_type == 'table field')? FALSE : TRUE,
  1191. );
  1192. $form['edit_fields']['columns']['sheet_name'] = array(
  1193. '#type' => 'textfield',
  1194. '#title' => t('Worksheet'),
  1195. '#description' => t('Specify the name of the worksheet.'),
  1196. '#size' => 5,
  1197. '#default_value' => ($form_state['values']['sheet_name'])? $form_state['values']['sheet_name'] : $template_field['spreadsheet sheet'],
  1198. );
  1199. $form['edit_fields']['columns']['column_number'] = array(
  1200. '#type' => 'textfield',
  1201. '#title' => t('Column'),
  1202. '#description' => t('Specify the column in the spreadsheet that this field maps to where the first column is 1.'),
  1203. '#size' => 5,
  1204. '#default_value' => ($form_state['values']['column_number']) ? $form_state['values']['column_number'] : $template_field['spreadsheet column'],
  1205. );
  1206. // Global Value
  1207. $form['edit_fields']['constant'] = array(
  1208. '#type' => 'fieldset',
  1209. '#title' => t('Constant'),
  1210. '#collapsible' => TRUE,
  1211. '#collapsed' => ($field_type == 'constant')? FALSE : TRUE,
  1212. );
  1213. $form['edit_fields']['constant']['constant_value'] = array(
  1214. '#type' => 'textfield',
  1215. '#title' => t('Constant Value'),
  1216. '#description' => t('Specify the value you wish this field to have regardless of spreadsheet data.'),
  1217. '#default_value' => ($form_state['values']['constant_value']) ? $form_state['values']['constant_value'] : $template_field['constant value'],
  1218. );
  1219. // Foreign Key
  1220. $form['edit_fields']['foreign_key'] = array(
  1221. '#type' => 'fieldset',
  1222. '#title' => 'Foreign Key',
  1223. '#collapsible' => TRUE,
  1224. '#collapsed' => ($field_type == 'foreign key')? FALSE : TRUE,
  1225. );
  1226. $form['edit_fields']['foreign_key']['foreign_record'] = array(
  1227. '#type' => 'select',
  1228. '#title' => 'Record to refer to',
  1229. '#descripion' => 'Select the record that this foreign key shouold refer to. The record needs to already exist and be of the correct table.',
  1230. '#options' => $fk_options,
  1231. );
  1232. // Chado Field
  1233. $form['edit_fields']['chado'] = array(
  1234. '#type' => 'fieldset',
  1235. '#title' => t('Chado Field/Column Details'),
  1236. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  1237. );
  1238. $form['edit_fields']['chado']['chado_table'] = array(
  1239. '#type' => 'select',
  1240. '#title' => t('Chado Table'),
  1241. '#options' => $tables,
  1242. '#default_value' => $table,
  1243. '#ahah' => array(
  1244. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1245. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1246. 'effect' => 'fade'
  1247. ),
  1248. );
  1249. $form['edit_fields']['chado']['chado_field'] = array(
  1250. '#type' => 'select',
  1251. '#title' => t('Chado Field/Column'),
  1252. '#options' => $chado_fields,
  1253. '#default_value' => ($form_state['values']['chado_field']) ? $form_state['values']['chado_field'] : $template_field['field'],
  1254. '#ahah' => array(
  1255. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1256. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1257. 'effect' => 'fade'
  1258. ),
  1259. );
  1260. $form['edit_fields']['additional'] = array(
  1261. '#type' => 'fieldset',
  1262. '#title' => 'Additional Options',
  1263. );
  1264. $form['edit_fields']['additional']['required'] = array(
  1265. '#type' => 'checkbox',
  1266. '#title' => 'Make this file required',
  1267. '#default_value' => $template_field['required'],
  1268. );
  1269. $form['edit_fields']['additional']['regex_transform'] = array(
  1270. '#type' => 'fieldset',
  1271. '#title' => 'Transform Spreadsheet Value Rule'
  1272. );
  1273. $form['edit_fields']['additional']['regex_transform']['transform'] = array(
  1274. '#type' => 'checkbox',
  1275. '#title' => 'Apply transformation to this field'
  1276. );
  1277. $form['edit_fields']['additional']['regex_transform']['pattern'] = array(
  1278. '#type' => 'textfield',
  1279. '#title' => 'Match Pattern',
  1280. '#description' => 'You can use standard php regular expressions in this field to specify a '
  1281. .'pattern. Only if this pattern matches the value in the spreadsheet does the replacement '
  1282. .'pattern get applied to the value. To capture a section of your value for use in the '
  1283. .'replacement patten surround with round brackets. For example, <i>GI:(\d+)</i> will match '
  1284. .' NCBI gi numbers and will capture the numerical digits for use in the replacement pattern. '
  1285. .' To match and capture any value use <i>.*</i>',
  1286. '#default_value' => ($template_field['regex']['pattern']) ? $template_field['regex']['pattern'] : '^(.*)$',
  1287. );
  1288. $form['edit_fields']['additional']['regex_transform']['replace'] = array(
  1289. '#type' => 'textfield',
  1290. '#title' => 'Replacement Pattern',
  1291. '#description' => 'This pattern should contain the text you want to replace the match pattern '
  1292. .'mentioned above. It can include references of the form \n where n is the number of the '
  1293. .'capture in the match pattern. For example, \1 will be replaced with the text matched in your '
  1294. .'first set of round brackets.',
  1295. '#default_value' => ($template_field['regex']['replace']) ? $template_field['regex']['replace'] : '\1'
  1296. );
  1297. $form['edit_fields']['submit-edit_field'] = array(
  1298. '#type' => 'submit',
  1299. '#value' => 'Edit Field'
  1300. );
  1301. $form['edit_fields']['submit-cancel'] = array(
  1302. '#type' => 'submit',
  1303. '#value' => 'Cancel'
  1304. );
  1305. return $form;
  1306. }
  1307. /**
  1308. * Edit Field Form Submit
  1309. *
  1310. * @param $form
  1311. * The form that was submitted
  1312. * @param $form_state
  1313. * The values and storage for the form
  1314. */
  1315. function tripal_bulk_loader_edit_template_field_form_submit ($form, &$form_state) {
  1316. if (!$form_state['ahah_submission']) {
  1317. if ($form_state['values']['op'] == 'Edit Field') {
  1318. // If new record
  1319. if (preg_match('/NEW/',$form_state['values']['field_group'])) {
  1320. // add new record
  1321. $record_name = $form_state['values']['record_name'];
  1322. $priority = sizeof($form_state['storage']['template_array']) + 1;
  1323. $old_priority = $form_state['storage']['original_field']['priority'];
  1324. $field_index = $form_state['storage']['original_field']['field_index'];
  1325. $form_state['storage']['record2priority'][$record_name] = $priority;
  1326. $form_state['storage']['template_array'][$priority]['table'] = $form_state['values']['chado_table'];
  1327. $form_state['storage']['template_array'][$priority]['record_id'] = $record_name;
  1328. } else {
  1329. $priority = $form_state['values']['field_group'];
  1330. $old_priority = $form_state['storage']['original_field']['priority'];
  1331. $field_index = $form_state['storage']['original_field']['field_index'];
  1332. $record_name = $form_state['storage']['record2priority'][$priority];
  1333. }
  1334. if ($form_state['values']['field_type'] == 'table field') {
  1335. $field = array(
  1336. 'type' => 'table field',
  1337. 'title' => $form_state['values']['field_title'],
  1338. 'field' => $form_state['values']['chado_field'],
  1339. 'required' => $form_state['values']['required'],
  1340. //'allowed values' => empty by default,
  1341. 'spreadsheet sheet' => $form_state['values']['sheet_name'],
  1342. 'spreadsheet column' => $form_state['values']['column_number'],
  1343. //'exposed' => 'true|false' If exposed, will give a select box first from allowed values if set, second from database if values not set.
  1344. );
  1345. } elseif ($form_state['values']['field_type'] == 'constant') {
  1346. $field = array(
  1347. 'type' => 'constant',
  1348. 'title' => $form_state['values']['field_title'],
  1349. 'field' => $form_state['values']['chado_field'],
  1350. 'required' => $form_state['values']['required'],
  1351. //'allowed values' => empty by default,
  1352. 'constant value' => $form_state['values']['constant_value'],
  1353. //'exposed' => 'true|false' If exposed, will give a select box first from allowed values if set, second from database if values not set.
  1354. );
  1355. } elseif ($form_state['values']['field_type'] == 'foreign key') {
  1356. $field = array(
  1357. 'type' => 'foreign key',
  1358. 'title' => $form_state['values']['field_title'],
  1359. 'field' => $form_state['values']['chado_field'],
  1360. 'foreign key' => $form_state['values']['foreign_record'],
  1361. 'required' => $form_state['values']['required'],
  1362. );
  1363. }
  1364. // Deal with any additional options
  1365. if ($form_state['values']['transform']) {
  1366. $field['regex']['pattern'] = $form_state['values']['pattern'];
  1367. $field['regex']['replace'] = $form_state['values']['replace'];
  1368. }
  1369. // if the record has changed...
  1370. $form_state['storage']['template_array'][$priority]['table'] = $form_state['values']['chado_table'];
  1371. if ($old_priority != $priority) {
  1372. $form_state['storage']['template_array'][$priority]['fields'][] = $field;
  1373. unset($form_state['storage']['template_array'][$old_priority]['fields'][$field_index]);
  1374. // if there are no fields left delete the old record
  1375. if (!$form_state['storage']['template_array'][$old_priority]['fields']) {
  1376. unset($form_state['storage']['template_array'][$old_priority]);
  1377. }
  1378. } else {
  1379. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $field;
  1380. }
  1381. // Save Template
  1382. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  1383. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1384. if ($success) {
  1385. drupal_set_message('Successfully Updated Field');
  1386. drupal_set_message('Template Saved.');
  1387. $path = explode('?',$form_state['storage']['referring URL']);
  1388. parse_str($path[1], $query);
  1389. $query['template_id'] = $form_state['storage']['template']->template_id;
  1390. drupal_goto($path[0], $query);
  1391. } else {
  1392. drupal_set_message('Unable to Save Template!', 'error');
  1393. watchdog('T_bulk_loader',
  1394. 'Unable to save bulk loader template: %template',
  1395. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1396. WATCHDOG_ERROR
  1397. );
  1398. }
  1399. } elseif ($form_state['values']['op'] == 'Cancel') {
  1400. $path = explode('?',$form_state['storage']['referring URL']);
  1401. parse_str($path[1], $query);
  1402. $query['template_id'] = $form_state['storage']['template']->template_id;
  1403. drupal_goto($path[0], $query);
  1404. }
  1405. }
  1406. }
  1407. //////////////////////////////////////////////////////////////////////////////////////
  1408. // AHAH Callbacks
  1409. //////////////////////////////////////////////////////////////////////////////////////
  1410. /**
  1411. * AHAH Function: Replace $form['add_fields'] in tripal_bulk_loader_add_template_field_form
  1412. *
  1413. * @return
  1414. * JSON Data printed to the screen
  1415. */
  1416. function tripal_bulk_loader_add_field_ahah () {
  1417. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  1418. $form_build_id = $_POST['form_build_id'];
  1419. $form = form_get_cache($form_build_id, $form_state);
  1420. $args = $form['#parameters'];
  1421. $form_id = array_shift($args);
  1422. $form_state['post'] = $form['#post'] = $_POST;
  1423. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  1424. $form_state['ahah_submission'] = TRUE;
  1425. $form['#programmed'] = $form['#redirect'] = FALSE;
  1426. drupal_process_form($form_id, $form, $form_state);
  1427. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  1428. $form_element = $form['add_fields'];
  1429. // Remove the wrapper so we don't double it up.
  1430. unset($form_element['#prefix'], $form_element['#suffix']);
  1431. $output = theme('status_messages');
  1432. $output .= drupal_render($form_element);
  1433. // Final rendering callback.
  1434. print drupal_json(array('status' => TRUE, 'data' => $output));
  1435. exit();
  1436. }
  1437. /**
  1438. * AHAH Function: Replace $form['edit_fields'] in tripal_bulk_loader_edit_template_field_form
  1439. *
  1440. * @return
  1441. * JSON Data printed to the screen
  1442. */
  1443. function tripal_bulk_loader_edit_field_ahah () {
  1444. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  1445. $form_build_id = $_POST['form_build_id'];
  1446. $form = form_get_cache($form_build_id, $form_state);
  1447. $args = $form['#parameters'];
  1448. $form_id = array_shift($args);
  1449. $form_state['post'] = $form['#post'] = $_POST;
  1450. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  1451. $form_state['ahah_submission'] = TRUE;
  1452. $form['#programmed'] = $form['#redirect'] = FALSE;
  1453. drupal_process_form($form_id, $form, $form_state);
  1454. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  1455. $form_element = $form['edit_fields'];
  1456. // Remove the wrapper so we don't double it up.
  1457. unset($form_element['#prefix'], $form_element['#suffix']);
  1458. $output = theme('status_messages');
  1459. $output .= drupal_render($form_element);
  1460. // Final rendering callback.
  1461. print drupal_json(array('status' => TRUE, 'data' => $output));
  1462. exit();
  1463. }