Well I have had this idea in mind for a while. In my script I have php variables passed to html(now tpl thanks to smarty!) template file, and the process of building tables and forms are handled in PHP. This is usually sufficient for small forms/tables, like this one:
$form = "<form name='form1' method='post' action='login.php'>
<p>Username: <input name='username' type='text' id='username'></p>
<p>Password: <input name='password' type='password' id='password'></p>
<p><input type='submit' name='Submit' value='Submit'></p>
The form above is a simple login form, which is easy to build anyway. Nonetheless, real life aint as simple as we imagine. In circumstances we come across different types of html elements, some have pre-defined values from MYSQL database! Even worse is a situation of select/option dropdown menu, which means we have to use a while/for loop in any forms/tables that contain them. This can soon turn our script into a mess.
So what is the solution? You get it, its this PHP GUI package I am meditating and actually have already begun working on. With this GUI package, it will be possible to write professional and well organized object oriented code like the one below to construct a page:
$page = new Frame;
$form = new Form("myform", "post", "index.php");
$form->add(new TextField("mytext", "This is a text field"), 1);
$text = new TextField();
$text->setValue("This is yet another text field");
$form->add(new TextArea("mytextarea", 45))
$submit = new Button();
// Default type is submit for button object
// We are adding submit button to the index 500, lol. It works so long as we do not override existing indexes.
The GUI structure is defined as below:
So this looks quite complicated and ambitious? Maybe, but I'd say its worth trying. The GUI has two basic categories: Components and Renderers. Components can be pure components, or containers that enclose other components or containers. Renderers are objects that convert data field in each GUI component/container to the corresponding html readable string format. This GUI package is written in Composite design pattern, which turns out to be rather powerful. I also considered Decorator pattern, it might work out as well but right now I am sticking to Composite.
The idea is that each page is defined by a frame, a frame is apparently the top level container. But an empty frame renders to nothing, so you may want to add components and other containers to it. Many pages on my site have forms and tables, they are major players in the GUI system since almost all GUI components can be added to them. Basic components include button, which also have subcategories such as StdButton, RadioButton and CheckBox. TextField component has subcategories such as TextField, PasswordField and TextArea. Each component can be added to a container at any given index, the index determines the order they are displayed. The way the script is designed allows multi-layer rendering, which means you can simply render the frame and all tables, forms and buttons, textfields it contains will get rendered too.
So if you want to see an example, this is how it works out on a test script I have just written for StdButton. The script works rather well, as you can see from the screenshot. Right now I only have StdButton and ButtonRenderer concrete classes, but in not so distant all these GUI components will be made available one by one. Should be interesting, isnt it?
$button = new StdButton();
$button->setText("This is a Button!");
$button2 = new StdButton("Yet Another Button", "your button", "Meh, what's going on?");
So what do you think? I hope this will not take me more than 2-3 months to complete, and in fact my winter break is coming up soon so I should have fun doing this GUI project. I know many of you here are better programmers than me, so I definitely would love to hear suggestions and criticism. Thanks, and hope you all have enjoyed your thanksgiving.