A quick look at CGI_WRKDBF and Xataface
In December 2008, we introduced you to the open-source PHP framework ATK. This month we thought we’d introduce you to two more tools that have recently caught our attention.
CGI_WRKDBF
The first is CGI_WRKDBF, the latest open-source offering from our friend Giovanni Perotti of CGIDEV2 fame.
Many will be familiar with the original green-screen WRKDBF utility, which for years was the tool of choice for IBM i programmers who needed a simple, powerful method for updating and querying databases. Of course, other than its utility, one of the primary advantages of the tool was its price—free! But it was always offered as freeware and not open source—in other words, the source code was unavailable. When IBM i 6.1 came along, the program couldn’t be recreated due to its use of features no longer supported for user state programs. Faced with this problem, the developer initially decided to simply stop distributing the program but, after many pleas from his users, he relented and once again offered the program—but this time as shareware for $449.
Seeing the user-community panic when WRKDBF was unavailable, Giovanni decided to build an open-source, Web-based equivalent and so CGI_WRKDBF was born.
The first public release of the new tool came on Aug. 4, and along with the kudos that poured in from many happy users came the requests for an update facility. This enhancement was released Aug. 15, much to the delight of its users who now have a complete solution. As you might expect, the tool is based on the CGIDEV2 library and all source code is included in the distribution—so if you don’t like a feature, you can change it!
Since you can freely download the source code, and because Giovanni has included some screen captures on the Web site, we won’t go into any more detail here. We’ll just say thanks to Giovanni for another great contribution to the community.
As good as Giovanni’s new utility is, it’s not a tool you’d want to place in an end-user’s hands. Of course, the underlying code of CGI_WRKDBF could be used to create an end-user tool, but why reinvent the wheel?
A quick look at CGI_WRKDBF and Xataface
Xataface
As we’ve noted before, one of the great strengths of the PHP language is its capability to simply build highly flexible applications. This results in several tools and frameworks that can be used to build end-user CRUD (Create, Read, Update, Delete) applications very simply. The latest one we’ve been reviewing is Xataface. We’re impressed with how little you must do to get a simple CRUD application up and running.
Unlike ATK, Xataface is primarily configuration driven. Rather than write PHP code to set up the application, you make entries in configuration files. For more complex extensions, you must write code but, as we’ll demonstrate, you can do an incredible amount without writing one line of PHP code.
The base configuration file is conf.ini, which controls, among other things, what databases are used by the application and how to sign on to the database. Here’s the basic configuration file for our example:
[_database]
host = "localhost"
user = "Jon"
password = "password"
name = "database"
[_tables]
product=Product Details
category=Product Categories
The [_database] entries are pretty self-explanatory. The [_tables] entries identify the actual tables to be used (in this case, product and category) and give them a different name for display purposes (“Product Details” and “Product Category”). Other than the conf.ini file, the only other file we need is the basic PHP startup script (index.php) that’s supplied with the package. This is a slightly edited version (we removed the code that displays the response time for the page):
<?php
/**
* File: index.php
*
* To use the application simply point your browser to this file
*/
require_once 'xataface/dataface-public-api.php';
// include the code containing the APIs
df_init(__FILE__, 'xataface');
// initialize the application and identify root directory for API
$app =& Dataface_Application::getInstance();
// get an application instance
$app->display();
// And display the application - that's it - all done!
?>
A quick look at CGI_WRKDBF and Xataface
With just these two files (only one of which is specific to our application), Xataface provides a basic application that lets us display our tables and rows, enter new rows, perform searches, etc. Take a quick look at Figure 1 for an idea of what’s produced.
In the upper-left corner, you can see the tabs that are used to select which table we want to be displayed. The left-hand side of the window is blank right now. In the basic template, it’s reserved for your own menu items so you can integrate the application into your existing Web applications. We haven’t defined anything, so ours is blank. Note that everything you see here is fully customizable by modifying the application templates and using the configuration files. We wanted you to see just how much you get with almost no work! You’ll also notice that on the right-hand side are icons for exporting the data as a comma delimited file, XML file or RSS feed. Other export options are available but these are the standard ones.
To demonstrate how easy it is to customize an Xataface application, we’ll tidy up a few of the column headings and provide a drop-down selection list for the Product Category column.
The first thing to understand is that Xataface is convention driven. If we want to customize the way tables are displayed, we must start by creating a directory called “tables” in the application root (i.e., the directory where our index.php file is located) and within that create a directory for each of our tables. It’s within these table directories that we place our other configuration files. We’ll only deal with two of them here—fields.ini and valuelists.ini.
fields.ini lets us do things like provide more meaningful column headings, provide narrative text to explain the content and determine the column type (check box, selection list, etc.). In our example (a portion of the total file), we’ve provided the label “Product Code” for the ID column of the Product table.
[id]
widget:label="Product Code"
widget:description=“Enter the unique product code here:"
[description]
widget:label="Product Description"
[category]
widget:label="Product Category"
widget:description="Please select the appropriate category"
widget:type=select
vocabulary=category
A quick look at CGI_WRKDBF and Xataface
Notice the entry for the category column. It specifies that the type of the column is “select” (i.e., a selection list will be presented when we attempt to add a new record or update one). How is the content of that list populated? That’s the function of the entries in the vocabulary file (valuelists.ini). By saying “vocabulary=category” we inform Xataface that in the valuelists.ini file in the products directory is a definition identified as “category” that will specify the contents of the pull-down. This could be a simple list of values and names, or an SQL statement that retrieves the data we need. For our purposes, the file contains:
[category]
__sql__ = “select id, name from category order by name”
This simple SQL select will be used to populate the category pull-down from the entries in the category table. Figure 2 shows the results of these simple configuration file entries in the edit screen. Note the additional instructions for the field contents as well as the nicer field titles, etc. These improved headings will also appear as column headings in the main list.
Without writing a single line of PHP, we can create quite a powerful mini-application. We could do many other things, such as change the logo on the page, set the field width, add validation to the entry fields, group fields together for display purposes and much more. If we have date columns in the table, we can designate them as such in the fields.ini file and a calendar will automatically be incorporated into the add and edit pages.
We should mention that Xataface, like most PHP applications, uses the MySQL database. To modify it to use DB2 instead would be a major undertaking, but luckily we don’t have to do that! The advent of the DB2 Storage Engine for MySQL means that we can expose our DB2 tables as MySQL tables and use Xataface with them. We’re working with the folks at the YiPs Web site to set up a live demonstration site for Xataface running on IBM i. Once that’s available, we’ll mention it in our weekly blog. In the meantime, we’ve produced a brief video to show you this mini-application in action.