Presented at the Joomla! User Group Toronto meeting of August 20, 2013. Covers setting up an environment under Linux, using Bluefish to create PHP code that can be executed using the Sourcerer extension to create custom capabilities for the Joomla! CMS.
3. What is it?
“Sourcerer enables you to place PHP and any kind of
HTML style code (including CSS and JavaScript) right
into your content! Not only in your articles, but also in
sections, categories, modules, components, META
tags, etc.”
4. What does that mean?
Without needing to learn all that it takes to develop
your own components, you can add custom capabilities
to your Joomla! sites.
11. Create Ubuntu Virtual Machine
● Create New machine
Options:
● Ubuntu Linux
● 1G memory
● 32G disk
● Change network to “bridged”
12. Install Ubuntu
● Click “Start” for the newly defined virtual machine
● When prompted, select your saved .iso image for
Linux
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47. Final Steps in Setting up development
Environment
Install
•Joomla
•Sourcerer
(directly or via Extension Manager)
•JCE (optional)
48. Advanced Configuration
Modify php.ini
Settings for apache: /etc/php5/apache2
Settings for CLI: /etc/php5/cli
Settings may need to be changed to
increase memory, upload size, or execution time
49. Code Resources
PHP site: www.php.net
great for syntax and code samples
Google
Your friend for finding PHP code,
and finding Joomla! API calls
50. Starting to code
Open a terminal, become root
Create a directory to put your PHP code
Set ownership/permissions
Open Bluefish
51. PHP key points
● Variables always start with $
● Variables are automatically defined (mostly)
● Variable names are case sensitive
● If you mis-spell or mis-capitalize, it isn't the same variable
● =, == and === are not the same thing
● Error messages are cryptic
● Arrays use associative indexes, which can be very useful
52. Error Messages
● When a Joomla! page ends up white, malformed, or
with an error message, check:
/var/log/apache2/error.log
● Go to the end of the file, and look back until you
find the first of what is usually a bunch of messages.
● If you're lucky the message will be useful, but it will
at least give you a line number
53. A simple code snippet
● Create new article
● Click on insert code
● Between the <?php and ?> tags, enter:
printf(“Today is %s<br>n”,date(“Y-m-d”));
● Save article, add menu item, open page.
54.
55.
56.
57. The other way to insert source...
● Create a suitable directory (e.g. php under your
installation root). Don't forget to set the
ownership/permissions correctly.
● Reference your.php file by putting a require_once
into your article between the {source}{/source} tags
58. Bluefish
●A simple but relatively useful editor, which is
language aware.
●You'll probably prefer to run it as root, so open a
terminal window, then “sudo bash”, and then
“bluefish&”
●Click on “New”, then “Save As”, and start writing.
●The file you've created should be the one you've put
into the “require_once” statement.
59. Monospace text from file
<?php
function monofilter($filePath){
printf("%sn",'<style type="text/css"> textMonospace
{ font-family: monospace, "Courier New", Courier }
</style><textMonospace>');
$handle=@fopen($filePath, "r");
if($handle){while(($line=fgets($handle))!==false)
{printf("%s%s",str_replace(" "," ",
$line),"<br>");};fclose($handle);};
printf("%s",'</textMonospace>');
return;
} ?>
60. Match a string in a text file
<!-- needs to be run by Sourcerer within Joomla -->
<?php
require_once("/usr/local/charts/utilPHP/htmlUtil.php");
defineChartMono();
$session = JFactory::getSession();
$mainframe=& JFactory::getApplication();
$sessionID = $session->getID();
$submitForm = JRequest::getVar( 'submit' );
$searchStrForm = strip_tags(strtoupper(JRequest::getVar( 'searchStr' )));
// save values if needed for print
if ($submitForm == "Display" ) {
$mainframe->setUserState( $sessionID.".searchStr", $searchStrForm );
};
61. Part 2
$printForm = JRequest::getVar( 'print' );
// print variable set to 1 by print icon
if ($printForm > 0) {
$searchStrForm=$mainframe->getUserState($sessionID.".searchStr");
}
else {
// don't put up input fields if printing
?>
<form method="post" action="sourcerer">
<label for="searchStr">Look For: </label><input type="text" id="searchStr" name="searchStr"
size=32 value= <?php printf(""%s"",$searchStrForm); ?> />
<input type="submit" value="DISPLAY" name="submit" />
</form>
<?php
};
defineChartMono();
?>
62. Part 3
<BR>
<textChartMono>
<?php
// echo $searchStrForm;
// 2 is used since the variable includes ""
if (strlen($searchStrForm)>0) {
$cmd="/bin/fgrep -i -- " . $searchStrForm . " ./images/txt/names.txt" ;
// var_dump($cmd);
exec($cmd, &$answer);
// var_dump($answer);
$nlines=count($answer);
for( $i=0; $i<$nlines; $i++) printf("%s", formatNBR($answer[$i]));
}
?>
</textchartmono>
<br />
63. Summary
● Sourcerer allows you to develop your own
HTML/JavaScript/PHP code without needing to
learn how to create your own component/plugin
● Using “require” to pull in an external PHP that is
edited in Bluefish allows for rapid development
without needing to learn a complicated IDE
But you probably will want to, eventually...