PHP WinBinder 3: Open and Save As dialog boxes

From The Uniform Server Wiki
Revision as of 20:02, 6 April 2010 by Ric (talk | contribs) (New page: {{Nav PHP WinBinder 3}} '''''Open dialog box and Save As dialog box''''' Running either the Open dialog box function wb_sys_dlg_open or Save As dialog wb_sys_dlg_save produces the followi...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

 

UniServer 5-Nano
PHP WinBinder 3.

Open dialog box and Save As dialog box

Running either the Open dialog box function wb_sys_dlg_open or Save As dialog wb_sys_dlg_save produces the following error :

  • Fatal error: Call to undefined function wb_sys_dlg_open()
  • Fatal error: Call to undefined function wb_sys_dlg_save()

Reason for this error; our basic script lacks support for either function.

One solution is to include WinBinder include files. However this conflicts with our single script approach.

Solution is to extract the appropriate functions from the WinBinder include file wb_windows.inc.php

That legwork has already been done and included on this page which covers using the above two functions directly from a PHP CLI script.

Add Missing Functions

Edit a clean copy of Pro_demo_wb.php add the following functions at the end of WINBINDER MINIMUM REQUIRED section as shown blow:

//--- OPENS STANDARD OPEN DIALOG BOX ------------------------------------------
function wb_sys_dlg_open($parent=null, $title=null, $filter=null, $path=null, $filename=null){
  $filter = _make_file_filter($filter ? $filter : $filename);
  return wbtemp_sys_dlg_open($parent, $title, $filter, $path);
}
//-------------------------------------- END OPENS STANDARD OPEN DIALOG BOX ---
function wb_sys_dlg_save($parent=null, $title=null, $filter=null, $path=null, $filename=null, $defext=null){
  $filter = _make_file_filter($filter ? $filter : $filename);
  return wbtemp_sys_dlg_save($parent, $title, $filter, $path, $filename, $defext);
}
//--- OPENS STANDARD SAVE DIALOG BOX ------------------------------------------

//-------------------------------------- END OPENS STANDARD SAVE DIALOG BOX ---

//--- CREATE FILE FILTER ------------------------------------------------------
// Creates a file filter for Open/Save dialog boxes based on an array.
function _make_file_filter($filter){
  if(!$filter) return "All Files (*.*)\0*.*\0\0";
  if(is_array($filter)) {
    $result = "";
    foreach($filter as $line)
      $result .= "$line[0] ($line[1])\0$line[1]\0";
      $result .= "\0";
      return $result;
  } else
     return $filter;
}
//-------------------------------------------------- END CREATE FILE FILTER ---

//###== END FROM WINBINDER MINIMUM REQUIRED FOR TEXT AND BUTTONS ==############


Top

General

Before looking at each function its important to understand what they do.

In a nutshell they return a full path to a file. They do not save or read to or from a file. Once you have a returned path you can use it in your script to perform the appropriate task.

A user has the option to cancel; in this situation returned path is empty. Both functions are obtaining user input at first site this may seem strange.

Save function provides you with a users intention that the file will be saved to a particular location and possibly with a different file name and extension.

Open function is straightforward; it returns the full path including file name and extension of a file selected by a user.

Top

Filter

This is an optional two-dimensional array consisting of a description and file extension.

static $file_filter = array(
  array("PHP source code",    "*.php*"),
  array("Web page",           "*.htm*"),
  array("Text document",      "*.txt*"),
  array("All files",          "*.*")
);

 

Example:

  • The array populates the file drop down menu (save as type) or (File of type).
  • It’s a nicety hence instead of specifying an array use NULL instead this defaults to files of all type.
  • Add code on left to the INIT section we will use it the examples.

Top

Dialog box details

For connivance I have reproduced each function's specification below.

These were taken from the WinBinder manual page links as follows:

Top

Open dialog box

string wb_sys_dlg_open (int parent [, string title [, string filter[, string path[, string filename]]]])

Displays the standard Open dialog box. Returns the selected file name, if any, or a blank string if the dialog box was canceled. Returns NULL if not successful.

Parameters:

parent is a handle to the WinBinder object that will serve as the parent for the dialog box.
title is the string to be displayed as the dialog box caption.
filter is an two-dimensional array composed of pairs of file descriptions and valid file name patterns (usually file extensions). If filter is NULL, the dialog box will display a default "All files" filter. The filter takes the following generic form:
     array(array(descr1, pat1) [, array(descr2, pat2) [, ...]])
path is an folder used to initialize the dialog box.
filename is optional. If parameter filter is NULL, the file extension will be used as the filter instead.

Top

Save dialog box

string wb_sys_dlg_save (int parent [, string title [, string filter[, string path[, string filename [, string default_extension]]]]])

Displays the standard Save As dialog box. Returns the typed file name, if any, or a blank string if the dialog box was canceled. Returns NULL if not successful.

Parameters:

parent is a handle to the WinBinder object that will serve as the parent for the dialog box.
title is the optional string to be displayed as the dialog box caption.
filter is an optional two-dimensional array composed of pairs of file descriptions and valid file name patterns (usually
file extensions If filter is NULL, the dialog box will display a default "All files" filter. The filter takes the following generic form:
     array(array(descr1, pat1) [, array(descr2, pat2) [, ...]])
path is an optional folder used to initialize the dialog box.
filename is an optional default name for the file. If parameter filter is NULL, the file extension will be used as the filter instead.
default_extension is optional and defines a default extension for the dialog box.

Top

Use NULL

To run the above two functions directly from your script replace use NULL for the parent parameter.

Real reason for reproducing the above is to highlight number of parameters in square brackets. Majority of WinBinder functions have these; what’s important they are optional. If you don’t specify a parameter a default is used.

Note:

If you want to use something like the following without specifying a title string:

string wb_sys_dlg_save (int parent , string title , string filter)
 

Replace ” string title” with NULL or leave it blanks, do not delete the comma.

Top

Test Code

//=== INIT ====================================================================

static $file_filter = array(
 array("PHP source code",    "*.php*"),
 array("Web page",           "*.htm*"),
 array("Text document",      "*.txt*"),
 array("All files",          "*.*")
);

$filename = wb_sys_dlg_open(NULL, 'Get file 1');
 if($filename) {
   print  $filename ;       // Do action here
 }

$filename = wb_sys_dlg_open(NULL, 'Get file 2', $file_filter);
 if($filename) {
   print  $filename ;       // Do action here
 }

$filename = wb_sys_dlg_save(NULL, "Save As 3", NULL, NULL, NULL, 'txt');
 if($filename) {
   print  $filename ;       // Do action here 
 }

$filename = wb_sys_dlg_save(NULL, "Save As 4", $file_filter, NULL, NULL, 'txt');
 if($filename) {
   print  $filename ; // Do action here 
 }
//=============================================================== END INIT ====

 

Add the lines of code to INIT as shown.

  • Run the script
  • At the pop-up select a file click OK or Cancel or Open
  • Note what is displayed in the command window.
  • Window will close


  • There are a further three pop-ups
  • Repeat same test as above


Results:

This code prints a path returned if any.

if($filename) {
   print  $filename ;       // Do action here 
}

If cancel is pressed code is skipped since returned string is blank


Top

Associates files

That completes this short tutorial.

Download includes the following two files for the above example :

  • Pro_demo_3_wb.bat
  • Pro_demo_3_wb.php

These are located in folder UniServer\docs\SRC\pro_demo

Copy them to folder UniServer and run.

Top

Summary

The above two functions were included for completeness. From a scripting point of view if you need to use them, you probably should consider creating a real WinBinder windows application. That said, they could be just what you want.

This tutorial has shown you can run any of the dialogue boxes directly from a PHP CLI script.

If none of the standard dialogue boxes meet your requirements using the techniques explained for creating a name password pop-up you can create a customised one

The WinBinder extension combined with PHP’s scripting core provides a very powerful and versatile solution to scripting.

Related links

Top