18.4. Modifying the PHP-Nuke Reviews module

Figure 18-3. Administration panel: Reviews.

Administration panel: Reviews.

In this section we modify the PHP-Nuke Reviews module:

18.4.1. How to allow only registered users to enter a review

If you want only your registered users to be able to enter a review, you can achieve it with the following simple change:

Edit the modules/Reviews/index.php file and find the write_review() function:

function write_review() {
    global $admin, $sitename, $user, $cookie, $prefix, $user_prefix, 
    $currentlang, $multilingual, $dbi, $module_name;
    include ('header.php');
    OpenTable();

Add the following check after the call to OpenTable():

    if (!is_user($user)) {
        echo "You need to be 
        <a href=\"modules.php?name=Your_Account\">logged in</a> or 
        <a href=\"modules.php?name=Your_Account&op=new_user\">become a member</a> 
        to submit reviews.";
    } else {

This check will only allow registered users to continue with a review, while pointing others to the login or register page. Of course, the IF statement has to be closed - just put a } at the end of the function, as shown below:

    CloseTable();
    include ("footer.php");
}
}

Unregistered users will be able to view reviews, but only site members will be able to submit new ones.

TipTip
 

This simple check can be used to restrict access to registered users in any module that does not offer this functionality, not only in Reviews.

18.4.2. How to choose images from a dropdown list

Isn't it more comfortable being able to choose the image for your review (if you are an administrator) from a dropdown list that offers the names of all available images in your image directory, rather than having to enter the full URL to the image each time? You could easily incorporate this functionality if you just cut and pasted the code from the admin/modules/topics.php file. The functionality is in the Topics module already - you don't need to write full URLs to the topics icons, do you?

The relevant part in the Reviews to introduce the code from the Topics module is the following code in modules/Reviews/index.php:

    if(is_admin($admin)) {
        echo "
        <b>"._RIMAGEFILE.":</b><br>
        <input type=\"text\" name=\"cover\" size=\"40\" maxlength=\"100\"><br>
        <i>"._RIMAGEFILEREQ."</i><br><br>
        ";
    }
Replace it with:
    if(is_admin($admin)) {
        echo "
        <b>"._RIMAGEFILE.":</b><br>
        echo "<select name="cover">";
        $path1 = explode ("/", "images/reviews/");
        $path = "$path1[0]/$path1[1]";
        $handle=opendir($path);
        while ($file = readdir($handle)) {
          if ( (ereg("^([_0-9a-zA-Z]+)([.]{1})([_0-9a-zA-Z]{3})$",$file)) 
            AND $file != "AllTopics.gif") {
            $tlist .= "$file ";
          }
        }
        closedir($handle);
        $tlist = explode(" ", $tlist);
        sort($tlist);
        for ($i=0; $i < sizeof($tlist); $i++) {
          if($tlist[$i]!="") {
            if ($topicimage == $tlist[$i]) {
              $sel = "selected";
            } else {
              $sel = "";
            }
            echo "<option name="cover" value="$tlist[$i]" $sel>$tlist[$i]n";
          }
        }
        echo "</select><br>";
        echo "<i>"._RIMAGEFILEREQ."</i><br><br>
        ";
    }

The part of code that was stolen from the Topics module (from the admin/modules/topics.php file) is between the blank lines in the above code. It will offer you a dropdown list of all images in the images/reviews folder. Just like with the topics images, you must use all small letters and no underscores or other special characters. See choose image for review.