ZoneMinder, Template Engine and my App Framework

If you've made a patch to quick fix a bug or to add a new feature not yet in the main tree then post it here so others can try it out.
Post Reply
macguru
Posts: 5
Joined: Tue Jun 01, 2004 10:09 am

ZoneMinder, Template Engine and my App Framework

Post by macguru »

Hi, Philip,

I do not know if you have looked at stuff I have mentioned last time or not,
anyway...

1) I have checked a LOT of template engines, and finally have choosen Savant.
Basically, almos all template engines parse template file, substitute macros
(or process their own markup language file), generate final PHP script and
execute it. All this require so called template compilation step. On the
other side, Savant uses raw HTTML/PHP files.

------------------------------------------------------------------------------------------------------------

Here is snippet from HTML Savant template (contacts.tpl.html):

<td width = "200" class="PlainTextLeft">
<?php echo $tpl_contacts_id_text; ?>
<input type="hidden" name="form_current_rec_id" value="<?php echo
$tpl_current_rec_id; ?>">
<input type="hidden" name="form_db_action" value="<?php echo
$tpl_db_action; ?>">
<input type="hidden" name="form_old_values" value="<?php echo
$tpl_old_values; ?>">
<input type="hidden" name="form_current_related_entity" value="<?php echo
$tpl_current_related_entity; ?>">
</td>

// Create tpl object.
$tpl_contacts_data = new Savant;
// Assign tpl vars.
$tpl_contacts_data -> assign('company', $company);
// Render tpl.
$tpl_contacts_data -> display('contacts_data.tpl.html');

As you can see, no template compilation is necessary, as weill no writeable
web dir for storing compiled templates.

------------------------------------------------------------------------------------------------------------

2) Sometimes final html layout is too event-dependent. In this case it is much
more convinient to generate HTML with OOP PHP library. I have chosen
phphtmllib, which is very complete and designed

Example:

$phphtmllib = '../../utils/phphtmllib';
include_once("$phphtmllib/includes.inc");
$diary =& html_table('98%', 1, 1);

$td_event1 = new TDTag(array('bgcolor' => 'ffe2b1'), 'event one');
$td_event2 = new TDTag(array('bgcolor' => 'ffe2b1'), 'event two');
// loop which initializes events.
---> $diary -> add_row($td_time1, $td_event1, $td_time2, $td_event2);

print $diary -> render();

phphtmllib is capable to generate much more complex layouts, e.g. tab
navigation dialog with all content. I have even combined Savant and
phphtmllib in some complex cases.

------------------------------------------------------------------------------------------------------------

Finally, what my app framework does. In the past I have programmed in C++ and
Object Pascal using OOP frameworks like MacApp, THINK Class Library and
PowerPlant. I really liked the idea having only one global varibale (usually
called gApp), which encapsulated all prefs from class library itself and
derived class (usually something MyApp). The closest analog of C++ global var
in PHP is a session variable. I have generic app class (ANVphpApp), number of
utility functions, and collection of third-party libs like Savant,
phphtmllib, adodb, etc...

Here is what my framework does:
1) BuildTplEngVars() which assigns template vars built from table name and
assotiative array of fields => values.
2) RecordSet2HTMLMenu() builds HTML menu data from ADODB recordset
3) FormVars2SQLAssArray() and FormVars2InsertSQL() which greatly aid
generation of INSERT/UPDATE SQL from form vars.
4) FormVars2UpdateSQL() - generates update SQL from form vars.
5) FindChangedFormVars(), which compares initial and new form vars and returns
assotiative array of fields => values which contains ONLY changed data.
6) SelectSQL2HTMLMenu() - handy function used to builds HTML menu data for
related entities (i.e. company => profile, stock item => category or unit of
mesaure, etc.).

I am use names built upon standard prefix for form/template vars and db field
names to avoid unecessary typing chore.

Please look at FindChangedFormVars(), for example. Suppose you have an update
form, where most of values have to be saved in the database. This function
will check WHICH values have been changed, and will use FormVars2UpdateSQL()
to generate update SQL with ONLY changed values.

Here is how typical form display handling script looks like:

include_once('../../Get_gApp.php');
include_once('../../ANVSavant.php');

// Connect to db
$gApp -> Connect();
$rs = $gApp -> ExecuteSQL($sql);

$tpl_contacts_profiles = new ANVSavant;

// Retrieve data from db and initialize template vars.
$rs -> MoveFirst();
$row = $rs -> FetchRow();
$gApp -> StripSlashesRnt($row);
$gApp -> BuildTplEngVars('contacts_profiles', $row, $tpl_contacts_profiles);

$gApp -> SaveOldTplEngVars($tpl_contacts_profiles);
$gApp -> SetDbActionUpdate($tpl_contacts_profiles);
$tpl_contacts_profiles -> display('contacts_profiles.tpl.html');

----------------- SUBMIT Script ---------------

include_once('../../Get_gApp.php');

$all_form_vars = $_POST;
$gApp -> CleanFormVars($all_form_vars);
$old_tpl_vars = $gApp -> GetOldTplEngVars($all_form_vars);
$changed_form_vars = array();

$gApp -> Connect();

// ************************
// *** Insert new record.
// ************************
if ($gApp -> IsDbActionInsert($all_form_vars)) {
$changed_form_vars = $all_form_vars;
$gApp -> AssArray_RemoveEmpty($changed_form_vars);
// Convert form vars to insert SQL and execute resulting query.
$sql = $gApp -> FormVars2InsertSQL($changed_form_vars,
'contacts_profiles');
$result = $gApp -> ExecuteSQL($sql);
}
// *****************************
// *** Update existing record.
// *****************************
else if ($gApp -> IsDbActionUpdate($all_form_vars)) {
// Find changed form variables for simple (text) fields.
$gApp -> FindChangedFormVars(
$all_form_vars,
$old_tpl_vars,
'contacts_profiles',
$changed_form_vars);

// Convert form vars to update SQL and execute resulting query.
$sql = $gApp -> FormVars2UpdateSQL($changed_form_vars,
'contacts_profiles', $where, true);
$result = $gApp -> ExecuteSQL($sql);
}

------------------------------------------------------------------------------------------------------------

Please let me know what do you think. My app framework is not yet complete
(you have to hard code user authentication), and function which check
permissions for db operations always return true, but you got the idea. Its
LGPL, after all.
I am now working to comment it everywhere possible to make it as clear as
possible for others.


************************************************
*** with best regards
*** Andrei Verovski (aka MacGuru)
*** Mac, Linux, DTP, Programming Web Site
***
*** http://snow.prohosting.com/guru4mac/
************************************************
Post Reply