Declaring global variables and using in template
#1
Okay so I'm currently finishing up a pretty shabby plugin... The last is to create a displayable text for the postbit based on the users leechstatus.
The reason why I'm not doing template conditionals is because we' have pretty much of that already, as such I'm in need of some help.

I have in db a row named leechstatus, I'm effectively able to grasp and use that in template by doing {$post['leechstatus']}

global $db, $mybb, $post, $leechamount, $leechtext;


And although the above is declared first in my function and this is the hook:

$plugins->add_hook("postbit", "advancedleech_post");

I'm unable to use either leechamount or leechtext variables in template, I've tried the following:

<?php echo $leechamount; echo $leechtext; echo $post['leechtext']; echo $post['leechamount']; ?>

and the corresponding {$leechtext} {$leechamount} {$post['leechamount']} {$post['leechamount']}

I'm not sure what I'm doing wrong, I'm making sure it's initialized, but I'm not adding it to an array of data or anything like that, is there something I'm missing?


function advancedleech_post(&$post)
{
	global $db, $mybb, $post, $leechamount, $leechtext;
	
	require_once "inc/functions_post.php";
	
	$query = $db->query("SELECT leechstatus FROM ".TABLE_PREFIX."users WHERE uid ='".$post['uid']."'");
	
	$leechamount = '0';
	
	while($result = $db->fetch_array($query))
	{
		$leechamount = $result['leechstatus'];
	}
	
	$highbounds = $mybb->settings['high_bounds'];
	$lowbounds = $mybb->settings['low_bounds'];
	$verylowbounds = $mybb->settings['verylow_bounds'];
	
	$leechtexticon = $mybb->settings['leechtext_icon'];
	
	if ((int)$leechamount >= (int)$highbounds) {
		// GREAT leechstatus
		$leechtext = '<div class="post-row-s">
		<div class="post-row">
		<div class="post-icon">'.$leechtexticon.'</div>
		<div class="post-row-inner">Leech:<span class="ml-auto" color="green">GREAT</span></div>
		</div>
		</div>';
	} elseif ((int)$leechamount > (int)$lowbounds) {
		// GOOD leechstatus
		$leechtext = '<div class="post-row-s">
		<div class="post-row">
		<div class="post-icon">'.$leechtexticon.'</div>
		<div class="post-row-inner">Leech:<span class="ml-auto" color="green">GOOD</span></div>
		</div>
		</div>';
	} elseif ((int)$leechamount < (int)$lowbounds) {
		// BAD status
		$leechtext = '<div class="post-row-s">
		<div class="post-row">
		<div class="post-icon">'.$leechtexticon.'</div>
		<div class="post-row-inner">Leech:<span class="ml-auto" color="orange">BAD</span></div>
		</div>
		</div>';
	} elseif ((int)$leechamount < (int)$verylowbounds) {
		// LEECHER status
		$leechtext = '<div class="post-row-s">
		<div class="post-row">
		<div class="post-icon">'.$leechtexticon.'</div>
		<div class="post-row-inner">Leech:<span class="ml-auto" color="red">LEECHER</span></div>
		</div>
		</div>';
	}
}

Also another reason why I want to do it like this is because I'm very eager to learn how to implement and develop plugins, but as this is my first actual plugin it feels a bit hard to comprehend for me :3

It should also be said the reason I'm not catching on is because of the fact that I've succesfully edited a smaller plugin where declaring a variable as global was enough to make me use it on index_start hook. But now I'm just failing at this xD
Reply
#2
The method you're doing would work if you modified a postbit variable by appending your results to the end of it, i.e. $post['user_details'] .= $leechtext. That will sort of inject your results into the postbit.

If your intentions are to make your own post variables, try something like this:
  • Get rid of $leechamount, $leechtext from the global declaration.
  • Use simple_select, i.e.: $query = $db->simple_select('users', 'leechstatus', 'WHERE uid = '.$post['uid']);.
  • Then do $post['leechamount'] = $result['leechstatus'];.
  • Change $leechtext references to $post['leechtext'].
  • Then return $post; at the end of your function.

I believe you should then be able to reference {$post['leechamount']} and {$post['leechtext']} in postbit.

EDIT: If you're able to use {$post['leechstatus']} already then you should be able to perform comparisons on $post['leechstatus'] in your plugin without the extra queries. Try if($post['leechstatus'] >= $mybb->settings['high_bounds']), etc...
Reply
#3
(2019-08-12, 02:33 AM)Settyness Wrote: The method you're doing would work if you modified a postbit variable by appending your results to the end of it, i.e. $post['user_details'] .= $leechtext. That will sort of inject your results into the postbit.

If your intentions are to make your own post variables, try something like this:
  • Get rid of $leechamount, $leechtext from the global declaration.
  • Use simple_select, i.e.: $query = $db->simple_select('users', 'leechstatus', 'WHERE uid = '.$post['uid']);.
  • Then do $post['leechamount'] = $result['leechstatus'];.
  • Change $leechtext references to $post['leechtext'].
  • Then return $post; at the end of your function.

I believe you should then be able to reference {$post['leechamount']} and {$post['leechtext']} in postbit.

EDIT: If you're able to use {$post['leechstatus']} already then you should be able to perform comparisons on $post['leechstatus'] in your plugin without the extra queries. Try if($post['leechstatus'] >= $mybb->settings['high_bounds']), etc...

You're a legend, thanks a lot for the help so far, but I'm having problems with returning the $post.
Once I add the return $post at the end of my function postbit becomes 0 and nothing is displayed. 


- Edit:
I fixed it, obviously since I declared global variable $post, it also means that what I returned was not the original reference that I got from the postbit hook, it was in actual fact the global variable, now that I got everything working because of you I can also finally make a bit more sense of the mybb plugin system.

Youv'e helped me a lot, you deserve a thumbs-up <3
Reply
#4
Ah yeah, overlooked the global $post. I'm a bit rusty myself. Glad I could be helpful.

Since the content in $leechtext is virtually identical, save for the leech status itself and its colors, you could make a new postbit_leechstatus template, and switch the status with a $lang variable from a leechstatus.lang.php file.

inc/languages/english/leechstatus.lang.php
$l['leech'] = 'Leech:';
$l['great'] = 'GREAT';
$l['good'] = 'GOOD';
$l['bad'] = 'BAD';
$l['leecher'] = 'LEECHER';

I would iterate through this list in the opposite direction you posted. The way you had it made it so no one would ever have "LEECHER" status, since the conditions happen in order, and if they are lower than verylow_bounds, then they are definitely below low_bounds, so the "BAD" status would trigger first. Also watch your value comparisons, some "equal to" conditions went undhandled. Hopefully this is a better order:

inc/plugins/leechstatus.php
if($post['leechstatus'] <= $mybb->settings['verylow_bounds'])
{
 $post['leechcolor'] = 'red';
 $post['leechtext'] = $lang->leecher;
}
elseif($post['leechstatus'] <= $mybb->settings['low_bounds'])
{
 $post['leechcolor'] = 'orange';
 $post['leechtext'] = $lang->bad;
}
elseif($post['leechstatus'] < $mybb->settings['high_bounds'])
{
 $post['leechcolor'] = 'green';
 $post['leechtext'] = $lang->good;

}
elseif($post['leechstatus'] >= $mybb->settings['high_bounds'])
{
 $post['leechcolor'] = 'green';
 $post['leechtext'] = $lang->great;
}

$post['leechstatus'] = '<span class="ml-auto" color="'.$post['leechcolor'].'">'.$post['leechtext'].'</span>';

postbit_leechstatus
<div class="post-row-s">
 <div class="post-row">
 <div class="post-icon">'.{$mybb->settings['leechtext_icon']}.'</div>
 <div class="post-row-inner">
 {$lang->leech} {$post['leechstatus']}
 </div>
 </div>
</div>

I'm not sure what your experience is, but it looks like you're familiar with the documentation. Eval'ing templates would be the next step. It helps to dissect other plugins as well. I have a really simple RPG stats plugin that does a lot of the same stuff. I tried to lay it out easily to make it easy to dissect for others, so look over it if you'd like.

Good luck.
Reply
#5
(2019-08-12, 10:00 AM)Settyness Wrote: Ah yeah, overlooked the global $post. I'm a bit rusty myself. Glad I could be helpful.

Since the content in $leechtext is virtually identical, save for the leech status itself and its colors, you could make a new postbit_leechstatus template, and switch the status with a $lang variable from a leechstatus.lang.php file.

inc/languages/english/leechstatus.lang.php
$l['leech'] = 'Leech:';
$l['great'] = 'GREAT';
$l['good'] = 'GOOD';
$l['bad'] = 'BAD';
$l['leecher'] = 'LEECHER';

I would iterate through this list in the opposite direction you posted. The way you had it made it so no one would ever have "LEECHER" status, since the conditions happen in order, and if they are lower than verylow_bounds, then they are definitely below low_bounds, so the "BAD" status would trigger first. Also watch your value comparisons, some "equal to" conditions went undhandled. Hopefully this is a better order:

inc/plugins/leechstatus.php
if($post['leechstatus'] <= $mybb->settings['verylow_bounds'])
{
 $post['leechcolor'] = 'red';
 $post['leechtext'] = $lang->leecher;
}
elseif($post['leechstatus'] <= $mybb->settings['low_bounds'])
{
 $post['leechcolor'] = 'orange';
 $post['leechtext'] = $lang->bad;
}
elseif($post['leechstatus'] < $mybb->settings['high_bounds'])
{
 $post['leechcolor'] = 'green';
 $post['leechtext'] = $lang->good;

}
elseif($post['leechstatus'] >= $mybb->settings['high_bounds'])
{
 $post['leechcolor'] = 'green';
 $post['leechtext'] = $lang->great;
}

$post['leechstatus'] = '<span class="ml-auto" color="'.$post['leechcolor'].'">'.$post['leechtext'].'</span>';

postbit_leechstatus
<div class="post-row-s">
 <div class="post-row">
 <div class="post-icon">'.{$mybb->settings['leechtext_icon']}.'</div>
 <div class="post-row-inner">
 {$lang->leech} {$post['leechstatus']}
 </div>
 </div>
</div>

I'm not sure what your experience is, but it looks like you're familiar with the documentation. Eval'ing templates would be the next step. It helps to dissect other plugins as well. I have a really simple RPG stats plugin that does a lot of the same stuff. I tried to lay it out easily to make it easy to dissect for others, so look over it if you'd like.

Good luck.

Alright, thanks a lot, this will be my next step for the plugin, I'd like it to be also somewhat sorted when I reach the end.

I do have another question though, I am also using the hook (datahandler_post_insert_post) and I get the reference from the handler as $post, now I want to grab some information from this, I'm already making sure I'm not looking at a draft post which means I should only retrieve this 

https://github.com/mybb/mybb/blob/mybb_1821/inc/datahandlers/post.php#L1113

H
ow would I go around calling the key value pairs for $this->post_insert_data ?

Actually if you have discord .. It's a lot to ask, but add me if you want :3 Xazin#2327
Reply
#6
If I'm not mistaken, $this is a reference to $post, so as long as you're passing $post into your function, you should be able to reference that array with $post. I might have to look into it more, I've never used that hook, and I only glanced (it's late where I am and I'm about ready for bed).

I don't like Discord, but if you have Steam, my username is the same as it is here.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)