Jump to content

How do I make smarty data load on kbcat page? It loads on kbarticle page just fine...


cdeese8

Recommended Posts

How do I load data on the knowledgebasecat.tpl page?

When I add {debug} to the file I can't see the specific "votes" and "useful" data being loaded. But if I add {debug} to the knowledgebasearticle.tpl I can see them being loaded. How do I get that specific smarty data / variable to pass on the cat page?

{$kbarticle.votes}
{$kbarticle.useful}

I noticed the knowledgebasecat.tpl loads the "views" .. I just wish it also loaded the votes and useful smarty data too. Any ideas? Anything you can offer would be most helpful.

Thanks,

Link to comment
Share on other sites

Submit a feature request and while you're waiting use a hook and template changes to do what you need. 

<?php
use WHMCS\Database\Capsule;

add_hook('ClientAreaPageKnowledgebase', 1, function($vars)
{
	$x=0;
 	foreach($vars['kbarticles'] as $article)
	{
		$dbarticle = Capsule::table('tblknowledgebase')->where('id', '=', $article['id'])->first(); // get article from database since we aren't given votes / useful here.
       	$vars['kbarticles'][$x]['useful'] = $dbarticle->useful; // add to the array
		$x++;
	}
return $vars; 

});

Then in the template just add:

{kbarticle.useful}

where you want within the article loop.

Link to comment
Share on other sites

Thank you very much @steven99 - at first, i tried to create two hook files (and dupe the code and replace the keyword) but that didn't work, so i just duped what you shared, merged it into a single hook file and it magically works. Brilliant! If you have any advise or wisdom, maybe this isn't written properly... I'm all ears. Thanks again!!

Now, on the knowledgebasecat.tpl page, I can use both {$kbarticle.votes} and {$kbarticle.useful} to get the extracted data I desired.

<?php

// written by steven99
// https://whmcs.community/topic/293372-how-do-i-make-smarty-data-load-on-kbcat-page-it-loads-on-kbarticle-page-just-fine/?do=findComment&comment=1311909

use WHMCS\Database\Capsule;

add_hook('ClientAreaPageKnowledgebase', 1, function($vars)
{
	$x=0;
 	foreach($vars['kbarticles'] as $article)
	{
		$dbarticle = Capsule::table('tblknowledgebase')->where('id', '=', $article['id'])->first(); // get article from database since we aren't given useful here.
       	$vars['kbarticles'][$x]['useful'] = $dbarticle->useful; // add to the array
		$x++;
	}

	$x=0;
 	foreach($vars['kbarticles'] as $article)
	{
		$dbarticle = Capsule::table('tblknowledgebase')->where('id', '=', $article['id'])->first(); // get article from database since we aren't given votes here.
       	$vars['kbarticles'][$x]['votes'] = $dbarticle->votes; // add to the array
		$x++;
	}
return $vars; 

});

 

Edited by cdeese8
updated code
Link to comment
Share on other sites

i'd suggest writing the hook as below - it's a little redundant to use two separate database queries and foreach loops, so i've merged them into just one...

<?php

# Add Votes & Useful Values To kbarticles Array Hook
# Written by brian! 

use Illuminate\Database\Capsule\Manager as Capsule;

function kbarticles_add_votes_and_useful_fields_hook($vars) {

	if ($vars['templatefile'] == 'knowledgebasecat') {
		$kbarticles = $vars['kbarticles'];
		foreach($kbarticles as $key => $article) {
			$results = Capsule::table('tblknowledgebase')->where('id',$article['id'])->select('votes','useful')->first();
			$kbarticles[$key]['votes'] = $results->votes;
			$kbarticles[$key]['useful'] = $results->useful;
		}	
		return array("kbarticles" => $kbarticles);
	}
}
add_hook("ClientAreaPageKnowledgebase", 1, "kbarticles_add_votes_and_useful_fields_hook");
?>

this will still give you the $kbarticle.votes} and {$kbarticle.useful} values that you can output in your template. thanks.png

Link to comment
Share on other sites

Excellent optimization and thanks for the clean hook. 😋 You know, it was nice to see Steven drop in here but when I see Brian... it's ALWAYS a treat!

Thanks dude. It works perfectly! 

If you are still reading this, and happen to know the answer, what is the difference in the hook between the two snippets?

  • use Illuminate\Database\Capsule\Manager as Capsule;
  • use WHMCS\Database\Capsule;

Is one telling Smarty to dig into the database and do a global search and the other is specifically telling Smarty to do something else? Are the two hooks a combination of PHP, Smarty and SQL language? I've never seen things like "$x=0;" and "$x++;".

Thanks again!!

😎

Link to comment
Share on other sites

19 hours ago, cdeese8 said:

If you are still reading this

I read everything (I think!)...

19 hours ago, cdeese8 said:

what is the difference in the hook between the two snippets? 

  • use Illuminate\Database\Capsule\Manager as Capsule;
  • use WHMCS\Database\Capsule;

Is one telling Smarty to dig into the database and do a global search and the other is specifically telling Smarty to do something else? Are the two hooks a combination of PHP, Smarty and SQL language?

they're just telling the hook to get ready, we're about to query/update the database... mine is probably from how the example hooks were posted when v6 came out; steven's is how they're posted now... they're both doing the same thing and at this stage, I wouldn't worry about it too much - when i'm writing a new hook, i'm using one of a number of hook templates and it will likely contain that line that i've always used... and I don't know if that new method would only work on v7.... so when I get the chance, i'll have a play and see, but don't worry about it for now - as long as the hook can find Capsule, then the queries will work.

19 hours ago, cdeese8 said:

I've never seen things like "$x=0;" and "$x++;".

they're fine and are just incremental counter, e.g., $x++ just adds 1 to the value of $x... steven99 was using a loop with a value but no key; whereas in mine, i'm specifying to use both the value and the key, and subsequently don't need to use a separate counter variable.

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