Get started with PHP-Fusion

PHP-Fusion v7 BBCodes API

Generally bbcodes parser contains 3 main files:

1) name_bbcode_include.php - in that file we're building our bbcode parser. Simple example BOLD bbcode (dropped licence header):

< ?php
if (!defined("IN_FUSION")) { die("Access Denied"); }
$text = preg_replace('#\[b\](.*?)\[/b\]#si', '< strong>\1< / strong>', $text);
?>


As you can see first line contains direct access security code, and second line main parser procedure. Variable $text is parsed to look around for any [b][/b] tags and replacing it with STRONG HTML tag.

That include is called every time when we're parsing any text with parseubb() function, of course if that bbcode is enabled in BBCodes Admin Panel.

variable name $text is required! You can't use other variable name.

IMPORTANT! non interactive bbcodes names (I mean which has no buttons, but should be parsed, ex. autolink bbcode) MUST begin with ! sign, ex. !autolink_bbcode_include.php, !autolink_bbcode_include_var.php

2) name_bbcode_include_var.php - that file contains common definitions for bbcode. Example file (full set, dropped licence header):

< ?php
if (!defined("IN_FUSION")) { die("Access Denied"); }
if (!function_exists("generate_geshi_langs")) {
function generate_geshi_langs($textarea_name, $inputform_name) {
$generated = "";
$geshi_langs = array("php", "mysql", "html4strict", "javascript", "css", "xml");
for ($i=0;$i $generated .= "< input type='button' value='".$geshi_langs[$i]."' class='button' style='width:100px' onClick=\"addText('".$textarea_name."', '[geshi=".$geshi_langs[$i]."]', '[/geshi]', '".$inputform_name."');return false;\">< br >";
}
return $generated;
}
}

$__BBCODE__[] =
array(
'description' => $locale['bb_geshi_description'],
'value' => "geshi",
'bbcode_start' => "[geshi=".$locale['bb_geshi_lang']."]",
'bbcode_end' => "[/geshi]",
'usage' => "[geshi=".$locale['bb_geshi_lang']."]".$locale['bb_geshi_usage']."[/geshi]",
'onclick' => "return overlay(this, 'bbcode_geshi_".$textarea_name."', 'rightbottom');",
'onmouseover' => "",
'onmouseout' => "",
'html_start' => "< div id='bbcode_geshi_".$textarea_name."' class='tbl1' style='display: none; border:1px solid black; position: absolute; width: auto; height: auto; text-align: center' onClick=\"overlayclose('bbcode_geshi_".$textarea_name."');\">",
'includejscript' => "",
'calljscript' => "",
'phpfunction' => "echo generate_geshi_langs('".$textarea_name."', '".$inputform_name."');",
'html_middle' => "",
'html_end' => "< / div>"
);
?>


This file needs further explanation:
- first line contains direct access security code
- as you can see we can define new functions which can be used by our bbcodes. IMPORTANT: always check that your function doesn't exists because bbcodes can be called more than once on one site entry (use function_exists())
- $__BBCODE__ multi-array:
    + description - main description of bbcode; locale can be hard coded or defined in proper locale file (locales/LANG/bbcodes/NAME.php)
    + value - it's a name of bbcode; must be the same as prefix of _bbcode_include.php, _bbcode_include_var.php and locale filename
    + bbcode_start - starting tag of bbcode; that parameter can be set up as stand alone if you want to parse that bbcode to for ex. direct link to article or page
    + bbcode_end - ending tag of bbcode; if your want to create stand alone bbcode (look above) then leave that value empty
    + usage - simple description of bbcode usage; can be hardcoded or defined in proper locale file
    + onclick - this can be used for example for expandable bbcodes like in above example of GeSHi bbcode; main purpose of that value is to use JS overlay() function introduced in jscript.js v7 (usage of overlay() JS function will be explained later in this article)
    + onmouseover - if you want to do something when mouse is over the bbcode button then...;) JS purposes
    + onmouseout - as above
    + html_start - here you can define starting HTML code for expandable bbcodes; in most cases that will be DIV tag; remember to add overlayclose() JS function to DIV's onclick in other way once clicked expandable bbcode won't close to next page reload :) Above you can see basic scheme for expandable bbcodes DIV tag
    + includejscript - if your bbcode needs some JS functions here you can include some JS file which contain needed JS functions; COLOR bbcode is one of example when JS file is included
    + calljscript - here you can call JS function from included above JS file
    + phpfunction - if you need to call some php functions; SMILEY bbcode is one of example when we're calling phpfunction (display_smileys()); in GeSHi bbcode we're calling our defined function generate_geshi_langs()
    + html_middle - here we're placing our expandable HTML code; as you can see we're just placed that code above by generate_geshi_langs() function; there're main INPUT buttons which contains addText() JS function which adding our bbcodes to TEXTAREA form (usage of addText() and insertText() JS function will be explained later in this article)
    + html_end - in most cases there will be DIV close tag

2.a) little explanation about $__BBCODE_NOT_QUOTABLE__ array...
Let see an example (hide_bbcode_include_var.php; dropped licence header):

< ?php
if (!defined("IN_FUSION")) { die("Access Denied"); }
if (iADMIN) {
$__BBCODE__[] =
array(
"description" => $locale['bb_hide_description'],
"value" => "hide",
"bbcode_start" => "[hide]",
"bbcode_end" => "[/hide]",
"usage" => "[hide]".$locale['bb_hide_usage']."[/hide]"
);
} else {
$__BBCODE_NOT_QUOTABLE__[] = "hide";
}
?>

This featured array allows to strip this bbcode from quoted text in forum. Why? It was reported as a bug when message posted by hide bbcode to site admins weren't visible for users until... they quoted message posted by admin and this message contained hidden message :) That's why. If your bbcode shouldn't be quoted for some reasons, just add new item (which in fact is bbcode "value") to $__BBCODE_NOT_QUOTABLE__ array...

3) locale/LANG/bbcodes/name.php - that file contains common language definitions for bbcodes. It is not standarized but I decided to use below listed basic scheme (example: Allegro bbcode locale):

< ?php
$locale['bb_allegro'] = "Allegro";
$locale['bb_allegro_description'] = "Searches items on Allegro - Polish auctions service";
$locale['bb_allegro_usage'] = "Text to search on Allegro";
?>



- $locale['bb_allegro'] - name of bbcode because that text appears before called bbcode parser (after parsing it look like "Allegro: cabria exclusive" where Allegro is bolded and cabria exclusive is a link to Allegro Auctions Service search engline with "cabria exclusive" as parameter)
- $locale['bb_allegro_description'] - description of Allegro bbcode
- $locale['bb_allegro_usage'] - usage of Allegro bbcode

Of course each bbcode locale file can be more complicated if there's a need. As I wrote above I prefer to use bellow array scheme:

$locale['bb_BBNAME_WHAT'] instead of numbers used in other PHP-Fusion locale files.




Now I will explain usage of overlay() JS function which is widely used in expandable bbcodes. Calling that function is very simple. We're calling that function in name_bbcode_include_var.php file in $__BCODE__ array in onclick section:

< ?php
$__BBCODE__[] =
array (
'onclick' => "return overlay(this, 'bbcode_NAME_".$textarea_name."', 'rightbottom');"
);
?>


One thing which should be changed in our expandable bbcode is NAME. Once opened bbcode should be closed so we're calling overlayclose() JS function in html_start DIV (onclick).

< ?php
$__BBCODE__[] =
array (
'html_start' => "< div id='bbcode_geshi_".$textarea_name."' class='tbl1' style='display: none; border:1px solid black; position: absolute; width: auto; height: auto; text-align: center' onClick=\"overlayclose('bbcode_NAME_".$textarea_name."');\">"
);
?>





addText() and insertText() JS functions in expandable bbcodes. There's no need to explain how to use that JS functions in non-expandable bbcodes becouse they are called automatically by display_bbcodes() function. When building expandable bbcodes then we need to call that functions in INPUT onclick parameter in html_middle:
- closed bbcodes

< input type='button' value='VALUE' class='button' style='width:100px' onClick=\"addText('".$textarea_name."', '[BBCODE]', '[/BBCODE]', '".$inputform_name."');return false;\">


- stand alone bbcodes

< input type='button' value='VALUE' class='button' style='width:100px' onClick=\"insertText('".$textarea_name."', '[BBCODE]', '".$inputform_name."');return false;\">


Only values we're need to change is VALUE and BBCODE and style if there's a need.




And at least explanation of display_bbcodes(), parseubb() and strip_bbcodes() functions.
1) display_bbcodes()

< ?php
include_once INCLUDES."bbcode_include.php";
echo "< form name='some_form' method='POST' action='".FUSION_SELF."'>";
echo "< textarea name='some_textarea'>";
echo display_bbcodes("some_textarea", "some_form", "b|u|i");
echo "< input type='submit' name='post' value='Submit'>";
echo "< / form>";
?>


We're just displayed bbcodes for some_textarea (default value is "message") which is placed in some_form (default value is "inputform") and we want to display only b, u, and i bbcodes (by default all enabled bbcodes = true).

2) parseubb()

< ?php
//query calls
$sometext = $data['some_field'];
$sometext = parseubb($sometext, "b|u|i");
echo $sometext;
?>


We're just parsed $sometext and only b, u and i bbcodes. If other bbcodes are available in $sometext they won't be parsed.

3) strip_bbcodes()

Currently this function is used on forum's post reply script only. It stripes bbcodes which are defined in $__BBCODE_NOT_QUOTABLE__ array (see paragraph 2.a) for further informations)




I hope that aricle helps you to create more brilliant bbcodes PHP-Fusion v7 which will be best CMS ever ;)

Always yours:
Robert Gaudyn aka Wooya
Posted by Wooya on June 25 2008 19:24:02
10 Comments · 72492 Reads · Print
Comments
#1 | gabeszm on Jun 26 2008 at 11:30:46
wow! very niceGrin
#2 | T-N-Z on Jun 26 2008 at 12:22:54
nice Smile
#3 | maxaas on Jul 11 2008 at 17:30:49
i don't understand at all
#4 | guy18iran on Jul 26 2008 at 21:50:14
COOL Grin
#5 | neltek on Aug 28 2008 at 17:40:39
i think this is a great article but starts assuming we know what BB codes are and why we want them...

Is there any documentation showing the new features of V7 (including bbcodes) and starts from the beginning?

I notice there are some options on main settings now to exclude panels - what is this for etc.

v7 looks ace...
just need to do lots of testing before upgrading or I may have some unhappy customers!
#6 | Wooya on Sep 24 2008 at 22:52:20
Documentation is under construction...
#7 | grr on Oct 26 2008 at 17:55:28
Great tutorial, thanks!
#8 | webmasterlar on Mar 17 2009 at 23:56:06
I liked the article thanks.
#9 | kadinca on Mar 18 2009 at 11:16:26
Ok. I'll use this tutorial on my fusion site.
#10 | edis2 on May 27 2009 at 20:56:11
It is very simple but clear, thank Wink
Post Comment
Please Login to Post a Comment.
Ratings
Rating is available to Members only.

Please login or register to vote.

Awesome! Awesome! 100% [2 Votes]
Very Good Very Good 0% [No Votes]
Good Good 0% [No Votes]
Average Average 0% [No Votes]
Poor Poor 0% [No Votes]