This tutorial is still a work in progress; by writing this I'm hoping I can improve the quality of the code and more importantly attract feedback from users. Please post any feedback you have on the forums.
It requires knowledge of Smarty to customize the templates for the add, edit, details and list pages. How much depends on how far you want to go.
I'm trying to keep the templates as easy as possible, so users can easily customize them for a certain table configuration. I realize that this goal isn't reached by far in the current code, but as said, I'm hoping this tutorial can help improve that part of PHP GEN.
You can find the templates for the CRUD pages in 'php_gen-x.x/engine/generic/templates/', each named after it's functionality (eg 'add.tpl' and 'edit.tpl'). Each time PHP GEN generates CRUD pages for a certain table, it copies those templates to a directory in the output directory (generally php_gen-x.x/output/<table>/templates/). You can alter both the original templates and copied templates.
WARNING: when altering templates in the output directory, make sure you have backups when you generate a new configuration. Since PHP GEN will overwrite any file that may already exist (this behaviour will probably change in the near future). Note: changing permissions on these files will cause (non-fatal) errors in 'Scripts Generation' (which, I must admit, has terrible error checking/handling).
When you look at the template-file 'add.tpl' you can find following code in the middle of the file ('edit.tpl' contains the same code with minor adjustments):
{foreach name=outer key=field item=field_options from=$record_fields} <tr bgcolor={cycle values="#e0e0e0, #f0f0f0"}> <td class="colored_tables field_alias" valign="top"> <b>{$field_options.alias|htmlspecialchars}{if $field_options.notnull}*{/if}</b> </td> <td> {form_element field_options=$field_options} {$field_options.form_hint} {if $field_options.type eq "file" && $field_options.file_ext ne ""} <br />Allowed file extensions: {$field_options.file_ext|htmlspecialchars} {/if} </td> </tr> {/foreach} |
This piece of code is responsible for rendering the appriopiate form elements on the add page.
As you can see, the code loops over an array named $record_fields while assigning each element of the array to variable '$field_options' (red). The array $record_fields has following structure (please find an example):
$records_fields = array(
'column_name' => array( 'alias' => '', 'type' => '', 'content_type' => '', 'value' => '', ...),
'column_name' => array( ...),
...
);
To actually print the form element, a custom Smarty template function is defined in PHP GEN >= 1.5, namely {form_element} (blue). It accepts only one attribute: 'field_options'.
Furthermore, other elements of $field_options are used to print other information like alias, hint,... (green).
The template-file 'details.tpl' contains similar code, looping over a similar array $record_fields:
{foreach name=outer key=field item=field_options from=$record_fields}
<tr bgcolor={cycle values="#e0e0e0, #f0f0f0"}>
<td valign="top" class="colored_tables field_alias" style="white-space: nowrap"><b>{$field_options.alias|htmlspecialchars}</b></td>
<td style="white-space: nowrap">{$field_options.value}</td>
</tr>
{/foreach}
|
The only difference here is that $field_options['value'] does not contain bare DB values, but the values are already prepared for HTML display. For example: nl2br() on textarea's if it's not configured to contain HTML, htmlspecialchars() on non-HTML fields, etc.
Still a bit too complicated to customize (and explain) I'm afraid...
To understand this example, it might be necessary to know the structure and configuration of the 'product' table used in the sample database.
When editing a record of this table, the array $record_fields could have following contents. By using code explained in basics, PHP GEN is able to render the appropriate form elements.
We could customize the foreach-loop by adding code to print a ruler every 3 rows:
{foreach name=outer key=field item=field_options from=$record_fields}
<tr bgcolor={cycle values="#e0e0e0, #f0f0f0"}>
<td class="colored_tables field_alias" valign="top">
<b>{$field_options.alias|htmlspecialchars}{if $field_options.notnull}*{/if}</b>
</td>
<td>
{form_element field_options=$field_options} {$field_options.form_hint}
{if $field_options.type eq "file" && $field_options.file_ext ne ""}
<br />Allowed file extensions: {$field_options.file_ext|htmlspecialchars}
{/if}
</td>
</tr>
{if $smarty.foreach.outer.iteration % 3 == 0}
<tr><td colspan="2"><hr></td></tr>
{/if}
{/foreach}
|
Or print a ruler after specific rows:
{assign var='hr_pos' value=','|explode:'1, 4, 5'} {assign var='pos_count' value=0} {foreach name=outer key=field item=field_options from=$record_fields} <tr bgcolor={cycle values="#e0e0e0, #f0f0f0"}> <td class="colored_tables field_alias" valign="top"> <b>{$field_options.alias|htmlspecialchars}{if $field_options.notnull}*{/if}</b> </td> <td> {form_element field_options=$field_options} {$field_options.form_hint} {if $field_options.type eq "file" && $field_options.file_ext ne ""} <br />Allowed file extensions: {$field_options.file_ext|htmlspecialchars} {/if} </td> </tr> {if $smarty.foreach.outer.iteration == $hr_pos[$pos_count]} <tr><td colspan="2"><hr></td></tr> {assign var='pos_count' value=$pos_count+1} {/if} {/foreach} |
Or we could replace the whole foreach-loop altogether, and create rows by hand:
<tr bgcolor={cycle values="#e0e0e0, #f0f0f0"}>
<td class="colored_tables field_alias" valign="top"><b>Name *</b></td>
<td>{form_element field_options=$record_fields.name}</td>
</tr>
<tr bgcolor={cycle values="#e0e0e0, #f0f0f0"}>
<td class="colored_tables field_alias" valign="top"><b>Brand</b></td>
<td>{form_element field_options=$record_fields.brand}</td>
</tr>
<tr bgcolor={cycle values="#e0e0e0, #f0f0f0"}>
<td class="colored_tables field_alias" valign="top"><b>Description</b></td>
<td>{form_element field_options=$record_fields.description}</td>
</tr>
<tr bgcolor={cycle values="#e0e0e0, #f0f0f0"}>
<td class="colored_tables field_alias" valign="top"><b>Attachment</b></td>
<td>{form_element field_options=$record_fields.attachment}{$record_fields.attachment.file_ext|htmlspecialchars})
</td>
</tr>
<tr bgcolor={cycle values="#e0e0e0, #f0f0f0"}>
<td class="colored_tables field_alias" valign="top"><b>Link</b></td>
<td>{form_element field_options=$record_fields.link}</td>
</tr>
<tr bgcolor={cycle values="#e0e0e0, #f0f0f0"}>
<td class="colored_tables" colspan="2"><hr width="100%"></td>
</tr>
<tr bgcolor={cycle values="#e0e0e0, #f0f0f0"}>
<td class="colored_tables field_alias" valign="top"><b>Price</b></td>
<td>{form_element field_options=$record_fields.price}</td>
</tr>
<tr bgcolor={cycle values="#e0e0e0, #f0f0f0"}>
<td class="colored_tables field_alias" valign="top"><b>Stock</b></td>
<td>{form_element field_options=$record_fields.stock}</td>
</tr>
<tr bgcolor={cycle values="#e0e0e0, #f0f0f0"}>
<td class="colored_tables field_alias" valign="top"><b>Checkbox</b></td>
<td>{form_element field_options=$record_fields.checkbox}</td>
</tr>
|
And finally, a sample on how the foreach-loop used to look in PHP GEN < 1.5. It is still possible to use this code, but I thought I'd implement the custom Smarty function to make things easier.
print_r( $record_fields);" to the bottom of add.php, edit.php or details.php (found in output/<table>/)Past changes to this document include: