Quick links: Content - sections - sub sections
EN

Trace: jevent

Jelix et la Communication inter modules

A nugget among so many others contained Jelix, is the internal communication between modules.

But what is this?

Sometimes modules may need to communicate with each other or whether they need information from each other.

Imagine a simple case, an administrative interface that lists the modules (articles, wiki, news), present on his favorite site.

The “Jelixian” solution is to make the administration module communicate with all the others.

The administration module will send a message and retrieve the responses of the modules.

implementation

This page will be made of one template and the responses of modules will be made with the help of Zones (Remind : the Zones are part of page)

So for that, I define a controller “modules” with one action index by default

the controller

class modulesCtrl extends jController {
 
    function index() {
	$rep = $this->getResponse('html');
        $tpl = new jTpl();
        $tpl->assign('modules',jEvent::notify('HfnuAboutModule')->getResponse()); 
        $rep->body->assign('MAIN',$tpl->fetch('modules'));
	return $rep;
	}
}

the interesting line of code here is

$tpl->assign('modules',jEvent::notify('HfnuAboutModule')->getResponse()); 

This line makes 3 things in same time :

  1. it sends a message named HfnuAboutModule
  2. it get the data returned by the sent message
  3. it assigns the data to the variable “modules” of the template.

the following line tell to Jelix, the name of the module, which will display the data :

$rep->body->assign('MAIN',$tpl->fetch('modules'));

the template

<h1>List of modules </h1>
{if count($modules)}
{assign $count = count($modules)}
{for $i=0; $i<$count;$i++}
<div class="two-cols">
    <div class="col">
        {$modules[$i]}            
    </div>
</div>
{/for}
{/if}

Good so we see a little bit what will happened “à the end” but how our modules “news”,“wiki”,“articles” are going to respond to the event HfnuAboutModule ?

To all jEvent::notify, a listener can respond, so we are going to define a listener as follow, in 2 times

  1. defining an events.xml file, describing the name of the event and the response classe, so events.xml is the link
  2. defining of the listener itself.

events.xml file

<?xml version="1.0" encoding="iso-8859-1"?>
<events xmlns="http://jelix.org/ns/events/1.0">
   <listener name="hfnuadmin">
		 <event name="HfnuAboutModule" />
   </listener>   
</events>

We will find here the name of the event HfnuAboutModule to which the listener hfnuadmin will be responsible for responding

the listener

class hfnuadminListener extends jEventListener{
 
   function onHfnuAboutModule ($event) {
        $event->add( jZone::get('hfnuadmin~about',array('modulename'=>'hfnuadmin')) );
   }   
}

when HfnuAboutModule is triggered, then onHfnuAboutModule enter in action and respond to the event (with $event→add())

$event->add() can receive all kind of datas. Here we just return a Zone (that we already see in one of the 2 previous articles) named “about”

the zone

class aboutZone extends jZone {
    protected $_tplname='zone.about';
 
    protected function _prepareTpl(){
        $moduleName = $this->param('modulename');
 
        if ($moduleName == '') return;
        jClasses::inc('havefnubb~modulexml');
        $moduleInfo = modulexml::parse($moduleName);         
        $this->_tpl->assign('moduleInfo',$moduleInfo);  		
    }
}

our zone retrieve the parameter of the name of the module, then parse the file module.xml and assign the result to the template “zone.about”

the template

<h1>{$moduleInfo['name']}</h1>
<dl>
<dt>{@hfnuadmin~hfnuabout.about.version@} :</dt><dd> {$moduleInfo['version']} ({@hfnuadmin~hfnuabout.about.date.create@} {$moduleInfo['dateCreate']})</dd>
<dt>{@hfnuadmin~hfnuabout.about.label@} :</dt><dd> {$moduleInfo['label']|escxml}</dd>
<dt>{@hfnuadmin~hfnuabout.about.desc@} :</dt><dd> {$moduleInfo['desc']}</dd>
<dt>{@hfnuadmin~hfnuabout.about.notes@} :</dt><dd> {$moduleInfo['notes']}</dd>
<dt>{@hfnuadmin~hfnuabout.about.licence@} :</dt><dd> {if $moduleInfo['licenceURL'] != ''}<a href="{$moduleInfo['licenceURL']}">{$moduleInfo['licence']}</a>{else}{$moduleInfo['licence']}{/if}</dd>
<dt>{@hfnuadmin~hfnuabout.about.copyright@} :</dt><dd> {$moduleInfo['copyright']}</dd>
{foreach $moduleInfo['creators'] as $author}
<dt>{@hfnuadmin~hfnuabout.about.authors@} :</dt><dd> {if $author['email'] != ''}<a href="mailto:{$author['email']}">{$author['name']|escxml}{else}{$author['name']|escxml}{/if}</a></dd>
{/foreach}
<dt>{@hfnuadmin~hfnuabout.about.links@}</dt><dd><a href="{$moduleInfo['homepageURL']}">{@hfnuadmin~hfnuabout.about.homepageURL@}</a> - <a href="{$moduleInfo['updateURL']}">{@hfnuadmin~hfnuabout.about.updateURL@}</a></dd>
</dl>

Result:

List des modules
Name : 
    News!
Version :
    stable 1.1.2 (2008-12-16)
Label :
    Module of News management
Description :
    This module permits to manage the news of your website
Notes :
    N/A
License :
    GNU General Public Licence
Copyright :
    2008 FoxMaSk
Authors :
    FoxMaSk
Links : 
    Home page of the module - Update link

Name : 
    Wiki
Version :
    stable 1.0.2 (2009-01-25)
Libellé :
    Wiki
Description :
    Wiki home made for the documentation of the website
Notes :
    N/A
License :
    GNU General Public Licence
Copyright :
    2008 FoxMaSk
Authors :
    FoxMaSk
Links
    Home page of the module - Update link

PS : here I didnt detailled of the events.xml of the 3 nor their listenenr but the code is the same ;)

Conclusion

Here is the pearl, which in a few short lines, has permitted to all the modules to “find” and gather informations in one place.

The same mechanics jEvents::notify() allows to chain of such shares after the registration of a member (such as to send a mail) jEvents::notify() can also enhance the functionality of a module A via other modules B, C, D without changing the module A, etc …

read more about communication between modules

en/tutorials/modules/jevent.txt · Last modified: 2012/01/31 12:56 by foxmask

Recent changes RSS feed Creative Commons License