17.2. How to build custom module blocks

Figure 17-1. The standard Modules block

The standard Modules block

A module block is a block that displays links to all available modules of a PHP-Nuke site (Figure 17-1). It is the block that appears at the top of the left column, with the standard title “Modules”. The module block is a standard PHP-Nuke block, for its construction you have to follow the same rules as for any other block (see Section 20.2). It is thus fairly easy to construct your own module block, if you are not satisfied with the standard one. Below are some examples:

17.2.1. Simple module block

The links that are inside the standard PHP-Nuke module block are constructed dynamically, using information that is available about the modules in the database. For example, to present only active modules in the links, the block (located in blocks/block-Modules.php) checks the “active” field of the $prefix_modules table (which is table nuke_modules, if $prefix is left to the standard “nuke” in config.php, see Section 3.7):

$sql = "SELECT title, custom_title FROM ".$prefix."_modules 
WHERE active='1' AND inmenu='0' ORDER BY title ASC"; 

As we can easily read in the SELECT query above, only modules with the “active” bit set to 1 are selected. But we also see that the order in which they are selected is the standard ascending lexicographic order of the modules' title (ORDER BY title ASC). If we wish a different ordering, we are left with only a few possibilities:

But if we wish a custom grouping of the modules links, we will have to write our own modules block. You can use the following code as a starting point for your own creations. This script will display certain links only when an admin or user is logged in. Just name it block-menuSample.php and put in the blocks folder, deactivate the block-Modules.php and activate this one (see Different links - some for registered users only and How would I add a clickable link in a Modules Block?):

<?php
if (eregi("block-menuSample.php",$PHP_SELF)) {
Header("Location: index.php");  
die(); 
}
Global $user, $admin; 
$title = "Main Menu";
$content = "
<font size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\">
<B><u>Navigation</u>
<BR> 
<STRONG><BIG>·</BIG></STRONG> 
<a href=index.php>Start Page</a><br><br>
<u>Information</u><BR>
<STRONG><BIG>·</BIG></STRONG> 
<A href=\"modules.php?name=News&file=index\">News</A><BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=aboutus\">About us</A><BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=somefolder&file=index\">Entry with Some word</A><BR>
<BR>
<u>Other things</u>
<BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=Web_Links
&amp;file=index&amp;l_op=viewlink&amp;cid=1\">Links</A><BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=My_eGallery\">All sorts of Pictures</A><br>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=Reviews\">Reviews</A><BR>
<BR>
<u>Interact</u>
<BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=GuestBook\">Guestbook</A><BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=Feedback\">Feedback</A><BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=Recommend_Us\">Recommend Us</A><BR>
<BR>
<u>Misc.</u>
<BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=Web_Links\">Links</A><BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=FAQ\">FAQ</A><BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=Web_Links
&amp;file=index&amp;l_op=AddLink\">Add A Link</A><BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=Downloads\">Downloads</A><BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=Statistics\">Site Statistics</A><BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=HeadLines\">News Headlines</A><BR>
<BR>
//******************************************
//Below is for users and admins ONLY!!! 
//******************************************
<u>User Control Panel</u>
<BR>";  
if (!IsSet($user))
{;
$content .= "
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=Your_Account\">Reg. Users Login. </A><br>";
}
if ((IsSet($user)) or (IsSet($admin)))
{;
$content .= "
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=Your_Account\">Your Account</A><BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=Your_Account&amp;op=edituser\">Edit Profile</A><BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=Members_Photo_Upload\">Members Photo upload</A><BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=Members_List\">View Member List</A><BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=Your_Account&amp;op=logout\">logout</A><BR>
<BR>
<u>Private Messages</u>
<BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=Private_Messages\">View Messages</A><BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=Private_Messages
&amp;file=reply&amp;send=1\">Compose A Message</A><BR>
";
};
//***************************************
//ENDS HERE
//***************************************
$content .= "
<BR>
<u>Send Content</u>
<BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=Submit_News\">Submit News</A><BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=Reviews&amp;rop=write\">Write A Review</A><BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=Web_Links
&amp;file=index&amp;l_op=AddLink\">Add A Link</A><BR>
<STRONG><BIG>·</BIG></STRONG>
<A href=\"modules.php?name=My_eGallery
&amp;file=index&amp;do=upload\">Add images to the Gallery</A><BR>
<BR>
</font>
</B>
<HR size=3>
";
?>

17.2.2. Treemenu block

A commonly asked question in PHPNuke forums (see for example Menu Hack Needed (show different menu or block depending on the category)), is if there exists some Module block out there that displays a dynamic view of the available modules. The webmaster needs a functionality that will allow for the display of only certain module links, depending on, say, the interests, access level, or preferences of the viewer.

A typical Module block, as shipped with the standard PHPNuke package, looks as in Figure 17-2.

Figure 17-2. The standard Modules block.

The standard Modules block.

What we need instead is a module block that displays some links (to modules, or generally, to pages) when a certain condition is met (say, when Category A was previously chosen) and some other ones when a different condition is true (e.g. when Category B was chosen).

17.2.2.1. The general idea

To met the above requirements, the Treemenu block for PHP-Nuke adapts the well-known Treemenu concept to a PHPNuke Block and creates a PHPNuke block containing a Treemenu which looks as in Figure 17-3.

Figure 17-3. Treemenu Block.

Treemenu Block.

When the user clicks on an item like “Links”, the subtree under this item is unfolded (Figure 17-4).

Figure 17-4. Treemenu Block with “Links” expanded.

Treemenu Block with “Links” expanded.

A further click on the subcategory “My work” will reveal another level of groups (Figure 17-5).

Figure 17-5. Treemenu Block with “My work” expanded.

Treemenu Block with “My work” expanded.

Finally, a click on a subsubcategory like “Linux” will unfold the “leaves” of the Treemenu (Figure 17-6).

Figure 17-6. Treemenu Block with “Linux” expanded.

Treemenu Block with “Linux” expanded.

Of course, the naming and nesting of categories and/or items is fully arbitrary. The above functionality can easily be adapted to suit more advanced needs too (see refinements of the PHP-Nuke Treemenu).

17.2.2.2. What is Treemenu

The Treemenu block makes use of a Treemenu in a PHPNuke Block. Treemenu is a PHP class created by Bjorge Dijkstra (original script to be found Treemenu Class from Bjorge) and adapted by Denny Shimkoski (his version to be found in Treemenu from Denny). Chris have fixed some bugs in the latter one and integrated it into a Treemenu block for PHP-Nuke.

There are two ways you can use the Treemenu class - Chris uses the one that takes as input a simple text file and creates a tree menu, in a style that most users are familiar with from a graphical file manager (see Figure 17-3). Navigation through such a tree is done intuitively by expanding and collapsing the various tree levels by clicking on the node icons (the icons are discussed in custom node icons for the PHP-Nuke Treemenu).

To create a Treemenu, once you have written the input text file with your favorite text editor (see input file method for the Treemenu) or used the alternative method to fill the nodes and leaves of the Treemenu, you just have to write this 3-liner to get it up and running:

include("blocks/treemenu/treemenu.inc");
$tree = new TreeMenu("a", "blocks/treemenu/sitemap.txt");
$tree->show();

NoteNote
 

In order to be able to display the output of the above code in a PHPNuke Block, we have to capture it in an output buffer with the ob_start(), ob_get_contents() and ob_end_clean() mechanism:

ob_start();
include("blocks/treemenu/treemenu.inc");
$tree = new TreeMenu("a", "blocks/treemenu/sitemap.txt");
$tree->show();
$output = ob_get_contents();
ob_end_clean();
$content = $output;

See Treemenu block for PHP-Nuke for more information of how to use the Treemenu block as a module block for PHP-Nuke.