Thursday 12 July 2012

Make a simple Module in Joomla 2.5

I've just made a real simple module for Joomla 2.5 and I think it's worth recording as a reference for future module creation - as a decent sample template for MVC module in Joomla.

It tackles some simple questions I was asking when looking to create a module.

Like ' where to add the logic for Database calls in a Joomla Module' and how to fix a Failed loading XML file error when uploading a module.

Heres the files and folders we needed to create.

modules/mod_PRSwarnings/
tmpl/default.php
tmpl/index.html
helper.php
index.html
modPRSwarnings.php
modPRSwarnings.xml


and here's the code


modules/mod_PRSwarnings/tmpl/default.php
[code]



/**
 * PayRollSystem Warnings
 *
 * @package PayRollSystem Warnings
 * @subpackage PayRollSystem Warnings
 * @version   1.0 July, 2012
 * @author    DJ http://www.littleripples.com
 * @copyright Copyright (C) 2010 - 2012 www.littleripples.com, LLC
 * @license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
 *
 */

// no direct access
defined('_JEXEC') or die;





if($items != NULL ){

 foreach ($items as $item) {
$user_id = $item->id;
$user_id2 = $item->user_id;
$project_id = $item->project_id;
$cdate = $item->cdate;
$start_time = $item->start_time;
$end_time = $item->end_time;
$firstname = $item->firstname;
$lastname = $item->lastname;

echo "Last Shift: id $user_id ($user_id2)- $firstname $lastname  - Started $start_time - finished $end_time at ";

}
}

[/code]

modules/mod_PRSwarnings/tmpl/index.html
[code]

[/code]



modules/mod_PRSwarnings/helper.php
[code]


/**
 * PayRollSystem Warnings
 *
 * @package PayRollSystem Warnings
 * @subpackage PayRollSystem Warnings
 * @version   1.0 July, 2012
 * @author    DJ http://www.littleripples.com
 * @copyright Copyright (C) 2010 - 2012 www.littleripples.com, LLC
 * @license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
 *
 */

// no direct access
defined('_JEXEC') or die;

class ModPRSwarnings
{
 public function getLastShift(){
      $db = &JFactory::getDBO();

      $query  = "select ";
      $query .= "tt.id, tt.project_id, tt.cdate, tt.start_time, tt.end_time, cp.user_id, cp.firstname, cp.lastname ";
      $query .= "from #__pf_time_tracking as tt ";
    $query .= "LEFT JOIN (#__comprofiler as cp) ON tt.user_id=cp.user_id ";
$query .= "limit 1 ";
   

      $db->setQuery($query);
      $items = ($items = $db->loadObjectList())?$items:array();
      return $items;
    }


}

[/code]


modules/mod_PRSwarnings/modPRSwarnings.php

[code]


//no direct access
defined('_JEXEC') or die('Direct Access to this location is not allowed.');

// include the helper file
require_once(dirname(__FILE__).DS.'helper.php');


$items = ModPRSwarnings::getLastShift();

// include the template for display
echo 'Hello World';
require(JModuleHelper::getLayoutPath('mod_PRSwarnings'));

[/code]




modules/mod_PRSwarnings/modPRSwarnings.xml

[code]


    PayRollSystem Warnings
    DJ Millward
    July 2012
    Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
    http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
    dj@lrip.eu
    littleripples.com
    1.0.0
    This module works with the PayRollSystem component to show the last shift added and any warningings on the system
   
        mod_PRSwarnings.php
        helper.php
        mod_PRSwarnings.xml
        index.html
        tmpl/default.php
        tmpl/index.html
   
   
       
   


[/code]


When uploading the module to my website I tried to use the 'Discover' function .  But got the error.

 *Failed loading XML file** * XML: failed to load  external entity * joomla development


the fix for this was to use the 'Install from Directory' method of installing a module. 



No comments: