tripal_bulk_loader.admin.inc 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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. // Create Template
  18. //////////////////////////////////////////////////////////////////////////////////////
  19. /**
  20. * tripal_bulk_loader_admin_template_form
  21. */
  22. function tripal_bulk_loader_create_template_base_form (&$form_state = NULL) {
  23. // Basic Details--------------------------------------------------------------
  24. $form['bulk_loader'] = array(
  25. '#type' => 'fieldset',
  26. '#title' => t('Step 1: Basic Template Details'),
  27. );
  28. $modules = module_invoke_all('tripal_bulk_loader_supported_modules');
  29. $modules[''] = 'Select A Module';
  30. if ($form_state['storage']['base_table']) {
  31. $base_table = $form_state['storage']['base_table'];
  32. } else {
  33. $base_table = '';
  34. }
  35. $form['bulk_loader']['chado_module'] = array(
  36. '#title' => t('Chado Module'),
  37. '#description' => t('Please select the module for which you would like to create an importer'),
  38. '#type' => 'select',
  39. '#options' => $modules,
  40. '#default_value' => $base_table,
  41. '#weight' => 0,
  42. '#required' => TRUE
  43. );
  44. $form['bulk_loader']['template_name'] = array(
  45. '#type' => 'textfield',
  46. '#title' => t('Template Name'),
  47. '#default_value' => $form_state['storage']['template_name'],
  48. );
  49. if ($base_table) {
  50. // Add Fields---------------------------------------------------------------------
  51. $form = array_merge($form, tripal_bulk_loader_add_template_field_form($form_state));
  52. // List Current Fields------------------------------------------------------------
  53. $form['current_fields'] = array(
  54. '#type' => 'fieldset',
  55. '#title' => t('Step 3: Confirm Fields')
  56. );
  57. if ($form_state['storage']['template']) {
  58. $form['current_fields']['items'] = array(
  59. '#type' => 'item',
  60. '#value' => theme('tripal_bulk_loader_template', $form_state['storage']['template_id']),
  61. );
  62. } else {
  63. $form['current_fields']['items'] = array(
  64. '#type' => 'item',
  65. '#value' => t('There are currently no fields added to this template.'),
  66. );
  67. }
  68. // Submit-------------------------------------------------------------------------
  69. $form['submit-save'] = array(
  70. '#type' => 'submit',
  71. '#value' => 'Save Template'
  72. );
  73. } else {
  74. $form['submit-next_step'] = array(
  75. '#type' => 'submit',
  76. '#value' => 'Next Step'
  77. );
  78. } //end of if base details are set
  79. return $form;
  80. }
  81. /**
  82. * tripal_bulk_loader_admin_template_form_validate
  83. */
  84. function tripal_bulk_loader_create_template_base_form_validate($form, &$form_state){
  85. if(!$form_state['ahah_submission']) {
  86. if ($form_state['values']['op'] == 'Add Field') {
  87. if ($form_state['values']['type'] == 'column') {
  88. if (!$form_state['values']['column']) {
  89. form_set_error('column', 'Column is Required!');
  90. }
  91. if (!$form_state['values']['sheet']) {
  92. form_set_error('sheet', 'Worksheet Name is Required!');
  93. }
  94. } elseif ($form_state['values']['type'] == 'constant') {
  95. if (!$form_state['values']['constant_value']) {
  96. form_set_error('constant_value', 'Value of Constant is Required!');
  97. }
  98. }
  99. } elseif ($form_state['values']['op'] == 'Save Template') {
  100. if (!$form_state['storage']['template']) {
  101. form_set_error('', 'Tempalte must contain at least one field');
  102. }
  103. }
  104. }
  105. }
  106. /**
  107. * tripal_bulk_loader_admin_template_form_submit
  108. */
  109. function tripal_bulk_loader_create_template_base_form_submit($form, &$form_state){
  110. //dpm($form_state, 'form_state: submit');
  111. // AHAH Storage ----------------------------------------------------------
  112. $form_state['rebuild'] = TRUE;
  113. $form_state['storage']['base_table'] = $form_state['values']['chado_module'];
  114. $form_state['storage']['template_name'] = $form_state['values']['template_name'];
  115. if (!$form_state['ahah_submission']) {
  116. // When template is first defined
  117. if ($form_state['values']['op'] == 'Next Step') {
  118. $record = array(
  119. 'name' => $form_state['values']['template_name'],
  120. 'template_array' => ' '
  121. );
  122. drupal_write_record('tripal_bulk_loader_template', $record);
  123. $form_state['storage']['template_id'] = $record['template_id'];
  124. // Add Field to Template----------------------------------------------
  125. } elseif ($form_state['values']['op'] == 'Add Field') {
  126. $form_state = tripal_bulk_loader_add_template_field_form_submit($form_state);
  127. // Save Template ----------------------------------------------------
  128. }
  129. $record = array(
  130. 'name' => $form_state['values']['template_name'],
  131. 'template_array' => serialize($form_state['storage']['template'])
  132. );
  133. //Check if template exists
  134. $sql = "SELECT count(*) as count FROM tripal_bulk_loader_template WHERE name='%s'";
  135. if (db_result(db_query($sql, $form_state['values']['template_name']))) {
  136. // Update Previous
  137. drupal_write_record('tripal_bulk_loader_template', $record, array('name'));
  138. } else {
  139. // Insert New
  140. drupal_write_record('tripal_bulk_loader_template', $record);
  141. }
  142. } //end of if not ahah submission
  143. else {
  144. $form_state = tripal_bulk_loader_add_template_field_form_submit($form_state);
  145. }
  146. }
  147. //////////////////////////////////////////////////////////////////////////////////////
  148. // Edit Template
  149. //////////////////////////////////////////////////////////////////////////////////////
  150. /**
  151. *
  152. */
  153. function tripal_bulk_loader_edit_template_base_form($form_state = NULL){
  154. $form = array();
  155. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  156. $resource = db_query($sql);
  157. $templates = array();
  158. $templates[''] = 'Select a Template';
  159. while ($r = db_fetch_object($resource)) {
  160. $templates[$r->template_id] = $r->name;
  161. }
  162. $form['template_name'] = array(
  163. '#title' => t('Template'),
  164. '#description' => t('Please select the template you would like to edit.'),
  165. '#type' => 'select',
  166. '#options' => $templates,
  167. '#default_value' => $form_state['storage']['template_id'],
  168. '#weight' => 0,
  169. '#required' => TRUE,
  170. );
  171. $form['fields'] = array(
  172. '#type' => ($form_state['storage']['template_id'])? 'fieldset' : 'hidden',
  173. '#title' => t('Fields'),
  174. );
  175. if ($form_state['storage']['template']) {
  176. // List Current Fields -------------------------------------------------------------
  177. $i=1;
  178. foreach ($form_state['storage']['template'] as $table => $table_array) {
  179. if (!is_array($table_array)) { continue; }
  180. foreach ($table_array['field'] as $field_index => $field) {
  181. $form['fields']["field_name-$i"] = array(
  182. '#type' => 'item',
  183. '#value' => $field['title'],
  184. );
  185. $form['fields']["chado_table_name-$i"] = array(
  186. '#type' => 'item',
  187. '#value' => $table,
  188. );
  189. $form['fields']["chado_table_hidden-$i"] = array(
  190. '#type' => 'hidden',
  191. '#value' => $table,
  192. );
  193. $form['fields']["chado_field_name-$i"] = array(
  194. '#type' => 'item',
  195. '#value' => $field['field'],
  196. );
  197. $form['fields']["sheet_name-$i"] = array(
  198. '#type' => 'item',
  199. '#value' => $field['spreadsheet sheet'],
  200. );
  201. $form['fields']["column_num-$i"] = array(
  202. '#type' => 'item',
  203. '#value' => $field['spreadsheet column'],
  204. );
  205. $form['fields']["constant_value-$i"] = array(
  206. '#type' => 'item',
  207. '#value' => $field['constant value'],
  208. );
  209. $form['fields']["field_index-$i"] = array(
  210. '#type' => 'hidden',
  211. '#value' => $field_index
  212. );
  213. $form['fields']["edit-$i"] = array(
  214. '#type' => 'submit',
  215. '#value' => "Edit Field #$i",
  216. '#ahah' => array(
  217. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit/edit_fields_ahah',
  218. 'wrapper' => 'tripal_bulk_loader-edit_field',
  219. 'effect' => 'fade'
  220. )
  221. );
  222. $form['fields']["delete-$i"] = array(
  223. '#type' => 'submit',
  224. '#value' => "Delete Field #$i",
  225. );
  226. $i++;
  227. }
  228. }
  229. $form['fields']['total_fields'] = array(
  230. '#type' => 'item',
  231. '#value' => $i,
  232. );
  233. // Edit Field Form --------------------------------------------------------------------
  234. $form = array_merge($form, tripal_bulk_loader_edit_template_field_form($form_state));
  235. // Add Field Form ----------------------------------------------------------------------
  236. $form = array_merge($form, tripal_bulk_loader_add_template_field_form($form_state));
  237. $form['add_fields']['#title'] = 'Add Fields';
  238. }
  239. $value = ($form_state['storage']['template_id'])? 'Save Template' : 'Edit Template';
  240. $form['submit'] = array(
  241. '#type' => 'submit',
  242. '#value' => $value
  243. );
  244. return $form;
  245. }
  246. /**
  247. *
  248. */
  249. function tripal_bulk_loader_edit_template_base_form_submit($form, &$form_state){
  250. $form_state['rebuild'] = TRUE;
  251. $form_state['storage']['template_id'] = $form_state['values']['template_name'];
  252. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  253. $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
  254. $form_state['storage']['template'] = unserialize($result->template_array);
  255. $form_state['storage']['base_table'] = $form_state['storage']['template']['module'];
  256. if (!$form_state['ahah_submission']) {
  257. // Add Field to Template----------------------------------------------
  258. if (preg_match('/Add Field/', $form_state['values']['op'])) {
  259. $form_state = tripal_bulk_loader_add_template_field_form_submit($form_state);
  260. } elseif (preg_match('/Edit Field$/', $form_state['values']['op'])) {
  261. $form_state = tripal_bulk_loader_edit_template_field_form_submit($form_state);
  262. } elseif (preg_match('/Delete Field #(\d+)/', $form_state['values']['op'], $matches)) {
  263. $table = $form_state['values']["chado_table_hidden-".$matches[1]];
  264. $field_key = $form_state['values']["field_index-".$matches[1]];
  265. unset($form_state['storage']['template'][$table]['field'][$field_key]);
  266. if (!$form_state['storage']['template'][$table]['field']) {
  267. unset($form_state['storage']['template'][$table]);
  268. }
  269. }
  270. // Save Template ----------------------------------------------------
  271. $record = array(
  272. 'template_id' => $form_state['values']['template_name'],
  273. 'template_array' => serialize($form_state['storage']['template'])
  274. );
  275. drupal_write_record('tripal_bulk_loader_template', $record, array('template_id'));
  276. } //end of if not ahah submission
  277. else {
  278. //Add AHAH associated with Add Form
  279. $form_state = tripal_bulk_loader_add_template_field_form_submit($form_state);
  280. $form_state = tripal_bulk_loader_edit_template_field_form_submit($form_state);
  281. }
  282. }
  283. //////////////////////////////////////////////////////////////////////////////////////
  284. // Delete Template
  285. //////////////////////////////////////////////////////////////////////////////////////
  286. function tripal_bulk_loader_delete_template_base_form () {
  287. $form = array();
  288. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  289. $resource = db_query($sql);
  290. $templates = array();
  291. $templates[''] = 'Select a Template';
  292. while ($r = db_fetch_object($resource)) {
  293. $templates[$r->template_id] = $r->name;
  294. }
  295. $form['template_name'] = array(
  296. '#title' => t('Template'),
  297. '#description' => t('Please select the template you would like to edit.'),
  298. '#type' => 'select',
  299. '#options' => $templates,
  300. '#weight' => 0,
  301. '#required' => TRUE,
  302. );
  303. $form['submit'] = array(
  304. '#type' => 'submit',
  305. '#value' => 'Delete Template',
  306. );
  307. return $form;
  308. }
  309. function tripal_bulk_loader_delete_template_base_form_submit ($form, &$form_state) {
  310. $sql = "DELETE FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  311. db_query($sql, $form_state['values']['template_name']);
  312. }
  313. //////////////////////////////////////////////////////////////////////////////////////
  314. // Add/Edit Field Forms (meant to be returned as part of a larger form)
  315. //////////////////////////////////////////////////////////////////////////////////////
  316. /**
  317. * Add Field Form
  318. *
  319. * This form meant to be part of a larger form. Currently it is part of the following:
  320. * - tripal_bulk_loader_add_template_base_form
  321. * - tripal_bulk_loader_edit_template_base_form
  322. *
  323. * @param $form_state
  324. * The $form_state from the parent form
  325. * @return
  326. * The parent form with the edit field fieldset added
  327. *
  328. * Usage:
  329. * @code
  330. $form = array_merge($form, tripal_bulk_loader_add_template_field_form($form_state));
  331. * @endcode
  332. */
  333. function tripal_bulk_loader_add_template_field_form ($form_state) {
  334. $form = array();
  335. $base_table = $form_state['storage']['base_table'];
  336. $form['add_fields'] = array(
  337. '#type' => 'fieldset',
  338. '#title' => t('Step 2: Add Fields'),
  339. '#prefix' => '<div id="tripal_bulk_loader-add_field">',
  340. '#suffix' => '</div>',
  341. );
  342. $field_type = ($form_state['storage']['add']['field_type'])? $form_state['storage']['add']['field_type'] : 'table field';
  343. $form['add_fields']['add-type'] = array(
  344. '#type' => 'radios',
  345. '#title' => t('Type of Field'),
  346. '#options' => array(
  347. 'table field' => t('Fields which maps to a Spreadsheet Column'),
  348. 'constant' => t('Field which remains Constant throughout the Spreadsheet'),
  349. ),
  350. '#default_value' => $field_type,
  351. '#ahah' => array(
  352. 'path' => 'admin/tripal/tripal_bulk_loader_template/add/field_type_ahah',
  353. 'wrapper' => 'tripal_bulk_loader-add_field',
  354. 'effect' => 'fade'
  355. ),
  356. );
  357. $form['add_fields']['add-field_title'] = array(
  358. '#type' => 'textfield',
  359. '#title' => t('Human-readable Title for Field')
  360. );
  361. // Spreadsheet column
  362. $form['add_fields']['columns'] = array(
  363. '#type' => ($field_type == 'table field')? 'fieldset' : 'hidden',
  364. '#title' => t('Spreadsheet Column'),
  365. );
  366. $form['add_fields']['columns']['add-sheet'] = array(
  367. '#type' => 'textfield',
  368. '#title' => t('Worksheet'),
  369. '#description' => t('Specify the name of the worksheet.'),
  370. '#size' => 5,
  371. '#default_value' => ($form_state['storage']['add']['sheet_name'])? $form_state['storage']['add']['sheet_name'] : 'Sheet1',
  372. );
  373. $form['add_fields']['columns']['add-column'] = array(
  374. '#type' => 'textfield',
  375. '#title' => t('Column'),
  376. '#description' => t('Specify the column in the spreadsheet that this field maps to where the first column is 1.'),
  377. '#size' => 5,
  378. '#default_value' => $form_state['storage']['add']['column_number'],
  379. );
  380. // Global Value
  381. $form['add_fields']['constant'] = array(
  382. '#type' => ($field_type == 'constant')? 'fieldset' : 'hidden',
  383. '#title' => t('Constant'),
  384. );
  385. $form['add_fields']['constant']['add-constant_value'] = array(
  386. '#type' => 'textfield',
  387. '#title' => t('Constant Value'),
  388. '#description' => t('Specify the value you wish this field to have regardless of spreadsheet data.'),
  389. '#default_value' => $form_state['storage']['add']['constant_value']
  390. );
  391. // Chado Field
  392. $form['add_fields']['chado'] = array(
  393. '#type' => 'fieldset',
  394. '#title' => t('Chado Field/Column Details'),
  395. '#prefix' => '<div id="tripal_bulk_loader-add_chado_column">',
  396. '#suffix' => '</div>',
  397. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  398. );
  399. $related_tables = module_invoke_all('tripal_bulk_loader_'.$base_table.'_related_tables');
  400. $table = ($form_state['storage']['add']['chado_table'])? $form_state['storage']['add']['chado_table'] : $base_table;
  401. $form['add_fields']['chado']['add-chado_table'] = array(
  402. '#type' => 'select',
  403. '#title' => t('Chado Table'),
  404. '#options' => $related_tables,
  405. '#default_value' => $table,
  406. '#ahah' => array(
  407. 'path' => 'admin/tripal/tripal_bulk_loader_template/add/chado_column_ahah',
  408. 'wrapper' => 'tripal_bulk_loader-add_chado_column',
  409. 'effect' => 'fade'
  410. ),
  411. );
  412. if ($related_tables['Indirect Relations'][$table]) {
  413. //Display fields for current table plus related tables
  414. $table_description = module_invoke_all('chado_'.$table.'_schema');
  415. $tables2list = array_keys($table_description['foreign keys']);
  416. unset($tables2list[array_search($base_table, $tables2list)]);
  417. $tables2list[] = $table;
  418. $form['field_is_indirect'] = array(
  419. '#type' => 'hidden',
  420. '#value' => implode('#',$tables2list),
  421. );
  422. } else {
  423. $tables2list = array($table);
  424. }
  425. $chado_fields = array();
  426. foreach ($tables2list as $table) {
  427. $table_description = module_invoke_all('chado_'.$table.'_schema');
  428. foreach($table_description['fields'] as $field_name => $field_array) {
  429. $chado_fields[$field_name] = $field_name;
  430. }
  431. }
  432. $form['add_fields']['chado']['add-chado_field'] = array(
  433. '#type' => 'select',
  434. '#title' => t('Chado Field/Column'),
  435. '#options' => $chado_fields
  436. );
  437. $form['add_fields']['submit-add_field'] = array(
  438. '#type' => 'submit',
  439. '#value' => 'Add Field'
  440. );
  441. return $form;
  442. }
  443. /**
  444. * Add Field Submit
  445. *
  446. * This submit meant to be part of a larger form submit. Currently it is part of the following:
  447. * - tripal_bulk_loader_add_template_base_form_submit
  448. * - tripal_bulk_loader_edit_template_base_form_submit
  449. *
  450. * @param $form_state
  451. * The $form_state from the parent submit
  452. * @return
  453. * The parent form_state with any changes made
  454. *
  455. * Usage:
  456. * @code
  457. $form_state = tripal_bulk_loader_add_template_field_form_submit($form_state);
  458. * @endcode
  459. */
  460. function tripal_bulk_loader_add_template_field_form_submit ($form_state) {
  461. dpm($form_state, 'form state in add submit');
  462. $form_state['storage']['add']['field_title'] = $form_state['values']['add-field_title'];
  463. $form_state['storage']['add']['field_type'] = $form_state['values']['add-type'];
  464. $form_state['storage']['add']['sheet_name'] = $form_state['values']['add-sheet'];
  465. $form_state['storage']['add']['column_number'] = $form_state['values']['add-column'];
  466. $form_state['storage']['add']['constant_value'] = $form_state['values']['add-constant_value'];
  467. $form_state['storage']['add']['chado_table'] = $form_state['values']['add-chado_table'];
  468. $form_state['storage']['add']['chado_field'] = $form_state['values']['add-chado_field'];
  469. if (!$form_state['ahah_submission']) {
  470. if ($form_state['values']['op'] == 'Add Field') {
  471. $template = $form_state['storage']['template'];
  472. $template['module'] = $form_state['storage']['base_table'];
  473. //if table relationship to base is indirect, fields from 2+ tables are listed in the fields select
  474. if ($form_state['values']['field_is_indirect']) {
  475. $listed_tables = preg_split('/#/', $form_state['values']['field_is_indirect']);
  476. $field = $form_state['storage']['add']['chado_field'];
  477. foreach ($listed_tables as $table) {
  478. $table_desc = module_invoke_all('chado_'.$table.'_schema');
  479. if ($table_desc['fields'][$field]) {
  480. if (preg_match('/'.$table.'/', $form_state['storage']['add']['chado_table'])) {
  481. //ensure linking table is added
  482. if (!$template[$form_state['storage']['add']['chado_table']]) {
  483. $template[$form_state['storage']['add']['chado_table']]['field'] = array();
  484. }
  485. }
  486. $form_state['storage']['add']['chado_table'] = $table;
  487. break;
  488. }
  489. }
  490. }
  491. if ($form_state['storage']['add']['field_type'] == 'table field') {
  492. $template[$form_state['storage']['add']['chado_table']]['field'][] = array(
  493. 'type' => 'table field',
  494. 'title' => $form_state['storage']['add']['field_title'],
  495. 'field' => $form_state['storage']['add']['chado_field'],
  496. //'required' => <true|false>,
  497. //'allowed values' => empty by default,
  498. 'spreadsheet sheet' => $form_state['storage']['add']['sheet_name'],
  499. 'spreadsheet column' => $form_state['storage']['add']['column_number'],
  500. //'exposed' => 'true|false' If exposed, will give a select box first from allowed values if set, second from database if values not set.
  501. //'mapping' => array(
  502. // 'from' => 'to'
  503. // '/from re/' => 'to'
  504. //),
  505. );
  506. } elseif ($form_state['storage']['add']['field_type'] == 'constant') {
  507. $template[$form_state['storage']['add']['chado_table']]['field'][] = array(
  508. 'type' => 'constant',
  509. 'title' => $form_state['storage']['add']['field_title'],
  510. 'field' => $form_state['storage']['add']['chado_field'],
  511. //'required' => <true|false>,
  512. //'allowed values' => empty by default,
  513. 'constant value' => $form_state['storage']['add']['constant_value'],
  514. //'exposed' => 'true|false' If exposed, will give a select box first from allowed values if set, second from database if values not set.
  515. );
  516. }
  517. drupal_set_message('Successfully Added Field to Template');
  518. $form_state['storage']['template'] = $template;
  519. }
  520. }
  521. dpm($form_state['storage']['template'], 'template array from add submit');
  522. return $form_state;
  523. }
  524. /**
  525. * Edit Field Form
  526. *
  527. * This form meant to be part of a larger form. Currently it is part of the following:
  528. * - tripal_bulk_loader_edit_template_base_form
  529. *
  530. * @param $form_state
  531. * The $form_state from the parent form
  532. * @return
  533. * The parent form with the edit field fieldset added
  534. *
  535. * Usage:
  536. * @code
  537. $form = array_merge($form, tripal_bulk_loader_edit_template_field_form($form_state));
  538. * @endcode
  539. */
  540. function tripal_bulk_loader_edit_template_field_form($form_state) {
  541. $form = array();
  542. $base_table = $form_state['storage']['base_table'];
  543. $form['edit_fields'] = array(
  544. '#type' => ($form_state['storage']['edit_field']) ? 'fieldset' : 'hidden',
  545. '#title' => t('Edit Fields'),
  546. '#prefix' => '<div id="tripal_bulk_loader-edit_field">',
  547. '#suffix' => '</div>',
  548. );
  549. $field_type = ($form_state['storage']['edit']['field_type'])? $form_state['storage']['edit']['field_type'] : 'table field';
  550. $form['edit_fields']['edit-type'] = array(
  551. '#type' => 'radios',
  552. '#title' => t('Type of Field'),
  553. '#options' => array(
  554. 'table field' => t('Fields which maps to a Spreadsheet Column'),
  555. 'constant' => t('Field which remains Constant throughout the Spreadsheet'),
  556. ),
  557. '#default_value' => $field_type,
  558. '#ahah' => array(
  559. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit/field_type_ahah',
  560. 'wrapper' => 'tripal_bulk_loader-edit_field',
  561. 'effect' => 'fade'
  562. ),
  563. );
  564. $form['edit_fields']['edit-field_title'] = array(
  565. '#type' => 'textfield',
  566. '#title' => t('Human-readable Title for Field'),
  567. '#default_value' => $form_state['storage']['edit']['field_title'],
  568. );
  569. // Spreadsheet column
  570. $form['edit_fields']['columns'] = array(
  571. '#type' => ($field_type == 'table field')? 'fieldset' : 'hidden',
  572. '#title' => t('Spreadsheet Column'),
  573. );
  574. $form['edit_fields']['columns']['edit-sheet'] = array(
  575. '#type' => 'textfield',
  576. '#title' => t('Worksheet'),
  577. '#description' => t('Specify the name of the worksheet.'),
  578. '#size' => 5,
  579. '#default_value' => ($form_state['storage']['edit']['sheet_name'])? $form_state['storage']['edit']['sheet_name'] : 'Sheet1',
  580. );
  581. $form['edit_fields']['columns']['edit-column'] = array(
  582. '#type' => 'textfield',
  583. '#title' => t('Column'),
  584. '#description' => t('Specify the column in the spreadsheet that this field maps to where the first column is 1.'),
  585. '#size' => 5,
  586. '#default_value' => $form_state['storage']['edit']['column_number'],
  587. );
  588. // Global Value
  589. $form['edit_fields']['constant'] = array(
  590. '#type' => ($field_type == 'constant')? 'fieldset' : 'hidden',
  591. '#title' => t('Constant'),
  592. );
  593. $form['edit_fields']['constant']['edit-constant_value'] = array(
  594. '#type' => 'textfield',
  595. '#title' => t('Constant Value'),
  596. '#description' => t('Specify the value you wish this field to have regardless of spreadsheet data.'),
  597. '#default_value' => $form_state['storage']['edit']['constant_value']
  598. );
  599. // Chado Field
  600. $form['edit_fields']['chado'] = array(
  601. '#type' => 'fieldset',
  602. '#title' => t('Chado Field/Column Details'),
  603. '#prefix' => '<div id="tripal_bulk_loader-edit_chado_column">',
  604. '#suffix' => '</div>',
  605. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  606. );
  607. $related_tables = module_invoke_all('tripal_bulk_loader_'.$base_table.'_related_tables');
  608. $table = ($form_state['storage']['edit']['chado_table'])? $form_state['storage']['edit']['chado_table'] : $base_table;
  609. $form['edit_fields']['chado']['edit-chado_table'] = array(
  610. '#type' => 'select',
  611. '#title' => t('Chado Table'),
  612. '#options' => $related_tables,
  613. '#default_value' => $table,
  614. '#ahah' => array(
  615. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit/chado_column_ahah',
  616. 'wrapper' => 'tripal_bulk_loader-edit_chado_column',
  617. 'effect' => 'fade'
  618. ),
  619. );
  620. $table_description = module_invoke_all('chado_'.$table.'_schema');
  621. $chado_fields = array();
  622. foreach($table_description['fields'] as $field_name => $field_array) {
  623. $chado_fields[$field_name] = $field_name;
  624. }
  625. $form['edit_fields']['chado']['edit-chado_field'] = array(
  626. '#type' => 'select',
  627. '#title' => t('Chado Field/Column'),
  628. '#options' => $chado_fields,
  629. '#default_value' => $form_state['storage']['edit']['chado_field'],
  630. );
  631. $form['edit_fields']['submit-Edit_field'] = array(
  632. '#type' => 'submit',
  633. '#value' => 'Edit Field'
  634. );
  635. return $form;
  636. }
  637. /**
  638. * Edit Field Submit
  639. *
  640. * This submit meant to be part of a larger form submit. Currently it is part of the following:
  641. * - tripal_bulk_loader_edit_template_base_form_submit
  642. *
  643. * @param $form_state
  644. * The $form_state from the parent submit
  645. * @return
  646. * The parent form_state with any changes made
  647. *
  648. * Usage:
  649. * @code
  650. $form_state = tripal_bulk_loader_edit_template_field_form_submit($form_state);
  651. * @endcode
  652. */
  653. function tripal_bulk_loader_edit_template_field_form_submit ($form_state) {
  654. if (preg_match('/Edit Field #(\d+)/', $form_state['values']['op'], $matches)) {
  655. $table = $form_state['values']["chado_table_hidden-".$matches[1]];
  656. $field_key = $form_state['values']["field_index-".$matches[1]];
  657. $form_state['storage']['edit_field'] = $form_state['storage']['template'][$table]['field'][$field_key];
  658. $form_state['storage']['edit_field']['table'] = $table;
  659. $form_state['storage']['edit_field']['field_index'] = $field_key;
  660. $set_default = TRUE;
  661. $field_template = $form_state['storage']['edit_field'];
  662. } else {
  663. $set_default = FALSE;
  664. }
  665. if ($set_default) {
  666. $form_state['storage']['edit']['field_title'] = $field_template['title'];
  667. $form_state['storage']['edit']['field_type'] = $field_template['type'];
  668. $form_state['storage']['edit']['sheet_name'] = $field_template['spreadsheet sheet'];
  669. $form_state['storage']['edit']['column_number'] = $field_template['spreadsheet column'];
  670. $form_state['storage']['edit']['constant_value'] = $field_template['constant value'];
  671. $form_state['storage']['edit']['chado_table'] = $field_template['table'];
  672. $form_state['storage']['edit']['chado_field'] = $field_template['field'];
  673. } else {
  674. $form_state['storage']['edit']['field_title'] = $form_state['values']['edit-field_title'];
  675. $form_state['storage']['edit']['field_type'] = $form_state['values']['edit-type'];
  676. $form_state['storage']['edit']['sheet_name'] = $form_state['values']['edit-sheet'];
  677. $form_state['storage']['edit']['column_number'] = $form_state['values']['edit-column'];
  678. $form_state['storage']['edit']['constant_value'] = $form_state['values']['edit-constant_value'];
  679. $form_state['storage']['edit']['chado_table'] = $form_state['values']['edit-chado_table'];
  680. $form_state['storage']['edit']['chado_field'] = $form_state['values']['edit-chado_field'];
  681. }
  682. if (!$form_state['ahah_submission']) {
  683. if ($form_state['values']['op'] == 'Edit Field') {
  684. if ($form_state['storage']['edit']['field_type'] == 'table field') {
  685. $field = array(
  686. 'type' => 'table field',
  687. 'title' => $form_state['storage']['edit']['field_title'],
  688. 'field' => $form_state['storage']['edit']['chado_field'],
  689. //'required' => <true|false>,
  690. //'allowed values' => empty by default,
  691. 'spreadsheet sheet' => $form_state['storage']['edit']['sheet_name'],
  692. 'spreadsheet column' => $form_state['storage']['edit']['column_number'],
  693. //'exposed' => 'true|false' If exposed, will give a select box first from allowed values if set, second from database if values not set.
  694. //'mapping' => array(
  695. // 'from' => 'to'
  696. // '/from re/' => 'to'
  697. //),
  698. );
  699. } elseif ($form_state['storage']['edit']['field_type'] == 'constant') {
  700. $field = array(
  701. 'type' => 'constant',
  702. 'title' => $form_state['storage']['edit']['field_title'],
  703. 'field' => $form_state['storage']['edit']['chado_field'],
  704. //'required' => <true|false>,
  705. //'allowed values' => empty by default,
  706. 'constant value' => $form_state['storage']['edit']['constant_value'],
  707. //'exposed' => 'true|false' If exposed, will give a select box first from allowed values if set, second from database if values not set.
  708. );
  709. }
  710. $table = $form_state['storage']['edit_field']['table'];
  711. $field_index = $form_state['storage']['edit_field']['field_index'];
  712. $form_state['storage']['template'][$table]['field'][$field_index] = $field;
  713. drupal_set_message('Successfully Updated Field');
  714. //reset form
  715. unset($form_state['storage']['edit_field']);
  716. unset($form_state['storage']['edit']);
  717. }
  718. }
  719. return $form_state;
  720. }
  721. //////////////////////////////////////////////////////////////////////////////////////
  722. // AHAH Callbacks
  723. //////////////////////////////////////////////////////////////////////////////////////
  724. /**
  725. * AHAH Function: Replace $form['add_fields']['chado'] in tripal_bulk_loader_add_template_field_form
  726. *
  727. * @return
  728. * JSON Data printed to the screen
  729. */
  730. function tripal_bulk_loader_add_chado_column_ahah () {
  731. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  732. $form_build_id = $_POST['form_build_id'];
  733. $form = form_get_cache($form_build_id, $form_state);
  734. $args = $form['#parameters'];
  735. $form_id = array_shift($args);
  736. $form_state['post'] = $form['#post'] = $_POST;
  737. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  738. $form_state['ahah_submission'] = TRUE;
  739. $form['#programmed'] = $form['#redirect'] = FALSE;
  740. drupal_process_form($form_id, $form, $form_state);
  741. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  742. $form_element = $form['add_fields']['chado'];
  743. // Remove the wrapper so we don't double it up.
  744. unset($form_element['#prefix'], $form_element['#suffix']);
  745. $output = theme('status_messages');
  746. $output .= drupal_render($form_element);
  747. // Final rendering callback.
  748. print drupal_json(array('status' => TRUE, 'data' => $output));
  749. exit();
  750. }
  751. /**
  752. * AHAH Function: Replace $form['add_fields'] in tripal_bulk_loader_add_template_base_form
  753. *
  754. * @return
  755. * JSON Data printed to the screen
  756. */
  757. function tripal_bulk_loader_add_field_type_ahah () {
  758. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  759. $form_build_id = $_POST['form_build_id'];
  760. $form = form_get_cache($form_build_id, $form_state);
  761. $args = $form['#parameters'];
  762. $form_id = array_shift($args);
  763. $form_state['post'] = $form['#post'] = $_POST;
  764. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  765. $form_state['ahah_submission'] = TRUE;
  766. $form['#programmed'] = $form['#redirect'] = FALSE;
  767. drupal_process_form($form_id, $form, $form_state);
  768. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  769. $form_element = $form['add_fields'];
  770. // Remove the wrapper so we don't double it up.
  771. unset($form_element['#prefix'], $form_element['#suffix']);
  772. $output = theme('status_messages');
  773. $output .= drupal_render($form_element);
  774. // Final rendering callback.
  775. print drupal_json(array('status' => TRUE, 'data' => $output));
  776. exit();
  777. }
  778. /**
  779. * AHAH Function: Replace $form['edit_fields'] in tripal_bulk_loader_edit_template_base_form
  780. *
  781. * @return
  782. * JSON Data printed to the screen
  783. */
  784. function tripal_bulk_loader_edit_fields_ahah () {
  785. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  786. $form_build_id = $_POST['form_build_id'];
  787. $form = form_get_cache($form_build_id, $form_state);
  788. $args = $form['#parameters'];
  789. $form_id = array_shift($args);
  790. $form_state['post'] = $form['#post'] = $_POST;
  791. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  792. $form_state['ahah_submission'] = TRUE;
  793. $form['#programmed'] = $form['#redirect'] = FALSE;
  794. drupal_process_form($form_id, $form, $form_state);
  795. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  796. $form_element = $form['edit_fields'];
  797. // Remove the wrapper so we don't double it up.
  798. unset($form_element['#prefix'], $form_element['#suffix']);
  799. $output = theme('status_messages');
  800. $output .= drupal_render($form_element);
  801. // Final rendering callback.
  802. print drupal_json(array('status' => TRUE, 'data' => $output));
  803. exit();
  804. }
  805. /**
  806. * AHAH Function: Replace $form['edit_fields'] in tripal_bulk_loader_edit_template_field_form
  807. *
  808. * @return
  809. * JSON Data printed to the screen
  810. */
  811. function tripal_bulk_loader_edit_field_type_ahah () {
  812. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  813. $form_build_id = $_POST['form_build_id'];
  814. $form = form_get_cache($form_build_id, $form_state);
  815. $args = $form['#parameters'];
  816. $form_id = array_shift($args);
  817. $form_state['post'] = $form['#post'] = $_POST;
  818. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  819. $form_state['ahah_submission'] = TRUE;
  820. $form['#programmed'] = $form['#redirect'] = FALSE;
  821. drupal_process_form($form_id, $form, $form_state);
  822. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  823. $form_element = $form['edit_fields'];
  824. // Remove the wrapper so we don't double it up.
  825. unset($form_element['#prefix'], $form_element['#suffix']);
  826. $output = theme('status_messages');
  827. $output .= drupal_render($form_element);
  828. // Final rendering callback.
  829. print drupal_json(array('status' => TRUE, 'data' => $output));
  830. exit();
  831. }
  832. /**
  833. * AHAH Function: Replace $form['edit_fields']['chado'] in tripal_bulk_loader_edit_template_field_form
  834. *
  835. * @return
  836. * JSON Data printed to the screen
  837. */
  838. function tripal_bulk_loader_edit_chado_column_ahah () {
  839. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  840. $form_build_id = $_POST['form_build_id'];
  841. $form = form_get_cache($form_build_id, $form_state);
  842. $args = $form['#parameters'];
  843. $form_id = array_shift($args);
  844. $form_state['post'] = $form['#post'] = $_POST;
  845. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  846. $form_state['ahah_submission'] = TRUE;
  847. $form['#programmed'] = $form['#redirect'] = FALSE;
  848. drupal_process_form($form_id, $form, $form_state);
  849. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  850. $form_element = $form['edit_fields']['chado'];
  851. // Remove the wrapper so we don't double it up.
  852. unset($form_element['#prefix'], $form_element['#suffix']);
  853. $output = theme('status_messages');
  854. $output .= drupal_render($form_element);
  855. // Final rendering callback.
  856. print drupal_json(array('status' => TRUE, 'data' => $output));
  857. exit();
  858. }