Dynamic Articles with TinyButStrong

version 1.02 (2009-08-17)
a content plugin for Joomla 1.5
(help file updated on 2010-11-02)

This plug-in allows to include dynamic content in Joomla articles using the TinyButStrong Template Engine. The possibilities of dynamic content are very rich. For example, it can be listing data from MySQL tables, or it can complete user interface asking and processing data given by the user.
TinyButStrong is very well adapted for CMS because, unlike other Template Engines, it allows to have templates build with visual or WYSIWYG editors. It means that a template can be a Joomla article build with the inline Html editor provide by default (TinyMCE).

Requirement:

Joomla 1.5 or higher.

How it works:

Since the plug-in is installed and activated in the Joomla website, you can use 3 types of plugin tags (Direct MergeBlock, External Script and Internal Script) that you can put anywhere in your articles.
Because adding active content in articles can be powerfull, the plug-in has also several parameters to manage security levels that can be tuned for all articles or for specific articles.

Please note:

This help file assumes that you understand the basic concepts of Template Engines and more specifically the TinyButStrong Template Engine.

Type #1: Direct MergeBlock

Syntax: {tbs}mergeblock=blockname;db=dbname;sql=sqlstatement{/tbs}

This type of plugin tag takes the current article as current template and performs a TBS->MergeBlock() over it.
The result of the merging is displayed instead of the original article.
There is no need to code any PHP to use this type of plugin tag.

blockname: Name of the TBS block to merge. Assuming that such a block exists in the current article.
dbname: Optional. Name of the database where to use the SQL statement. The database must be available with the current Joomla connection. You can specify the keyword %current% for the Joomla database. (keyword %joomla% is also supported for compatibility with version 1.0 of this plugin)
If you want to connect to a database with another account and password, you must use another type of plugin tag because this one cannot do it.
sqlstatement: The SQL statement that should return the data to display.
Since version 1.02 of this plugin: In order to facilitate the usage of operators '<' and '>' in the SQL statement, the plugin automatically replaces strings '-+' and '+-' with those operators. You can prevent this behavior by adding the parameter sql_preserve in the plugin tag.
Example:
{tbs}mergeblock=u;sql=SELECT name FROM t_user WHERE id+-0{/tbs}
is the same as
{tbs}mergeblock=u;sql=SELECT name FROM t_user WHERE id>0;sql_preserve{/tbs}

Example:
Joomla article:
{tbs}mergeblock=u;sql=SELECT name, email FROM jos_users ORDER BY name{/tbs}
Name Email
[u.name;block=tr] [u.email]

Type #2: External script

Syntax: {tbs}script=scriptname.php;db=dbname{/tbs}

This type of plugin tag is the same as the previous one but instead of a simple TBS->MergeBlock(), it run the specified PHP script.
The script should drive the merging assuming that the current article is already loaded as the current template. There is no need to call TBS->Show() because the plugin will do it. Please note that your PHP script is run in the same context as a function and that the local variable $TBS contains the ready to use TBS instance. The local variable $PrmLst contains the parameters of the plugin tag if you need it. You can leave the current script and return to the Joomla process using command return; in your main script (it hasn't the same behavior inside a function of course). They are other coding facilities given somewhere below.

scriptname.php: The PHP script to run. You can specify a path. The default path for all scripts can also be specified in the plug-in parameters.
dbname: Optional. Name of the database to select before the script is run. The database must be available with the current Joomla connection. You can specify the keyword %joomla% for the Joomla database.
If you want to connect to a database with another account and password, then your script must open this connection by itself, and you should specify explicitly the new open connected for any use of MergeBlock().

Example:

Joomla article:
{tbs}script=myappli.php{/tbs}
Name Email
[u.name;block=tr] [u.email]

myappli.php: (external script using the same MySQL connection and database)
<?php
$key = (isset($_GET['key'])) ? intval($_GET['key']) : 0;
$TBS->MergeBlock('u','mysql','SELECT name, email FROM jos_users WHERE id='.$key);
?>

myappli.php: (external script using a different MySQL connection)
<?php
$key = (isset($_GET['key'])) ? intval($_GET['key']) : 0;
$cid2 = mysql_connect('localhost', 'user', 'password');
$TBS->MergeBlock('u','mysql','SELECT name, email FROM association_users WHERE id='.$key);
mysql_close($cid2);
?>

Coding help:

Moreover, the plug-in gives to you other coding facilities:

Three properties are given by the TinyButStrong plug-in to retrieve information about the current Joomla context:

Two functions are given by the TinyButStrong plug-in to work with the Joomla Database Object which is a database connectivity tool. It helps to query and execute SQL statements in the Joomla database and other databases available with the same MySQL connection as Joomla.

Tips and trics using the Joomla framework:

How to get Code Comments
Current Article's id:   $id = $TBS->JoomlaArticle->id;    
Current Article's title:   $title = $TBS->JoomlaArticle->title;   read/write
URL of the current article:   $url = JRoute::_(ContentHelperRoute::getArticleRoute($art_id));   $art_id must be the current article id.
This expression give the good URL coresponding to the current URL mode configured in the Joomla instance (long, short, with html extension or not)
Current user object   $user =& JFactory::getUser();   If ($user->id==0) then the current user is not logged.
    $user->username    
Prefix of Joomla tables   $conf = new JConfig();
$jpref = $conf->dbprefix;
   

You can found more coding ressource at the Joomla API Documentation

Type #3: Internal script

Syntax: {tbs}embedded;db=dbname{/tbs}

This type of plugin tag is the same as the previous one but instead of an external script, it run the parts of PHP code which are embedded in the current article. This can be very dangerous because all PHP commands are available. The parts of PHP code must be placed between tags <!--TBS and -->. Those tags are compatible with HTML comment tags, which means that they won't destroy the article looking, but they cannot be seen is visual mode. You must edit them by editing the HTML source. The PHP code is deleted before the article is displayed.

dbname: Optional. Name of the database to select before the script is run. The database must be available with the current Joomla connection. You can specify the keyword %joomla% for the Joomla database.
If you want to connect to a database with another account and password, then your script must open this connection by itself, and you should specify explicitly the new open connected for any use of MergeBlock().

Example:

Joomla article:
{tbs}embedded{/tbs}
Name Email
[u.name;block=tr] [u.email]

Tag embedded in the Joomla article: (viewable using button [html])
<!--TBS

$key = (isset($_GET['key'])) ? intval($_GET['key']) : 0;
$TBS->MergeBlock('u','mysql','SELECT name, email FROM jos_users WHERE id='.$key);

-->

TBS Dynamic articles and PDF documents:

In the original Joomla 1.5.x installation, the generation of PDF document (using the PDF button available on any article by default) does not run any Joomla plug-ins. Therefore your TBS Dynamic Article will not be converted as we can expect when a user will click on the PDF button. Instead he will see the article like the Admin can see it.
In order to avoid this behaviour, you can try two solutions:
• Deactivate the PDF generation for all articles or for the current article only. This can be done by tuning the global property or only the property of the article.
Content >> Article Manager >> Parameters : "PDF Icon"
Content >> Article Manager >> (your article) >> Parameters - Advanced : "PDF Icon"
Sometimes you may also need to change menu settings:
Menus >> Main Menu >> Home >>Parameters - Component : "PDF Icon"
or
• Edit the PDF Generator script provided by Joomla in order to make it run all content plug-ins. To do so, just perform the following:
Edit the file <Joomla root folder>/components/com_content/views/article/view.pdf.php
And after the lines:
// process the new plugins
JPluginHelper::importPlugin('content', 'image');
$dispatcher->trigger('onPrepareContent', array (& $article, & $params, 0));
Add the two lines:
JPluginHelper::importPlugin('content');
$results = $dispatcher->trigger('onPrepareContent', array (& $article, & $params, 0));