Jump to content

Hook for user domain name


jarod

Recommended Posts

I'm new to WHMCS 7+ and the six theme. How do I grab the current users domain name and display it in the sidebar? I've got the hook working and a new sidebar, but I've spent a few hours trying to figure out how to display the current users primary domain without luck.

Link to comment
Share on other sites

This is what I have. It works, but not on all pages.

 

<?php

 

/**

* Display Client's Credit Balance in Client Area

*

* @author WHMCMS

* @link http://www.whmcms.com

* @since WHMCS v6.0.0+

*/

 

use WHMCS\View\Menu\Item as MenuItem;

 

# Add Balance To Sidebar

add_hook('ClientAreaPrimarySidebar', 1, function(MenuItem $primarySidebar){

 

$filename = basename($_SERVER['REQUEST_URI'], ".php");

 

$parseFile = explode('.', $filename);

 

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

 

$clientid = intval($client->id);

 

if ($parseFile['0']!=='clientarea' || $clientid===0){

return;

}

 

$primarySidebar->addChild('Client-Balance', array(

'label' => "Available Credit",

'uri' => '#',

'order' => '1',

'icon' => 'fa-money'

));

 

# Get Currency

$getCurrency = Capsule::table('tblcurrencies')->where('id', $client->currency)->get();

 

# Retrieve the panel we just created.

$balancePanel = $primarySidebar->getChild('Client-Balance');

 

// Move the panel to the end of the sorting order so it's always displayed

// as the last panel in the sidebar.

$balancePanel->moveToBack();

$balancePanel->setOrder(0);

 

# Add Balance.

$balancePanel->addChild('balance-amount', array(

'uri' => 'clientarea.php?action=addfunds',

'label' => '<h4 style="text-align:center;">'.$getCurrency['0']->prefix.$client->credit.' '. $getCurrency['0']->suffix.'</h4>',

'order' => 1

));

 

$balancePanel->setFooterHtml(

'<a href="clientarea.php?action=addfunds" class="btn btn-success btn-sm btn-block">

<i class="fa fa-plus"></i> Add Funds

</a>'

);

 

});

Link to comment
Share on other sites

it works, but not in the sense that it shows the domain - it's only sentq's credit sidebar hook (unaltered I think)...

 

you can do it in a hook using only a few lines - but the problem you have is going to be defining the "primary" domain - what if the client has 10 domains... which one is primary? if you then say that you want to link it to domains with hosting, what if they have more than one hosting account - which is primary ??

 

you can easily add the sidebar to the domaindetails page because there you know which domain is primary (or more accurately which domain is being managed)... beyond that, you'd need to be more specific about defining which domain to show. :idea:

Link to comment
Share on other sites

AHH...

 

That was not what I mean to post LOL.

 

Here's what I've actually come up with. It does display on the clientarea.php?action=productdetails&id=1 page, but doesn't anywhere else.

 

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

   add_hook('ClientAreaPrimarySidebar', 1, function (MenuItem $primarySidebar) 
   {

       $webAccess = $primarySidebar->addChild('webaccess', array( 
       'label' => 'Web Access', 
       'uri' => '#', 
       'icon' => 'fa-desktop', 
       )); 
       $webAccess->moveToFront();

	$service = Menu::context('service'); 
	$domain = "{$service->domain}"; 

       $webAccess->addChild(
		'domain', 
		array( 
			'label' => $domain, 
			'order' => 3, 
			'icon' => 'fa-globe', 
			'uri' => 'https://'.$domain, 
		)
	);

       $client = Menu::context('client');
       $name = is_null($client)
           ? ''
           : "<strong>{$client->firstName}</strong>";


       $webAccess->setBodyHtml(
           $webAccessAvailable
               ? "{$name}"
               : "{$name}"
       ); 
   });

 

- - - Updated - - -

 

I would like to display all the domain names associated with a specific product. Let's say I have product ABC which will always have a domain name attached to it, the domain assigned to that ABC product would display. I'm a little under the weather and not running on all pistons...

Link to comment
Share on other sites

I'm a little under the weather and not running on all pistons...

you never want to be writing hooks under those conditions! :)

 

that hook is working correctly - not in the way you want it to perhaps, but it makes sense it would only work on that productdetails page.

 

if this sidebar needs to be on all client area pages, you may end up needing to query the database for the information you require.... it should only be a very simple query to the tblhosting table. :idea:

Link to comment
Share on other sites

it's going to be something along the lines of...

 

<?php

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

add_hook('ClientAreaPrimarySidebar', 1, function(MenuItem $primarySidebar){

   $client = Menu::context("client");
   $domains = Capsule::table('tblhosting')
               ->where('userid', $client->id)
               ->where('packageid','2')
               ->get();

   if ($domains) {
       $primarySidebar->addChild('webaccess', array( 
                       'label' => 'Web Access', 
                       'icon' => 'fa-desktop', 
       )); 

       foreach ($domains as $child) {
           $primarySidebar->getChild('webaccess')
                           ->addChild($child->domain, array( 
                           'label' => $child->domain, 
                           'icon' => 'fa-globe', 
                           'uri' => 'https://'.$child->domain,
           )); 
       }
   }
}); 

i'm assuming a client can have multiple numbers of the same product, each with it's own unique domain name - the only thing you should need to change for now is the packageid (product ID) value for your specific product. :idea:

 

PmheFqM.png

 

if a client doesn't have this specific product, then the sidebar isn't created. :)

Link to comment
Share on other sites

Thank you for this!

 

My final product is as such:

 

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

add_hook('ClientAreaPrimarySidebar', 1, function(MenuItem $primarySidebar){

   $client = Menu::context("client");
// -- Database queries	
   $systemurl = Capsule::table('tblconfiguration')
               ->where('setting','SystemURL')
               ->get();
$sys = '';
	foreach ($systemurl as $URL) {
		$sys .= $URL->value . PHP_EOL;
}  
$caPD = 'clientarea.php?action=productdetails&id=';
   $domain = Capsule::table('tblhosting')
               ->where('packageid','1')
               ->get();
// -- Sidebars			
$webAccess = $primarySidebar->addChild('webaccess', array( 
				'label' => 'Web Access', 
				'icon' => 'fa-globe', 
			));
// -- Misc variables	
$aO = "<a href='";
$aOl = "<a class='list-group-item' href='https://";
$iO = "'><i class='fa ";
$iC = "' aria-hidden='true'></i>  ";
$aC = '</a>';

       foreach ($domain as $acc) {
	// -- Sidebar panel	
		$webAccess->setBodyHtml(
			"Get to where you need to go."
		);	
	// -- Sidebar access links	
		$webAccess->addChild(
					$aO.$acc->domain.$iO."fa-home fa-fw".$iC.$acc->domain.$aC
				."<div class='webaccess-list'>"
				//-- Access link
					.$aOl.$acc->domain."/edit".$iO."fa-tachometer".$iC."Dashboard".$aC
					.$aOl.$acc->domain."/tools".$iO."fa-wrench".$iC."Tools".$aC
					.$aOl.$sys.$caPD.$acc->id.$iO."fa-cog fa-fw".$iC."Details".$aC
				."</div>"	
		);	
	}
});

 

- - - Updated - - -

 

Here's what the thing looks like :)

 

Capture.PNG

Link to comment
Share on other sites

you must be taking some seriously strong medication looking at those misc variables! :twisted:

 

a couple of points - you don't need to query the database to get Configuration variables, you can access them by declaring them as global (as in the thread below)...

 

https://forum.whmcs.com/showthread.php?123874-Hook-to-add-link-to-client-nav-bar-under-Your-Account&p=497214#post497214

 

in fact, I think for your purpose you don't even need the variable at all because you only use it in the clientarea.php link and as it's in a sidebar, you know that it must always be a WHMCS page... so just the link without any url or https should be sufficient... homepage link is wrong too... also, you've added double font awesome icons to two of the links - they only need one, and if you remove the two "fa-fw", then the icons will align.

 

... it's given me an idea to try something at the weekend with this... :)

Link to comment
Share on other sites

I appreciate your feedback, Brian. Here's my updated code:

 

include ('includes/_sidebar.php');
global $CONFIG;
$client = Menu::context("client");
// -- Database queries
$domain = Capsule::table('tblhosting')
		//->where('userid', $client->id)
		->where('packageid','1')
		->get();
// -- Sidebar			
$netAccess = $primarySidebar->addChild('yourwebsites', array( 
				'label' => 'Network Access', 
				'icon' => 'fa-globe', 
			));
// -- Sidebar panel	
$netAccess->setBodyHtml(
	"Get to where you need to go."
);	
// Go time
foreach ($domain as $acc) {
	// -- Sidebar access links	
	$netAccess->addChild(
		$aO.$togl.$iO."fa-home fa-fw".$iC.$acc->domain.$aC
		."<a class='close hide'><i class='fa fa-times' aria-hidden='true'></i> Close</a>"
			."<div class='collapsed' id='collapse'>"
				//-- Access links
				.$aOl.$https.$acc->domain.$iO."fa-external-link".$iC."Visit".$aC
				.$aOl.$https.$acc->domain."/edit".$iO."fa-tachometer".$iC."Dashboard".$aC
				.$aOl.$https.$acc->domain."/tools".$iO."fa-wrench".$iC."Tools".$aC
				.$aOl.$sys.$caPD.$acc->id.$iO."fa-cog".$iC."Details".$aC
			."</div>"	
	);	
}

 

Here's the _sidebar include:

 

// -- Misc variables
$sys = $CONFIG['SystemURL'];
$caPD = '/clientarea.php?action=productdetails&id=';
$togl = " class='link'";	
$aO = "<a ";
$aOl = "<a class='list-group-item' href='";
$https = 'https://';
$http = 'http://';
$iO = "'><i class='fa ";
$iC = "' aria-hidden='true'></i>  ";
$aC = '</a>';
?>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
.pointer {
   cursor: pointer;
}
</style>
<script>
$("document").ready(function () {;
$.each($(".link"), function(index, value){
	var num = index + 1;
	$(value).attr("class","pointer open-"+ num);
});
$.each($(".collapsed"), function(index, value){
	var num = index + 1;
	$(value).attr("class","hide webaccess-list collapsed-"+ num);
});
$.each($(".close"), function(index, value){
	var num = index + 1;
	$(value).attr("class","pointer hide close-"+ num);
});
$(".open-1").click(function() {
	$('.open-1').addClass("hide");
	$('.open-2').removeClass("hide");$(".collapsed-2").addClass("hide");
	$('.close-1')
		.removeClass("hide");$(".collapsed-1").removeClass("hide");
	$('.close-2').addClass("hide");
});
$(".close-1").click(function() {
	$('.close-1').addClass("hide");
	$('.open-1')
		.removeClass("hide");$(".collapsed-1").addClass("hide");
});
$(".open-2").click(function() {
	$('.open-2').addClass("hide");
	$('.open-1').removeClass("hide");$(".collapsed-2").removeClass("hide");
	$('.close-1').addClass("hide");
	$('.close-2').removeClass("hide");$(".collapsed-1").addClass("hide");
});
$(".close-2").click(function() {
	$('.open-2').removeClass("hide");$(".collapsed-2").addClass("hide");
	$('.close-2').addClass("hide");
});
});
</script>

 

I was trying at creative for the variables lol.

 

- - - Updated - - -

 

I commented out where('userid', $client->id) so you can see this public :)

 

https://btwebnetwork.com/accounts/

 

- - - Updated - - -

 

HA - and removed the rouge fa-fw classes. I'm only fairly experienced with PHP Javascript, enough to be dangerous anyway, but I'm sure there are many ways I could improve the code. I'm open to suggestions. I would like it clean and tidy and minimal :D

Link to comment
Share on other sites

I was trying at creative for the variables lol.

I think my point was if there was any need for them - not to move them to a separate include file! :roll:

 

unless you were going to use those same variables in multiple hooks, or a massive hook, I personally wouldn't bother to use them - you gain nothing by using them, and it would make the code clearer just to code the output normally.

 

I commented out where('userid', $client->id) so you can see this public :)

https://btwebnetwork.com/accounts/

it's worth noting that the js part of the hook wouldn't work if 3 or more results were generated by the query - the first two could open/close, but none after that.

also, you could tweak your query to check if the domain is active (e.g pointless adding a link if the account has been cancelled).

and what happens if the query contains no results - it still creates the sidebar.

 

I'm open to suggestions. I would like it clean and tidy and minimal :D

clean and tidy is good... but it has to be readable to others if you're posting it here - i'm all for taking shortcuts - but not if you can get lost using them! :)

Link to comment
Share on other sites

I do intend to use the variables for further development. If any of them need adjusting it's just easier to manage. Learned that was worth the hassle lol. The include also helps keep things organized. How would I check against an active domain / ? The js can be manually cloned which is no ideal imo. Any direction in consolidating this it would be appreciated.

Link to comment
Share on other sites

I do intend to use the variables for further development. If any of them need adjusting it's just easier to manage. Learned that was worth the hassle lol.

each to their own! :)

 

The include also helps keep things organized.

as above - it's giving WHMCS more chance to fail calling multiple files...

 

How would I check against an active domain / ?

as it's in the table you're querying, it's just another where to the query...

 

$domain = Capsule::table('tblhosting') 
           ->where('userid', $client->id) 
           ->where('packageid','1') 
           ->where('domainstatus','Active') 
           ->get();

 

The js can be manually cloned which is no ideal imo. Any direction in consolidating this it would be appreciated.

I had the whole thing rewritten in 15 mins on Saturday (in 1 file) and working fine with multiple children... but got interrupted during further tweaks and didn't save the finished working file - so when I tried it again yesterday, it wouldn't work. :mad:

 

i'll take another look at it at the weekend (or sooner if I get the chance).

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.

  • 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