📜 ⬆️ ⬇️

Creating a simple module on Joomla 1.5. # (Using the connection to the database)

There are many examples in the network of creating the Hello World module, but this is often not the main goal for the first time a person wants to make his own module. Usually the task is to derive some data from the database, which were not provided by the usual standard Joomla modules.
To do this, create a directory and put there any index.html file (located in any folder of any Joomla module) to avoid direct contact with the directory. If you do not want to suffer with installing it, and you have Joomla on localhost, you can create a folder of your module right in the modules folder, after creating all the files, you can immediately go to the admin panel, go to the module control panel, click “create” and will be displayed in the list and ready to be included ...

For example, the module will be called mod_my . His task will be to display a list of users from the database and limit it to the number specified manually from the admin to manage this module.

The minimum set of files for the full operation of the module:
/modules/mod_my/index.html
/modules/mod_my/mod_my.php
/modules/mod_my/mod_my.xml
/modules/mod_my/helper.php
/modules/mod_my/tmpl/index.html
/modules/mod_my/tmpl/default.php

This module uses the MVC architecture (Model, View, Controller - Model, View, Controller).
First, we separate the logic of the module (controller) into the file helper.php, so that all work with the data is done only there. View (View) / template, (X) HTML we will render in tmpl / default.php. This is a good programming style — to separate logic from presentation.
')
Now let's start creating files. Let's start with mod_my.php
 <? php
 // 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 ');

 // get a parameter from the module configuration
 $ userCount = $ params-> get ('usercount');

 // get the items to display from the helper
 $ items = ModMyHelper :: getItems ($ userCount);

 // include the template for display
 require (JModuleHelper :: getLayoutPath ('mod_my'));

 ?>


The main stages of this file:


The following file, mod_my.xml , will contain the description of the module and some of its settings:
 <? xml version = "1.0" encoding = "utf-8"?>
 <install type = "module" version = "1.5.0">
     <! - Name of the Module ->
         <module> My Module </ name>

     <! - Name of the Author ->
         <author> Nick </ author>

     <! - Version Date of the Module ->
         <creationDate> 2009-03-30 </ creationDate>

     <! - Copyright information ->
         <copyright> Copyright </ copyright>

     <! - License Information ->
         <license> GPL 2.0 </ license>

     <! - Author's email address ->
         <authorEmail> info@info.com </ authorEmail>

     <! - Author's website ->
         <authorUrl> www..com </ authorUrl>

     <! - Module version number ->
         <version> 1.0.0 </ version>

     <! - Description of what the module does ->
         <description> Any </ description>

     <! - for the module to function ->
         <files>
         <! - The "module" attribute defines the main controller file ->
                 <filename module = "mod_my"> mod_my.php </ filename>
                 <filename> index.html </ filename>
                 <filename> helper.php </ filename>
                 <filename> tmpl / default.php </ filename>
                 <filename> tmpl / index.html </ filename>
         </ files>

  
     <! - Optional parameters ->
         <params>
         <! - parameter for the module table / xhtml display ->
                 <param name = "moduleclass_sfx" type = "text" default = "" label = "Module Class Suffix" description = "PARAMMODULECLASSSUFFIX" />

         <! - just gives us a little room between the previous paramter and the next ->
                 <param name = "@ spacer" type = "spacer" default = "" label = "" description = "" />

         <! - This parameter will display ->
         <param name = "usercount" type = "text" default = "5" label = "Number of users" description = "Specify the number of users" />
     </ params>
 </ install>


Helper.php itself will contain the request code itself:
 <? php
 defined ('_ JEXEC') or die ('Direct Access to this location is not allowed.');

 class ModMyHelper
 {
     / **
      * Returns a list of post items
     * /
     public function getItems ($ userCount)
     {
         // get a reference to the database
         $ db = & JFactory :: getDBO ();

             // get a list of $ userCount randomly ordered users
         $ query = 'SELECT a.name FROM `#__ users` AS a LIMIT'.  $ userCount.  '';

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

         return $ items;

       
     } // end getItems

 } // end SimplestForumLatestPostsHelper

 ?> 


It simply receives a list of users from the database and randomly fills the array with a subset of users, the dimension of which is specified via the module parameter and passed to the getItems method via the $ userCount parameter.

And finally, so that all this is displayed in the simplest list, we create the tmpl / default.php file.
 <? php defined ('_ JEXEC') or die ('Restricted access');  // no direct access?>

 <ul>
   <? php foreach ($ items as $ item) {?>
     <li>
         <? php echo $ item-> name;  ?>
     </ li>
     <? php}?>

 </ ul>



As a result, we get a fully working module, the simplest content, but having a connection to the database and an example of its implementation! Then of course you can archive it in a ZIP and it will be ready for installation in any joomla 1.5

Source: https://habr.com/ru/post/56512/


All Articles