Fine-Tuning Our Jumplist in MODX

If you’ve been following along with this series of articles on using DirWalker to produce a report on MODX System Events and have looked recently at the output here, you may have noticed that the problem of having multiple references to the same event has been solved. This article is for those curious about how it was done.

To review, the problem with our previous code was that when the same event was fired more than once, either in the same file or another one, the same link was produced for each instance. Any link in the jumplist for that event name would take the user to the first instance.

The Solution

In the previous article, we “solved” the problem by calling array_unique() on the array used in the jumplist, but that wasn’t a real solution. It just removed any links to the other instances of the event from the jumplist.

The real solution to the problem is to make each link unique by adding a suffix to each event name, using the name with the suffix for the link and removing the suffix before displaying the event name.

MODX logo

The Code

I won’t burden you with the whole snippet. We’ll just look at the changes necessary to solve the problem. First, in the processFile() method, we add the suffix using PHP’s uniqid() function, which creates a unique number based on the current time (in milliseconds):

'event' => $match[1] . '-' . uniqid(),

The dash is there so we can easily strip the suffix when displaying the output. It’s a suffix, rather than a prefix, so it won’t affect the sorting of the jumplist.

Next, in the section that assembles the jumplist, we use the full name (with the suffix) for the link and the name without the suffix for the displayed event name:

$jumpList = '';
$jumpArray = array();
/* Put the event names in an array so we can sort them */
foreach($files as $file => $events) {
    foreach ($events as $event) {
        $jumpArray[] = $event['event'];
    }
}
sort($jumpArray);

/* Make the jumplist */
foreach ($jumpArray as $link) {
    $eventName = substr($link, 0, strpos($link, '-'));
    $jumpList .= '<a href="[[~[[*id]] ]]#' . $link . '">' .
        $eventName . '</a> &nbsp;&nbsp;';
}

The first foreach loop above creates an array containing the event names with their suffixes. In the second foreach loop, $link is the event name with the suffix, and we set the $eventName variable, which is the event name without the suffix. This line strips the suffix:

$eventName = substr($link, 0, strpos($link, '-'));

In the PHP substr() function, the first argument is the full string being processed, the second is the starting position of the substring we want (in this case, 0), and the third is its length — which we get using strpos(). The strpos() function reports the position the hyphen (counting from 0), which is exactly what we want.

Notice that the $tempArray variable we used in the last version is gone, because we no longer need it.

Finally, we do the same thing with the anchor links in the report and the displayed names of the events:

foreach($events as $event) {
        $l = $event['event']; /* link */
        $e = substr($l, 0, strpos($l, '-')); /* event name */
        $v = $event['variables'];
        if (strpos($v, 'array') !== false) {
            $v = preg_replace('#^\s*\'#m', '<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\'', $v);
            $v = preg_replace('#^\s*\)\);#m', '<br />&nbsp;&nbsp;&nbsp;&nbsp;);', $v);
        } else {
            $v = str_replace(');', '', $v);
        }
        /* Anchor link to event */
       $output .= '<a name="' . $l . '"></a>';
       /* Event name and variables */
       $output .= "<p>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color:teal;\">Event:</span> <b>" . $e . '</b><br />';
       $output .= "&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color:teal;\">Variables:</span> " . $v . '</p>';
    }

We’ve added a variable, $l, which we use for the link target. The variable $e is still the event name, but now we have to get it by stripping the suffix as we did for the jumplist. Otherwise the code is the same.

One last change was made. Since we don’t mind having multiple references to the same event, we search for all .php files instead of just .class files. When looking just in the class files, we miss some events that occur in legacy processors that do not yet have corresponding class-based processors. So now our include line looks like this:

$dw->setIncludes('.php');

Many of the event invocations will appear several times because we’re now processing both the new, class-based processors and the legacy .php processors.

Performance Issues

The new code does slow down the process slightly, but not much. And since the whole page can be cached, it’s not much of a penalty. You may have noticed that every link in the jumplist and every anchor link in the report gets a unique suffix, whether they need it or not. Only repeated events need a unique link. The fact is, though, that it’s faster to create and add a unique link than to figure out whether or not it’s needed. Having a suffix on some event names and not others would also slow down the code that creates the output.


For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray

I am the author of MODX: The Official Guide and over 30 MODX add-on components. I host Bob's Guides, a source of valuable information for MODX users, and I've been very active in the MODX Forums with over 14,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *


2 × = 6

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>