Progressbar demo

  1. <?php
  2. /**
  3.  * page callback for javascript
  4.  *
  5.  * Normally I'd just include a javascript file
  6.  * but I wanted to put all the code in one file for this demo
  7.  *
  8.  */
  9. function progresbardemo_javascript() {
  10.  
  11.         $content = '
  12. if (Drupal.jsEnabled) {
  13.  $(document).ready(function() {
  14.    $("#clickme").click(function() {
  15.      titlediv = $("#clickme").parent();
  16.      pb = new Drupal.progressBar(\'myProgressBar\');
  17.  
  18.     // $("#progress").appendChild(pb.element);
  19.    document.getElementById("progress").appendChild(pb.element);
  20.        pb.setProgress("0", "processing...");
  21.      uri = \'/progresbardemo/work_ajax\';
  22.      $.get(uri , \'\', function(data) {
  23.        $("#fillme").empty();
  24.        $("#fillme").append(data);
  25.        pb.stopMonitoring();
  26.          pb.setProgress("100", "done");
  27.      });
  28.      pb.startMonitoring(\'/progresbardemo/monitor\', 1000);
  29.      return false;
  30.    })
  31.  })
  32. }
  33.         ';
  34.  
  35.         header("Content-type: text/javascript");
  36.         print $content;
  37.         exit();
  38. }
  39.  
  40. /**
  41.  * Page callback for the demo start page
  42.  *
  43.  * @return html output
  44.  */
  45. function progresbardemo_page() {
  46.  
  47.  
  48.     drupal_add_js('misc/progress.js');
  49.     drupal_add_js('progresbardemo/javascript');
  50.     $output .= '<h2>Progress Bar Demo</h2>';
  51.     $output .= '<div><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'. l('Start', 'progresbardemo/work', array('id' => 'clickme')) .'</p></div>';
  52.     $output .= '<div id="fillme"></div>';
  53.     $output .= '<div id="progress"></div>';
  54.    
  55.     return $output;
  56. }
  57.  
  58. /**
  59.  * Do the work
  60.  * This function can be called directly as a menu callback where it will provide the page content
  61.  * Or via the ajax callback function
  62.  *
  63.  * @return html content
  64.  */
  65. function progresbardemo_work() {
  66.        
  67.         $progres = 0;
  68.         db_query("insert into {variable} (name, value) values ('progresbardemo_progres', '%s')", $progres);
  69.         define('PERCENT', 100);
  70.         define('STEPS', 20);
  71.         while ($progres < PERCENT) {
  72.          sleep(1);
  73.          $progres += (PERCENT / STEPS);
  74.          db_query("update {variable} set value = '%s' where name = 'progresbardemo_progres'", $progres);
  75.         }
  76.         db_query("delete from {variable} where name='progresbardemo_progres'");
  77.         return "<p>all done</p>";
  78. }
  79.  
  80. /**
  81.  * Do the task - called in ajax context - return the result without the page wrapper
  82.  */
  83. function progresbardemo_work_ajax() {
  84.         print progresbardemo_work();
  85.         exit();
  86.  
  87. }
  88. /**
  89.  * called by the ajax monitor
  90.  */
  91. function progresbardemo_monitor() {
  92.         $progres = (int) db_result(db_query("select value from {variable}  where name='progresbardemo_progres'"));
  93.    
  94.     print drupal_to_js(array('status' => TRUE, 'percentage' => $progres, 'message' => "working... $progres"));
  95.     exit();
  96. }
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103. /**
  104.  * define the menu
  105.  * implementation of hook_menu
  106.  *
  107.  * @return menu array
  108.  */
  109.  
  110. function progresbardemo_menu($may_cache) {
  111.         $items = array();
  112.  
  113.         if (!$may_cache) {
  114.  
  115.                 $items[] =  array(
  116.                     'type'          => MENU_NORMAL_ITEM,
  117.                     'title'             => t("Progress Bar Demo"),
  118.                     'path'          => 'progresbardemo',
  119.                     'callback'  => 'progresbardemo_page',
  120.                     'access'        => TRUE
  121.                 );
  122.  
  123.                 $items[] =  array(
  124.                     'type'          => MENU_CALLBACK,
  125.                     'path'          => 'progresbardemo/work',
  126.                     'callback'  => 'progresbardemo_work',
  127.                     'access'        => TRUE
  128.                 );
  129.  
  130.                 $items[] =  array(
  131.                     'type'          => MENU_CALLBACK,
  132.                     'path'          => 'progresbardemo/work_ajax',
  133.                     'callback'  => 'progresbardemo_work_ajax',
  134.                     'access'        => TRUE
  135.                 );
  136.  
  137.                 $items[] =  array(
  138.                     'type'          => MENU_CALLBACK,
  139.                     'path'          => 'progresbardemo/monitor',
  140.                     'callback'  => 'progresbardemo_monitor',
  141.                     'access'        => TRUE
  142.                 );
  143.  
  144.                 $items[] =  array(
  145.                     'type'          => MENU_CALLBACK,
  146.                     'path'          => 'progresbardemo/javascript',
  147.                     'callback'  => 'progresbardemo_javascript',
  148.                     'access'        => TRUE
  149.                 );
  150.         }
  151.         return $items;
  152. }