MongoDB Tutorial 2: Buttons 2

From The Uniform Server Wiki
Revision as of 13:14, 1 August 2010 by Ric (talk | contribs) (New page: {{Nav MongoDB Tutorial 2}} == Introduction == This page covers adding functionality to buttons '''''Start MongoDB''''' and '''''Start Mongo Client''''' == Top button == A button’s text...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

 

MongoDB Plugin
UniServer 6-Carbo.

Introduction

This page covers adding functionality to buttons Start MongoDB and Start Mongo Client

Top button

A button’s text is dynamic allowing it to be written and read. This capability allows a button to have more that one state. Current state is determined by reading its text.

Top button of our application has two states Start MongoDB and Apache and Stop MongoDB and Apache. following shows how to create a toggle button:

Toggle action:

  • Read button text
  • Use an if statement to compare text to START_DB_BTXT
  • If match (start) change button text to stop
  • Else change button text to start
$text =  wb_get_text(wb_get_control($window, ID_DB_BUTTON));

if($text == START_DB_BTXT ){ // Start servers
  wb_set_text(wb_get_control($window, ID_DB_BUTTON),STOP_DB_BTXT); 
else{                         // Stop   servers
 wb_set_text(wb_get_control($window,    ID_DB_BUTTON),START_DB_BTXT); 
}

Top

Start

If the button is displaying Start MongoDB and Apache when clicked we want to perform the following actions:

  • Start MongoDB server
  • Start Apache server
  • Set button text to stop
  • Enable Mongo-client button
  • Enable phpMoAdmin button

Gives the following code:

     if($text == START_DB_BTXT ){ // Start server

       //== Start Apache
       $cmd = "../../usr/local/php/php.exe -n ../main/start_servers.php 1"; // Cmd to run hidden
       run_cmd_hidden($cmd);                               // Run command
       wb_set_text($statusbar_mongo, " Starting Apache");  // Inform user 

       //== Start MongoDB
       start_mongo_no_auth();
       wb_set_text($statusbar_mongo, " Starting MongoDB");  // Inform user 

       //== Set buttons
       wb_set_text(wb_get_control($window,    ID_DB_BUTTON),STOP_DB_BTXT);  // set button text
       wb_set_enabled(wb_get_control($window, ID_CLIENT_BUTTON), TRUE);     // Enable Client button 
       wb_set_enabled(wb_get_control($window, ID_PHPMOADMIN_BUTTON), TRUE); // Enable Admin button 

       wb_set_text($statusbar_mongo, " Servers running");  // Inform user 
     }

Top

Stop

If the button is displaying Stop MongoDB and Apache when clicked we want to perform the following actions:

  • If Mongo-client running
  1. Prompt user to close it.
  • Mongo-client not running
  1. Stop MongoDB server
  2. Stop Apache server
  3. Set button text to start
  4. Disable Mongo-client button
  5. Disable phpMoAdmin button

Gives the following code:

     else{                         // Stop   server
       if(mongo_client_running()){ // Client must be stopped before Mongo Server
         $str  = "Before you can stop MongoDB \n";
         $str .= "Please close Mongo Client \n\n";

         $str .= "Type EXIT to close client\n";
         $str .= "Data from RAM needs to be written to disk.\n";

         wb_message_box($window, $str, "CLIENT RUNNING", WBC_INFO);          
         wb_set_text($statusbar_mongo, " Clienr running");  // Inform user         
         break;
       }
       else{                   // Client not running
         //== Stop Apache
         $cmd = "../../usr/local/php/php.exe -n ../main/stop_servers.php 1";  // Cmd to run hidden
         run_cmd_hidden($cmd);                            // Run command
         wb_set_text($statusbar_mongo, " Stopping Apache");  // Inform user 

         //== Stop MongoDB
         stop_mongo_no_auth();
         wb_set_text($statusbar_mongo, " Stopping MongoDB");  // Inform user           

         //==Set button text
         wb_set_text(wb_get_control($window,    ID_DB_BUTTON),START_DB_BTXT);  // set button text
         wb_set_enabled(wb_get_control($window, ID_CLIENT_BUTTON), FALSE);  // DISABLE Client button 
         wb_set_enabled(wb_get_control($window, ID_PHPMOADMIN_BUTTON), FALSE); // Disable Test button 
         wb_set_text($statusbar_mongo, " Servers stopped");                    // Inform user 
      }
     }

Top

Complete button code

 //=== Start-Stop Servers ===================================================
   case ID_DB_BUTTON:          // Button toggles
     $text =  wb_get_text(wb_get_control($window, ID_DB_BUTTON));  // get button text

     if($text == START_DB_BTXT ){ // Start server

       //== Start Apache
       $cmd = "../../usr/local/php/php.exe -n ../main/start_servers.php 1"; // Cmd to run hidden
       run_cmd_hidden($cmd);                               // Run command
       wb_set_text($statusbar_mongo, " Starting Apache");  // Inform user 

       //== Start MongoDB
       start_mongo_no_auth();
       wb_set_text($statusbar_mongo, " Starting MongoDB");  // Inform user 

       //== Set buttons
       wb_set_text(wb_get_control($window,    ID_DB_BUTTON),STOP_DB_BTXT);  // set button text
       wb_set_enabled(wb_get_control($window, ID_CLIENT_BUTTON), TRUE);     // Enable Client button 
       wb_set_enabled(wb_get_control($window, ID_PHPMOADMIN_BUTTON), TRUE); // Enable Admin button 

       wb_set_text($statusbar_mongo, " Servers running");  // Inform user 
     }

     else{                         // Stop   server
       if(mongo_client_running()){ // Client must be stopped before Mongo Server
         $str  = "Before you can stop MongoDB \n";
         $str .= "Please close Mongo Client \n\n";

         $str .= "Type EXIT to close client\n";
         $str .= "Data from RAM needs to be written to disk.\n";

         wb_message_box($window, $str, "CLIENT RUNNING", WBC_INFO);          
         wb_set_text($statusbar_mongo, " Clienr running");  // Inform user         
         break;
       }
       else{                   // Client not running
         //== Stop Apache
         $cmd = "../../usr/local/php/php.exe -n ../main/stop_servers.php 1";  // Cmd to run hidden
         run_cmd_hidden($cmd);                            // Run command
         wb_set_text($statusbar_mongo, " Stopping Apache");  // Inform user 

         //== Stop MongoDB
         stop_mongo_no_auth();
         wb_set_text($statusbar_mongo, " Stopping MongoDB");  // Inform user           

         //==Set button text
         wb_set_text(wb_get_control($window,    ID_DB_BUTTON),START_DB_BTXT);  // set button text
         wb_set_enabled(wb_get_control($window, ID_CLIENT_BUTTON), FALSE);  // DISABLE Client button 
         wb_set_enabled(wb_get_control($window, ID_PHPMOADMIN_BUTTON), FALSE); // Disable Test button 
         wb_set_text($statusbar_mongo, " Servers stopped");                    // Inform user 
      }
     }

   break; 
   //================================================ END Start-Stop Servers ==

Top

Start Client

Starting the client requires running its corresponding function. If already running inform user and exit case statement. Code snippets as shown below:

   //=== Start Client =========================================================
   case ID_CLIENT_BUTTON:          // Button 
    if(mongo_client_running()){    // Client must be stopped before Mongo Server
      wb_set_text($statusbar_mongo, " No action taken client already running");  // Inform user        
      break; 
    }
    else{
       client_no_auth();           // Start a cmd window and run mohgo command line interface
       wb_set_text($statusbar_mongo, " Client started");  // Inform user 
    }
   break; 
   //===================================================== END Start Client ===

Top

Complete handler function

Adding all the snippets of code gives the following

//=== 4) Handler Function =====================================================
function process_mongo($window, $id){
  global $win_mongo;
  global $statusbar_mongo;

   switch($id) {

   //=== Start-Stop Servers ===================================================
   case ID_DB_BUTTON:          // Button toggles
     $text =  wb_get_text(wb_get_control($window, ID_DB_BUTTON));  // get button text

     if($text == START_DB_BTXT ){ // Start server

       //== Start Apache
       $cmd = "../../usr/local/php/php.exe -n ../main/start_servers.php 1"; // Cmd to run hidden
       run_cmd_hidden($cmd);                               // Run command
       wb_set_text($statusbar_mongo, " Starting Apache");  // Inform user 

       //== Start MongoDB
       start_mongo_no_auth();
       wb_set_text($statusbar_mongo, " Starting MongoDB");  // Inform user 

       //== Set buttons
       wb_set_text(wb_get_control($window,    ID_DB_BUTTON),STOP_DB_BTXT);  // set button text
       wb_set_enabled(wb_get_control($window, ID_CLIENT_BUTTON), TRUE);     // Enable Client button 
       wb_set_enabled(wb_get_control($window, ID_PHPMOADMIN_BUTTON), TRUE); // Enable Admin button 

       wb_set_text($statusbar_mongo, " Servers running");  // Inform user 
     }

     else{                         // Stop   server
       if(mongo_client_running()){ // Client must be stopped before Mongo Server
         $str  = "Before you can stop MongoDB \n";
         $str .= "Please close Mongo Client \n\n";

         $str .= "Type EXIT to close client\n";
         $str .= "Data from RAM needs to be written to disk.\n";

         wb_message_box($window, $str, "CLIENT RUNNING", WBC_INFO);          
         wb_set_text($statusbar_mongo, " Clienr running");  // Inform user         
         break;
       }
       else{                   // Client not running
         //== Stop Apache
         $cmd = "../../usr/local/php/php.exe -n ../main/stop_servers.php 1";  // Cmd to run hidden
         run_cmd_hidden($cmd);                            // Run command
         wb_set_text($statusbar_mongo, " Stopping Apache");  // Inform user 

         //== Stop MongoDB
         stop_mongo_no_auth();
         wb_set_text($statusbar_mongo, " Stopping MongoDB");  // Inform user           

         //==Set button text
         wb_set_text(wb_get_control($window,    ID_DB_BUTTON),START_DB_BTXT);  // set button text
         wb_set_enabled(wb_get_control($window, ID_CLIENT_BUTTON), FALSE);  // DISABLE Client button 
         wb_set_enabled(wb_get_control($window, ID_PHPMOADMIN_BUTTON), FALSE); // Disable Test button 
         wb_set_text($statusbar_mongo, " Servers stopped");                    // Inform user 
      }
     }

   break; 
   //================================================ END Start-Stop Servers ==

   //=== Start Client =========================================================
   case ID_CLIENT_BUTTON:          // Button 
    if(mongo_client_running()){    // Client must be stopped before Mongo Server
      wb_set_text($statusbar_mongo, " No action taken client already running");  // Inform user        
      break; 
    }
    else{
       client_no_auth();           // Start a cmd window and run mohgo command line interface
       wb_set_text($statusbar_mongo, " Client started");  // Inform user 
    }
   break; 
   //===================================================== END Start Client ===


   //=== Open command window ==================================================
   // Open a command window in folder bin
   case ID_OPEN_CMD_BUTTON:          // Button 
    open_cmd_window();
   break;
   //============================================== END Open command window ===


   //=== Display help info ====================================================
   case ID_HELP_BUTTON:          // Button 

    wb_exec('Notepad',INFO_TXT);

   break; 
   //================================================ END Display help info ===

   case IDCLOSE:                             // Constant IDCLOSE (8) predefined
      wb_destroy_window($window);            // Destroy the window
      break;
   }
}
//================================================ END 4) Handler Function ===

Test

Run Run_mongo_db.bat

  • Check server functions as expected
  • Check client has access to the server
  • Perform other tests as required
  • Finally test Stop server works

Top

Summary

At this stage we have a fully working plugin.

All that remains is to add functionality to phpMoAdmin button and clean up covered on next page.


Top