====== 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 : - it sends a message named **HfnuAboutModule** - it get the data returned by the sent message - 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 =====

List of modules

{if count($modules)} {assign $count = count($modules)} {for $i=0; $i<$count;$i++}
{$modules[$i]}
{/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 - defining an events.xml file, describing the name of the event and the response classe, so events.xml is the link - defining of the listener itself. ===== events.xml file ===== 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 ([[en:tutorials:modules:generic1|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 =====

{$moduleInfo['name']}

{@hfnuadmin~hfnuabout.about.version@} :
{$moduleInfo['version']} ({@hfnuadmin~hfnuabout.about.date.create@} {$moduleInfo['dateCreate']})
{@hfnuadmin~hfnuabout.about.label@} :
{$moduleInfo['label']|escxml}
{@hfnuadmin~hfnuabout.about.desc@} :
{$moduleInfo['desc']}
{@hfnuadmin~hfnuabout.about.notes@} :
{$moduleInfo['notes']}
{@hfnuadmin~hfnuabout.about.licence@} :
{if $moduleInfo['licenceURL'] != ''}{$moduleInfo['licence']}{else}{$moduleInfo['licence']}{/if}
{@hfnuadmin~hfnuabout.about.copyright@} :
{$moduleInfo['copyright']}
{foreach $moduleInfo['creators'] as $author}
{@hfnuadmin~hfnuabout.about.authors@} :
{if $author['email'] != ''}{$author['name']|escxml}{else}{$author['name']|escxml}{/if}
{/foreach}
{@hfnuadmin~hfnuabout.about.links@}
{@hfnuadmin~hfnuabout.about.homepageURL@} - {@hfnuadmin~hfnuabout.about.updateURL@}
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 @@C@jEvents::notify()@@ allows to chain of such shares after the registration of a member (such as to send a mail) @@C@jEvents::notify()@@ can also enhance the functionality of a module A via other modules B, C, D without changing the module A, etc ... [[http://jelix.org/articles/en/manual-1.3/events|read more about communication between modules]]