Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: dataspiller on February 02, 2012, 05:08:49 AM

Title: Custom BBcode - changing the output of the bbcode
Post by: dataspiller on February 02, 2012, 05:08:49 AM
Good morning...


I have been trying to figure out for the past few hours how to go about adding my own custom BBcode.

I have built my BBcode array following examples listed in Subs.php and the documentation there.


My BBcode is just a simple [showviewers] - nothing else will need to be added to it. The purpose of the BBcode is to enable admins (and eventually other member groups, logic for it is there just have to add the permissions) to see who has viewed the topic.

Where I am stumped is how to output the list generated by a function... I am not getting any errors from my code, though since I'm not sure I did it right I can't say if my function is being used...



Here is my BBcode code:


array(
'tag' => 'showviewers',
'type' => 'closed',
'content' => 'Old replacement',
'validate' => create_function('&$tag, &$data, $disabled', '
global $context, $db_prefix, $topic, $smcFunc;
$tag[\'content\'] .= \'new text\';
$data = \'New\';
if (isAllowedTo(\'topic_viewers\') || ($context[\'user\'][\'is_admin\'])){
$result = $smcFunc[\'db_query\'](\'\',\'
SELECT lt.id_member, lt.id_topic, mem.member_name
FROM {$db_prefix}log_topics AS lt
LEFT JOIN {$db_prefix}members AS mem ON (mem.id_member =
lt.id_member) WHERE id_topic = {int:topic}\',
array ( \'topic\' => $topic)
);
$topicView = \'<span class="smalltext_modified">\';
if ($result) {
while ($row = $smcFunc[\'db_fetch_assoc\']($result)){
$topicView .= $row["member_name"] . \', \';
}

}
$topicView .= \'</span>\';
$tag[\'content\'] .= $topicView . \'Hi\';
}
'),
),




As it stands now, if I use the bbcode [showviewers] I get "Old Replacement" to show up.


Instead, I want to see my list of names. You can see through the code where I tried to just addon text


As I didn't see any examples of BBcode that was the same as mine (simple tag, no user supplied data, custom output) I tried to build off of the examples for the br bbcode and time bbcode.


Any help is greatly appreciated,

Thanks in advance,

Seth
Title: Re: Custom BBcode - changing the output of the bbcode
Post by: Matthew K. on February 02, 2012, 12:33:07 PM
With a quick look, one thing that is incorrect is you are using "isAllowedTo();" when you should be using "allowedTo();".

You can look directly in the SMF Function Database, although to keep it short and sweet, isAllowedTo(); throws a fatal_lang_error(); if false, and allowedTo(); just returns a boolean.
Title: Re: Custom BBcode - changing the output of the bbcode
Post by: dataspiller on February 02, 2012, 01:06:11 PM
Good to know....


I made that change - and then threw in a random "echo" statement in the php code, out side of any if statements - which didn't echo any thing.

Still nothing. No errors any where either.


Seth

Title: Re: Custom BBcode - changing the output of the bbcode
Post by: Chen Zhen on February 02, 2012, 04:15:34 PM

You have added the code but not the button.
For: SMF 2.0x
Navigate to Sources -> Subs-Editor.php

search the file for.. $context['bbc_tags'] .. and you will see where you need to add the code.
Title: Re: Custom BBcode - changing the output of the bbcode
Post by: Matthew K. on February 02, 2012, 04:36:19 PM
The button doesn't stop the BBC from being parsed though...he also never stated that he DIDN'T add a button for it.
Title: Re: Custom BBcode - changing the output of the bbcode
Post by: dataspiller on February 02, 2012, 05:34:48 PM
I haven't added a button - I took a look at the code for the buttons and as far as I can see, that doesn't have any thing to do with bbcode's actually being displayed - its only used when posting.


I did some more playing around with it - changing the 'type' field from closed, to parsed_equals, unparsed_equals - even removed it all together.

Never got the out put I wanted - one time I got the actual BBcode it self - [showviewers], other times nothing...


Seth
Title: Re: Custom BBcode - changing the output of the bbcode
Post by: Chen Zhen on February 02, 2012, 08:37:04 PM
Hmm.. I'm not sure if I'm reading this properly:
Quote
validate: except when type is missing or 'closed', a callback to
           validate the data as $data.  Depending on the tag's type, $data
           may be a string or an array of strings (corresponding to the
           replacement.)
... does not replace the data when using the closed type?

I got it to work using unparsed_content, although I understand you'd prefer just a single closed tag.


array(
'tag' => 'showviewers',
'type' => 'unparsed_content',
'content' => '$1',
'validate' => create_function('&$tag, &$data, $disabled', '
global $context, $topic, $smcFunc;

$tag[\'content\'] .= \' new text \';
$data = \'New\';
if (allowedTo(\'topic_viewers\') || ($context[\'user\'][\'is_admin\']))
{
$result = $smcFunc[\'db_query\'](\'\',"
SELECT lt.id_member, lt.id_topic, mem.member_name
FROM {db_prefix}log_topics AS lt
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member =
lt.id_member) WHERE id_topic = {int:topic}",
array ( \'topic\' => $topic)
);
$topicView = \'<span class="smalltext_modified">\';
if ($result)
{
while ($row = $smcFunc[\'db_fetch_assoc\']($result))
$topicView .= $row["member_name"] . \', \';
}
$topicView .= \'</span>\';
$tag[\'content\'] .= $topicView . \'Hi\';
$smcFunc["db_free_result"]($result);

}
'),
'block_level' => false,

),


I assume you just put some random text to see the behavior.
$data is displayed first followed by your edited content.
Quote
[showviewers][/showviewers]
.. any content between the tags is omitted because you used $data (without it - it will display).

Just out of curiosity:
Whether you use a closed tag or not, one must type in the tag to use it, correct? Why not create a button that is only displayed under condition they have permission to use it? 

You can maybe look into changing it to allow validate for a closed tag (perhaps on condition of using this tag only).
Title: Re: Custom BBcode - changing the output of the bbcode
Post by: dataspiller on February 02, 2012, 11:50:03 PM
Cool - now it is working almost perfectly... just got to get the formatting working.


I will eventually add a button - It is setup for typing now because this started as a spur of the moment project at 1am in the morning and I went for the hardest task first - and my forum only has 2 people who will be using it initially.

I went with the closed tag because I was trying to follow the example for the br bbcode. In the end doesn't really matter - especially since as I thought about this I'll most likely add options to the tag to enable overriding of permissions so that an admin can flag the out put to be displayed to every one.


Thanks for the help - I'm considering this solved.


Seth