User

Difference between revisions of "Rvsjoen/tutorial/Developing a Module/Part 06"

From Joomla! Documentation

< User:Rvsjoen‎ | tutorial/Developing a Module
(Added language file to manifest, previous entry just copies language folder to module)
 
(5 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
= Accessing the database =
 
= Accessing the database =
 +
 +
This part is meant to provide you with a short example on how to access the database and run a query from a module. This is no different than running a query anywhere else in Joomla! but since modules are often the first place you start to learn to write extensions, this tutorial seems like a natural place to cover it.
  
 
With your favorite editor, edit the following file
 
With your favorite editor, edit the following file
Line 35: Line 37:
 
</source>
 
</source>
 
</span>
 
</span>
 +
 +
Here we add a function to our helper class, the function will simply return the number of currently active sessions by counting the number of rows in the session table.
  
 
With your favorite editor, edit the following file
 
With your favorite editor, edit the following file
Line 59: Line 63:
 
</source>
 
</source>
 
</span>
 
</span>
 +
 +
Here we modify the module template to echo out the number of currently active users, as provided by our helper.
  
 
With your favorite editor, edit the following file
 
With your favorite editor, edit the following file
Line 88: Line 94:
 
<filename>helper.php</filename>
 
<filename>helper.php</filename>
 
<folder>tmpl</folder>
 
<folder>tmpl</folder>
<folder>language</folder>
 
 
</files>
 
</files>
 
+
        <languages folder="language">
 +
<language tag="en-GB">en-GB/en-GB.mod_helloworld.ini</language>
 +
<language tag="en-GB">en-GB/en-GB.mod_helloworld.sys.ini</language>
 +
</languages>
 
<config>
 
<config>
 
<fields name="params">
 
<fields name="params">
Line 112: Line 120:
  
 
* <tt>[[#mod_helloworld.xml|mod_helloworld.xml]]</tt>
 
* <tt>[[#mod_helloworld.xml|mod_helloworld.xml]]</tt>
* <tt>[[#mod_helloworld.php|mod_helloworld.php]]</tt>
+
* <tt>[[User:Rvsjoen/tutorial/Developing_a_Module/Part_05#mod_helloworld.php|mod_helloworld.php]]</tt>
 
* <tt>[[#tmpl/default.php|tmpl/default.php]]</tt>
 
* <tt>[[#tmpl/default.php|tmpl/default.php]]</tt>
 +
* <tt>[[User:Rvsjoen/tutorial/Developing_a_Module/Part_04#language/en-GB.mod_helloworld.ini|language/en-GB.mod_helloworld.ini]]</tt>
 +
* <tt>[[User:Rvsjoen/tutorial/Developing_a_Module/Part_04#language/en-GB.mod_helloworld.sys.ini|language/en-GB.mod_helloworld.sys.ini]]</tt>
 +
* <tt>[[#helper.php|helper.php]]</tt>
 +
 +
== Testing your module ==
 +
 +
For details on how to install the module into your Joomla! site, refer to the information provided in
 +
[[User:Rvsjoen/tutorial/Developing_a_Module/Part_01#Installation_and_Testing|Part 01]].
  
 
== Download this part ==
 
== Download this part ==
  
To be continued...
+
[https://github.com/downloads/rvsjoen/joomla-tutorials/mod_helloworld-part06.zip Download example package]
  
 
{{:Chunks:Developing_a_Module_Contents}}
 
{{:Chunks:Developing_a_Module_Contents}}

Latest revision as of 10:43, 16 May 2012

Accessing the database[edit]

This part is meant to provide you with a short example on how to access the database and run a query from a module. This is no different than running a query anywhere else in Joomla! but since modules are often the first place you start to learn to write extensions, this tutorial seems like a natural place to cover it.

With your favorite editor, edit the following file

helper.php

<?php 

/**
 * @package	Joomla.Tutorials
 * @subpackage	Module
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @license	License GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die;

class modHelloWorldHelper
{
	public static function getGreeting() {
		return "Message from the helper";
	}

	public static function getCurrentUsers() {
		$db = JFactory::getDBO();
		$db->setQuery($db->getQuery(true)
			->select("COUNT(*) as users")
			->from("#__session")
		);
		return $db->loadResult();
	}
}

Here we add a function to our helper class, the function will simply return the number of currently active sessions by counting the number of rows in the session table.

With your favorite editor, edit the following file

tmpl/default.php

<?php 

/**
 * @package	Joomla.Tutorials
 * @subpackage	Module
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @license	License GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die;
?>

<p><?php echo $params->get('greeting', JText::_('MOD_HELLOWORLD_GREETING_DEFAULT')); ?></p>
<p><?php echo modHelloWorldHelper::getGreeting(); ?></p>
<p>There are <?php echo modHelloWorldHelper::getCurrentUsers(); ?> user(s) logged in</p>

Here we modify the module template to echo out the number of currently active users, as provided by our helper.

With your favorite editor, edit the following file

mod_helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="2.5.0" method="upgrade">

	<name>MOD_HELLOWORLD</name>
	<!-- The following elements are optional and free of formatting constraints -->
	<creationDate>Once upon a time</creationDate>
	<author>John Doe</author>
	<authorEmail>john.doe@example.org</authorEmail>
	<authorUrl>http://www.example.org</authorUrl>
	<copyright>Copyright Info</copyright>
	<license>License Info</license>
	<!--  The version string is stored in the extension table -->
	<version>0.0.6</version>
	<!-- The description is optional and defaults to the name -->
	<description>MOD_HELLOWORLD_XML_DESCRIPTION</description>

	<!-- Note the folder attribute: This attribute describes what to copy
		into the module folder -->
	<files>
		<filename module="mod_helloworld">mod_helloworld.php</filename>
		<filename>mod_helloworld.xml</filename>
		<filename>helper.php</filename>
		<folder>tmpl</folder>
	</files>
        <languages folder="language">
		<language tag="en-GB">en-GB/en-GB.mod_helloworld.ini</language>
		<language tag="en-GB">en-GB/en-GB.mod_helloworld.sys.ini</language>
	</languages>
	<config>
		<fields name="params">
			<fieldset name="basic">
				<field
					name="greeting"
					type="text"
					default="MOD_HELLOWORLD_GREETING_DEFAULT"
					label="MOD_HELLOWORLD_GREETING_LABEL"
					description="MOD_HELLOWORLD_GREETING_DESC"
				/>
			</fieldset>
		</fields>
	</config>

</extension>

File listing[edit]

Testing your module[edit]

For details on how to install the module into your Joomla! site, refer to the information provided in Part 01.

Download this part[edit]

Download example package

Articles in this series[edit]

This tutorial is supported by the following versions of Joomla!

Joomla 2.5