Difference between revisions of "MongoDB Tutorial 2: Buttons 3"

From The Uniform Server Wiki
Jump to navigation Jump to search
(New page: {{Nav MongoDB Tutorial 2}} == Introduction == This page covers adding functionality to button '''''Run phpMoAdmin'''''. Clicking phpMoAdmin opens it in a browser this MongoDB GUI administ...)
 
(No difference)

Latest revision as of 13:15, 1 August 2010

 

MongoDB Plugin
UniServer 6-Carbo.

Introduction

This page covers adding functionality to button Run phpMoAdmin.

Clicking phpMoAdmin opens it in a browser this MongoDB GUI administration tool is a single PHP script allowing you to administer the server.

From a scripting point of view difficult part is to run this disk file as a PHP page and have it served from the Apache server.

Trick here is to use html file association and redirection.

Redirection

There are two file associations that are sacrosanct htm and .html these files are opened in the default browser. This redirection solution assumes a user has not changed these file association.

Double clicking on a file with a .html extension you will see something similar to this displayed in your browser address bar:

file:///C:/Carbo_6_0_0_mongo/z_mongo/UniServer/docs/
  • file:///C:/ Shows it is being read directly from disk

Whats impotent page is displayed in the browser all browsers honor code contained in <head> tag.

Placing a refresh Meta tag in the header section we can force a page to automatically refresh and more impotently redirect to another page.

For example:

<meta http-equiv="refresh" content="1;url=http://localhost:port/apanel/phpmoadmin/moadmin.php" />

Note:

A user can change the Apache server port hence this is included in the meta tag.

Top

Implementation

We can create a separate html page. Every time the phpMoAdmin button is clicked perform a port search and replace. Then load the page into a browser.

Alternatively create page on the fly and load it into browser.

Every time button is clicked both methods require disk access. Creating page on the fly has a small advantage; user can delete page with no adverse effect on operation hence is the preferred solution.

Top

Code

   //=== View phpMoAdmin page =================================================
   case ID_PHPMOADMIN_BUTTON:          // Button 

       //== Create or update a new redirect page 
       $apache_port = get_apache_port(); // Get real port this may have changed
       $str  = '<html><head>'."\n";
       $str .= '<meta http-equiv="refresh" content="1;url=http://localhost:';
       $str .= $apache_port.'/apanel/phpmoadmin/moadmin.php">'."\n";
       $str .= '<title>Uniform Server</title></head>'."\n";
       $str .= '<body></body></html>'."\n";
 
       file_put_contents(REDIRECT_PAGE,$str); // Save string to file

       wb_exec(REDIRECT_PAGE);   // Run redirect file mongo_redirect.html

   break; 
   //============================================= END View phpMoAdmin page ===
  • Intercept button - ID_PHPMOADMIN_BUTTON
  • Read Apache port from configuration file - get_apache_port()
  • Build page as a single string -$str
  • Save string to file - file_put_contents()
  • Load page into browser using wb_exec()

Top

Path

We need to add the constant REDIRECT_PAGE to file mongo_db_inc.php

define("REDIRECT_PAGE", "$mongo_base_f/UniServer/home/admin/www/phpMoAdmin/mongo_redirect.html");

Top

Complete handler function

Adding the above code snippets gives the following final handler function

//=== 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 ===

   //=== View phpMoAdmin page =================================================
   case ID_PHPMOADMIN_BUTTON:          // Button 

       //== Create or update a new redirect page 
       $apache_port = get_apache_port(); // Get real port this may have changed
       $str  = '<html><head>'."\n";
       $str .= '<meta http-equiv="refresh" content="1;url=http://localhost:';
       $str .= $apache_port.'/apanel/phpmoadmin/moadmin.php">'."\n";
       $str .= '<title>Uniform Server</title></head>'."\n";
       $str .= '<body></body></html>'."\n";
 
       file_put_contents(REDIRECT_PAGE,$str); // Save string to file

       wb_exec(REDIRECT_PAGE);   // Run redirect file mongo_redirect.html

   break; 
   //============================================= END View phpMoAdmin page ===


   //=== 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 ===

Top

Finishing touches

Create a folder z_mongo\UniServer\usr\local\mongo\top_level

It contains a single file Run_MongoDB.bat with the following content:

cd usr\local\php
php.exe -c mongo_cli.ini ..\..\..\unicon\tray_menu_2\mongo_db.php
EXIT

To run server copy this file to folder UniServer. Alternatively convert the file to an exe using bat2exe

Finally delete folder z_mongo\temp no longer required.

Top

Summary

Top