PDA

View Full Version : [SOLVED] vbulletin doesn't like ternary operators?


Dr.CustUmz
05-13-2016, 02:52 AM
for simplistic code sake I'm trying to use ternary operators in my products.

Here's what im trying to simplify:

if ($vbulletin->options['drc_embed_vine_smpl']) {
$drc_embed_vine = '<iframe src="https://vine.co/v/$2/embed/simple" width="480" height="480" frameborder="0"></iframe>';
} else {
$drc_embed_vine = '<iframe src="https://vine.co/v/$2/embed/postcard" width="480" height="480" frameborder="0"></iframe>';
}

Now I have tried

$drc_embed_vine = '<iframe src="https://vine.co/v/$2/embed/'.(($vbulletin->options['drc_embed_vine_smpl']) ? "simple" : "postcard").'" width="480" height="480" frameborder="0"></iframe>';

$drc_embed_vine='<iframe src="https://vine.co/v/$2/embed/'.(($vbulletin->options['drc_embed_vine_smpl'])?'simple':'postcard')).' width="480" height="480" frameborder="0">';


$drc_embed_vine='<iframe src="https://vine.co/v/$2/embed/'.(($vbulletin->options[\'drc_embed_vine_smpl\'])?'simple':'postcard')).' width="480" height="480" frameborder="0">';

not one of these worked =/

although
$drc_embed_vine = '<iframe src="https://vine.co/v/$2/embed/'.(($vbulletin->options['drc_embed_vine_smpl']) ? "simple" : "postcard").'" width="480" height="480" frameborder="0"></iframe>';

does use the postcard option but it does whether the option is yes or no

btw
$vbulletin->options['drc_embed_vine_smpl']

is a simple yesno boolean

where am i going wrong =/

Dave
05-13-2016, 12:29 PM
vBulletin does like it, it's just that you probably use the if statement wrong of the ternary operator.

Does $vbulletin->options['drc_embed_vine_smpl'] literally contain a boolean: true/false or does it contain a number 1 (true) and 0 (false)?

If it's really a boolean, then the following should work just fine:
($vbulletin->options['drc_embed_vine_smpl'] === true ? "simple" : "postcard")

If it contains a number:
($vbulletin->options['drc_embed_vine_smpl'] === 1 ? "simple" : "postcard")

Dr.CustUmz
05-13-2016, 03:18 PM
It does return a number, but for some reason it still not working, the option exist. I do toggle it one to the other, but it always shows postcard.

I Tried
($vbulletin->options['drc_embed_vine_smpl'] === 1 ? "simple" : "postcard")

and

($vbulletin->options['drc_embed_vine_smpl'] == 1 ? "simple" : "postcard")

this is the plugin
<hookname>postbit_display_complete</hookname>
<phpcode><![CDATA[
$drc_embed_vine = '<iframe src="https://vine.co/v/$2/embed/'.($vbulletin->options['drc_embed_vine_smpl'] == 1 ? "simple" : "postcard").'" width="480" height="480" frameborder="0"></iframe>';
$this->post['message'] = preg_replace('~(<a href="https?://vine.co)/v/(.*)" target="_blank">(https?://vine.co)/v/(.*)<\/a>~', $drc_embed_vine, $this->post['message']);
]]></phpcode>

and the option
<setting varname="drc_embed_vine_smpl" displayorder="1">
<datatype>boolean</datatype>
<optioncode>yesno</optioncode>
<defaultvalue>1</defaultvalue>
</setting>

Dave
05-13-2016, 03:22 PM
Add the following somewhere before you call the ternary operator so you can see what it contains:
var_dump($vbulletin->options['drc_embed_vine_smpl']);

Dr.CustUmz
05-13-2016, 03:29 PM
i placed it at the beginning of the plugin, this is what it spat out in my showthread.

https://vborg.vbsupport.ru/external/2016/05/21.png

Dr.CustUmz
05-13-2016, 03:40 PM
Adding the product for reference

I changed the option in this version, thinking maybe i had a conflict, but no luck

Dave
05-13-2016, 04:53 PM
drc_embed_vine_smpl is not defined in your import file. I think you're using the wrong variable.

Dr.CustUmz
05-13-2016, 05:41 PM
drc_embed_vine_smpl is not defined in your import file. I think you're using the wrong variable.

i change it all to "drc_ae_vine_styl" in that version attached thinking maybe I had a conflict somewhere.

Lynne
05-13-2016, 07:06 PM
I think you need to globalize $vbulletin in the postbit_display_complate hook location.

Dr.CustUmz
05-13-2016, 07:09 PM
I think you need to globalize $vbulletin in the postbit_display_complate hook location.

ive never had to do this, but have seen it done somewhere before.

this is as simple as adding
global $vbulletin;

correct?

--------------- Added 1463174769 at 1463174769 ---------------

yup, THANK YOU SO MUCH LYNNE!

I had no clue why this wouldn't work, so for future ref. how do I know when I need to globalize a var other than just giving it a shot lol

MarkFL
05-13-2016, 09:19 PM
I discovered once I began publishing products that even though I might not need to globalize variables on my site(s), other folks would have issues because it wasn't available on theirs. I don't know why this is.

So as insurance, I now tend to globalize $vbulletin, $vbphrase, $db whenever I use them in any plugin just to make sure this won't be an issue for anyone who installs my products. :)

Paul M
05-13-2016, 10:23 PM
You dont need to globalize it, and its bad practice to do so.

What you need to do is use the correct variable, which in a lot of core class functions is $this->registry, not $vbulletin.

(e.g. $this->registry->options['xxx'], not $vbulletin->options['xxx']).

Dr.CustUmz
05-14-2016, 12:13 AM
You dont need to globalize it, and its bad practice to do so.

What you need to do is use the correct variable, which in a lot of core class functions is $this->registry, not $vbulletin.

(e.g. $this->registry->options['xxx'], not $vbulletin->options['xxx']).

-_- facepalm, seeing this now I remember, Ive used that before. Thanks Paul =)