Programowanie
Moduły
Moduł Witaj świecie 2 | Moduł Witaj świecie 2 |
|
|
IntroductionThis tutorial aims to build on the helloworld Module started in part one. You will learn how to retrieve information from the Database and how to present this data is a table using patTemplate. RequirementsYou need for this tutorial:
Let's RollYou will recall that we finished part 1 with your module displaying a message and the current time. This is all very interesting but far more interesting is the information held in the tables of the Joomla database. What we will do in this example is develop a type of Current News module. The Presentation LayerLet's start with the output of the module. Because the code (or the logic) of the module is now separated from the presentation layer (the html), it gives the graphic designer an opportunity to design the output separate from the module. The module code designer has informed us that a list of content items will be provided to the template. The Content Item data will include the Item id, the Title of the item, and the number of hits the item has received. Now, open the <mos:comment> @version 1.0 @package HelloWorld @copyright (C) 2005 Andrew Eddie @license http://www.gnu.org/copyleft/gpl.html GNU/GPL </mos:comment> <mos:tmpl name="helloworld"> <h1>Hello World</h1> This is the latest and the greatest from <strong>{SITENAME}</strong> <table> <tr> <th> Title </th> <th> Hits </th> </tr> <mos:tmpl name="rows"> <tr> <td> {ROW_TITLE} </td> <td> {ROW_HITS} </td> </tr> </mos:tmpl> </table> </mos:tmpl> As in part 1, we wrap our whole module output in a template that we've named helloworld. It all looks like standard HTML except for a few things. You'll see we display a message with the The other thing you'll notice is an embedded template that we have named rows. It enclosed a single row of the HTML table. The module designer has told us that he has prefixed all variables in the rows template with "row_", and that he has provided at least the Title, which therefore maps to Well, that's the template finished (is it that easy I hear you say?). The Data LayerLet's move back to the module file, <?php /** * @version 1.0 $ * @package HelloWorld * @copyright (C) 2005 Andrew Eddie * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL */ /** ensure this file is being included by a parent file */ defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' ); // COLLECT DATA // assemble query $query = ' SELECT id, title, hits FROM #__content ORDER BY hits DESC LIMIT 5' ; // prepare the query in the database connector $database ⇒ setQuery( $query ); // retrieve the rows as objects $rows = $database ⇒ loadObjectList(); // DISPLAY DATA // load the patTemplate library require_once( $mosConfig_absolute_path . '/includes/patTemplate/patTemplate.php' ); // create the template $tmpl =& patFactory::createTemplate( '', false, false ); // set the path to look for html files $tmpl ⇒ setRoot( <a href="http://www.php.net/dirname">dirname</a> ( __FILE__ ) . '/mod_helloworld' ); // load the template $tmpl ⇒ readTemplatesFromInput( 'default.html' ); // add the 'rows' to the rows template with a prefix $tmpl ⇒ addObject( 'rows', $rows, 'row_' ); // output the template $tmpl ⇒ displayParsedTemplate( 'helloworld' ); ?> Let's examine what's happening here:
That's it for the first half of the module, that is, the collecting of the data. The last half of the module is much that same as our original module from part 1. The only difference is that we use the Note: the addObject method can take either a single object or an array of objects.The thing to note here is that for each array member in
What's wrong with this picture. Well, you'll see that the query takes no account for publishing dates, whether they are indeed Publish(ed) at all and the security level of the items. You have to apply that logic yourself. To do this, modifying the query variable in the following way: // assemble query <a href="http://www.php.net/global">global</a> $mosConfig_offset; $now = <a href="http://www.php.net/date">date</a>( 'Y-m-d H:i:s', <a href="http://www.php.net/time">time</a>() + $mosConfig_offset * 3600 ); $query = ' SELECT id, title, hits FROM #__content WHERE ( state = \'1\' AND checked_out = \'0\' ) AND ( publish_up = \'0000-00-00 00:00:00\' OR publish_up <= \'' . $now . '\' ) AND ( publish_down = \'0000-00-00 00:00:00\' OR publish_down >= \'' . $now . '\' ) AND access <= \'' . $my ⇒ gid .'\' ORDER BY hits DESC LIMIT 5' ; In the next part of this series, we will look at adding Parameters to the module. You can download the files for the final part of the tutorial here (mod_helloworld_2.zip). |
|
| Zmieniony ( 19.06.2007. ) |
| « poprzedni artykuł | następny artykuł » |
|---|






Save all files and refresh your Browser. You should see that the module now displays an opening message that includes the name of your site, and also a table of the most hit content items.