https://wiki.uniformserver.com/index.php?title=MongoDB_Tutorial_2:_Buttons_3&feed=atom&action=historyMongoDB Tutorial 2: Buttons 3 - Revision history2024-03-29T15:01:06ZRevision history for this page on the wikiMediaWiki 1.41.0https://wiki.uniformserver.com/index.php?title=MongoDB_Tutorial_2:_Buttons_3&diff=4330&oldid=prevRic: 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...2010-08-01T13:15:27Z<p>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...</p>
<p><b>New page</b></p><div>{{Nav MongoDB Tutorial 2}}<br />
== Introduction ==<br />
This page covers adding functionality to button '''''Run phpMoAdmin'''''.<br />
<br />
Clicking phpMoAdmin opens it in a browser this MongoDB GUI administration tool is a single PHP script allowing you to administer the server.<br />
<br />
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.<br />
<br />
Trick here is to use html file association and redirection.<br />
<br />
== Redirection ==<br />
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.<br />
<br />
Double clicking on a file with a '''''.html''''' extension you will see something similar to this displayed in your browser address bar:<br />
<pre><br />
file:///C:/Carbo_6_0_0_mongo/z_mongo/UniServer/docs/<br />
</pre><br />
<br />
* '''file:///C:/''' Shows it is being read directly from disk<br />
Whats impotent page is displayed in the browser all browsers honor code contained in <head> tag.<br />
<br />
Placing a refresh Meta tag in the header section we can force a page to automatically refresh and more impotently redirect to another page.<br />
<br />
For example:<br />
<pre><br />
<meta http-equiv="refresh" content="1;url=http://localhost:port/apanel/phpmoadmin/moadmin.php" /><br />
</pre><br />
'''''Note'':'''<br />
<br />
A user can change the Apache server port hence this is included in the meta tag.<br />
<br />
'''''[[#top | Top]]'''''<br />
== Implementation ==<br />
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.<br />
<br />
Alternatively create page on the fly and load it into browser.<br />
<br />
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.<br />
<br />
'''''[[#top | Top]]'''''<br />
=== Code ===<br />
{|<br />
|-<br />
|<br />
<pre><br />
//=== View phpMoAdmin page =================================================<br />
case ID_PHPMOADMIN_BUTTON: // Button <br />
<br />
//== Create or update a new redirect page <br />
$apache_port = get_apache_port(); // Get real port this may have changed<br />
$str = '<html><head>'."\n";<br />
$str .= '<meta http-equiv="refresh" content="1;url=http://localhost:';<br />
$str .= $apache_port.'/apanel/phpmoadmin/moadmin.php">'."\n";<br />
$str .= '<title>Uniform Server</title></head>'."\n";<br />
$str .= '<body></body></html>'."\n";<br />
<br />
file_put_contents(REDIRECT_PAGE,$str); // Save string to file<br />
<br />
wb_exec(REDIRECT_PAGE); // Run redirect file mongo_redirect.html<br />
<br />
break; <br />
//============================================= END View phpMoAdmin page ===<br />
</pre><br />
|<br />
* Intercept button - ID_PHPMOADMIN_BUTTON<br />
* Read Apache port from configuration file - get_apache_port()<br />
* Build page as a single string -$str<br />
* Save string to file - file_put_contents()<br />
* Load page into browser using wb_exec() <br />
<br />
|}<br />
'''''[[#top | Top]]'''''<br />
=== Path ===<br />
We need to add the constant REDIRECT_PAGE to file mongo_db_inc.php<br />
<pre><br />
define("REDIRECT_PAGE", "$mongo_base_f/UniServer/home/admin/www/phpMoAdmin/mongo_redirect.html");<br />
</pre><br />
<br />
'''''[[#top | Top]]'''''<br />
=== Complete handler function ===<br />
Adding the above code snippets gives the following final handler function <br />
<pre><br />
//=== 4) Handler Function =====================================================<br />
function process_mongo($window, $id){<br />
global $win_mongo;<br />
global $statusbar_mongo;<br />
<br />
switch($id) {<br />
<br />
//=== Start-Stop Servers ===================================================<br />
case ID_DB_BUTTON: // Button toggles<br />
$text = wb_get_text(wb_get_control($window, ID_DB_BUTTON)); // get button text<br />
<br />
if($text == START_DB_BTXT ){ // Start server<br />
<br />
//== Start Apache<br />
$cmd = "../../usr/local/php/php.exe -n ../main/start_servers.php 1"; // Cmd to run hidden<br />
run_cmd_hidden($cmd); // Run command<br />
wb_set_text($statusbar_mongo, " Starting Apache"); // Inform user <br />
<br />
//== Start MongoDB<br />
start_mongo_no_auth();<br />
wb_set_text($statusbar_mongo, " Starting MongoDB"); // Inform user <br />
<br />
//== Set buttons<br />
wb_set_text(wb_get_control($window, ID_DB_BUTTON),STOP_DB_BTXT); // set button text<br />
wb_set_enabled(wb_get_control($window, ID_CLIENT_BUTTON), TRUE); // Enable Client button <br />
wb_set_enabled(wb_get_control($window, ID_PHPMOADMIN_BUTTON), TRUE); // Enable Admin button <br />
<br />
wb_set_text($statusbar_mongo, " Servers running"); // Inform user <br />
}<br />
<br />
else{ // Stop server<br />
if(mongo_client_running()){ // Client must be stopped before Mongo Server<br />
$str = "Before you can stop MongoDB \n";<br />
$str .= "Please close Mongo Client \n\n";<br />
<br />
$str .= "Type EXIT to close client\n";<br />
$str .= "Data from RAM needs to be written to disk.\n";<br />
<br />
wb_message_box($window, $str, "CLIENT RUNNING", WBC_INFO); <br />
wb_set_text($statusbar_mongo, " Clienr running"); // Inform user <br />
break;<br />
}<br />
else{ // Client not running<br />
//== Stop Apache<br />
$cmd = "../../usr/local/php/php.exe -n ../main/stop_servers.php 1"; // Cmd to run hidden<br />
run_cmd_hidden($cmd); // Run command<br />
wb_set_text($statusbar_mongo, " Stopping Apache"); // Inform user <br />
<br />
//== Stop MongoDB<br />
stop_mongo_no_auth();<br />
wb_set_text($statusbar_mongo, " Stopping MongoDB"); // Inform user <br />
<br />
//==Set button text<br />
wb_set_text(wb_get_control($window, ID_DB_BUTTON),START_DB_BTXT); // set button text<br />
wb_set_enabled(wb_get_control($window, ID_CLIENT_BUTTON), FALSE); // DISABLE Client button <br />
wb_set_enabled(wb_get_control($window, ID_PHPMOADMIN_BUTTON), FALSE); // Disable Test button <br />
wb_set_text($statusbar_mongo, " Servers stopped"); // Inform user <br />
}<br />
}<br />
<br />
break; <br />
//================================================ END Start-Stop Servers ==<br />
<br />
//=== Start Client =========================================================<br />
case ID_CLIENT_BUTTON: // Button <br />
if(mongo_client_running()){ // Client must be stopped before Mongo Server<br />
wb_set_text($statusbar_mongo, " No action taken client already running"); // Inform user <br />
break; <br />
}<br />
else{<br />
client_no_auth(); // Start a cmd window and run mohgo command line interface<br />
wb_set_text($statusbar_mongo, " Client started"); // Inform user <br />
}<br />
break; <br />
//===================================================== END Start Client ===<br />
<br />
//=== View phpMoAdmin page =================================================<br />
case ID_PHPMOADMIN_BUTTON: // Button <br />
<br />
//== Create or update a new redirect page <br />
$apache_port = get_apache_port(); // Get real port this may have changed<br />
$str = '<html><head>'."\n";<br />
$str .= '<meta http-equiv="refresh" content="1;url=http://localhost:';<br />
$str .= $apache_port.'/apanel/phpmoadmin/moadmin.php">'."\n";<br />
$str .= '<title>Uniform Server</title></head>'."\n";<br />
$str .= '<body></body></html>'."\n";<br />
<br />
file_put_contents(REDIRECT_PAGE,$str); // Save string to file<br />
<br />
wb_exec(REDIRECT_PAGE); // Run redirect file mongo_redirect.html<br />
<br />
break; <br />
//============================================= END View phpMoAdmin page ===<br />
<br />
<br />
//=== Open command window ==================================================<br />
// Open a command window in folder bin<br />
case ID_OPEN_CMD_BUTTON: // Button <br />
open_cmd_window();<br />
break;<br />
//============================================== END Open command window ===<br />
<br />
<br />
//=== Display help info ====================================================<br />
case ID_HELP_BUTTON: // Button <br />
<br />
wb_exec('Notepad',INFO_TXT);<br />
<br />
break; <br />
//================================================ END Display help info ===<br />
<br />
case IDCLOSE: // Constant IDCLOSE (8) predefined<br />
wb_destroy_window($window); // Destroy the window<br />
break;<br />
}<br />
}<br />
//================================================ END 4) Handler Function ===<br />
</pre><br />
'''''[[#top | Top]]'''''<br />
<br />
== Finishing touches ==<br />
Create a folder z_mongo\UniServer\usr\local\mongo\'''top_level'''<br />
<br />
It contains a single file '''''Run_MongoDB.bat''''' with the following content:<br />
<pre><br />
cd usr\local\php<br />
php.exe -c mongo_cli.ini ..\..\..\unicon\tray_menu_2\mongo_db.php<br />
EXIT<br />
</pre> <br />
To run server copy this file to folder UniServer. Alternatively convert the file to an exe using [[PHP WinBinder: Introduction#Bat To Exe Converter | '''bat2exe''']]<br />
<br />
Finally delete folder z_mongo\temp no longer required.<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== Summary ==<br />
<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
----<br />
<br />
[[Category: Tutorials]]<br />
[[Category: How To]]<br />
[[Category: Uniform Server 6-Carbo]]</div>Ric