PDA

View Full Version : "If is first post" within a plugin PHP code


Infoman4ever
12-20-2011, 01:59 PM
Hello folks,
I got a question, I want know what's the condition that says whether it's the first post of a given thread or not within a "plugin" (which is linked to "showthread_postbit_create" hook), it's the equivalent of :
<vb:if condition="$post['isfirstshown']">something</vb:if>
of the template side, right?
Thanks in advance.

kh99
12-20-2011, 02:24 PM
That hook is called before the isfirstshown is set, but you can use this:

if ($counter == 1 AND $fetchtype == 'post' AND $post['visible'] == 1)
{
}


Also, that hook is called froom some scripts other than showthread (newreply.php, editpost.php, comments.php). The above might work because $fetchtype is probably not set in the other scripts, but you could add THIS_SCRIPT == 'showthread' to be safe.

Infoman4ever
12-20-2011, 02:39 PM
Nice, it works except something, I have something to add just to the first post, but -and you're right about the hook- now I'm getting it for each post of the thread, what's the point kh99?
And where to put THIS_SCRIPT == 'showthread'?

kh99
12-20-2011, 02:44 PM
if (THIS_SCRIPT == 'showthread' AND $counter == 1 AND $fetchtype == 'post' AND $post['visible'] == 1)
{
}

Infoman4ever
12-20-2011, 02:58 PM
Thank you dear but unluckily it still the same problem, it shows the thing in every post, very annoyed. I think it's logical, as long as we're on the same thread, "showthread_postbit_create" should be called in all posts of the same thread, or what?

kh99
12-20-2011, 03:03 PM
Thank you dear but unluckily it still the same problem, it shows the thing in every post, very annoyed. I think it's logical, as long as we're on the same thread, "showthread_postbit_create" should be called in all posts of the same thread, or what?

Yes but $counter should only be 1 for the first post. That if condition I posted above is exactly the same as is used to set $post['isfirstshown'].

Infoman4ever
12-20-2011, 03:08 PM
Wow, I printed the $counter and I found it's the same (is 1) on every post, it's weird, does this make sense to you?

kh99
12-20-2011, 03:10 PM
No, it doesn't. You said you're using hook location showthread_postbit_create, right? You could post your plugin code if you want us to check it out.

Edit: Actually it might make some sense - are you using threaded or hybrid display mode?

Infoman4ever
12-20-2011, 03:15 PM
Absolutely I could:

<?php
if($vbulletin->options['data_onoff']){

if(THIS_SCRIPT == 'showthread' AND $counter == 1 AND $fetchtype == 'post' AND $post['visible'] == 1)
{
$result = $db->query("SELECT data FROM thread WHERE threadid = ".$threadid);
$fetch = $db->fetch_array($result);
$find = '<span class="date">';
$replace = '<span class="date"> - '.$fetch['data'] ;
$vbulletin->templatecache['postbit_legacy'] = str_replace($find, $replace, $vbulletin->templatecache['postbit_legacy']);
}
}
?>
That's it and the hook is double checked: showthread_postbit_create.
Honestly I don't know what the display mode is :)

kh99
12-20-2011, 03:21 PM
Oh...well the reason that it's showing up in every post is because you're changing the cached template, which is used for every post. And if you inserted the value of $counter in the template, it would of course always show a value of 1.

What you could do is, instead of modifying the cached template for every post, change it once and insert some php code that checks $post['isfirstshown']. That will get even trickier because you need to make the replacement string so that the resulting cached template is valid php code that build the string you want. (I hope that makes sense).

Infoman4ever
12-20-2011, 03:29 PM
Well, I'm not sure I've totally understand, but, you know, I gotta try.
Edit: I must add if($post['isfirstshown']) to the cached template so It does not affect the functionality of the template, am I right? if so, It's gonna be a little bit difficult :). what about changing the hook? so far, it's the only one that did the purpose.

kh99
12-20-2011, 03:43 PM
Right. If I knew exactly how to tell you to do it I would, but I think it would take me a while to work it out. If you've seen the thread started by Scanu (https://vborg.vbsupport.ru/showthread.php?t=275691), he's having the same problem getting all the different levels of quotes to work out.

Infoman4ever
12-20-2011, 04:09 PM
Well, I've already experienced the problem of the quotes, I'll do my best getting it works, thanks again Mr kh99.

--------------- Added 1324417995 at 1324417995 ---------------

Wow, unbelievable, it worked :).
I've never imagined I can experience something complicated like that, anyway, take a look at the code (however it is :) ) because I have a little issue:

$find = '<span class="date">';
$replace = '<span class="date">\';if($post[\'isfirstshown\']) { $final_rendered .=\'hh\'.$fetch[\'data\'].\'gg\'; } $final_rendered .=\'';

Now it shows "hhgg" just in the first post, but not $fetch[\'data\'], what the problem?
I'm glad that the big problem is solved.

kh99
12-20-2011, 09:07 PM
I think you probably need the $fetch['data'] to be evaluated when you create the string, like maybe:

Edit: oops, that string probably needs quotes around it, so maybe
$replace = '<span class="date">\';if($post[\'isfirstshown\']) { $final_rendered .=\'' . $fetch['data'] . '\'; } $final_rendered .=\'';


ETA: I see you figured that out...

Infoman4ever
12-20-2011, 09:16 PM
Congratulations, it works perfectly, just added '/ surrounding the $fetch['data'] and here we go, the last code is:
$replace = '<span class="date">\';if($post[\'isfirstshown\']) { $final_rendered .=\'' . $fetch['data'] . '\'; } $final_rendered .=\'';
Thank you from heart Mr kh99, you don't know how much did you do help and save my time.
Regards.