Jump to content
  • 0

How to create custom provisioning modules and hook custom .tpl files with WHMCS?


Keval Bhogayata

Question

7 answers to this question

Recommended Posts

  • 1

To start with, do NOT use php code inside of template files. This has been deprecated for years now. Follow basic common sense practices and you'll be fine

<edit>
My bad, a bit more research shows that this can be done with provisioning modules.

You'll want to take a look at their sample provisioning module . Download it, dissect it, then go from there. Once you're done with that you should understand how to add your template files in

Edited by twhiting9275
corrected a couple things
Link to comment
Share on other sites

  • 1

If you're just starting out, you don't. Find a way to implement this outside of the template

PHP code inside of smarty templating has been deprecated for quite some time, and will eventually be removed. You need to find a proper method to doing what is being done. There's always a way to do that.

Link to comment
Share on other sites

  • 1
10 hours ago, Keval Bhogayata said:

I do not know how to include and run php code inside .tpl file.

what you need to do is to write your PHP logic inside the PHP file, then pass the results as Smarty variables to be displayed in your .TPL file,

check this file: https://github.com/WHMCS/sample-provisioning-module/blob/master/modules/servers/provisioningmodule/provisioningmodule.php#L744

function provisioningmodule_ClientArea(array $params)
{
    // Determine the requested action and set service call parameters based on
    // the action.
    $requestedAction = isset($_REQUEST['customAction']) ? $_REQUEST['customAction'] : '';
    if ($requestedAction == 'manage') {
        $serviceAction = 'get_usage';
        $templateFile = 'templates/manage.tpl';
    } else {
        $serviceAction = 'get_stats';
        $templateFile = 'templates/overview.tpl';
    }
    try {
        // Call the service's function based on the request action, using the
        // values provided by WHMCS in `$params`.
        $response = array();
        $extraVariable1 = 'abc';
        $extraVariable2 = '123';
        return array(
            'tabOverviewReplacementTemplate' => $templateFile,
            'templateVariables' => array(
                'extraVariable1' => $extraVariable1,
                'extraVariable2' => $extraVariable2,
            ),
        );
    } catch (Exception $e) {
        // Record the error in WHMCS's module log.
        logModuleCall(
            'provisioningmodule',
            __FUNCTION__,
            $params,
            $e->getMessage(),
            $e->getTraceAsString()
        );
        // In an error condition, display an error page.
        return array(
            'tabOverviewReplacementTemplate' => 'error.tpl',
            'templateVariables' => array(
                'usefulErrorHelper' => $e->getMessage(),
            ),
        );
    }
}

as you can see the "templateVariables" include "extraVariable1" and "extraVariable2",  these variables for example will have the result of your logic (PHP code executed to do certain task), so in your tpl file you can write {$extraVariable1} and it should display the value of that variable.

it should be as easy as that, but if you need further explanation just ask :)

Link to comment
Share on other sites

  • 1
6 hours ago, Keval Bhogayata said:

Thank you very much, it worked for me. How to know which action will be triggered on which client area buttons/links if you can help?

the following part check if "custom" action were called, if so you can specify different options for each action (template file, variables, etc):

$requestedAction = isset($_REQUEST['customAction']) ? $_REQUEST['customAction'] : '';
if ($requestedAction == 'manage') {
	$serviceAction = 'get_usage';
    $templateFile = 'templates/manage.tpl';
} else {
   	$serviceAction = 'get_stats';
    $templateFile = 'templates/overview.tpl';
}

 

Link to comment
Share on other sites

  • 0
14 hours ago, twhiting9275 said:

To start with, do NOT use php code inside of template files. This has been deprecated for years now. Follow basic common sense practices and you'll be fine

<edit>
My bad, a bit more research shows that this can be done with provisioning modules.

You'll want to take a look at their sample provisioning module . Download it, dissect it, then go from there. Once you're done with that you should understand how to add your template files in

I have already downloaded similar module earlier,  but I do not know how to include and run php code inside .tpl file.

Link to comment
Share on other sites

  • 0
On 2/2/2018 at 4:31 AM, sentq said:

what you need to do is to write your PHP logic inside the PHP file, then pass the results as Smarty variables to be displayed in your .TPL file,

check this file: https://github.com/WHMCS/sample-provisioning-module/blob/master/modules/servers/provisioningmodule/provisioningmodule.php#L744


function provisioningmodule_ClientArea(array $params)
{
    // Determine the requested action and set service call parameters based on
    // the action.
    $requestedAction = isset($_REQUEST['customAction']) ? $_REQUEST['customAction'] : '';
    if ($requestedAction == 'manage') {
        $serviceAction = 'get_usage';
        $templateFile = 'templates/manage.tpl';
    } else {
        $serviceAction = 'get_stats';
        $templateFile = 'templates/overview.tpl';
    }
    try {
        // Call the service's function based on the request action, using the
        // values provided by WHMCS in `$params`.
        $response = array();
        $extraVariable1 = 'abc';
        $extraVariable2 = '123';
        return array(
            'tabOverviewReplacementTemplate' => $templateFile,
            'templateVariables' => array(
                'extraVariable1' => $extraVariable1,
                'extraVariable2' => $extraVariable2,
            ),
        );
    } catch (Exception $e) {
        // Record the error in WHMCS's module log.
        logModuleCall(
            'provisioningmodule',
            __FUNCTION__,
            $params,
            $e->getMessage(),
            $e->getTraceAsString()
        );
        // In an error condition, display an error page.
        return array(
            'tabOverviewReplacementTemplate' => 'error.tpl',
            'templateVariables' => array(
                'usefulErrorHelper' => $e->getMessage(),
            ),
        );
    }
}

as you can see the "templateVariables" include "extraVariable1" and "extraVariable2",  these variables for example will have the result of your logic (PHP code executed to do certain task), so in your tpl file you can write {$extraVariable1} and it should display the value of that variable.

it should be as easy as that, but if you need further explanation just ask :)

Thank you very much, it worked for me. How to know which action will be triggered on which client area buttons/links if you can help?

Link to comment
Share on other sites

  • 0
20 hours ago, sentq said:

the following part check if "custom" action were called, if so you can specify different options for each action (template file, variables, etc):


$requestedAction = isset($_REQUEST['customAction']) ? $_REQUEST['customAction'] : '';
if ($requestedAction == 'manage') {
	$serviceAction = 'get_usage';
    $templateFile = 'templates/manage.tpl';
} else {
   	$serviceAction = 'get_stats';
    $templateFile = 'templates/overview.tpl';
}

 

Ok, got it. Thanks again.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated