Retrieving Global Component Parameters From Another Component

Joomla provides the ability through a helper file in your current component to access another component’s global parameters.

The following will retrieve the parameter values of the component set at the jos_components table level.


jimport('joomla.application.component.helper');
$params = &JComponentHelper::getParams('com_somecomponent');
$val = $params->get('someparam');

Using Correct Callback Method Scope Within a PHP Class

If your calling method in a class has a callback function as a parameter such as usort() or array_walk() you need to call the callback function in the correct scope or php will not find it.

If your using array_walk() within a class method then you need to define the callback this way.


class SomeClass {

function doIt() {
  array_walk($someArr,array($this,"someFunc"));
}

function someFunc() {

}

}

$this refers to the container SomeClass class.

If someFunc() is a static function in the class then do this way.


array_walk($someArr,array(__class__,"someFunc"));

Overriding Response Http Header in Joomla

By default Joomla will set the page not to cache sending ‘cache-control: no-cache,no-store’ and ‘Pragma: no-cache’. If your using a non-Joomla caching solution you might need to modify this response header.

You can override the default response header by using the JResponse::allowCache() function.

This will prevent the no-cache header values that Joomla sets.


jimport('joomla.environment.response');
JResponse::allowCache(true);

Then you can set your custom response header values using setHeader(). You will now be able to control the caching behavior of your pages.


$dt = gmdate('D, d M Y H:i:s', time()).' GMT';
JResponse::setHeader('Date', $dt, true );
JResponse::setHeader('Last-Modified', $dt, true );
JResponse::setHeader('Cache-Control', 'max-age=300,must-revalidate', true );

You can place this code just about anywhere in the view, controller, model etc…

Preventing CRSF Attacks Using Form Request Token in Joomla

You should place a token in your form to verify that its a legitimate request to your site. This will prevent CSRF (Cross Site Request Forgery) attacks.

This will generate a hidden tag containing the token value. Place this in your form.



echo JHTML::_( 'form.token' );


Token is a series of random strings that is unique per session and user.

You can also add the token to the url if your submitting form with GET method like this.



echo JRoute::_( 'index.php?option=com_example&task=save&'. JUtility::getToken() .'=1' ); 


Before the form processing code look for the token. If there is no token or is invalid then display a simple and direct error message.



//For post method
JRequest::checkToken() or die( 'Invalid Token' );
//For get method
JRequest::checkToken( 'get' ) or die( 'Invalid Token' );


Web Page Caching Tutorial

This is a good tutorial on the different types of web page caching. It will give you insight on the best way to cache your site.

Caching Tutorial for Web Authors and Webmasters

Loading an User Object with User Name or User ID in Joomla

You can load a specific user object with just the user name or user ID when using the getUser() method.


$user = JFactory::getUser('username');
$user = JFactory::getUser(55);

Making custom drop down list using JHTML in Joomla

You can use the JHTML helper library to draw a custom drop down lists.


JHTML::_('select.genericlist', $dataArray, $name, $attributes,
$valueField, $textField, $selectedValue);

The $dataArray must be an associative array like this


$arr[] = Array('val'=>1,'text'=>'Jan');
$arr[] = Array('val'=>2,'text'=>'Feb');

The $name is the name you want to give the drop down list.

The $attributes are the list properties you want to add.

The $valueField is name of the array field used for the actual value of list item.

The $textField is name of the array field used for the list item text to display.

This will display the dropdown named lstMonths with two months from $arr:


echo JHTML::_('select.genericlist', $arr, 'lstMonths', 'onChange=\"changeFunc()\"', 'val', 'text', $_POST['lstMonths']);

The actual select helper file is located in /libraries/joomla/html/html/select.php

HTTP Requests Using Curl and Decoding JSON Responses in PHP

You can do http requests using the Curl library and decode the JSON result with json_decode() function. PHP 5.2 and above comes with the JSON library functions.

The following example will send a request using curl to a video library api and extract links to the video thumbnail images. The result will be a list of links to thumbnail images in a json data format. The json_decode() function will then convert the json object into an associative array and will finally print out the links. This is all done within the php script.


<?php
//check if you have curl loaded
if(!function_exists("curl_init")) die("cURL extension is not installed");

$url = 'http://api.brightcove.com/services/library?command=find_all_videos'.
'&video_fields=thumbnailURL&token=0Z2dtxTdJAxtbZ-d0U7Bhio2V1Rhr5Iafl5FFtDPY8E.';

$ch=curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$r=curl_exec($ch);
curl_close($ch);

$arr = json_decode($r,true);

foreach($arr['items'] as $val)
{
	echo $val['thumbnailURL'].'<br>';
}

Displaying Raw Output in Joomla 1.5 by Setting Format in the Component

The most common way to produce raw output in Joomla is to pass the format= raw parameter in the url.

Joomla has four supported document type objects: html, pdf, feed and raw. When passing no format parameter the default is html. Html format is just the normal html output. This article covers the use of raw. You use raw to generate content that do not fit within the other document types.

The following method will allow you to set the document format within the component itself. This might be useful if you want to keep your url clean of any query string values.

You cannot simply do a setVar(‘format’,'raw’) in the component. The document type is set before the component is even called. So you have to manipulate the document object itself in order to set the format within the component.

Place the following three lines in the display() function of the controller before the view is displayed.

You first get an instance of the current document object.


$document = &JFactory::getDocument();

Then get instance of the raw document object.


$doc = &JDocument::getInstance('raw');

Replace the current document type with the new one.


$document = $doc;

This has the same effect as passing format=raw in the url.

Now set the mime type of the document. This document will be a rss feed.


$document->setMimeEncoding('application/rss+xml');

So This is what the display() function looks like in your controller file


	function display()
	{
		$document = &JFactory::getDocument();
		$doc = &JDocument::getInstance('raw');
		$document = $doc;
		$document->setMimeEncoding('application/rss+xml');

		if ( ! JRequest::getCmd( 'view' ) ) {
			JRequest::setVar('view', 'someview' );
		}

		parent::display();
	}

Once you do this the controller will be looking for the view entry file named view.raw.php in the /components/com_somecomponent/views/someview folder. Just copy the view.html.php file and name it view.raw.php

Protected: Care Project

This post is password protected. To view it please enter your password below: