displaying most commented post on archive page

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Post Reply
peggylon
Regular
Posts: 51
Joined: Tue Jul 01, 2008 6:32 pm
Location: Berlin
Contact:

displaying most commented post on archive page

Post by peggylon »

Hi,

I'm about to touch up my archive page. At the top I would like to insert the most commented entry of the month and below every other entry of the particular month (as usual).
I realized, that there was a plugin for popular entries, but not as event plugin.

Therefore I'm working on a solution and started with a modifier, recognizing whether one is on an archive page (which works just fine). On archive pages the entry with most comments is recognized and some entry parameters saved in variable $mostcommented (also working).

What I can't figure out whether there is a way of using $mostcommented before it is actually assigned. scope (parent | root | global) didn't make a difference.

in entries.tpl:

Code: Select all

{assign var="entry_type" value=$smarty.server.REQUEST_URI|check_entryType}
{if $entry_type eq "archiv"}   
   		<h2 class="serendipity_date">am meisten kommentiert</h2>
   		Test: {$mostcommented.title}
{/if}
{include file="entries_full.tpl"}
and in entries_full.tpl (part of the original entries.tpl):

Code: Select all

{assign var="mycommentcount" value=0}
{foreach from=$entries item="dategroup"}
	{foreach from=$dategroup.entries item="entry"}
		{assign var="entry" value=$entry scope="root"}
		{if $entry_type eq "archiv" && $entry.comments > $mycommentcount}
			{assign var="mycommentcount" value=$entry.comments}
	 		{assign var="mostcommented" value=['comments'=>$entry.comments,'timestamp'=>$dategroup,'title'=>$entry.title] scope="parent"}
		{/if}
...
 {/foreach} 
 {/foreach}
I wouldnt want to iterate over $entries twice. Could {capture} possibly be of any help?
Thanks for your time!
----------
peggylon aka multikulinaria http://www.multikulinarisch.es
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: displaying most commented post on archive page

Post by Timbalu »

I don't know if I really fully understood what you want to have, but couldn't this help?

Code: Select all

{include 'entries_full.tpl' scope="parent"}
http://www.smarty.net/docs/en/language. ... nclude.tpl

Apart from different scopes, to access something that hasn't been assigned yet by time, is not possible (I think).
Regards,
Ian

Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: displaying most commented post on archive page

Post by garvinhicking »

Hi!

Great to hear how you're getting into this. However I believe your route could provide more challenging than an alternate way.

If I were to do what you want, I would better create a custom smarty function or a modifier to do what you want in raw PHP and additional templates.

I would do that through the config.inc.php of your template (however you could also do it as a complete event plugin...). Inside the file, you'd use something like:

Code: Select all

function serendipity_mostcomments($params, &$smarty) {
  global $serendipity;

  // Query the database to fetch the entry ID with the highest comment count
  $entries = serendipity_db_query("SELECT id FROM {$serendipity['dbPrefix']}entries ORDER BY comments DESC LIMIT 1");

  // Store the ID that we fetched
  $entryid = $entries[0]['id'];

  // Now let's pull the whole entry!
  $entry = serendipity_fetchEntry('id', $entryid);

  // Parse this array as if it were a normal blog entry!
  serendipity_printEntries($entry, false, false, 'ENTRIES', false);
  
  // Capture the resulting smarty output and return it
  // Hint: You could use your own duplicate entries.tpl if you want to apply
  // special markup to the formatted entry!
  return serendipity_smarty_fetch('mostcommentblock', 'entries.tpl');
}
The code is untested, but this should lead you right on track I hope? :)

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
peggylon
Regular
Posts: 51
Joined: Tue Jul 01, 2008 6:32 pm
Location: Berlin
Contact:

Re: displaying most commented post on archive page

Post by peggylon »

Hi Ian and Garvin,

thanks to you both!
Before I read Garvin's answer I have done some more testing of that first approach.
The assigned variable $mostcommented is only read-able from index.tpl (even ahead of $content), no matter whether scopes were added to the template call or not. This is certainly not the best way to go.

I was primarily thinking of custom php calls to retrieve needed information from the database. I just didn't know, how to go about this. Modifier didn't seem applicable, since I had no idea what variable to call with attached modifier. So I'm very thankful for the advise and will go with the smarty function approach.

One question remains though. How could such a smarty-function be called from within the template?
Last edited by peggylon on Thu Oct 31, 2013 11:08 am, edited 1 time in total.
----------
peggylon aka multikulinaria http://www.multikulinarisch.es
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: displaying most commented post on archive page

Post by garvinhicking »

Hi!

Whoops, one ting I forgot, in the config.inc.php you also need to register the smarty function:

Code: Select all

$serendipity['smarty']->register_function('serendipity_mostcomments', 'serendipity_mostcomments');
and then in your .tpl file you can use:

Code: Select all

{serendipity_mostcomments}
at the place where you want that entry to appear. There you could also wrap {if} calls around to only show it when specific {$view} variables match...

(About the scoping, I'm afraid I never really dug into this for Smarty3 yet. So there Timbalu is surely the best guy to talk to :D)

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
peggylon
Regular
Posts: 51
Joined: Tue Jul 01, 2008 6:32 pm
Location: Berlin
Contact:

Re: displaying most commented post on archive page

Post by peggylon »

Code: Select all

$serendipity['smarty']->register_function('serendipity_mostcomments', 'serendipity_mostcomments');
Thanks Garvin!
Someone mentioned to me a little while ago, that registering syntax has changed from
$serendipity['smarty']->register_modifier('name', 'name'); to
$serendipity['smarty']->registerPlugin('modifier', 'name', 'name');

Both ways work, but does that also apply for registring functions? If so, I might go with

Code: Select all

$serendipity['smarty']->registerPlugin('function', 'serendipity_mostcomments', 'serendipity_mostcomments');
----------
peggylon aka multikulinaria http://www.multikulinarisch.es
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: displaying most commented post on archive page

Post by garvinhicking »

Hi!

s9y provides a API layer that internally rewrites the "old way" (register-function) to the "new way". This will most likely stay some time, but if you want to be extra "politically correct", you should use the registerPlugin way, yes!

(I simply worked all the time with register_function, so this comes naturally to me, the other variant I would have needed to lookup first ;)).

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: displaying most commented post on archive page

Post by Timbalu »

peggylon wrote:Both ways work, but does that also apply for registring functions? If so, I might go with

Code: Select all

$serendipity['smarty']->registerPlugin('function', 'serendipity_mostcomments', 'serendipity_mostcomments');
Brave! :)
Its good not to use and learn old syntax any more, while sooner or later this will lead into trouble. Such an old horse like Garvin will take its own time ... :P

I would even go further and say

Code: Select all

$serendipity['smarty']->registerPlugin('function', 'serendipity_mostcomments', 'serendipity_smarty_mostcomments');
and rename the function name to function serendipity_smarty_mostcomments($params, &$smarty) {
Regards,
Ian

Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian
peggylon
Regular
Posts: 51
Joined: Tue Jul 01, 2008 6:32 pm
Location: Berlin
Contact:

Re: displaying most commented post on archive page

Post by peggylon »

Ah okay, thanks for the explanation.

I tested the smarty function. Unfortunately I'm not familiar with serendipity functions such as serendipity_printEntries and serendipity_smarty_fetch.
When I implement them as suggested, I get a blank page. Therefore I thought perhaps it would have to be:

serendipity_printEntries($entry, false, false, 'mostcommentblock', false); and
return serendipity_smarty_fetch('mostcommentblock', 'entries.tpl');

Just guessing, really. The page loads without error but also without the desired output.

Is there a way to debug smarty functions called via config.inc.php?
----------
peggylon aka multikulinaria http://www.multikulinarisch.es
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: displaying most commented post on archive page

Post by garvinhicking »

Hi!

Hm, this might be a problem of either scoping or internal "recusion". Can you try to place the code inside index.tpl instead of entries.tpl, just to see if that is the issue here?

You could also change:

Code: Select all

return serendipity_smarty_fetch('mostcommentblock', 'entries.tpl');
to:

Code: Select all

return '<!-- MOSTCOMMENTS -->' . serendipity_smarty_fetch('mostcommentblock', 'entries.tpl');
and see if that actually puts this HTML comment inside your blog output, or if maybe the function itself is not getting called properly.

Part of the code I took from include/functions_smarty.inc.php, from the serendipity_smarty_fetchPrintEntries() function. The function definities of serendipity_fetchEntry and serendipity_printEntries can be found in include/functions_entries.inc.php. Those place would allow further debugging to see what gets fetched/compiled.

Maybe that helps - if not, please post back here next week then I'll setup a testblog to check and help for further debugging!

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
peggylon
Regular
Posts: 51
Joined: Tue Jul 01, 2008 6:32 pm
Location: Berlin
Contact:

Re: displaying most commented post on archive page

Post by peggylon »

Now I'm in trouble.

First of all, I might have been wrong with my previous statement. I do get output in entries.tpl when running config.inc.php as suggested by you.

<!-- ENTRIES START -->
<h2 class="serendipity_date">am meisten kommentiert</h2> <!-- ENTRIES START -->
<h2 class="serendipity_date">am meisten kommentiert</h2> <!-- ENTRIES START -->
<h2 class="serendipity_date">am meisten kommentiert</h2> <!-- ENTRIES START -->
...

859 times (that's about as often as number entries)

If I implement {serendipity_mostcomments} in index.tpl instead I get the normal page with all entries listed plus

Code: Select all

<div class="serendipity_overview_noentries">Keine Einträge vorhanden</div>
I tried debugging in include/function_entries.inc.php by adding a line

Code: Select all

echo "$key - $val";
Instead since then I get a mysql error message saying

Code: Select all

<div class="blog_right" valign="top"><ul class="plainList">
SELECT count(DISTINCT e.id) AS orderkey FROM xxx_entries AS e WHERE e.timestamp >= 1380578400 AND e.timestamp <= 1383256799 AND isdraft = 'false' AND e.timestamp <= 1383235500    ORDER BY 

/ You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 9
Seems at some point an ORDER BY value is missing. But where? I reset function_entries.inc.php, commented out the smarty function but am still getting the sql error message. To hide it I temporarily set css for .blog_right to 'display:none;'

What happened here and how can I solve it? What confuses me most, is that one could assume the error's got to do with a sidebar plugin (due to the location it occured) and might just be a weird coincidence...

*slightly panicking*
----------
peggylon aka multikulinaria http://www.multikulinarisch.es
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: displaying most commented post on archive page

Post by Timbalu »

Sound like you will have to drop some bad compile files, which do not seem to update any more. If you are really sure you set everything back to normal, look into templates_c/multikulinarisch/. In there will be some short named folders, eg like templates_c/multikulinarisch/2f/c2/2a - for each nesting dirs in there, there is is a compile file at the end like long_number-entries.tpl.php etc., eg
- templates_c/multikulinarisch/2f/c2/2a/9b9018096ae1110287cf494124494d739d08f120.file.entries.tpl.php [todays date]
- templates_c/multikulinarisch/9b/3a/1d/32b740bd20ad4edee709cf5ee89daa19edcc42e6.file.sidebar.tpl.php [todays date]
- templates_c/multikulinarisch/54/3a/91/543a91984d49cadb8c5843f6f808e87ed9b1c12b.file.index.tpl.php [todays date]
etc.
Now just purge the short dir ones right underneath templates_c/multikulinarisch/ from today (only!) or find in them the special files which makes trouble, possibly sidebar.tpl.php or so and only drop that nesting level.
This could help to recompile a new compile file for the ones you have played with today. No need to purge any others.
Regards,
Ian

Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian
peggylon
Regular
Posts: 51
Joined: Tue Jul 01, 2008 6:32 pm
Location: Berlin
Contact:

Re: displaying most commented post on archive page

Post by peggylon »

Thanks for the advice Ian,

I deleted all files (no folders) from today within the nested structure of tempates_c/multikuli/
It didn't help. Besides the mentioned ....file.sidebar.tpl.php from today, which is removed, there is only one more sidebar related file in the tempaltes_c directory.

Since it's outside the multikuli folder, I did not dare laying hands on it, thoug.
It is 'package_sidebar_de.xml' with today's date, but xml files don't create mysql errors, do they?
Otherwise there are several .cache files outside the folder 'multikuli' as well as 'staticpage_pagelist.dat' all of today's date, which I left untouched...
----------
peggylon aka multikulinaria http://www.multikulinarisch.es
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: displaying most commented post on archive page

Post by Timbalu »

Yes outside is not interesting. For compiles on S9y 1.7+ /Smarty 3 Series there is only relevance in the templates_c/multikulti directory.
But if you did not find any file which are relevant to your problem, purging them all in templates_c/multikulti is just a last option at the very end. While they need to be recompiled again, which can take some time, if you have lots. But you could try with purging all from the last days (and certainly including these short dirs!).
So if the error does not go away, you have not set all your testing files back to normal (release) state and possible still have a bad error in them.
Regards,
Ian

Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian
peggylon
Regular
Posts: 51
Joined: Tue Jul 01, 2008 6:32 pm
Location: Berlin
Contact:

Re: displaying most commented post on archive page

Post by peggylon »

I removed files and sub folders of the last 3 days from template_c/multikuli. The error message still displays.
I could track the occurance of the error down to the archives plugin though, which I will temporarily de-activate and look at closer. Strange, since I didn't update or change a thing about that plugin.
Anyway, I don't want to clogg this topic with off-topic problems.

Tanks Ian for your prompt emergency support!
----------
peggylon aka multikulinaria http://www.multikulinarisch.es
Post Reply