Jump to content

sidebar for specific product and connect to tabase


nimafire

Recommended Posts

Hey

is it possible to add a custom sidebar for specific product for client who has active service ?

 

sidebar name: custom

 

if customer has product id 32, then this sidebar visible in his panel !

 

also how can i connect to database and read a table and show its query in sidebar ?

Link to comment
Share on other sites

is it possible to add a custom sidebar for specific product for client who has active service ?

sidebar name: custom

if customer has product id 32, then this sidebar visible in his panel !

if you mean on the productdetails page, then yes - you'd ideally need to do it using an action hook...

 

if outside of productdetails, then the hook might need to query the database to see if the client has product x and is active...

 

an example of the first is shown in the hook below - it works on productdetails only and if the product uses cPanel, then two additional child links are added to the sidebar (login/webmail)...

 

https://forum.whmcs.com/showthread.php?104894-Login-to-cPanel-amp-Webmail-on-Client-Product-Details&p=435091#post435091

 

your hook would be vaguely similar, except you'd use the Class documentation to get the productid (PackageID) and if it equals x, then you create your new sidebar.

 

also how can i connect to database and read a table and show its query in sidebar ?

I assume that you don't want to show the query, but the result. :)

 

it really comes down to what information you want and how you can get it... so querying the database might be one option, using the Class docs could be another - or maybe even the API... each method requires a slightly different technique.

 

there have been plenty of example hooks posted that query the database - perhaps do a forum search on 'capsule and hook' and you should get a list of threads possibly containing example hook code... technically, the documentation for interacting with the database is @ https://developers.whmcs.com/advanced/db-interaction/ - but depending on your level, you might find it easier to just adapt a previously posted hook code for your own needs.

Link to comment
Share on other sites

ok let me explain better about what hook im working on it,

ive create a .php file which select part of data from whmcs database and display it on separate file.

visitors can check this by visiting http://domain.whmcs.com/file.php

but this is stupid !

 

im looking for a side bar which can (of course enable when ... ) display output of this file but for who has a active product id 15 on his profile.

Link to comment
Share on other sites

im looking for a side bar which can (of course enable when ... ) display output of this file but for who has a active product id 15 on his profile.

it would have been easier if you had posted file.php (or at least the gist of it), but without knowing what you're querying or what the output it, let me answer in generalities...

 

if you wanted to query the database to see if a client had a specific active product, then you would use a query similar to the code in the post below...

 

https://forum.whmcs.com/showthread.php?120798-How-to-check-for-an-Active-Product-by-an-Customer&p=486738#post486738

 

and so to incorporate that query in a hook that generates a sidebar would 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'); 

   $activeproduct = Capsule::table('tblhosting')
                   ->where('userid',$client->id)
                   ->where('packageid','15')
                   ->where('domainstatus','Active')
                   ->count();

   if ($activeproduct > 0) {

       $primarySidebar->addChild('prod15', array(
               'label' => 'My New Sidebar',
               'order' => '10',
               'icon' => 'fa-server'
       ));
   }
});

if the client has that specific active product, then they'll see a new sidebar when they login..

FA7Fznl.png

 

you're going to need to work out whether you want it to be a primary/secondary sidebar; when it's shown (e.g on all pages or just specific ones); where it appears (sort order) etc, icons, buttons, links etc...

 

ultimately, you'll also have to add your database query to the hook and output the result in the sidebar... that's likely to be by adding new children to it, or perhaps in the body or footer - but i've no idea which, as I don't know the content.

Link to comment
Share on other sites

thank you. this is the content that i want to show in this sidebar:

 


<?php

$username = "";
$password = "";
$hostname = "localhost"; 

$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("DATABASENAME",$dbhandle)
 or die("Could not select examples");

$result = mysql_query("SELECT rate FROM mod_gateway WHERE currency= 'USD'");
$row = mysql_fetch_array($result);
$exchange = $row['rate'];
echo "$exchange";

mysql_close($dbhandle);
?>

Link to comment
Share on other sites

then if you're only expecting one value and not an array, and assuming it's 7.1, you could do something like this...

 

<?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'); 

   $activeproduct = Capsule::table('tblhosting')
                   ->where('userid',$client->id)
                   ->where('packageid','2')
                   ->where('domainstatus','Active')
                   ->count();

   if ($activeproduct > 0) {

       $exchangerate = Capsule::table('mod_gateway')
                       ->where('currency','USD')
                       ->value('rate');    

       if ($exchangerate) {

       $primarySidebar->addChild('prod15', array(
               'label' => 'My New Sidebar',
               'order' => '10',
               'icon' => 'fa-money'
            ));

       $primarySidebar->getChild('prod15')
                       ->addChild('exchangerate', array(
                           'label' => 'Exchange Rate: '.$exchangerate
                       ));        
       }
   }
});

it's only a rough example, but it's written so that if it can't find a value for the rate in the database table, it doesn't attempt to create the sidebar... that should avoid the creation of a sidebar with a null value.

 

ENSCziZ.png

Link to comment
Share on other sites

mm get an error when upload it on users profile,

blank page. which means something goes wrong on hooks.

as you can check my example, there is a table which name is mod_gateway,

there is 2 column inside it and one row.

rate is the name of column, there is no array only one value is under rate column and i want to display it inside this sidebar

Link to comment
Share on other sites

mm get an error when upload it on users profile,

blank page. which means something goes wrong on hooks.

as you can check my example, there is a table which name is mod_gateway,

there is 2 column inside it and one row.

rate is the name of column, there is no array only one value is under rate column and i want to display it inside this sidebar

if you ever get a blank page with a hook, then go to general settings -> other -> display errors and enable it - it should at least give you a clue as to where the error is.

 

mm can you change that hook for 6.3 version?

the only change you should need to make for v6.3 is to change...

 

                        ->value('rate');

to...

                        ->pluck('rate');

PKK8Wbu.png

Link to comment
Share on other sites

  • 6 months later...

Hello

this sidebar hook work fine unless upgrade to new version after that it cant get rate, output change to : "my rate is: Array"

however nothing change in code,

    $activeproduct = Capsule::table('tblhosting')
                    ->where('userid',$client->id)
                    ->where('packageid','155')
                    ->where('domainstatus','Active')
                    ->count();
                    
    if ($activeproduct > 0) {

        $exchangerate = Capsule::table('mod_gateway_exchange_rate')
                        ->where('currency','USD')
                        ->pluck('exchange_rate');    

        if ($exchangerate) {

.......

 
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