ajax_example_progressbar.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * @file
  4. * Progress bar example.
  5. */
  6. /**
  7. * Implements hook_FORMID().
  8. *
  9. * Build a landing-page form for the progress bar example.
  10. *
  11. * @see https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7#ajax_progress
  12. */
  13. function ajax_example_progressbar_form($form, &$form_state) {
  14. $form_state['time'] = REQUEST_TIME;
  15. // We make a DIV which the progress bar can occupy. You can see this in use
  16. // in ajax_example_progressbar_callback().
  17. $form['status'] = array(
  18. '#markup' => '<div id="progress-status"></div>',
  19. );
  20. $form['submit'] = array(
  21. '#type' => 'submit',
  22. '#value' => t('Submit'),
  23. '#ajax' => array(
  24. // Here we set up our AJAX callback handler.
  25. 'callback' => 'ajax_example_progressbar_callback',
  26. // Tell FormAPI about our progress bar.
  27. 'progress' => array(
  28. 'type' => 'bar',
  29. 'message' => t('Execute..'),
  30. // Have the progress bar access this URL path.
  31. 'url' => url('examples/ajax_example/progressbar/progress/' . $form_state['time']),
  32. // The time interval for the progress bar to check for updates.
  33. 'interval' => 1000,
  34. ),
  35. ),
  36. );
  37. return $form;
  38. }
  39. /**
  40. * Get the progress bar execution status, as JSON.
  41. *
  42. * This is the menu handler for
  43. * examples/ajax_example/progressbar/progress/$time.
  44. *
  45. * This function is our wholly arbitrary job that we're checking the status for.
  46. * In this case, we're reading a system variable that is being updated by
  47. * ajax_example_progressbar_callback().
  48. *
  49. * We set up the AJAX progress bar to check the status every second, so this
  50. * will execute about once every second.
  51. *
  52. * The progress bar JavaScript accepts two values: message and percentage. We
  53. * set those in an array and in the end convert it JSON for sending back to the
  54. * client-side JavaScript.
  55. *
  56. * @param int $time
  57. * Timestamp.
  58. *
  59. * @see ajax_example_progressbar_callback()
  60. */
  61. function ajax_example_progressbar_progress($time) {
  62. $progress = array(
  63. 'message' => t('Starting execute...'),
  64. 'percentage' => -1,
  65. );
  66. $completed_percentage = variable_get('example_progressbar_' . $time, 0);
  67. if ($completed_percentage) {
  68. $progress['message'] = t('Executing...');
  69. $progress['percentage'] = $completed_percentage;
  70. }
  71. drupal_json_output($progress);
  72. }
  73. /**
  74. * Our submit handler.
  75. *
  76. * This handler spends some time changing a variable and sleeping, and then
  77. * finally returns a form element which marks the #progress-status DIV as
  78. * completed.
  79. *
  80. * While this is occurring, ajax_example_progressbar_progress() will be called
  81. * a number of times by the client-sid JavaScript, which will poll the variable
  82. * being set here.
  83. *
  84. * @see ajax_example_progressbar_progress()
  85. */
  86. function ajax_example_progressbar_callback($form, &$form_state) {
  87. $variable_name = 'example_progressbar_' . $form_state['time'];
  88. $commands = array();
  89. variable_set($variable_name, 10);
  90. sleep(2);
  91. variable_set($variable_name, 40);
  92. sleep(2);
  93. variable_set($variable_name, 70);
  94. sleep(2);
  95. variable_set($variable_name, 90);
  96. sleep(2);
  97. variable_del($variable_name);
  98. $commands[] = ajax_command_html('#progress-status', t('Executed.'));
  99. return array(
  100. '#type' => 'ajax',
  101. '#commands' => $commands,
  102. );
  103. }