tripal_bulk_loader.admin.inc 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  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. 'new_priority' => array(
  127. '#type' => 'select',
  128. '#options' => range(1, sizeof($form_state['storage']['template'])),
  129. '#default_value' => $priority,
  130. ),
  131. 'old_priority' => array(
  132. '#type' => 'hidden',
  133. '#value' => $priority,
  134. ),
  135. 'id' => array(
  136. '#type' => 'hidden',
  137. '#value' => $priority,
  138. ),
  139. 'submit-add_field' => array(
  140. '#type' => 'submit',
  141. '#name' => (string)$priority,
  142. '#value' => 'Add Field',
  143. ),
  144. );
  145. foreach ($table_array['fields'] as $field_index => $field) {
  146. $form['fields']['fields-data'][$i] = array(
  147. 'record_id' => array(
  148. '#type' => 'item',
  149. '#value' => $table_array['record_id'],
  150. ),
  151. 'priority_hidden' => array(
  152. '#type' => 'hidden',
  153. '#value' => $priority,
  154. ),
  155. 'field_name' => array(
  156. '#type' => 'item',
  157. '#value' => $field['title'],
  158. ),
  159. 'chado_table_name' => array(
  160. '#type' => 'item',
  161. '#value' => $table_array['table'],
  162. ),
  163. 'chado_table_hidden' => array(
  164. '#type' => 'hidden',
  165. '#value' => $table_array['table'],
  166. ),
  167. 'chado_field_name' => array(
  168. '#type' => 'item',
  169. '#value' => $field['field'],
  170. ),
  171. 'sheet_name' => array(
  172. '#type' => 'item',
  173. '#value' => $field['spreadsheet sheet'],
  174. ),
  175. 'column_num' => array(
  176. '#type' => 'item',
  177. '#value' => $field['spreadsheet column'],
  178. ),
  179. 'constant_value' => array(
  180. '#type' => 'item',
  181. '#value' => $field['constant value'],
  182. ),
  183. 'field_index' => array(
  184. '#type' => 'hidden',
  185. '#value' => $field_index
  186. ),
  187. 'foreign_record_id' => array(
  188. '#type' => 'item',
  189. '#value' => $field['foreign key'],
  190. ),
  191. 'edit_submit' => array(
  192. '#type' => 'submit',
  193. '#name' => (string)$i,
  194. '#value' => "Edit Field",
  195. ),
  196. 'delete_submit' => array(
  197. '#type' => 'submit',
  198. '#name' => (string)$i,
  199. '#value' => "Delete Field",
  200. ),
  201. );
  202. $i++;
  203. }
  204. }
  205. $form['fields']['total_fields'] = array(
  206. '#type' => 'item',
  207. '#value' => $i,
  208. );
  209. }
  210. if ($form['records']['no_records']['#value']) {
  211. $form['records']['description'] = array(
  212. '#type' => 'item',
  213. '#value' => 'There are currently no records.',
  214. );
  215. unset($form['records']['submit-reorder']);
  216. $form['fields']['description'] = array(
  217. '#type' => 'item',
  218. '#value' => 'There are currently no fields.',
  219. );
  220. }
  221. $mode_title = (preg_match('/create/',$mode)) ? 'Create Template' : 'Edit Template';
  222. $value = ($form_state['storage']['template_id'])? 'Save Template' : $mode_title;
  223. $form['submit'] = array(
  224. '#type' => 'submit',
  225. '#value' => $value,
  226. '#weight' => 4,
  227. );
  228. return $form;
  229. }
  230. /**
  231. *
  232. */
  233. function tripal_bulk_loader_modify_template_base_form_submit($form, &$form_state){
  234. $form_state['rebuild'] = TRUE;
  235. if ($form_state['storage']['template_id']) {
  236. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  237. $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
  238. $form_state['storage']['template'] = unserialize($result->template_array);
  239. }
  240. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  241. switch ($op) {
  242. // Initialize after template is chosen ----------------------------------------
  243. case 'Edit Template':
  244. $form_state['storage']['template_id'] = $form_state['values']['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. $form_state['storage']['template_name'] = $result->name;
  249. $form_state['storage']['record2priority'] = array();
  250. foreach ($form_state['storage']['template'] as $priority => $record_array) {
  251. if (!is_array($record_array)) { continue; }
  252. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  253. }
  254. break;
  255. case 'Create Template':
  256. $record = array(
  257. 'name' => $form_state['values']['new_template_name'],
  258. 'template_array' => array(),
  259. );
  260. drupal_write_record('tripal_bulk_loader_template', $record);
  261. $form_state['storage']['template_id'] = $record['template_id'];
  262. $form_state['storage']['template_name'] = $record['name'];
  263. $form_state['storage']['template'] = array();
  264. break;
  265. // Save Reordered Records -----------------------------------------------------
  266. case 'Save Order':
  267. $new_template = $form_state['storage']['template'];
  268. // unset old elements
  269. $form_state['storage']['record2priority'] = array();
  270. foreach ($new_template as $priority => $record_array) {
  271. if (preg_match('/\d+/', $priority)) { unset($new_template[$priority]); }
  272. }
  273. //set elements in new order
  274. foreach ($form_state['values']['records-data'] as $item) {
  275. $new_template[$item['new_priority']] = $form_state['storage']['template'][$item['old_priority']];
  276. $record_name = $new_template[$item['new_priority']]['record_id'];
  277. $form_state['storage']['record2priority'][$record_name] = $item['new_priority'];
  278. }
  279. ksort($new_template);
  280. $form_state['storage']['template'] = $new_template;
  281. break;
  282. case 'New Record/Field':
  283. $query = array(
  284. 'template_id'=> $form_state['storage']['template_id'],
  285. 'record_id' => 'NEW',
  286. );
  287. drupal_goto('admin/tripal/tripal_bulk_loader_template/add_field', $query);
  288. break;
  289. case 'Add Field':
  290. $query = array(
  291. 'template_id'=> $form_state['storage']['template_id'],
  292. 'record_id' => $form_state['clicked_button']['#name'],
  293. );
  294. drupal_goto('admin/tripal/tripal_bulk_loader_template/add_field', $query);
  295. break;
  296. case 'Edit Field':
  297. $field_data_index = $form_state['clicked_button']['#name'];
  298. $query = array(
  299. 'template_id'=> $form_state['storage']['template_id'],
  300. 'record_id' => $form_state['values']['fields-data'][$field_data_index]['priority_hidden'],
  301. 'field_index' => $form_state['values']['fields-data'][$field_data_index]['field_index'],
  302. );
  303. drupal_goto('admin/tripal/tripal_bulk_loader_template/edit_field', $query);
  304. break;
  305. case 'Delete Field':
  306. $field_data = $form_state['values']['fields-data'][$form_state['clicked_button']['#name']];
  307. $priority = $field_data['priority_hidden'];
  308. $field_key = $field_data['field_index'];
  309. unset($form_state['storage']['template'][$priority]['fields'][$field_key]);
  310. if (!$form_state['storage']['template'][$priority]['fields']) {
  311. unset($form_state['storage']['record2priority'][$form_state['storage']['template'][$priority]['record_id']]);
  312. unset($form_state['storage']['template'][$priority]);
  313. }
  314. drupal_set_message('Deleted Field from Template.');
  315. break;
  316. } //end of switch
  317. // Save Template
  318. $record = array(
  319. 'template_id' => $form_state['storage']['template_id'],
  320. 'template_array' => serialize($form_state['storage']['template'])
  321. );
  322. drupal_write_record('tripal_bulk_loader_template', $record, array('template_id'));
  323. drupal_set_message('Template Saved.');
  324. }
  325. //////////////////////////////////////////////////////////////////////////////////////
  326. // Delete Template
  327. //////////////////////////////////////////////////////////////////////////////////////
  328. function tripal_bulk_loader_delete_template_base_form () {
  329. $form = array();
  330. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  331. $resource = db_query($sql);
  332. $templates = array();
  333. $templates[''] = 'Select a Template';
  334. while ($r = db_fetch_object($resource)) {
  335. $templates[$r->template_id] = $r->name;
  336. }
  337. $form['template_name'] = array(
  338. '#title' => t('Template'),
  339. '#description' => t('Please select the template you would like to delete.'),
  340. '#type' => 'select',
  341. '#options' => $templates,
  342. '#weight' => 0,
  343. '#required' => TRUE,
  344. );
  345. $form['submit'] = array(
  346. '#type' => 'submit',
  347. '#value' => 'Delete Template',
  348. );
  349. return $form;
  350. }
  351. function tripal_bulk_loader_delete_template_base_form_submit ($form, &$form_state) {
  352. $sql = "DELETE FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  353. db_query($sql, $form_state['values']['template_name']);
  354. }
  355. function tripal_bulk_loader_import_export_template_form ($form_state = NULL, $mode) {
  356. $form = array();
  357. $form['mode'] = array(
  358. '#type' => 'hidden',
  359. '#value' => $mode,
  360. );
  361. if (preg_match('/import/', $mode)) {
  362. $form['new_template_name'] = array(
  363. '#type' => 'textfield',
  364. '#title' => 'Template Name',
  365. '#weight' => 1,
  366. );
  367. } elseif (preg_match('/export/', $mode)) {
  368. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  369. $resource = db_query($sql);
  370. $templates = array();
  371. $templates[''] = 'Select a Template';
  372. while ($r = db_fetch_object($resource)) {
  373. $templates[$r->template_id] = $r->name;
  374. }
  375. $form['template_id'] = array(
  376. '#title' => t('Template'),
  377. '#description' => t('Please select the template you would like to edit.'),
  378. '#type' => 'select',
  379. '#options' => $templates,
  380. '#default_value' => $form_state['storage']['template_id'],
  381. '#weight' => 0,
  382. '#required' => TRUE,
  383. '#weight' => 1,
  384. );
  385. }
  386. $form['template_array'] = array(
  387. '#type' => 'textarea',
  388. '#title' => 'Template Array',
  389. '#default_value' => $form_state['storage']['template_array'],
  390. '#weight' => 2,
  391. );
  392. $form['submit'] = array(
  393. '#type' => 'submit',
  394. '#value' => 'Submit',
  395. '#weight' => 10,
  396. );
  397. return $form;
  398. }
  399. function tripal_bulk_loader_import_export_template_form_submit ($form, &$form_state) {
  400. switch ($form_state['values']['mode']) {
  401. case 'export':
  402. $record = db_fetch_object(db_query("SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d", $form_state['values']['template_id']));
  403. $form_state['storage']['template_array'] = $record->template_array;
  404. $form_state['storage']['template_id'] = $form_state['values']['template_id'];
  405. dpm($record, 'record');
  406. break;
  407. case 'import':
  408. $record = array(
  409. 'name' => $form_state['values']['new_template_name'],
  410. 'template_array' => $form_state['values']['template_array'],
  411. );
  412. drupal_write_record('tripal_bulk_loader_template', $record);
  413. if ($record->template_id) {
  414. drupal_set_message('Successfully imported Tripal Bulk Loader Template.');
  415. }
  416. break;
  417. }
  418. }
  419. //////////////////////////////////////////////////////////////////////////////////////
  420. // Add/Edit Field Forms (meant to be returned as part of a larger form)
  421. //////////////////////////////////////////////////////////////////////////////////////
  422. /**
  423. * Add Field Form
  424. *
  425. * This form meant to be part of a larger form. Currently it is part of the following:
  426. * - tripal_bulk_loader_add_template_base_form
  427. * - tripal_bulk_loader_edit_template_base_form
  428. *
  429. * @param $form_state
  430. * The $form_state from the parent form
  431. * @return
  432. * The parent form with the edit field fieldset added
  433. *
  434. * Usage:
  435. * @code
  436. $form = array_merge($form, tripal_bulk_loader_add_template_field_form($form_state));
  437. * @endcode
  438. */
  439. function tripal_bulk_loader_add_template_field_form (&$form_state = NULL) {
  440. $form = array();
  441. $form['#cache'] = TRUE; // Make sure the form is cached.
  442. // get template id from path
  443. $template_id = ($_GET['template_id']) ? $_GET['template_id'] : $form_state['values']['template_id'];
  444. // if there is no template supplied don't return rest of form
  445. if (!$template_id) {
  446. return $form;
  447. }
  448. // Pre-set Variables needed for form proper------------------------------------------
  449. // If this is the first load of the form (no form state) we need to initialize some variables
  450. if (!$form_state['storage']['template']) {
  451. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  452. $template = db_fetch_object(db_query($sql, $template_id));
  453. $form_state['storage']['template_array'] = unserialize($template->template_array);
  454. $form_state['storage']['template'] = $template;
  455. $form_state['storage']['record2priority'] = array();
  456. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  457. if (!is_array($record_array)) { continue; }
  458. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  459. }
  460. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  461. } else {
  462. $template = $form_state['storage']['template'];
  463. }
  464. $field_type = ($form_state['values']['field_type'])? $form_state['values']['field_type'] : 'table field';
  465. // Tables and default table
  466. $tables = tripal_core_get_chado_tables();
  467. if ($form_state['values']) {
  468. if (!preg_match('/^'.current($tables).'$/', $form_state['values']['chado_table'])) {
  469. $table = $form_state['values']['chado_table'];
  470. } elseif ($form_state['values']['record_name']) {
  471. $record_name = $form_state['values']['record_name'];
  472. $priority = $form_state['storage']['record2priority'][$record_name];
  473. $table = $form_state['storage']['template_array'][$priority]['table'];
  474. } else {
  475. $priority = $form_state['values']['field_group'];
  476. $table = $form_state['storage']['template_array'][$priority]['table'];
  477. }
  478. }
  479. if (!$table) {
  480. $table = reset($tables);
  481. }
  482. // get the record_id from the path
  483. if ($_GET['record_id']) {
  484. $form_state['values']['field_group'] = $_GET['record_id'];
  485. if (preg_match('/\d+/', $_GET['record_id'])) {
  486. $priority = $form_state['values']['field_group'];
  487. $table = $form_state['storage']['template_array'][$priority]['table'];
  488. }
  489. }
  490. // Fields and foreign key mappings
  491. $chado_fields = array();
  492. $fk_options = array();
  493. $fk_options['NULL'] = 'None';
  494. $table_description = module_invoke_all('chado_'.$table.'_schema');
  495. if ($field_type == 'foreign key') {
  496. $foreign_field2table = array();
  497. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  498. foreach ($key_array['columns'] as $left_field => $right_field) {
  499. $chado_fields[$left_field] = $left_field;
  500. $foreign_field2table[$left_field] = $key_table;
  501. }
  502. }
  503. reset($chado_fields);
  504. // set default field
  505. if (empty($chado_fields)) {
  506. $field = NULL;
  507. } elseif ($chado_fields[$form_state['values']['chado_field']]) {
  508. $field = $form_state['values']['chado_field'];
  509. } else {
  510. $field = current($chado_fields);
  511. }
  512. // Foreign key options
  513. $foreign_table = $foreign_field2table[$field];
  514. if ($foreign_table) {
  515. foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) {
  516. if (preg_match('/^'.$foreign_table.'$/', $form_state['storage']['template_array'][$priority]['table'])) {
  517. $fk_options[$record_name] = $record_name;
  518. }
  519. }
  520. }
  521. } else {
  522. foreach($table_description['fields'] as $field_name => $field_array) {
  523. $chado_fields[$field_name] = $field_name;
  524. }
  525. }
  526. $variables = array(
  527. 'form_state' => $form_state,
  528. 'tables' => $tables,
  529. 'default table' => $table,
  530. 'fields' => $chado_fields,
  531. 'default_field' => $field,
  532. 'priority' => $priority,
  533. 'record_name' => $record_name,
  534. 'table description' => $table_description,
  535. 'foreign key options' => $fk_options,
  536. 'foreign field=>table' => $foreign_field2table,
  537. 'foreign table' => $foreign_table,
  538. );
  539. //dpm($variables, 'variables');
  540. // Start of Form Proper--------------------------------------------------------------
  541. $form['template_name'] = array(
  542. '#type' => 'item',
  543. '#title' => 'Template',
  544. '#value' => $template->name,
  545. );
  546. $form['template_id'] = array(
  547. '#type' => 'hidden',
  548. '#value' => $template_id,
  549. );
  550. $form['add_fields'] = array(
  551. '#type' => 'fieldset',
  552. '#prefix' => '<div id="tripal_bulk_loader_template-add_field">',
  553. '#suffix' => '</div>',
  554. );
  555. $form['add_fields']['field_type'] = array(
  556. '#type' => 'radios',
  557. '#title' => t('Type of Field'),
  558. '#options' => array(
  559. 'table field' => t('Spreadsheet: Fields which maps to a Spreadsheet Column'),
  560. 'constant' => t('Constant: Field which remains Constant throughout the Spreadsheet'),
  561. 'foreign key' => t('Foreign Key: Fields which map to a record in another table'),
  562. ),
  563. '#required' => TRUE,
  564. '#default_value' => $field_type,
  565. '#ahah' => array(
  566. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  567. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  568. 'effect' => 'fade'
  569. ),
  570. );
  571. // check template array for records then add one more
  572. if (!$form_state['storage']['record2priority']) {
  573. $groups = array();
  574. } else {
  575. $groups = array_flip($form_state['storage']['record2priority']);
  576. }
  577. $groups['NONE'] = 'Select a Record';
  578. $groups['NEW'] = 'New Record';
  579. $form['add_fields']['field_group'] = array(
  580. '#type' => 'select',
  581. '#title' => 'Record',
  582. '#description' => 'This is used to group a set of fields together allowing '
  583. .'multiple records to be inserted into the same table per line of the spreadsheet',
  584. '#options' => $groups,
  585. '#default_value' => (preg_match('/\w+.*/',$form_state['values']['field_group'])) ? $form_state['values']['field_group'] : 'NONE',
  586. '#ahah' => array(
  587. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  588. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  589. 'effect' => 'fade'
  590. ),
  591. '#required' => TRUE,
  592. );
  593. $form['add_fields']['record_name'] = array(
  594. '#type' => (preg_match('/NEW/', $form_state['values']['field_group'])) ? 'textfield' : 'hidden',
  595. '#title' => 'Unique Record Name',
  596. '#prefix' => '<div id="tripal_bulk_loader_template-add_record">',
  597. '#suffix' => '</div>',
  598. );
  599. $form['add_fields']['field_title'] = array(
  600. '#type' => 'textfield',
  601. '#title' => t('Human-readable Title for Field'),
  602. '#default_value' => $form_state['values']['field_title'],
  603. );
  604. // Spreadsheet column
  605. $form['add_fields']['columns'] = array(
  606. '#type' => 'fieldset',
  607. '#title' => t('Spreadsheet Column'),
  608. '#collapsible' => TRUE,
  609. '#collapsed' => ($field_type == 'table field')? FALSE : TRUE,
  610. );
  611. $form['add_fields']['columns']['sheet_name'] = array(
  612. '#type' => 'textfield',
  613. '#title' => t('Worksheet'),
  614. '#description' => t('Specify the name of the worksheet.'),
  615. '#size' => 5,
  616. '#default_value' => ($form_state['values']['sheet_name'])? $form_state['values']['sheet_name'] : 'Sheet1',
  617. );
  618. $form['add_fields']['columns']['column_number'] = array(
  619. '#type' => 'textfield',
  620. '#title' => t('Column'),
  621. '#description' => t('Specify the column in the spreadsheet that this field maps to where the first column is 1.'),
  622. '#size' => 5,
  623. '#default_value' => $form_state['values']['column_number'],
  624. );
  625. // Global Value
  626. $form['add_fields']['constant'] = array(
  627. '#type' => 'fieldset',
  628. '#title' => t('Constant'),
  629. '#collapsible' => TRUE,
  630. '#collapsed' => ($field_type == 'constant')? FALSE : TRUE,
  631. );
  632. $form['add_fields']['constant']['constant_value'] = array(
  633. '#type' => 'textfield',
  634. '#title' => t('Constant Value'),
  635. '#description' => t('Specify the value you wish this field to have regardless of spreadsheet data.'),
  636. '#default_value' => $form_state['values']['constant_value']
  637. );
  638. // Foreign Key
  639. $form['add_fields']['foreign_key'] = array(
  640. '#type' => 'fieldset',
  641. '#title' => 'Foreign Key',
  642. '#collapsible' => TRUE,
  643. '#collapsed' => ($field_type == 'foreign key')? FALSE : TRUE,
  644. );
  645. $form['add_fields']['foreign_key']['foreign_record'] = array(
  646. '#type' => 'select',
  647. '#title' => 'Record to refer to',
  648. '#descripion' => 'Select the record that this foreign key shouold refer to. The record needs to already exist and be of the correct table.',
  649. '#options' => $fk_options,
  650. );
  651. // Chado Field
  652. $form['add_fields']['chado'] = array(
  653. '#type' => 'fieldset',
  654. '#title' => t('Chado Field/Column Details'),
  655. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  656. );
  657. $form['add_fields']['chado']['chado_table'] = array(
  658. '#type' => 'select',
  659. '#title' => t('Chado Table'),
  660. '#options' => $tables,
  661. '#default_value' => $table,
  662. '#ahah' => array(
  663. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  664. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  665. 'effect' => 'fade'
  666. ),
  667. );
  668. $form['add_fields']['chado']['chado_field'] = array(
  669. '#type' => 'select',
  670. '#title' => t('Chado Field/Column'),
  671. '#options' => $chado_fields,
  672. '#default_value' => $form_state['values']['chado_field'],
  673. '#ahah' => array(
  674. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  675. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  676. 'effect' => 'fade'
  677. ),
  678. );
  679. $form['add_fields']['submit-add_field'] = array(
  680. '#type' => 'submit',
  681. '#value' => 'Add Field'
  682. );
  683. $form['add_fields']['submit-cancel'] = array(
  684. '#type' => 'submit',
  685. '#value' => 'Cancel'
  686. );
  687. return $form;
  688. }
  689. /**
  690. * Add Field Submit
  691. *
  692. * @param $form_state
  693. * The $form_state from the parent submit
  694. * @return
  695. * The parent form_state with any changes made
  696. *
  697. */
  698. function tripal_bulk_loader_add_template_field_form_submit ($form, &$form_state) {
  699. if (!$form_state['ahah_submission']) {
  700. if ($form_state['values']['op'] == 'Add Field') {
  701. $template = $form_state['storage']['template_array'];
  702. // If new record
  703. if (preg_match('/NEW/',$form_state['values']['field_group'])) {
  704. $record_name = $form_state['values']['record_name'];
  705. $priority = sizeof($form_state['storage']['template_array']) + 1;
  706. $record2priority[$record_name] = $priority;
  707. $template[$priority]['table'] = $form_state['values']['chado_table'];
  708. $template[$priority]['record_id'] = $record_name;
  709. } else {
  710. $priority = $form_state['values']['field_group'];
  711. $record_name = $record2priority[$priority];
  712. }
  713. // Add field to template array
  714. if ($form_state['values']['field_type'] == 'table field') {
  715. $template[$priority]['fields'][] = array(
  716. 'type' => 'table field',
  717. 'title' => $form_state['values']['field_title'],
  718. 'field' => $form_state['values']['chado_field'],
  719. //'required' => <true|false>,
  720. //'allowed values' => empty by default,
  721. 'spreadsheet sheet' => $form_state['values']['sheet_name'],
  722. 'spreadsheet column' => $form_state['values']['column_number'],
  723. //'exposed' => 'true|false' If exposed, will give a select box first from allowed values if set, second from database if values not set.
  724. //'mapping' => array(
  725. // 'from' => 'to'
  726. // '/from re/' => 'to'
  727. //),
  728. );
  729. } elseif ($form_state['values']['field_type'] == 'constant') {
  730. $template[$priority]['fields'][] = array(
  731. 'type' => 'constant',
  732. 'title' => $form_state['values']['field_title'],
  733. 'field' => $form_state['values']['chado_field'],
  734. //'required' => <true|false>,
  735. //'allowed values' => empty by default,
  736. 'constant value' => $form_state['values']['constant_value'],
  737. //'exposed' => 'true|false' If exposed, will give a select box first from allowed values if set, second from database if values not set.
  738. );
  739. } elseif ($form_state['values']['field_type'] == 'foreign key') {
  740. $template[$priority]['fields'][] = array(
  741. 'type' => 'foreign key',
  742. 'title' => $form_state['values']['field_title'],
  743. 'field' => $form_state['values']['chado_field'],
  744. 'foreign key' => $form_state['values']['foreign_record'],
  745. //'required' => <true|false>,
  746. );
  747. }
  748. // Save Template
  749. $form_state['storage']['template']->template_array = serialize($template);
  750. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  751. if ($success) {
  752. drupal_set_message('Successfully Added Field to Template');
  753. drupal_set_message('Template Saved.');
  754. $path = explode('?',$form_state['storage']['referring URL']);
  755. parse_str($path[1], $query);
  756. $query['template_id'] = $form_state['storage']['template']->template_id;
  757. drupal_goto($path[0], $query);
  758. } else {
  759. drupal_set_message('Unable to Save Template!', 'error');
  760. watchdog('T_bulk_loader',
  761. 'Unable to save bulk loader template: %template',
  762. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  763. WATCHDOG_ERROR
  764. );
  765. }
  766. } elseif ($form_state['values']['op'] == 'Cancel') {
  767. $path = explode('?',$form_state['storage']['referring URL']);
  768. parse_str($path[1], $query);
  769. $query['template_id'] = $form_state['storage']['template']->template_id;
  770. drupal_goto($path[0], $query);
  771. }
  772. }
  773. }
  774. /**
  775. * Edit Field Form
  776. *
  777. * @param $form_state
  778. * The $form_state from the parent form
  779. * @return
  780. * The parent form with the edit field fieldset added
  781. */
  782. function tripal_bulk_loader_edit_template_field_form (&$form_state = NULL) {
  783. $form = array();
  784. $form['#cache'] = TRUE; // Make sure the form is cached.
  785. // get template id from path
  786. $template_id = ($_GET['template_id']) ? $_GET['template_id'] : $form_state['values']['template_id'];
  787. // if there is no template supplied don't return rest of form
  788. if (!$template_id) {
  789. return $form;
  790. }
  791. // Pre-set Variables needed for form proper------------------------------------------
  792. // If this is the first load of the form (no form state) we need to initialize some variables
  793. if (!$form_state['storage']['template']) {
  794. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  795. $template = db_fetch_object(db_query($sql, $template_id));
  796. $form_state['storage']['template_array'] = unserialize($template->template_array);
  797. $form_state['storage']['template'] = $template;
  798. $form_state['storage']['record2priority'] = array();
  799. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  800. if (!is_array($record_array)) { continue; }
  801. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  802. }
  803. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  804. } else {
  805. $template = $form_state['storage']['template'];
  806. }
  807. // get the field from the path
  808. if (!($_GET['record_id']===NULL || $_GET['field_index']===NULL)) {
  809. $priority = $_GET['record_id'];
  810. $field_index = $_GET['field_index'];
  811. $template_field = $form_state['storage']['template_array'][$priority]['fields'][$field_index];
  812. $form_state['storage']['original_field'] = $template_field;
  813. $form_state['storage']['original_field']['priority'] = $priority;
  814. $form_state['storage']['original_field']['field_index'] = $field_index;
  815. }
  816. $field_type = ($form_state['values']['field_type'])? $form_state['values']['field_type'] : $template_field['type'];
  817. // Tables and default table
  818. $tables = tripal_core_get_chado_tables();
  819. if ($form_state['values']) {
  820. $table = $form_state['values']['chado_table'];
  821. } else {
  822. $table = $form_state['storage']['template_array'][$priority]['table'];
  823. }
  824. // Fields and foreign key mappings
  825. $chado_fields = array();
  826. $fk_options = array();
  827. $fk_options['NULL'] = 'None';
  828. $table_description = module_invoke_all('chado_'.$table.'_schema');
  829. if ($field_type == 'foreign key') {
  830. $foreign_field2table = array();
  831. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  832. foreach ($key_array['columns'] as $left_field => $right_field) {
  833. $chado_fields[$left_field] = $left_field;
  834. $foreign_field2table[$left_field] = $key_table;
  835. }
  836. }
  837. reset($chado_fields);
  838. // set default field
  839. if (empty($chado_fields)) {
  840. $field = NULL;
  841. } elseif ($chado_fields[$form_state['values']['chado_field']]) {
  842. $field = $form_state['values']['chado_field'];
  843. } else {
  844. $field = current($chado_fields);
  845. }
  846. // Foreign key options
  847. $foreign_table = $foreign_field2table[$field];
  848. if ($foreign_table) {
  849. foreach ($form_state['storage']['record2priority'] as $record_nameT => $priorityT ) {
  850. if (preg_match('/^'.$foreign_table.'$/', $form_state['storage']['template_array'][$priorityT]['table'])) {
  851. $fk_options[$record_nameT] = $record_nameT;
  852. }
  853. }
  854. }
  855. } else {
  856. foreach($table_description['fields'] as $field_name => $field_array) {
  857. $chado_fields[$field_name] = $field_name;
  858. }
  859. }
  860. // dpm(array( 'tables' => $tables, 'default table' => $table, 'record name' => $record_name, 'priority' => $priority,
  861. // 'fields' => $chad_fields, 'default field' => $field, 'foreign field=>table' => $foreign_field2table,
  862. // 'table desc' => $table_description, 'foreign record options' => $fk_options, 'foreign table' => $foreign_table
  863. // ), 'Variables');
  864. // Start of Form Proper--------------------------------------------------------------
  865. $form['template_name'] = array(
  866. '#type' => 'item',
  867. '#title' => 'Template',
  868. '#value' => $template->name,
  869. );
  870. $form['template_id'] = array(
  871. '#type' => 'hidden',
  872. '#value' => $template_id,
  873. );
  874. $form['edit_fields'] = array(
  875. '#type' => 'fieldset',
  876. '#prefix' => '<div id="tripal_bulk_loader_template-edit_field">',
  877. '#suffix' => '</div>',
  878. );
  879. $form['edit_fields']['field_type'] = array(
  880. '#type' => 'radios',
  881. '#title' => t('Type of Field'),
  882. '#options' => array(
  883. 'table field' => t('Spreadsheet: Fields which maps to a Spreadsheet Column'),
  884. 'constant' => t('Constant: Field which remains Constant throughout the Spreadsheet'),
  885. 'foreign key' => t('Foreign Key: Fields which map to a record in another table'),
  886. ),
  887. '#required' => TRUE,
  888. '#default_value' => $field_type,
  889. '#ahah' => array(
  890. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  891. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  892. 'effect' => 'fade'
  893. ),
  894. );
  895. // check template array for records then edit one more
  896. if (!$form_state['storage']['record2priority']) {
  897. $groups = array();
  898. } else {
  899. $groups = array_flip($form_state['storage']['record2priority']);
  900. }
  901. $groups['NONE'] = 'Select a Record';
  902. $groups['NEW'] = 'New Record';
  903. $form['edit_fields']['field_group'] = array(
  904. '#type' => 'select',
  905. '#title' => 'Record',
  906. '#description' => 'This is used to group a set of fields together allowing '
  907. .'multiple records to be inserted into the same table per line of the spreadsheet',
  908. '#options' => $groups,
  909. '#default_value' => (preg_match('/(\d+|\w+)/',$form_state['values']['field_group'])) ? $form_state['values']['field_group'] : $priority,
  910. '#ahah' => array(
  911. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  912. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  913. 'effect' => 'fade'
  914. ),
  915. '#required' => TRUE,
  916. );
  917. $form['edit_fields']['record_name'] = array(
  918. '#type' => (preg_match('/NEW/', $form_state['values']['field_group'])) ? 'textfield' : 'hidden',
  919. '#title' => 'Unique Record Name',
  920. '#prefix' => '<div id="tripal_bulk_loader_template-edit_record">',
  921. '#suffix' => '</div>',
  922. );
  923. $form['edit_fields']['field_title'] = array(
  924. '#type' => 'textfield',
  925. '#title' => t('Human-readable Title for Field'),
  926. '#default_value' => ($form_state['values']['field_title']) ? $form_state['values']['field_title'] : $template_field['title'],
  927. );
  928. // Spreadsheet column
  929. $form['edit_fields']['columns'] = array(
  930. '#type' => 'fieldset',
  931. '#title' => t('Spreadsheet Column'),
  932. '#collapsible' => TRUE,
  933. '#collapsed' => ($field_type == 'table field')? FALSE : TRUE,
  934. );
  935. $form['edit_fields']['columns']['sheet_name'] = array(
  936. '#type' => 'textfield',
  937. '#title' => t('Worksheet'),
  938. '#description' => t('Specify the name of the worksheet.'),
  939. '#size' => 5,
  940. '#default_value' => ($form_state['values']['sheet_name'])? $form_state['values']['sheet_name'] : $template_field['spreadsheet sheet'],
  941. );
  942. $form['edit_fields']['columns']['column_number'] = array(
  943. '#type' => 'textfield',
  944. '#title' => t('Column'),
  945. '#description' => t('Specify the column in the spreadsheet that this field maps to where the first column is 1.'),
  946. '#size' => 5,
  947. '#default_value' => ($form_state['values']['column_number']) ? $form_state['values']['column_number'] : $template_field['spreadsheet column'],
  948. );
  949. // Global Value
  950. $form['edit_fields']['constant'] = array(
  951. '#type' => 'fieldset',
  952. '#title' => t('Constant'),
  953. '#collapsible' => TRUE,
  954. '#collapsed' => ($field_type == 'constant')? FALSE : TRUE,
  955. );
  956. $form['edit_fields']['constant']['constant_value'] = array(
  957. '#type' => 'textfield',
  958. '#title' => t('Constant Value'),
  959. '#description' => t('Specify the value you wish this field to have regardless of spreadsheet data.'),
  960. '#default_value' => ($form_state['values']['constant_value']) ? $form_state['values']['constant_value'] : $template_field['constant value'],
  961. );
  962. // Foreign Key
  963. $form['edit_fields']['foreign_key'] = array(
  964. '#type' => 'fieldset',
  965. '#title' => 'Foreign Key',
  966. '#collapsible' => TRUE,
  967. '#collapsed' => ($field_type == 'foreign key')? FALSE : TRUE,
  968. );
  969. $form['edit_fields']['foreign_key']['foreign_record'] = array(
  970. '#type' => 'select',
  971. '#title' => 'Record to refer to',
  972. '#descripion' => 'Select the record that this foreign key shouold refer to. The record needs to already exist and be of the correct table.',
  973. '#options' => $fk_options,
  974. );
  975. // Chado Field
  976. $form['edit_fields']['chado'] = array(
  977. '#type' => 'fieldset',
  978. '#title' => t('Chado Field/Column Details'),
  979. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  980. );
  981. $form['edit_fields']['chado']['chado_table'] = array(
  982. '#type' => 'select',
  983. '#title' => t('Chado Table'),
  984. '#options' => $tables,
  985. '#default_value' => $table,
  986. '#ahah' => array(
  987. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  988. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  989. 'effect' => 'fade'
  990. ),
  991. );
  992. $form['edit_fields']['chado']['chado_field'] = array(
  993. '#type' => 'select',
  994. '#title' => t('Chado Field/Column'),
  995. '#options' => $chado_fields,
  996. '#default_value' => ($form_state['values']['chado_field']) ? $form_state['values']['chado_field'] : $template_field['field'],
  997. '#ahah' => array(
  998. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  999. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1000. 'effect' => 'fade'
  1001. ),
  1002. );
  1003. $form['edit_fields']['submit-edit_field'] = array(
  1004. '#type' => 'submit',
  1005. '#value' => 'Edit Field'
  1006. );
  1007. $form['edit_fields']['submit-cancel'] = array(
  1008. '#type' => 'submit',
  1009. '#value' => 'Cancel'
  1010. );
  1011. return $form;
  1012. }
  1013. /**
  1014. * Edit Field Form Submit
  1015. *
  1016. * @param $form_state
  1017. * The $form_state from the parent form
  1018. * @return
  1019. * The parent form with the edit field fieldset added
  1020. */
  1021. function tripal_bulk_loader_edit_template_field_form_submit ($form, &$form_state) {
  1022. if (!$form_state['ahah_submission']) {
  1023. if ($form_state['values']['op'] == 'Edit Field') {
  1024. // If new record
  1025. if (preg_match('/NEW/',$form_state['values']['field_group'])) {
  1026. // add new record
  1027. $record_name = $form_state['values']['record_name'];
  1028. $priority = sizeof($form_state['storage']['template_array']) + 1;
  1029. $old_priority = $form_state['storage']['original_field']['priority'];
  1030. $field_index = $form_state['storage']['original_field']['field_index'];
  1031. $form_state['storage']['record2priority'][$record_name] = $priority;
  1032. $form_state['storage']['template_array'][$priority]['table'] = $form_state['values']['chado_table'];
  1033. $form_state['storage']['template_array'][$priority]['record_id'] = $record_name;
  1034. } else {
  1035. $priority = $form_state['values']['field_group'];
  1036. $old_priority = $form_state['storage']['original_field']['priority'];
  1037. $field_index = $form_state['storage']['original_field']['field_index'];
  1038. $record_name = $form_state['storage']['record2priority'][$priority];
  1039. }
  1040. if ($form_state['values']['field_type'] == 'table field') {
  1041. $field = array(
  1042. 'type' => 'table field',
  1043. 'title' => $form_state['values']['field_title'],
  1044. 'field' => $form_state['values']['chado_field'],
  1045. //'required' => <true|false>,
  1046. //'allowed values' => empty by default,
  1047. 'spreadsheet sheet' => $form_state['values']['sheet_name'],
  1048. 'spreadsheet column' => $form_state['values']['column_number'],
  1049. //'exposed' => 'true|false' If exposed, will give a select box first from allowed values if set, second from database if values not set.
  1050. //'mapping' => array(
  1051. // 'from' => 'to'
  1052. // '/from re/' => 'to'
  1053. //),
  1054. );
  1055. } elseif ($form_state['values']['field_type'] == 'constant') {
  1056. $field = array(
  1057. 'type' => 'constant',
  1058. 'title' => $form_state['values']['field_title'],
  1059. 'field' => $form_state['values']['chado_field'],
  1060. //'required' => <true|false>,
  1061. //'allowed values' => empty by default,
  1062. 'constant value' => $form_state['values']['constant_value'],
  1063. //'exposed' => 'true|false' If exposed, will give a select box first from allowed values if set, second from database if values not set.
  1064. );
  1065. } elseif ($form_state['values']['field_type'] == 'foreign key') {
  1066. $field = array(
  1067. 'type' => 'foreign key',
  1068. 'title' => $form_state['values']['field_title'],
  1069. 'field' => $form_state['values']['chado_field'],
  1070. 'foreign key' => $form_state['values']['foreign_record'],
  1071. //'required' => <true|false>,
  1072. );
  1073. }
  1074. // if the record has changed...
  1075. $form_state['storage']['template_array'][$priority]['table'] = $form_state['values']['chado_table'];
  1076. if ($old_priority != $priority) {
  1077. $form_state['storage']['template_array'][$priority]['fields'][] = $field;
  1078. unset($form_state['storage']['template_array'][$old_priority]['fields'][$field_index]);
  1079. // if there are no fields left delete the old record
  1080. if (!$form_state['storage']['template_array'][$old_priority]['fields']) {
  1081. unset($form_state['storage']['template_array'][$old_priority]);
  1082. }
  1083. } else {
  1084. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $field;
  1085. }
  1086. // Save Template
  1087. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  1088. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1089. if ($success) {
  1090. drupal_set_message('Successfully Updated Field');
  1091. drupal_set_message('Template Saved.');
  1092. $path = explode('?',$form_state['storage']['referring URL']);
  1093. parse_str($path[1], $query);
  1094. $query['template_id'] = $form_state['storage']['template']->template_id;
  1095. drupal_goto($path[0], $query);
  1096. } else {
  1097. drupal_set_message('Unable to Save Template!', 'error');
  1098. watchdog('T_bulk_loader',
  1099. 'Unable to save bulk loader template: %template',
  1100. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1101. WATCHDOG_ERROR
  1102. );
  1103. }
  1104. } elseif ($form_state['values']['op'] == 'Cancel') {
  1105. $path = explode('?',$form_state['storage']['referring URL']);
  1106. parse_str($path[1], $query);
  1107. $query['template_id'] = $form_state['storage']['template']->template_id;
  1108. drupal_goto($path[0], $query);
  1109. }
  1110. }
  1111. }
  1112. //////////////////////////////////////////////////////////////////////////////////////
  1113. // AHAH Callbacks
  1114. //////////////////////////////////////////////////////////////////////////////////////
  1115. /**
  1116. * AHAH Function: Replace $form['add_fields'] in tripal_bulk_loader_add_template_field_form
  1117. *
  1118. * @return
  1119. * JSON Data printed to the screen
  1120. */
  1121. function tripal_bulk_loader_add_field_ahah () {
  1122. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  1123. $form_build_id = $_POST['form_build_id'];
  1124. $form = form_get_cache($form_build_id, $form_state);
  1125. $args = $form['#parameters'];
  1126. $form_id = array_shift($args);
  1127. $form_state['post'] = $form['#post'] = $_POST;
  1128. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  1129. $form_state['ahah_submission'] = TRUE;
  1130. $form['#programmed'] = $form['#redirect'] = FALSE;
  1131. drupal_process_form($form_id, $form, $form_state);
  1132. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  1133. $form_element = $form['add_fields'];
  1134. // Remove the wrapper so we don't double it up.
  1135. unset($form_element['#prefix'], $form_element['#suffix']);
  1136. $output = theme('status_messages');
  1137. $output .= drupal_render($form_element);
  1138. // Final rendering callback.
  1139. print drupal_json(array('status' => TRUE, 'data' => $output));
  1140. exit();
  1141. }
  1142. /**
  1143. * AHAH Function: Replace $form['edit_fields'] in tripal_bulk_loader_edit_template_field_form
  1144. *
  1145. * @return
  1146. * JSON Data printed to the screen
  1147. */
  1148. function tripal_bulk_loader_edit_field_ahah () {
  1149. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  1150. $form_build_id = $_POST['form_build_id'];
  1151. $form = form_get_cache($form_build_id, $form_state);
  1152. $args = $form['#parameters'];
  1153. $form_id = array_shift($args);
  1154. $form_state['post'] = $form['#post'] = $_POST;
  1155. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  1156. $form_state['ahah_submission'] = TRUE;
  1157. $form['#programmed'] = $form['#redirect'] = FALSE;
  1158. drupal_process_form($form_id, $form, $form_state);
  1159. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  1160. $form_element = $form['edit_fields'];
  1161. // Remove the wrapper so we don't double it up.
  1162. unset($form_element['#prefix'], $form_element['#suffix']);
  1163. $output = theme('status_messages');
  1164. $output .= drupal_render($form_element);
  1165. // Final rendering callback.
  1166. print drupal_json(array('status' => TRUE, 'data' => $output));
  1167. exit();
  1168. }