Composr Supplementary: Writing mini-modules
Written by Chris Graham
Potentially Outdated Tutorial
This supplementary tutorial might be outdated as it was written for a previous version of Composr CMS (version 10).
The Composr mini-module feature lets you create new Composr pages very easily. You don't need to code to any particular structure or API, just write plain PHP and output as normal from it. You have full access to Composr's APIs for when you need to interface with the rest of Composr.
This very practical tutorial shows you how via a number of simple examples.
Mini-modules allow:
- PHP programmers with no experience with Composr to hit the ground running
- experienced Composr developers to develop simple pages without any coding overhead
- easier porting of third-party PHP scripts into Composr (you may need to change some links around, and remove HTML header tags – but it's a lot easier than doing a rewrite)
We will present 3 examples. To try each out, simply save the code into a site/pages/mini-modules_custom/example_page.php file, then call it up via http://yourbaseurl/site/index.php?page=example_page.
As you can see, site/pages/mini-modules_custom/example_page.php corresponds to a page called example_page, in the site zone.
As mini-modules are just a kind of Composr page, you can control access to them using normal Composr page permissions (i.e. set from Admin Zone > Security > Permissions tree editor).
Example 1
The standard introductory example, Hello World.Code (PHP)
<?php
echo 'Hello World';
echo 'Hello World';
Example 2
Now let's do some simple Composr API calls.Code (PHP)
<?php
$username=$GLOBALS['FORUM_DRIVER']->get_username(get_member());
echo '<p>Hello, ' . htmlentities($username) . '.</p>';
$time=get_timezoned_date(time());
echo '<p>It is ' . htmlentities($time) . '.</p>';
$username=$GLOBALS['FORUM_DRIVER']->get_username(get_member());
echo '<p>Hello, ' . htmlentities($username) . '.</p>';
$time=get_timezoned_date(time());
echo '<p>It is ' . htmlentities($time) . '.</p>';
Example 3
Need to output a simple spreadsheet? You are allowed to set headers and exit(); within your page, so that Composr doesn't continue doing anything more after your spreadsheet has output and your code has run.Code (PHP)
<?php
header('Content-Type: text/plain; charset=utf-8');
header('Content-Disposition: attachment; filename="example.csv"');
// Some arbitrary data to output
$example_data = array(
array(
'country' => 'GB',
'capital' => 'London',
),
array(
'country' => 'France',
'capital' => 'Paris',
),
);
foreach ($example_data as $i => $row)
{
// If first row, show headings
if ($i == 0)
{
foreach (array_keys($row) as $heading)
{
echo csv_escape($heading) . ',';
}
echo "\n";
}
// Show values
foreach ($row as $value)
{
echo csv_escape($value) . ',';
}
echo "\n";
}
exit();
function csv_escape($value)
{
return str_replace('"', '""', $value);
}
header('Content-Type: text/plain; charset=utf-8');
header('Content-Disposition: attachment; filename="example.csv"');
// Some arbitrary data to output
$example_data = array(
array(
'country' => 'GB',
'capital' => 'London',
),
array(
'country' => 'France',
'capital' => 'Paris',
),
);
foreach ($example_data as $i => $row)
{
// If first row, show headings
if ($i == 0)
{
foreach (array_keys($row) as $heading)
{
echo csv_escape($heading) . ',';
}
echo "\n";
}
// Show values
foreach ($row as $value)
{
echo csv_escape($value) . ',';
}
echo "\n";
}
exit();
function csv_escape($value)
{
return str_replace('"', '""', $value);
}
Note that Composr has proper spreadsheet APIs, this is just a simple example that does not use them.
Integrations
You may wish to integrate with another database (i.e. not Composr's main database), third-party web-services, or other programming languages.These complexities can be dealt with in the same way as any PHP programmer would deal with them, while making use of Composr's APIs only as is convenient.
For example, you can use:
- standard PHP database functionality to open a new connection (MySQL, SQL Server, Oracle, …)
- PHP's file_get_contents function to download from REST web services
- PHP's file_get_contents function to download from scripts written in another language, bridging their output over into the Composr page
- PHP's SOAP support to integrate with more complex REST services (assuming the PHP SOAP extension is installed)
- PHP's COM support to integrate to ASP code (assuming the PHP COM extension is installed)
Example 4
This simple example shows how to bridge one web system, to another. The example embeds DuckDuckGo into your page. Naturally, this is a silly example, but the same technique can be used to bridge your own scripts, should you be more comfortable programming in a language other than PHP.Code (PHP)
<?php
echo '<div style="position: relative; overflow: hidden">';
echo file_get_contents('https://duckduckgo.com/');
echo '</div>';
echo '<div style="position: relative; overflow: hidden">';
echo file_get_contents('https://duckduckgo.com/');
echo '</div>';
Note I had to add a little CSS to stop DuckDuckGo trying to render parts of its layout on-top of the Composr website.
See also
- Making an addon (part 1)
- Introduction to programming
- Code Book, contents and introduction
- A PHP/Composr coding primer
- PHP website
Feedback
Please rate this tutorial:
Have a suggestion? Report an issue on the tracker.