Custom Module Development - Joomla

  • Registering the Module in the Database
As with the component, we will have to register the module in the database so that
it can be referenced in the back end and used effectively. Entering a record into the
jos_modules table will take care of this. Open your database console and enter the
following query:
INSERT INTO jos_modules (title, ordering,
position, published, module, showtitle, params)
VALUES ('Restaurant Reviews', 1, 'left', 1,
'mod_reviews', 1, 'style=simple\nitems=3\nrandom=1');

OR
If you're using phpMyAdmin, enter the fields as in the above structure manually.
If you refresh the front end right after entering the record in jos_modules, you'll
notice that the module doesn't appear, even though the published column is set
to 1. To fix this, go to Extensions | Module Manager in the back end and click the
Restaurants Reviews link. Under Menu Assignment, select All and click Save.
In the front end, the left-hand side of your front page should look similar to
the following:


  • Creating and Configuring a Basic Module
To create the module for our reviews, we will have to create a directory
mod_reviews under /modules. We will also need to create the mod_reviews.php file
inside mod_reviews. To start, we'll create a basic module that displays links to the
most recent reviews. In the mod_reviews.php file, add the following code:
defined('_JEXEC') or die('Restricted access');
$items = $params->get('items', 1);
$db =& JFactory::getDBO();
$query = "SELECT id, name FROM #__reviews WHERE
published = '1' ORDER BY review_date DESC";
$db->setQuery( $query, 0, $items );
$rows = $db->loadObjectList();
foreach($rows as $row)
{
echo '' . $row->name .
'

';
}
?>

When you save the file and refresh the homepage, your module should look similar
to the following:   




  •   Recruiting Some Helpers                                                                                                                             We would like to have our module do more than display links to the reviews. It
    would be helpful to include a summary of the review along with each link or have
    the opportunity to display a review at random. However, the way we have it coded
    currently is not sufficient to handle different scenarios efficiently. To fix this, we
    will centralize the data functions into a helper class. Create the helper.php file in
    /modules/mod_reviews and add the following code:
defined('_JEXEC') or die('Restricted access');
class modReviewsHelper
{
function getReviews(&$params)
{
$items = $params->get('items', 1);
$db =& JFactory::getDBO();
$query = "SELECT id, name, quicktake FROM #__reviews
WHERE published = '1' ORDER BY review_date DESC";
$db->setQuery( $query, 0, $items );
$rows = $db->loadObjectList();
return $rows;
}
function renderReview(&$review, &$params)
{
$link = JRoute::_
("index.php?option=com_reviews&task=view&id=" .
$review->id);
require(JModuleHelper::getLayoutPath
('mod_reviews', '_review'));
}
}
?>


  • Try Some Different Layouts
The helper class does not produce any output itself. Instead, renderReview()
formats the link and then calls the getLayoutPath() member function in
JModuleHelper to include a layout file named _review.
To create this file, make the folder tmpl under /modules/mod_reviews, create
_review.php inside tmpl, and then add the following line of code:



The underscore at the beginning of _review is a convention to remind us that the
layout is for internal use; it is not offered as a choice to the admin. In addition to
this internal layout, we can create other layouts as different display options. To
start, we will create one named default. Add the default.php file in /modules/
mod_reviews/tmpl and add the following code:






Notice that this layout cycles through a list of reviews, calling the helper class
function that prepares single reviews for display, which in turn loads in the _review
layout. Using the same method, we will also create a bulleted layout. Create the
bulleted.php file in /modules/mod_reviews/tmpl and add the following code to
create the links as a bulleted list:
The bulleted layout uses the same basic logic as the default layout; the only
difference is that it wraps the results in bullets. Both options ultimately load the _
review layout via the helper function, ensuring that the link formatting is consistent
across layouts.
We now have two different display options and a helper class, but none of this code
is yet accessible by the module. Open mod_reviews.php and replace the contents
with the following code:
defined('_JEXEC') or die('Restricted access');
require(dirname(__FILE__).DS.'helper.php');
$list = modReviewsHelper::getReviews($params);
require(JModuleHelper::getLayoutPath('mod_reviews'));
?>

Save all the open files and refresh the front page in your browser. The module
should look the same as in the last screenshot. Now, go back to mod_reviews.php
and edit the call to getLayoutPath() so that the bulleted layout is called instead
of default:
require(JModuleHelper::getLayoutPath('mod_reviews', 'bulleted'));
Save all the files and refresh your browser. The module should now appear similar to
the following:



It would be nice if we could display a small portion of the review along with each
link. Go back to /modules/mod_reviews/tmpl/_review.php and add the following
highlighted code:
Refresh your browser. If you entered something in the Quicktake field in the back
end when editing your reviews, you should see something like the following:

Go back to mod_reviews.php and set the second parameter of getLayoutPath() to
default. After saving the file and refreshing the browser, you should see the same
reviews and quotes as before, only without the bullets. While the layout is changing,
the output from _review is staying constant.
  • Mixing it Up
Our module is great at highlighting the latest opinions of our diners, but our
frequent visitors may want the past reviews. Let's fix that with some adjustments to
the module. Replace the line in mod_reviews.php where the $list function is set
with the highlighted code:
defined('_JEXEC') or die('Restricted access');
require(dirname(__FILE__).DS.'helper.php');
$random = $params->get('random', 0);
if($random)
{
$list = modReviewsHelper::getRandomReview();
}
else
{
$list = modReviewsHelper::getReviews($params);
}


require(JModuleHelper::getLayoutPath
('mod_reviews', 'default'));
?>
Now that the parameter checking code is in place, open helper.php and add the
following function to the modReviewsHelper class:
function getRandomReview()
{
$db =& JFactory::getDBO();
$query = "SELECT id, name, quicktake FROM #__reviews";
$db->setQuery( $query );
$rows = $db->loadObjectList();
$i = rand(0, count($rows) - 1 );
$row = array( $rows[$i] );
return $row;
}


Save the file and refresh your browser. Then refresh it repeatedly. With any luck,
single reviews should appear at random.












Enjoy!!!!! Custom  Module...


Custom Module Development - Joomla Custom Module Development - Joomla Reviewed by Web Technology Funda on 3:50:00 AM Rating: 5

No comments

Free! Free!Free! Subscribe to Get Free PHP (Magento) tutorial Update in Your Inbox!!! Hurry Up!!!