PHP WinBinder 4: Configuration file
PHP WinBinder 4 : Introduction | Rollover | Icons | Size and position | Configuration file | Command Processing | Coding | Final Tray Menu
|
|
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.
Basic format
Main menu and sub-menus have this structure:
|
|
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. |
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.
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
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) |
|
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 ) ) ) |
|
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.
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. |
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 |
|
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.
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.