Guest User

Untitled

a guest
Jun 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. <?php
  2. /**
  3. * Import submit function
  4. */
  5. function ret_user_import_upload_form_submit($form, &$form_state) {
  6. if ($file = file_save_upload($source = 'ret_user_import_csv_file', $validators = array(), $dest = FALSE, $replace = FILE_EXISTS_REPLACE)) {
  7. $offset = 0;
  8. $headers = _ret_user_import_get_row($file->filepath, $offset);
  9. if ($headers === FALSE) {
  10. // there needs to be consistency, so enforce the existence of a 'header' row
  11. return;
  12. }
  13. $srcinfo = new stdClass;
  14. $srcinfo->filepath = $file->filepath;
  15. $srcinfo->offset = $offset;
  16. $batch = array(
  17. 'title' => t('Importing user records from %filename',
  18. array('%filename' => $file->filename)),
  19. 'init_message' => t('User import initializing'),
  20. 'error_message' => t('Import failed'),
  21. 'progress_message' => '',
  22. 'operations' => array(
  23. array('_ret_user_import_import_csv', array($srcinfo)),
  24. ),
  25. 'finished' => '_ret_user_import_import_finish',
  26. );
  27. batch_set($batch);
  28.  
  29. $redirect_path = 'admin/ret/import/user/overview';
  30. batch_process($redirect_path);
  31. }
  32. else {
  33. drupal_set_message(t('Everything failed; try again.'), 'error', FALSE);
  34. $form_state['redirect'] = 'admin/ret/import/user/overview';
  35. }
  36. }
  37.  
  38. function _ret_user_import_import_csv($srcinfo, &$context) {
  39. if (!isset($context['sandbox']['max'])) {
  40. $context['sandbox']['max'] = (count(file($srcinfo->filepath)) - 1); // -1 because we read the first line as headers
  41. $context['sandbox']['done'] = $srcinfo->offset;
  42. $context['sandbox']['rows'] = 0;
  43. }
  44.  
  45. // Return for feedback every X seconds
  46. $timelimit = time() + 10;
  47.  
  48. while ($values = _ret_user_import_get_row($srcinfo->filepath, $context['sandbox']['done'])) {
  49. // do your work
  50.  
  51. $context['sandbox']['rows']++;
  52. if (time() > $timelimit) {
  53. break;
  54. }
  55. }
  56.  
  57. // provide a meaningful status message during the batch process
  58. $context['message'] = t('%rows of %total records imported.',
  59. array('%rows' => $context['sandbox']['rows'],
  60. '%total' => $context['sandbox']['max']));
  61.  
  62. // if not finished, give the Batch API a percentage of 'finished' to use for the progress bar
  63. if ($context['sandbox']['rows'] != $context['sandbox']['max']) {
  64. $context['finished'] = $context['sandbox']['rows'] / $context['sandbox']['max'];
  65. }
  66. else {
  67. $context['finished'] = 1;
  68. }
  69.  
  70. // if finished, wrap up
  71. // the _finish function below will be called after this
  72. if ($context['finished'] == 1) {
  73. $context['results']['srcinfo'] = $srcinfo;
  74. $context['message'] .= '<br/>' . t('Importation Complete.');
  75. }
  76. }
  77.  
  78. /**
  79. * Batch _finish function
  80. *
  81. * This function uses the success, results and operations parts of the $context variable,
  82. * executing after the batch has completed successfully.
  83. */
  84. function _ret_user_import_import_finish($success, $results, $operations) {
  85. if ($success) {
  86. $srcinfo = $results['srcinfo'];
  87. unlink($srcinfo->filepath);
  88. drupal_set_message($srcinfo->success_msg);
  89. }
  90. }
  91. ?>
Add Comment
Please, Sign In to add comment