tripal_bulk_loader.admin.inc 36 KB

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