PHP WinBinder 4: Configuration file

From The Uniform Server Wiki
Jump to navigation Jump to search

 

UniServer 5-Nano
PHP WinBinder 4 - Tray Menu.

WinBinder Part 4 - Configuration file

Introduction

Never reinvent the wheel! In keeping with this philosophy file format of PHP’s ini configuration file is more than suitable for this application.

KIS (keep it simple) Both main menu and sub-menus have a similar format although this introduces some redundancy it makes creating a menu configuration file very easy. Generally it’s a copy and paste exercise; change a few details and you are ready to run.

Top

Basic format

Main menu and sub-menus have this structure:

  • [block name]
  • Line 1[]
  • Line 2[]
  • Line 3[]
  • Line 4[]
  • Line 5[]

 

The configuration file consists of defined blocks each block has a unique name contained in square brackets.

Main menus block must start with [main_menu] sub-menu blocks must have a unique name enclosed in square brackets.

Note: Names must be alphanumeric lowercase and contain no spaces for example sub_menu_1

All menu entries consist of five lines Line 1[]-Line 2[] these are arrays and must have a value or set to a empty string.

A menu entry can be one of three types a click-able link, a separator or a sub-menu.

Top

A menu entry block

Each menu entry block consists of five arrays (lines) with the following names and function:

item[]   Text displayed in menu (menu item)
action[]   Action functions are: Command (shell or run). A sub-menu (sub). A menu spacer (separator)
file[]   File name if above is a command. Sub-menu name if above is a sub
parameter[]   Optional parameters to pass if command is the action.
icon[]   Left icon to display index=0 no icon displayed

Note: Every menu item must have a complete block of five lines.

Top

Concise expand details

The above is very concise that is the simplicity of our configuration file.

Although the command processor has not been defined lets assume “run” has.

Configuration file example

; File tray_menu_config.ini
; This is a sample configuration file
; Comments start with ';', as in php.ini

[main_menu]
title = "US Menu"                     ; Manin application name

item[]      = "Start Apache Service"  ; display menu item
action[]    = "run"                   ; shell, run , sub, separator 
file[]      = "net"                   ; file name or sub-name
parameter[] = "start  ApacheS1"       ; parameters to pass
icon[]      = "7"                     ; icon id number

item[]      = "Stop Apache service"   ; display menu item
action[]    = "run "                  ; shell, run , sub, separator 
file[]      = "net"                   ; file name or sub-name
parameter[] = "stop ApacheS1"         ; parameters to pass
icon[]      = "6"                     ; icon id number

item[]      = "Service 服务"          ; display menu item
action[]    = "sub"                   ; create sub-menu 
file[]      = "sub_1"                 ; name see section below 
parameter[] = ""                      ; no parameter
icon[]      = "0"                     ; no icon

item[]      = ""                      ; A spacer requires no title 
action[]    = "separator"             ; only a action name required
file[]      = ""                      ; no file
parameter[] = ""                      ; and no parameter
icon[]      = "0"                     ; optional icon. No icon = 0  

item[]      = "Exit 出口 "            ; Display menu item text
action[]    = "Exit"                  ; Do not change
file[]      = ""                      ; no file
parameter[] = ""                      ; and no parameter
icon[]      = "4"                     ; Icon cross  

item[]      = "Minimize  最小化"      ; Display menu item text
action[]    = "Minimize"              ; Do not change
file[]      = ""                      ; no file
parameter[] = ""                      ; and no parameter
icon[]      = "3"                     ; Icon down arrow 

;=== Sub-Menus =====================================================
[sub_1]
title       = "App SUB 1"             ; Optional

item[]      = "启动 Apache服务"        ; display menu item
action[]    = "run"                   ; shell, run , sub, separator 
file[]      = "net"                   ; file name or sub-name
parameter[] = "start  ApacheS1"       ; parameters to pass
icon[]      = "9"                     ; icon id number

item[]      = "停止 Apache服务"        ; display menu item
action[]    = "run "                  ; shell, run , sub, separator 
file[]      = "net"                   ; file name or sub-name
parameter[] = "stop ApacheS1"         ; parameters to pass
icon[]      = "8"                     ; icon id number

 


Example pop-up tray menu:

The example configuration file will produce the following tray menu:

All menu elements are shown:

Main Menu:

  • Two click-able menu items
  • A sub-menu hover link
  • A seperator
  • Exit link
  • Minimize link

Sub-menu:

  • Two click-able menu items


Note: The sub-menu title is optional you can supply an empty string for example:

  • title = ""  ; Optional


Top

PHP parse_ini_file() Function

The parse_ini_file() function reads a configuration file and returns settings in an array.

It has the following syntax:


parse_ini_file(file,process_sections)

  • file - Specifies the ini file to check
  • process_sections - Default FALSE returns a flat array. Set to TRUE returns a multidimensional array with section names and settings included.

Example

Run test_case_1.bat result shown below:

Array
(
    [main_menu] => Array
        (
            [title] => US Menu
            [item] => Array
                (
                    [0] => Start Apache Service
                    [1] => Stop Apache service
                    [2] => Service µ£ìÕèí
                    [3] =>
                    [4] => Exit Õç║ÕÅú
                    [5] => Minimize  µ£ÇÕ░ÅÕîû
                )

            [action] => Array
                (
                    [0] => run
                    [1] => run
                    [2] => sub
                    [3] => separator
                    [4] => Exit
                    [5] => Minimize
                )

            [file] => Array
                (
                    [0] => net
                    [1] => net
                    [2] => sub_1
                    [3] =>
                    [4] =>
                    [5] =>
                )

            [parameter] => Array
                (
                    [0] => start  ApacheS1
                    [1] => stop ApacheS1
                    [2] =>
                    [3] =>
                    [4] =>
                    [5] =>
                )

            [icon] => Array
                (
                    [0] => 7
                    [1] => 6
                    [2] => 0
                    [3] => 0
                    [4] => 4
                    [5] => 3
                )

        )

    [sub_1] => Array
        (
            [title] => App SUB 1
            [item] => Array
                (
                    [0] => ÕÉ»Õè¿ Apacheµ£ìÕèí
                    [1] => Õü£µ¡ó Apacheµ£ìÕèí
                )

            [action] => Array
                (
                    [0] => run
                    [1] => run
                )

            [file] => Array
                (
                    [0] => net
                    [1] => net
                )

            [parameter] => Array
                (
                    [0] => start  ApacheS1
                    [1] => stop ApacheS1
                )

            [icon] => Array
                (
                    [0] => 9
                    [1] => 8
                )

        )

)

 


Code:

The following lines reads configuration and saves to array $ini_array. This array is displayed using ptint_r()

<?php
//== Parse ini file with sections enabled
$ini_array = parse_ini_file("tray_menu_config.ini", true);
print_r($ini_array);
?>

Result:

Output as shown on the left what’s important it’s an array of arrays.

Each menu item has an entry in the four arrays [item],[action],[file],[parameter] and [icon].

Hence it is important each menu entry must have a value.

For Loop:

The above arrays can be scanned in a for-loop allowing each menu value to be picked up and associated with a menu item.

[main_menu]

I mentioned before only the main menu block name requires defining and written in stone. We use this to extract sub-menu block names from the [file] array.

Knowing the sub-menu name that block can be scanned in a for-loop and menu data extracted.


Note: The same technique can be applied to a sub-menu containing sub-menus.

Code snippet:

Scan configuration array and build master array main

// Scan config array and build main array
for($i = 0; $i < count($ini_array['main_menu']['action']); $i++) {
  $main[$i][0] = "Item ".$i;                               
  $main[$i][1] = "main_menu";                              
  $main[$i][2] = $ini_array['main_menu']['title'];         
  $main[$i][3] = $ini_array['main_menu']['item'][$i];      
  $main[$i][4] = $ini_array['main_menu']['action'][$i];    
  $main[$i][5] = $ini_array['main_menu']['file'][$i];      
  $main[$i][6] = $ini_array['main_menu']['parameter'][$i];
  $main[$i][7] = $ini_array['main_menu']['icon'][$i];   

  if($ini_array['main_menu']['action'][$i] == "sub"){
    $sub_name = $ini_array['main_menu']['file'][$i];
    subs($sub_name);  
  }
}

Above "if" statement is worthy of note it checks action for a "sub"

If it is then pick-up the sub-menu name and run function subs().

function subs($sub_name){
  // Now build part of main array
 for($i = 0; $i < count($ini_array[$sub_name]['action']); $i++) {

   $main[$items+$i][0] = "Item ".($items+$i);                    
   $main[$items+$i][1] = $sub_name;                               
   $main[$items+$i][2] = $ini_array[$sub_name]['title'];          
   $main[$items+$i][3] = $ini_array[$sub_name]['item'][$i];      
   $main[$items+$i][4] = $ini_array[$sub_name]['action'][$i];    
   $main[$items+$i][5] = $ini_array[$sub_name]['file'][$i];      
   $main[$items+$i][6] = $ini_array[$sub_name]['parameter'][$i]; 
   $main[$items+$i][7] = $ini_array[$sub_name]['icon'][$i];  
 }//
}

The above code snippets shows data extracted from the configuration file is flattened when entered into main array. A side effect of this main menu items are not sequential they are interspersed with sub—menu items.

This is not a problem when building the menu because array “main” is scanned for each individual menu item and placed accordingly.

Top

Main array elements

These are proposed main array elements:

  $main[$i][0]  // Item number
  $main[$i][1]  // Configuration section name
  $main[$i][2]  // Configuration sub-menu title or application name
  $main[$i][3]  // Configuration menu item text
  $main[$i][4]  // Configuration menu item action
  $main[$i][5]  // Configuration menu file path
  $main[$i][6]  // Configuration menu parameter to pass
  $main[$i][7]  // Configuration menu icon 0 = no icon
  $main[$i][8]  // y-top where an item or rollover starts
  $main[$i][9]  // y-rollover-end where a rollover ends
  $main[$i][10] // y-bottom where an item ends and the next one starts
  $main[$i][11] // x-icon1 where left icon starts
  $main[$i][12] // x-rollover-start where a rollover starts
  $main[$i][13] // x-rollover-end where rollover ends
  $main[$i][14] // x-rollover-width width of rollover 
  $main[$i][15] // x-rollover-height height of a rollover
  $main[$i][16] // x-text where text starts
  $main[$i][17] // Maximum string for either main menu or sub-menu 
  $main[$i][18] // x-icon2 where right icon starts
  $main[$i][19] // x-icon2 end where right icon ends

 


Main array contains menu item information from the configuration file and postioning information.

Advantage of having data in a master array it allows you to create sub-arrays from a known element’s reference point. These can be passed for further processing for example:

  // Get command
  if($main[$i][4]== "sub"){   // Is it a sub-menu
    $val[] =  $main[$i][4];   // retutn sub command
    $val[] =  $main[$i][5];   // return sub name 
    $val[] =  $main[$i][10];  // return sub y-position 
  }
  else{                       // No its a command
    $val[] =  $main[$i][4];   // retrn command
    $val[] =  $main[$i][5];   // return file
    $val[] =  $main[$i][6];   // return parameters
  }

I say proposed because it may change in the final design. May be more optimum to have a different order.

The following element is a good candidate for removal

  • $main[$i][2] // Configuration sub-menu title or application name

Having this option allows a sub-menu to have a title.

This is at the expense of adding complexity to our configuration file.

If the above element is removed so can each sub-menu title.

The main_menu (app name) can then be moved to the top level of the configuration file.

Top

Summary

The above has shown how to read a configuration file into an array. This array is scanned; to form a master array extracted data is combined with positioning information and save to the master arra..

Note: Master array elements to use and configuration file format are proposals, dependent on final design requirements.

Next page looks at command processing.

Top