Aggregate plug-in for TinyButStrong
by Skrol29, 2009-04-07
version 1.0.0
This plug-in can provide several types of aggregate
calculations when a block is merged. It can do Sum, Min, Max, Average, Count, Accumulated values and Changed values.
Note: The current version cannot deal with header or footer sections, calculations are made over all records of the block, without rupture (this will perhaps be given by a next version).
PHP side:
Requirements:
Include the file 'tbs_plugin_aggregate.php'.
This plug-in requires TBS version 3.1 or higher.
Installation mode:
Automatic when included before the creation of the TBS variable.
If you include the plug-in file after the creation of the TBS variable, you should force the installation by using the following command:
$TBS->PlugIn(TBS_INSTALL,TBS_AGGREGATE);
Command syntax: (no command)
Miscellaneous:
The last aggregate calculations are stored into a property of the TBS instance. This property is named Aggregate and its type is Array. Example:
include_once('tbs_plugin_aggregate.php');
$TBS = new clsTinyButStrong;
$TBS->MergeBlock('b',$data);
$sum = $TBS->Aggregate['price:sum'];
Template side:
1st step: Define calculations
Use parameter
aggregate to declare all computed columns you'd like to use. The name of a computed column is
columnX:operationX where
columnX is the name of the original column used for the calculation, and
operationX is the keyword of the operation (see the list below)
Example:
If you want to calculate the sum over column1, then add parameter "aggregate=column1:sum".
If you want to calculate several operations, then add "aggregate=column1:sum,column2:sum,..."
Note: Parameter
aggregate is a block parameter, you have to place it in the same TBS tag as the one containing "
block=...". Example:
[b.product;block=tr;aggregate=price:sum]
Tip: If you have many computed columns and your tag become very long, you can use parameter
comm to embed the TBS tag into a HTML comment. See parameter
comm in the manual for more information.
2nd step: Display computed values
The computed columns are added in the dataset. You can display them like any other columns
Example:
[b.price:sum;frm='0,000.00']
Note:
Some computed columns are available during the merge (like Acc and Chg), you can display their values by placing a TBS field inside the block. Some other columns are available only after the merge (like Sum, Min, Max,...), you can display their values by placing a TBS field only outside the block, otherwise a TBS error will raise. (see le list below).
Operation |
Description |
Placement of the TBS tag |
inside the block |
outside the block |
sum |
Sum of non null values |
|
• |
avg |
Average of non null values |
|
• |
min |
Min of non null values |
|
• |
max |
Maximum of non null values |
|
• |
count |
Number of records with a non null value |
|
• |
acc |
Accumulated values |
• |
• |
chg |
Values if changed, otherwise display '' |
• |
• |
Example:
Product |
Price |
[b.product;aggregate=price:sum] |
[b.price] |
Total: |
[b.price:sum] |
The field [b.price:sum] is on a computed column, this one must be placed outside the block (outise the <tr> here).
Note:
If you are merging several blocks with one MergeBlock(), then parameter aggregate will work correctly only with the first block's name. So you have to define all operations first the first block only. This should not be a problem since all data are available for all the merged blocks.
Example:
$TBS->MergeBlock('b1,b2,b3',$data);
In this example, [b1.product;
aggregate=
price:sum] will work, but [b2.product;
aggregate=
price:sum] won't work.