tripal_bulk_loader.admin.inc 36 KB

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