Jump to content

temporarilly change sorting of items


postcd

Recommended Posts

Hello, here is described the issue:

https://requests.whmcs.com/responses/temporarilly-change-sorting-of-items

 

WHMCS Client area upgrade page is messy. I want to ask how can we temporarilly change sorting of items so client is not lost on the upgrade page?

 

This is the upgrade.tpl code:

 

<table class="table table-striped table-framed">
{foreach key=num item=upgradepackage from=$upgradepackages}
<tr>
<td>
<form method="post" action="{$smarty.server.PHP_SELF}">
<input type="hidden" name="step" value="2">
<input type="hidden" name="type" value="{$type}">
<input type="hidden" name="id" value="{$id}">
<input type="hidden" name="pid" value="{$upgradepackage.pid}">
<strong>{$upgradepackage.groupname} - {$upgradepackage.name}</strong>
</td>

 

---

So far i at least added hyperlinks leading to the products category, so they can read info:

 

find:

<strong>{$upgradepackage.groupname} - {$upgradepackage.name}</strong>

 

relace by:

<strong><abbr title="Click this link to show this group packages details in a new window"><a href="http://YourHosting.com/cart.php?pid={$upgradepackage.pid}" target="_blank">{$upgradepackage.groupname} - {$upgradepackage.name}</a></abbr></strong>

 

then add header to the table:

<h3>Dont know which package? Then click group name bellow to show all packages details in a new window.</h3></ br>

 

 

But still sorting of packages acros the groups is not solved

 

This is what is needed:

"run through the foreach loop and check to see what the Group ID is of each product. You will then need to loop back through your new data to display the upgrade products."

Edited by postcd
Link to comment
Share on other sites

i've just had a play with upgrade.tpl and it is strange the order in which the available upgrade products are shown... :twisted:

 

one possible solution to this would be to add a Smarty plugin that you could use to sort the upgradepackages array in the template - that should remove the need for multiple loops...

 

if you create a file in /includes/classes/Smarty/plugins and call it 'modifier.sortby.php' - then add the following code into it...

 

<?php
#
# sorts an array of named arrays by the supplied fields

function array_sort_by_fields(&$data, $sortby){
     static $sort_funcs = array();

   if (empty($sort_funcs[$sortby]))
   {
       $code = "\$c=0;";
       foreach (split(',', $sortby) as $key)
       {
          $d = '1';
             if (substr($key, 0, 1) == '-')
             {
                $d = '-1';
                $key = substr($key, 1);
             }
             if (substr($key, 0, 1) == '#')
             {
                $key = substr($key, 1);
              $code .= "if ( \$a['$key'] > \$b['$key']) return $d * 1;\n";
$code .= "if ( \$a['$key'] < \$b['$key']) return $d * -1;\n";
             }
             else
             {
              $code .= "if ( (\$c = strcasecmp(\$a['$key'],\$b['$key'])) != 0 ) return $d * \$c;\n";
           }
       }
       $code .= 'return $c;';
       $sort_func = $sort_funcs[$sortby] = create_function('$a, $b', $code);
   }
   else
   {
       $sort_func = $sort_funcs[$sortby];
   }   
   uasort($data, $sort_func);   
}

#
# Modifier: sortby - allows arrays of named arrays to be sorted by a given field
#

function smarty_modifier_sortby($arrData, $sortfields) {
  array_sort_by_fields($arrData, $sortfields);
  return $arrData;
} 

next, you modify the upgrade.tpl template to use these new sort features - so replace the line below...

 

{foreach key=num item=upgradepackage from=$upgradepackages}

with...

{foreach key=num item=upgradepackage from=$upgradepackages|@sortby:"gid,pid"}

this will display the packages sorted first by group ID, and then by product ID...

 

if you wanted them in alphabetical order (a-z) by product name, you could use...

 

{foreach key=num item=upgradepackage from=$upgradepackages|@sortby:"name"}

or if you wanted them in reverse alphabetical order (z-a) by peoduct name...

 

{foreach key=num item=upgradepackage from=$upgradepackages|@sortby:"-name"}

or sorted alphabetically by groupname and product name...

 

{foreach key=num item=upgradepackage from=$upgradepackages|@sortby:"groupname,name"}

it would be a little trickier using this method to sort by price, so please try to avoid doing that! :)

 

also, if you give it an invalid sort order, e.g a variable that doesn't exist, it will just display the packages in its default order.

 

with regards to your request about adding product details, you can easily do that by adding the product description variable after the group/product heading... so replace the following line...

 

<strong>{$upgradepackage.groupname} - {$upgradepackage.name}</strong>

with...

 

<strong>{$upgradepackage.groupname} - {$upgradepackage.name}</strong>
<br><br>{$upgradepackage.description}

if your descriptions are too long and you have so many products on the upgrade page that you feel it is too lengthy, you can always truncate the length of the product descriptions to a smaller amount...

 

<strong>{$upgradepackage.groupname} - {$upgradepackage.name}</strong>
<br><br>{$upgradepackage.description|truncate:50}

obviously, you don't need to use break returns, you can use code/css that is more appropriate for your template layout.

 

I hope that helps! :idea:

Link to comment
Share on other sites

Thx, Your Mod works fine and the result look nice. Now we have properly sorted products with descriptions.

 

PS: i had older WHMCS and folder for modifier.sortby.php file was not:

/includes/classes/Smarty/plugins

but:

/includes/smarty/plugins

Thx

Link to comment
Share on other sites

Thx, Your Mod works fine and the result look nice. Now we have properly sorted products with descriptions.

i'm glad that you got it working.

 

I found that plugin posted in the Smarty forums a while back - it comes in useful if you want to manipulate an array's sort order within the template.

Link to comment
Share on other sites

  • 7 months later...

<?php
#
# sorts an array of named arrays by the supplied fields

function array_sort_by_fields(&$data, $sortby){
     static $sort_funcs = array();

   if (empty($sort_funcs[$sortby]))
   {
       $code = "\$c=0;";
       foreach (split(',', $sortby) as $key)
       {
          $d = '1';
             if (substr($key, 0, 1) == '-')
             {
                $d = '-1';
                $key = substr($key, 1);
             }
             if (substr($key, 0, 1) == '#')
             {
                $key = substr($key, 1);
              $code .= "if ( \$a['$key'] > \$b['$key']) return $d * 1;\n";
$code .= "if ( \$a['$key'] < \$b['$key']) return $d * -1;\n";
             }
             else
             {
              $code .= "if ( (\$c = strcasecmp(\$a['$key'],\$b['$key'])) != 0 ) return $d * \$c;\n";
           }
       }
       $code .= 'return $c;';
       $sort_func = $sort_funcs[$sortby] = create_function('$a, $b', $code);
   }
   else
   {
       $sort_func = $sort_funcs[$sortby];
   }   
   uasort($data, $sort_func);   
}

#
# Modifier: sortby - allows arrays of named arrays to be sorted by a given field
#

function smarty_modifier_sortby($arrData, $sortfields) {
  array_sort_by_fields($arrData, $sortfields);
  return $arrData;
} 

 

This is not working for me. Do you need a closing ?> in the script above? I am trying to sort domains by expiry date in clientsummary.tpl:

 

{foreach key=num from=$domainsummary|@sortby:expirydate item=domain}

 

All this does is break the page though

Link to comment
Share on other sites

This is not working for me. Do you need a closing ?> in the script above?

technically, it probably should - but works without it. :idea:

 

as I said in the other thread you posted in (http://forum.whmcs.com/showthread.php?101339-Any-way-to-sort-Products-in-summary-page), you'll probably need to change the location of where you've uploaded the Smarty plugin.

 

/vendor/smarty/smarty/libs/plugins/

 

I am trying to sort domains by expiry date in clientsummary.tpl:

 

{foreach key=num from=$domainsummary|@sortby:expirydate item=domain}

 

All this does is break the page though

probably because it can't find the plugin you're wanting to use - upload it to the above path and you should be good to go.

 

however, bear in mind what I said in the above thread...

 

because it's sorting by string or numerically, it doesn't work well with dates (unless they're in a YYYY/MM/DD format) or currencies - but if you only want to sort by text fields and/or numbers, it should work fine.

 

so you could use the following to sort by expiry date in reverse order, and then domain name in ascending...

 

{foreach key=num item=domain from=$domainsummary|@sortby:"-#expirydate,domain"}

if you use YYYY/MM/DD, this will sort correctly, any other date format and it won't - it just sees them as strings, so as I said previously, it's not great with dates.

Link to comment
Share on other sites

Thanks, but as noted in other thread I am still v5 (waiting for a little dust to settle on version changes). I was actually hoping to use expiry date, but tried sorting on domain name ("domain") to test it. I got the same result whether it is text or date so far.

 

Here is the error I see when using debug mode:

 

Fatal error: Call to undefined function smarty_modifier_sortby() in /home/xxxxxx/templates_c/%%28^287^28710B50%%clientssummary.tpl.php on line 638

 

Does that help shed light on it?

Link to comment
Share on other sites

Ding! I moved the plugin from /public_html/billing/includes/smarty/plugins to /public_html/xxxxx/includes/classes/Smarty/plugins and gee whiz, it works now! (someone mentioned the other location in the thread, and I guess I picked the wrong one).

 

But as you said, it doesn't work well on dates - it only sorts on month - the first digits in MM/DD/YYYY format. So I switched to YYYY/MM/DD format, and now have it working as expected with:

 

{foreach key=num item=domain from=$domainsummary|@sortby:"status,#expirydate,domain"}

 

This now sorts Active domains ahead of Cancelled, then soonest expiration date, then name. Very cool, much thanks to you brian!

 

I'm not sure if the date format change is going to negatively affect other things though... US clients typically expect to see MM/DD/YYYY

Edited by BuffaloWeb
Link to comment
Share on other sites

  • 2 years later...

Hello,

the solution posted by @brian! in the second post of this topic (the |@sortby ) is reporting an error in WHMCS 6.3.2 at https://mydomain.com/admin/clientslog.php?userid=1234 i see:

Quote

Smarty Error: Syntax error in template "/home/myusername/public_html/templates/default/upgrade.tpl" on line 25 "{foreach key=num item=upgradepackage from=$upgradepackages|@sortby:"gid,pid"}" unknown modifier "sortby"

I found this post (by brian!) and created that file modifier.sortby.php with suggested content. Is this what is needed to make it find @sortby modifier?

Edited by postcd
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