Jump to content

Converting some v5 custom template code to v6


Recommended Posts

Hi,

 

I had my whmcs upgraded from v5.3.14 to v6.3.1. I managed to get about everything to work and started chasing up bugs when I figured some of my custom code to display products pricing and currency on my different homepages (in different languages) was no longer working.

 

DOes anyone know what has to be changed to make it v6 compatible (note that for now smarty {php}tag is enabled):

 

	    {php}
if (isset($_REQUEST['cid'])) $_SESSION['currency'] = $_REQUEST['cid'];

$conversionrate = 0;

$result = select_query("tblcurrencies","id,code,rate,prefix,suffix","","code","ASC");
while ($data = mysql_fetch_array($result)) {

   $currencyid = $data['id'];
   $currencycode = $data['code'];
   $currencyrate = $data['rate'];

   if ($currencyid==$_SESSION['currency']) {
       $conversionrate = $currencyrate;
       $currencyprefix = $data['prefix'];
       $currencysuffix = $data['suffix'];
   }

   echo '<li><a href="'.$_SERVER['PHP_SELF'].'?cid='.$currencyid.'"><img src="images/flags/';
   if ($currencycode=="AUD") echo 'au';
   elseif ($currencycode=="CAD") echo 'ca';
   elseif ($currencycode=="CHF") echo 'ch';
   elseif ($currencycode=="EUR") echo 'eu';
   elseif ($currencycode=="GBP") echo 'gb';
   elseif ($currencycode=="INR") echo 'in';
   elseif ($currencycode=="JPY") echo 'jp';
   elseif ($currencycode=="USD") echo 'us';
   elseif ($currencycode=="ZAR") echo 'za';
   else echo 'na';
   echo '.png" alt="" /> '.$currencycode.'</a></li>';

}

if (!$conversionrate) {
   $conversionrate = 1;
   $currencyprefix = "";
   $currencysuffix = "€";
}

{/php}
 </ul>
<!-- End currency selector --></p>
 <div class="plan1">
   <h3><img src="templates/{$template}/english/images/plan_icon3.png" alt="" />Voice Servers</h3>
   <div class="planrate"> Starting only!<br />
     <span>{php}echo ''.$currencyprefix.number_format((1.50*$conversionrate), 2, '.', '').$currencysuffix.'';{/php}/mo</span> 

 

The above code is displaying 0.00 and no currency prefix/suffix.

 

Thank you

Link to comment
Share on other sites

I would be seriously tempted to just dump the {php} code entirely (you'll have to one day anyway) and split it into an action hook (to query the db and get a full currency array) and Smarty to handle the output in the template. :idea:

 

in that second {php} block, $conversionrate isn't global, hence not defined and will equal 0 - and anything multiplied by 0 gives zero!

you could get around that by making it one {php} block, but then you're starting to overcomplicate the whole process... and anyway, you'll still have a {php} block of code!

also, when you first go to the page, the currency will not be set in the session - initially cid wouldn't exist either... so you would have to define a default currency in some way.

 

the hook is really basic - just create a file in the /includes/hooks folder, call it 'currencies.php' and paste the following code into it...

 

<?php

use Illuminate\Database\Capsule\Manager as Capsule;

function currencies_hook($vars) 
{
   $currencies = Capsule::table('tblcurrencies')
                   ->get();

   $encodedata = json_encode($currencies);
   $decodedata = json_decode($encodedata, true);

   return array("currencies" => $decodedata);
}
add_hook("ClientAreaPage", 1, "currencies_hook");
?>

that will give you a full $currencies Smarty array to use in your page and replaces the partial array that is created by default.

 

then in your template, you can use these three blocks of Smarty code...

 

first we try to determine the current currency choice...

 

{if isset($smarty.get.cid)}
   {assign cid $smarty.get.cid}
{else}
   {assign cid '1'}
{/if}

i'm being slightly lazy with this code, but if the client has clicked on a currency link, then $cid will equal that value; if they haven't, then i'm assigning a default currency ID to the $cid variable... you could get the proper figure by querying the currencies array, but i'm saving time by just hard-coding the value.... i'm assuming you know your default currency ID! :?:

 

then we can use the following code to output the flags with their currency links...

 

{foreach $currencies as $cartcurrency}
   <li><a href="{$smarty.server.PHP_SELF}?cid={$cartcurrency.id}"><img src="{$BASE_PATH_IMG}/flags/{$cartcurrency.code|substr:0:2|strtolower}.png" alt="" /> {$cartcurrency.code}</a></li>
       {if $cartcurrency.id eq $cid}
           {assign conversionrate $cartcurrency.rate}
           {assign currencyprefix $cartcurrency.prefix}
           {assign currencysuffix $cartcurrency.suffix}
       {/if}
{/foreach}

with this, i've got rid of your long if statement and used some Smarty code to truncate the value to use as a filename... this could be tweaked further to see if the image file exists before first trying to show it... you may also need to play with the path to the flag icons.

 

and then you have your output code - which is very similar, but again no longer needs to use the dreaded {php} tags... :idea:

 

  </ul>
   <!-- End currency selector --></p>
 <div class="plan1">
   <h3><img src="templates/{$template}/english/images/plan_icon3.png" alt="" />Voice Servers</h3>
   <div class="planrate"> Starting only!<br />
     <span>{$currencyprefix}{($conversionrate*1.5)|number_format:2:".":","}{$currencysuffix}/mo</span>

i've quickly tested this locally and it works, but the real test will be when you try it on your own website. :)

Link to comment
Share on other sites

Wow that was one very helpful reply, thank you very much you made my day !

i'm glad to have helped...

 

one further minor tweak, which may not be necessary, would be to check if the currency has been set in the session - and then only as a last resort if neither the session or cid is set, to pass a default value...

 

{if isset($smarty.get.cid)}
   {assign cid $smarty.get.cid}
{elseif isset($smarty.session.currency)}
   {assign cid $smarty.session.currency}
{else}
   {assign cid '1'}
{/if}

Link to comment
Share on other sites

I went to finish the job and fully clean the {php} tags from my template but it seems that converting to hooks is ways out of my skills. May I abuse of your time and request your help for the following pieces of code too?

That would be really great but I would understand if not possible.

 

Code #1:

{php}
$clienthosting = $template->get_template_vars(service);
$dbid = $clienthosting['id'];  
$query = mysql_query("SELECT dedicatedip FROM tblhosting WHERE id = $dbid");  
$result = mysql_fetch_array($query); 
$dedicatedip = $result["dedicatedip"];   
$template->assign("dedicatedip", $dedicatedip); 
{/php} 

 

 

Code #2:

{php}echo date('Y-m-d H:i:s', (time() - 86400));{/php}

 

 

I should then be able to understand the logic and to apply changes myself to the other pieces of code.

 

Thank you

Link to comment
Share on other sites

to take them in reverse... to get yesterday's time & date in Smarty...

 

{"-1 day"|date_format:"%Y-%m-%d %H:%M:%S"}

 

and I recognise that first block of code! :idea:

 

https://forum.whmcs.com/showthread.php?89738-Variable-for-dedicated-IP-in-clientareaproducts-tpl&p=377822#post377822

 

one way to convert it to a hook would be to use...

 

<?php

use Illuminate\Database\Capsule\Manager as Capsule;

function dedicatedip_hook($vars) {

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

   $services = Capsule::table('tblhosting')
               ->where('userid',$client->id)
               ->select('id','dedicatedip')
               ->get();

   $encodedata = json_encode($services);
   $decodedata = json_decode($encodedata, true);

   return array("dedicatedip" => $decodedata);
}
add_hook("ClientAreaPageProductsServices", 1, "dedicatedip_hook");
?>

and then in the template, use...

 

                    <td><strong>{$service.product}</strong>{if $service.domain}<br /><a href="http://{$service.domain}" target="_blank">{$service.domain}</a> 
                       {foreach item=ip from=$dedicatedip}
                           {if $service.id eq $ip.id}
                               {$ip.dedicatedip}
                           {/if}
                       {/foreach}                            
                   {/if}</td>

it's not necessarily the most efficient way to do it from a programming pov, but i'm too lazy to append the existing $services array!

Link to comment
Share on other sites

thank you for the time in smarty, I was able to adjust the similarcode.

 

Indeed, you already assited me with that other code :)

However the conversion is not working and messing with the template, returning the actual "array" word etc.

 

 

isn't it possible to hire your services by any chance? If so please send me a quote for converting some more code, I believe I got only 3 template files still holding some {php}.

Link to comment
Share on other sites

  • 3 weeks later...
While your code was working flawlessly for currency conversion it turns out that it stopped working suddenly (most likely after 7.0.1 update?).

Was there any change in 7.0.1 that could explain this?

i've just tried this on a v7.0.1 dev and it still works fine - both the hook and the Smarty code. :idea:

 

however, I can see it's not working on your website - the chances are that this is simply being caused by one part being missing... either the hook, or the Smarty code. :?:

 

feel free to PM me one of your templates, e.g the one used for voiceservers.php and i'll take a look.

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