Jump to content

How to get $templatefile in a hook and use client Custom Fields in a SideBar?


gbotica

Recommended Posts

Hi,

 

I am trying to create a custom sidebar, to appear only on the domains list page of the client area.

 

I have created a custom hook file in /includes/hooks/.

 

Firstly, I'm wrapping the 'ClientAreaSecondarySidebar' hook in a conditional statement, so it's only displayed on the Client Area Domains page ($templatefile = 'clientareadomains').

 

Is this the correct way to get the current page / template file being viewed?

 

if (App::getCurrentFilename() == 'clientarea' &&  $_GET['action'] == 'domains') {

...

}

 

Is there a better way to do this? I tried just using $templatefile, but it's not available.

 

Next, I am creating a new secondary sidebar. I have created some client custom fields in WHMCS Admin and I can see them in the Smarty template debug, they are included in the $clientsdetails array as 'customfields2' and 'customfields3' -- how do I access these in my hook?

 

This is what I've tried, but it doesn't work.

 

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaSecondarySidebar', 1, function (MenuItem $secondarySidebar) {

$secondarySidebar->addChild('custom-fields', array(
'label' => 'Custom Fields'
))->moveToBack();

$customFieldsSidebar = $secondarySidebar->getChild('custom-fields');

$customFieldsSidebar->addChild('field1', array(
'label' => $clientsdetails['customfields1']
));

});

 

Is is possible to get the $clientsdetails array and use it in my hook like this?

 

Any help appreciated.

 

Thank you.

Link to comment
Share on other sites

UPDATE.

 

I've tried using the suggestion here: https://forum.whmcs.com/showthread.php?116334-Access-Custom-Field-in-Hook

 

But, all I get returned is an empty array.

 

use Illuminate\Database\Capsule\Manager as Capsule;
$fieldid = Capsule::table('tblcustomfields')->where('fieldname', '=', 'Dashboard')->where('type', '=', 'client') ->pluck('id');
$dashboardText = Capsule::table('tblcustomfieldsvalues')->where('fieldid', '=', "$fieldid")->where('relid', '=', "$clientid") ->pluck('value');

 

I don't see anywhere in the suggestion where the var $clientid is set -- how do I get that value to use the above code?

 

Thanks again for any help!

Link to comment
Share on other sites

Hi,

 

I am trying to create a custom sidebar, to appear only on the domains list page of the client area.

I have created a custom hook file in /includes/hooks/.

Firstly, I'm wrapping the 'ClientAreaSecondarySidebar' hook in a conditional statement, so it's only displayed on the Client Area Domains page ($templatefile = 'clientareadomains').

Is this the correct way to get the current page / template file being viewed?

 

if (App::getCurrentFilename() == 'clientarea' &&  $_GET['action'] == 'domains') {
}

Is there a better way to do this? I tried just using $templatefile, but it's not available.

it's the usual way to do it... if you *really* want to be specific and use the Smarty template name, you could do this...

 

    GLOBAL $smarty;
   $templatefile = $smarty->getVariable('templatefile');

and then use that in your IF statement...

 

Next, I am creating a new secondary sidebar. I have created some client custom fields in WHMCS Admin and I can see them in the Smarty template debug, they are included in the $clientsdetails array as 'customfields2' and 'customfields3' -- how do I access these in my hook?

This is what I've tried, but it doesn't work.

because the hook doesn't know you're trying to reference a Smarty variable.

 

Is is possible to get the $clientsdetails array and use it in my hook like this?

it is, but I wouldn't recommend it...

 

https://forum.whmcs.com/showthread.php?124926-Custom-Field-in-hook-for-Client-Area-Menu&p=501018#post501018

 

the weird thing about Custom Client Fields in WHMCS is that their order in the array is determined by their sort order in the admin area... so using Smarty variables in the hook, you can only access their ID or the field value, not the name - so as in the above hook, if you know the ID, you're good to go... if you're looping through the array of fields, it then becomes awkward as you don't know the field names... so if you setup your output to loop through the array and add the fieldnames, if you ever later add a new CCF and alter the sort order, it will potentially screw up the output...

 

*breathes* which is a long-winded way of saying that if at all possible, query the DB or use the Class documentation to get the values... if it's a one-off field, then you could use the above hook if necessary.

 

UPDATE.

I've tried using the suggestion here: https://forum.whmcs.com/showthread.php?116334-Access-Custom-Field-in-Hook

But, all I get returned is an empty array.

you have to be careful when using old code - I suspect that code would work fine in v6, but fail in v7.... because the use of 'pluck' changed between the versions. :roll:

 

I don't see anywhere in the suggestion where the var $clientid is set -- how do I get that value to use the above code?

you'd just need to add the following to your hook to access the $client information...

 

    $client = Menu::context('client');

if you wanted to write a hook to use the smarty templatename, to query the db for sidebar info and then output a secondary sidebar, you could do this...

 

<?
use WHMCS\View\Menu\Item as MenuItem;
use Illuminate\Database\Capsule\Manager as Capsule;

add_hook('ClientAreaSecondarySidebar', 1, function (MenuItem $secondarySidebar) {

   GLOBAL $smarty;

   $templatefile = $smarty->getVariable('templatefile');

   $client = Menu::context('client');

   $fieldid = Capsule::table('tblcustomfields')
               ->where('fieldname', 'Dashboard')
               ->where('type', 'client')
               ->value('id');
   $fieldvalue = Capsule::table('tblcustomfieldsvalues')
               ->where('fieldid', $fieldid)
               ->where('relid', $client->id)
               ->value('value');  

   if ($templatefile == 'clientareadomains') { 

       $secondarySidebar->addChild('custom-fields', array(
                   'label' => 'Custom Fields'
                   ));

       $secondarySidebar->getChild('custom-fields')
                       ->addChild('field1', array(
                   'label' => 'Dashboard: '.$fieldvalue
                   ));

   }
}); 

ideally, you should really check if the client has a value for the required CCF and if not, don't create the sidebar - but that's just an AND to add into your IF statement (or perhaps two IF statements - first to check the template filename, another to check the result of $fieldvalue).

 

assuming you have a CCF called "Dashboard" (if you haven't - update the query!), then the hook should work only on the intended page. :idea:

 

Px6w1Hg.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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