I have spend some extra time with this issue and have found a very handily solution: using PHP templates stored in DB to get current EE instance and create callback processing script.
As I needed this script for BrilliantRetail payment gateway module ( based upon Brilliant_retail_gateway ) main idea is to create PHP-template on module install and remove it on module deleting.
I decided to use custom template group to avoid template groups mess-up.
I'll describe all necessary action for this solution below.
First of all: manually create a template group and template itself for testing ( in Design > Template Management ) with such parameters:
- Type — Web Page
- Enable Caching — No
- Allow PHP — Yes
- PHP Parsing Stage — Output
For this example I will use tpl_group_name
for the group name, and tpl_name
for the template name
Turn on file synchronization and save template to the file. For now, you can create write pure PHP code to the template.
IMPORTANT: Your template file must have PHP close tag ( ?> ), to avoid template parsing error.
You can execute your template-script by opening URL like: http://example.com/tpl_group_name/tpl_name
Template will be stored in system/templates/default_site/tpl_group_name.group/tpl_name.html
Directory default_site
is depend on your system configuration and can be different
You can work with EE instance in your template via
$this->EE
After script codding was finished template-script must be packed and placed into the BR Payment module.
I decided to compress file content with gzip then after apply base64 encoding to get plaint text, which allows me to insert compressed content to the file as a string variable.
Finally, you can manipulate your template using template_model
class. Code below demonstrate how to add PHP-template and group to the DB.
// Adding group
$this->EE->load->model('template_model');
$grp_data = array(
'group_name' => 'tpl_group_name',
'is_site_default' => 'n', // do not replace default group
'site_id' => $this->EE->config->item('site_id')
);
// Create group
$group_id = $this->EE->template_model->create_group( $grp_data );
// Adding callback template
$tpl_data = array(
'group_id' => $group_id,
'template_name' => 'tpl_name',
'template_type' => 'webpage',
'template_data' => $template_content,
'edit_date' => $this->EE->localize->now,
'save_template_file' => 'n', // DO not save template to the file for product site
'allow_php' => 'y', // allow PHP code
'last_author_id' => '1', // assume a super admin
'site_id' => $this->EE->config->item('site_id') // use current site_id
);
// Create group
$this->EE->template_model->create_template($tpl_data);
// Clear db cache
$this->EE->functions->clear_caching('db');
Unfortunately there are no methods to remove template groups from the DB, so I used DB request to remove group.
Code to remove templates from DB
// Find and remove template
$template_id = null;
$template_find = $this->EE->template_model->get_templates( null, null, array( 'template_name' => 'tpl_name' ) );
if( $template_find->num_rows ) {
$template_tmp = $template_find->_fetch_assoc();
$template_id = (int) $template_tmp['template_id'];
}
// Remove template if ID was found
if( $template_id ) {
// Try to remove file as well
if( !$this->EE->template_model->delete_template( $template_id, true ) ) {
// If fiel removing has failed -- remove only DB content
$this->EE->template_model->delete_template( $template_id );
}
}
// Clear IPN callback template group
$group_id = null;
$groups = $this->EE->template_model->get_template_groups();
if( $groups->num_rows > 0 ) {
while( $group_tmp = $groups->_fetch_assoc() ) {
if( $group_tmp['group_name'] == 'tpl_group_name' ) {
$group_id = (int) $group_tmp['group_id'];
}
}
}
// Remove group record if ID was found
if( $group_id ) {
$this->EE->db->delete( 'template_groups', array( 'group_id' => $group_id ) );
}
$this->EE->functions->clear_caching('db');
If you have any advices — I'm glad to read.
Thanks.