PHP WinBinder: Alternative control 1

From The Uniform Server Wiki
Jump to: navigation, search

 

UniServer 5-Nano
PHP WinBinder.

Alternative control

WinBinder is very powerful allowing you to create either simple or complex Windows programs. Uniform server 4-Mona series introduced UniController a simple Windows program to control both Apache and MySQL servers. From a users point of view it is extremely easy to use and understand hence it remains popular.

This section covers a similar interface for Uniform Server Nano-5 series. It’s a no frills application that demonstrates the capability of WinBinder. It’s a step-by-step guide and includes a working example at each step of the design.

Specification

A design starts with a specification, this can be a written document or as in this case a description of what it is intended to do for example:

UniController:
  • Has a minimal user interface and built in diagnostics.
  • Checks server ports in use
  • Has the capability to run multi-servers.
  • Independent control of Apache and MySQL servers
  • Apache configuration syntax check.
  • Option to open a MySQL CMD (command) window.
UnicontrollerV2.gif

Apart from test all buttons perform what’s displayed.
Test hides its functionality because it supports options that are rarely used.

  • The initial click checks server port status.
  • Then offers a choice to check Apache configuration syntax.
  • Finally offers a choice to run a MySQL prompt.

Note:

Original buttons used a rollover effect this adds unnecessary complexity and will not be implemented in this design.

UniServer 5-Nano series don’t use a virtual drive the label and input box are deleted. This leaves a gap, we can fill with two additional buttons.

The "About" button occupies valuable space adding nothing to functionality hence replace with a button that performs something useful.

Top

Starting point

  • Create a new folder UniServer\plugins\winbinder\alt_con_1
  • From folder UniServer\plugins\winbinder\examples copy files test_1.bat and test_1.phpw to it.

Run test_1.bat it crates a basic window.

This window needs to be modified as follows:

Edit file test_1.phpw

There are three changes to section 1 and a single chage in section 3 as follows:

  • 1) Run as Single application instance see page
    Note: This program's name is UniController
  • 1) Application minimize to system tray see page (style WBC_TASKBAR)
  • 1) Application always on top use style WBC_TOP

test_1.phpw

<?php
Include "../php/include/winbinder.php";       // Location Of Winbinder Library

//=== 1) Create main window ---------------------------------------------------
define("CAPTION", "UniController");     // Set caption to match application
if(wb_get_instance(CAPTION, TRUE))      // Is there an existing instance?
    die;                                // Yes: bring it to the front and quit

$mainwin = wb_create_window(NULL, AppWindow, "UniController", WBC_CENTER,
 WBC_CENTER, 255, 165, WBC_NOTIFY| WBC_TASKBAR | WBC_TOP , WBC_HEADERSEL);


//=== 2) Create controls for the main window ----------------------------------

//=== 3) Assign handler function to the main window  --------------------------
wb_set_handler($mainwin, "process_main");
  wb_set_image($mainwin, "./" . "uslogo.ico");  // Add logo        

//=== 5) Enter application loop -----------------------------------------------
wb_main_loop();                                  

//=== 4) Handler Function -----------------------------------------------------
function process_main($window, $id) 
{
  switch($id) { 

   case IDCLOSE:                          // Constant IDCLOSE (8) predefined 
    wb_destroy_window($window);           // Destroy the window
    break; 
  }
}
?>

Top

Add Controls

Create a copy of test_1.phpw rename it test_2.phpw

Add controls as shown below:

test_2.phpw

<?php
Include "../php/include/winbinder.php";       // Location Of Winbinder Library

//-- Constants ----------------------------------------------------------------

define("ID_L1101", 1101);      // Label Server version 

define("ID_PB_ALL", 1201);     // Push button Start/Stop servers
define("ID_PB_APACHE", 1202);  // Push button Start/Stop Apache  
define("ID_PB_MYSQL", 1203);   // Push button Start/Stop MySQL

define("ID_PB_APANEL", 1204);     // Push button Apanel
define("ID_PB_PHPMYADMIN", 1205); // Push button phpMyAdmin
define("ID_PB_MYCMD", 1206);      // Push button My CMD
define("ID_PB_LOGS", 1207);       // Push button Logs
define("ID_PB_TEST", 1208);       // Push button Test
define("ID_PB_EDIT", 1209);       // Push button Edit

define("ID_F_APACHE_RED", 1420);   // Frame indicator red Apache 
define("ID_F_APACHE_GREEN", 1421); // Frame indicator green  Apache
define("ID_F_MYSQL_RED", 1422);    // Frame indicator red MySQL 
define("ID_F_MYSQL_GREEN", 1423);  // Frame indicator green  MySQL

define("ID_F_LOGO", 1424); // Images logo 

//=== 1) Create main window ===================================================
define("CAPTION", "UniController");     // Set caption to match application
if(wb_get_instance(CAPTION, TRUE))      // Is there an existing instance?
    die;                                // Yes: bring it to the front and quit

$mainwin = wb_create_window(NULL, AppWindow, "UniController", WBC_CENTER,
 WBC_CENTER, 240, 155, WBC_NOTIFY| WBC_TASKBAR | WBC_TOP , WBC_HEADERSEL);

//=== 2) Create controls for the main window ==================================
//-- Label version
wb_create_control($mainwin, Label, "Server 5.0-Nano", 114, 6, 101, 41,  ID_L1101, 0, 0, 3);

//-- Image logo
$frame = wb_create_control($mainwin, Frame, "", 4, 4, 99, 39, ID_F_LOGO, WBC_IMAGE); // Apache OFF
wb_set_image($frame,  'Logo.bmp', NOCOLOR);

//-- Create indicators
 $frame = wb_create_control($mainwin, Frame, "", 209, 27, 20, 12, ID_F_APACHE_RED, WBC_IMAGE); // Apache OFF
 wb_set_image($frame,  'red.bmp', NOCOLOR);
 $frame = wb_create_control($mainwin, Frame, "", 209, 27, 20, 12, ID_F_APACHE_GREEN, WBC_IMAGE); // Apache ON
 wb_set_image($frame,  'green.bmp', NOCOLOR);

 $frame = wb_create_control($mainwin, Frame, "", 209, 52, 20, 12, ID_F_MYSQL_RED, WBC_IMAGE); // MySQL OFF
 wb_set_image($frame,  'red.bmp', NOCOLOR);
 $frame = wb_create_control($mainwin, Frame, "", 209, 52, 20, 12, ID_F_MYSQL_GREEN, WBC_IMAGE); // MySQL ON
 wb_set_image($frame,  'green.bmp', NOCOLOR);

//-- Buttons
wb_create_control($mainwin, PushButton, 'Start All', 4, 49, 99, 20, ID_PB_ALL, 0, 0, 0);
wb_create_control($mainwin, PushButton, 'Start Apache', 110, 24, 90, 20, ID_PB_APACHE, 0, 0, 0);
wb_create_control($mainwin, PushButton, 'Start MySQL', 110, 49, 90, 20,  ID_PB_MYSQL, 0, 0, 0);
wb_create_control($mainwin, PushButton, 'Apanel', 4, 74, 99, 20, ID_PB_APANEL, 0, 0, 0);
wb_create_control($mainwin, PushButton, 'phpMyAdmin', 4, 99, 99, 20, ID_PB_PHPMYADMIN, 0, 0, 0);

wb_create_control($mainwin, PushButton, 'My CMD', 109, 74, 57, 20, ID_PB_MYCMD, 0, 0, 0);
wb_create_control($mainwin, PushButton, 'Logs', 173, 74, 57, 20, ID_PB_LOGS, 0, 0, 0);

wb_create_control($mainwin, PushButton, 'Test', 109, 99, 57, 20, ID_PB_TEST, 0, 0, 0);
wb_create_control($mainwin, PushButton, 'Edit', 173, 99, 57, 20, ID_PB_EDIT, 0, 0, 0);

//=== 3) Assign handler function to the main window ===========================
wb_set_handler($mainwin, "process_main");
  wb_set_image($mainwin, "./" . "uslogo.ico");  // Add logo        

//=== 5) Enter application loop ===============================================
wb_main_loop();                                  

//=== 4) Handler Function =====================================================
function process_main($window, $id) 
{
  switch($id) { 

   case IDCLOSE:                          // Constant IDCLOSE (8) predefined 
    wb_destroy_window($window);           // Destroy the window
    break; 
  }
}
?>

It’s a fully functional Windows program. To see it in action run file UniServer\plugins\winbinder\alt_con_1\test_2.bat

Top

Images

For comparision I have shown the two controllers side-by-side:

New Version
WinBinder Unicon 1.gif

Old Version
UnicontrollerV2.gif



New Version

  • In terms of physical size the new version is smaller.
  • Ergonomically it is cleaner
  • It retains a simple click and “go” approach.
  • New buttons add additional functionality.

It may look pretty! However for a real application it lacks functionality click any button and you will find it produces no results.

Remainder of this tutorial looks at adding functionality.

Make a copy of test_2.phpw and rename it test_3.phpw add the following:

Top

Includes

In order to tap into UniServr’s control core you need to include the following two files:

include "../../../unicon/main/includes/config.inc.php"; // US Core
include "../../../unicon/main/includes/functions.php";  // US Core 

They provide pre-defined variables, constants and functions that can be used in your windows application.

Top

Initialisation

Before adding any functionality to the push buttons some basic initialisation is required.

Port Check

Server ports may already be in use preventing this server running. Just below the constant section add port checking code as shown below

//Port check and alert
if(port_in_use(get_apache_port()) && get_apache_tracker() == "free"){
  $port = get_apache_port();
  wb_message_box(NULL, "Apache port: $port \nIn use by another program.","Apach Port", WBC_WARNING); 
}
if(port_in_use(get_apache_ssl_port()) && get_apache_tracker() == "free"){
  $port = get_apache_ssl_port();
  wb_message_box(NULL, "Apache poert: $port\nIn use by another program.","Apache Port", WBC_WARNING); 
}
if(port_in_use(get_mysql_port()) && get_mysql_tracker() == "free"){
  $port = get_mysql_port();
  wb_message_box(NULL, "MySQL port: $port\nIn use by another program.","MySQL Port", WBC_WARNING); 
}

The above could be written as a single alert however one per port emphasises there is a real problem.

Note: Ports are obtained from appropriate configuration files using core functions get_apache_port(), get_apache_ssl_port() and get_mysql_port()

A user can change these at any time so always read the current value from a configuration file before using.

Top

Server moved check

The servers are portable to achieve this every time the control program is started it must run function run_location_tracker().

This function compares current server location to that stored in tracker file (UniServer\unicon\main\location_tracker.txt) if the two paths are different all absolute paths in appropriate configuration files are rewritten to new location and tracker file updated accordingly. If the two paths match no action is taken.

Above the window creation line add the following:

run_location_tracker();  // Have servers moved if moved update configuration

Top

Server Version

Current server version is contained in variable $us_version to display this to a user: Change the following line:

//-- Label version
wb_create_control($mainwin, Label, "Server 5.0-Nano", 114, 6, 101, 41,  ID_L_VERSION, 0, 0, 3);

To this:

//-- Label version
wb_create_control($mainwin, Label, "Server $us_version", 114, 6, 101, 41,  ID_L_VERSION, 0, 0, 3);

The label caption is updated to that of the current server version.

Top

Main init function

Before starting the main loop with function wb_main_loop() add any initialisation to section 3. Currently the logo image is set in this section.

For our program we want a general purpose initialisation function main_init() that runs once when the program is started and thereafter when we perform specific actions. A call to this function is added to this section as follows:

  wb_set_image($mainwin, "./" . "uslogo.ico");  // Add logo        
  main_init();

If you run the script a fatal error will occur.

To the end of your script add the following dummy function. We will fill the details in later

//=== Initialise Main =========================================================
function main_init(){
 global $mainwin;
  wb_message_box($mainwin, "Main Init","Temp", WBC_OK); 
}
//===================================================== END Initialise Main ===

Top

Summary

That completes the initial groundwork to see an example run test_3.bat the code is contained in file test_3.phpw

With the above structure in place we can start adding functionally.

Top

Function main_init()

The three buttons "Start All", "Start Apache" and "Start MySQL" toggle between two states start and stop.

A separate variable is not required to store a button state by virtue of its caption each button inherently has a memory.

Apache and MySQL operating state define button and indicator states as follows.

  • Apache indicator: Red server not running. Green server running
  • MuSQL indicator: Red server not running. Green server running
  • Start All button: No servers running displays "Start All" both servers running displays "Stop All" One or other server running button disabled.
  • Apache button: Server not running displays "Start Apache" server running displays "Stop Apache"
  • MySQL button: Server not running displays "Start MySQL" server running displays "Stop MySQL"
  • Apanel button: Apache not running button disabled. Apache running button enabled.
  • phpMyAdmin button: Apache not running button disabled. Apache running button enabled.

Starting our application requires the above states to be checked and button and indicators updated accordingly. A user starting or stopping a server again requires the above state check and updates. Code for this is placed in the main_init() function

Code as follows:





Set status of servers

Variables $AP and $MP are shorthand for

  • Servers running “TRUE”
  • Or not running “FALSE”

just makes coding a little easier






Both servers not running

Set button message a set indicator accordingly








MySQL running $MP

Set button message a set indicator accordingly








Apache running $AP

Set button message a set indicator accordingly








Both servers running

Set button message a set indicator accordingly

//== Set status of servers

 // Apache running as a program $AP
 if(apache_running()){
  $AP = TRUE;
 }
 else{
  $AP = FALSE;
 }

 // MySQL running as a program $MP
 if(mysql_running()){
  $MP = TRUE;
 }
 else{
  $MP = FALSE;
 } 

 //00
 if(!$AP & !$MP){
   mp_apache_indicator_on(FALSE);                                      // OFF
   mp_mysql_indicator_on(FALSE);                                       // OFF
   wb_set_enabled(wb_get_control($mainwin, ID_PB_APANEL), FALSE);      // Disable Apanel
   wb_set_enabled(wb_get_control($mainwin, ID_PB_PHPMYADMIN), FALSE);  // Disable phpMyAdmin
   wb_set_enabled(wb_get_control($mainwin,  ID_PB_MYCMD), FALSE);      // Disabe MySQL CMD button
   wb_set_enabled(wb_get_control($mainwin, ID_PB_ALL), TRUE);          // Enable ALL server button 
   wb_set_text(wb_get_control($mainwin, ID_PB_APACHE),'Start Apache'); // set button
   wb_set_text(wb_get_control($mainwin, ID_PB_MYSQL),'Start MySQL');   // set button 
   wb_set_text(wb_get_control($mainwin, ID_PB_ALL),'Start All');       // set button 
   wb_set_enabled(wb_get_control($mainwin, ID_PB_APACHE), TRUE);       // Enable Apache button 
   wb_set_enabled(wb_get_control($mainwin, ID_PB_MYSQL), TRUE);        // Enable MySQL button
 }

 //01
 if(!$AP & $MP){
   mp_apache_indicator_on(FALSE);                                      // OFF
   mp_mysql_indicator_on(TRUE);                                        // ON
   wb_set_enabled(wb_get_control($mainwin, ID_PB_APANEL), FALSE);      // Disable Apanel
   wb_set_enabled(wb_get_control($mainwin, ID_PB_PHPMYADMIN), FALSE);  // Disable phpMyAdmin
   wb_set_enabled(wb_get_control($mainwin,  ID_PB_MYCMD), TRUE);       // Enable MySQL CMD button
   wb_set_enabled(wb_get_control($mainwin, ID_PB_ALL), FALSE);         // Disable All server button 
   wb_set_text(wb_get_control($mainwin, ID_PB_APACHE),'Start Apache'); // set button
   wb_set_text(wb_get_control($mainwin, ID_PB_MYSQL),'Stop MySQL');    // set button
   wb_set_text(wb_get_control($mainwin, ID_PB_ALL),'Stop ???');        // set button 
   wb_set_enabled(wb_get_control($mainwin, ID_PB_APACHE), TRUE);       // Enable Apache button 
   wb_set_enabled(wb_get_control($mainwin, ID_PB_MYSQL), TRUE);        // Enable MySQL button
 }

 //10
 if($AP & !$MP){
   mp_apache_indicator_on(TRUE);                                      // ON
   mp_mysql_indicator_on(FALSE);                                      // OFF 
   wb_set_enabled(wb_get_control($mainwin, ID_PB_APANEL), TRUE);      // Enable Apanel 
   wb_set_enabled(wb_get_control($mainwin, ID_PB_PHPMYADMIN), TRUE);  // Disable phpMyAdmin
   wb_set_enabled(wb_get_control($mainwin,  ID_PB_MYCMD), FALSE);     // Disabe MySQL CMD button
   wb_set_enabled(wb_get_control($mainwin, ID_PB_ALL), FALSE);        // Disable All server button 
   wb_set_text(wb_get_control($mainwin, ID_PB_APACHE),'Stop Apache'); // set button
   wb_set_text(wb_get_control($mainwin, ID_PB_MYSQL),'Start MySQL');  // set button 
   wb_set_text(wb_get_control($mainwin, ID_PB_ALL),'Stop ???');       // set button 
   wb_set_enabled(wb_get_control($mainwin, ID_PB_APACHE), TRUE);      // Enable Apache button 
   wb_set_enabled(wb_get_control($mainwin, ID_PB_MYSQL), TRUE);       // Enable MySQL button
 }

 //11
 if($AP & $MP){
   mp_apache_indicator_on(TRUE);                                      // ON 
   mp_mysql_indicator_on(TRUE);                                       // ON
   wb_set_enabled(wb_get_control($mainwin, ID_PB_APANEL), TRUE);      // Enable Apanel 
   wb_set_enabled(wb_get_control($mainwin, ID_PB_PHPMYADMIN), TRUE);  // Disable phpMyAdmin
   wb_set_enabled(wb_get_control($mainwin,  ID_PB_MYCMD), TRUE);      // Enable MySQL CMD button
   wb_set_enabled(wb_get_control($mainwin, ID_PB_ALL), TRUE);         // Enable ALL server button 
   wb_set_text(wb_get_control($mainwin, ID_PB_APACHE),'Stop Apache'); // set button
   wb_set_text(wb_get_control($mainwin, ID_PB_MYSQL),'Stop MySQL');   // set button
   wb_set_text(wb_get_control($mainwin, ID_PB_ALL),'Stop All');       // set button 
   wb_set_enabled(wb_get_control($mainwin, ID_PB_APACHE), TRUE);       // Enable Apache button 
   wb_set_enabled(wb_get_control($mainwin, ID_PB_MYSQL), TRUE);        // Enable MySQL button
 }

Top

Indicator functions


There are two funtions that toggle the indicator states

  • mp_apache_indicator_on(state)
    • State TRUE indicator red
    • State FALSE indicator green
  • mp_mysql_indicator_on(state)
    • State TRUE indicator red
    • State FALSE indicator green

The code switches between red and green images. Using images is relatively faster than drawing the indicators.


Add code to end of script

//=== Indicator 1 ==============================================================
// Apache standard program indicator
function mp_apache_indicator_on($status){
 global $mainwin;

 if($status){ // True on green
    wb_set_visible(wb_get_control($mainwin, ID_F_APACHE_GREEN),TRUE); // green on
    wb_set_visible(wb_get_control($mainwin, ID_F_APACHE_RED),FALSE);  // red off
 }
 else        // False off red
 {
    wb_set_visible(wb_get_control($mainwin, ID_F_APACHE_RED),TRUE);    // red on
    wb_set_visible(wb_get_control($mainwin, ID_F_APACHE_GREEN),FALSE); // green off
 }
}
//======================================================== END Indicator 1 ===

//=== Indicator 2 ==============================================================
// MySQL standard program indicator
function mp_mysql_indicator_on($status){
 global $mainwin;

 if($status){ // True on green
    wb_set_visible(wb_get_control($mainwin, ID_F_MYSQL_GREEN),TRUE); // green on
    wb_set_visible(wb_get_control($mainwin, ID_F_MYSQL_RED),FALSE);  // red off
 }
 else        // False off red
 {
    wb_set_visible(wb_get_control($mainwin, ID_F_MYSQL_RED),TRUE);    // red on
    wb_set_visible(wb_get_control($mainwin, ID_F_MYSQL_GREEN),FALSE); // green off
 }
}
//======================================================== END Indicator 2 ===

Top

Test

To see this code in action run test_4.bat (code contained in test_4.phpw)

Note: Test button calls function main_init() this forces a button and indicator refresh.

Run Uniform Server, from UniTray start and stop each server press Test button and observe button and indicator states.

After testing close both Uniform Server and application script.

Top

Summary

That’s covered a lot of ground however more functionality is requied.

Next step is to start and stop servers this is covered on the next page.


Top