I'm sure this is reinventing the wheel, but I couldn't find any prior art, so I'll
post this in case it helps other Midgarders.
I am not happy with the way that a page has to be linked to a topic solely by
hardcoding with the topic ID.
You know the drill -- mgd_get_topic(17) -- you first have to know what ID the topic
has that you're interested in. So I got to thinking -- why not create a topic tree
that matches the names of your page tree, and use the page name to look for a match
with the topic names? Anyway, I soon ran into a problem, but found an ugly
workaround, so I'm posting the code for feedback.
Specifically, I've got it working to two levels of depth. My problem is that the
mgd_get_topic_by_name requires the ID of the "up" record, i.e., the parent topic.
So, when looking at the top level, this will be zero, but below that, you need to know
where you are in the topic tree. The only solution I could think of was to
parse the URI, and look at the basename of the dirname, then assume that's top level.
Can anyone tell me a more elegant way to find the place you are in a topic tree
based on the URI (other than recursively parsing upwards) or perhaps a mechanism for
searching for topics by name without having to know the parent topic?
Here's my code:
<?
if ($article) {
?>
<[article]>
<?
} else {
// This code will try to find a topic with the same name as
// the current page. In some cases, we are not on a page
// with that name, but can use the page above this one,
// which probably has a topic too.
// We need to get the topic object, which we try to get from
// the basename. Unfortunately, to get the object with
// get_topic_by_name we also need to give the topic id
// of the "up" (parent) topic. To get the parent, we assume
// that the dirname above the current basename is a top-level
// topic. (This limits this approach to only two levels,
// unless we recursively look at each path component.
$debug = 1;
if ($topic > 0) { // has this been passed to us as a number?
if ($debug)
echo "<p>Passed topic id as parameter: $topic</p>\n";
$topicobj = mgd_get_topic($topic);
} else {
// otherwise, try to deduce topic name from basename of this page
if ($debug) {
$basename = basename($REQUEST_URI);
$dirname = dirname($REQUEST_URI);
$upname = basename($dirname);
echo "<p>basename is $basename</p>\n";
echo "<p>dirname is $dirname</p>\n";
echo "<p>upname is $upname</p>\n";
}
$topicobj = mgd_get_topic_by_name($topic,$basename);
$parenttopic = mgd_get_topic_by_name(0,$upname);
}
// If there's no topic object, then try to find it from the current
// parent's list of subtopics
if (!$topicobj) {
if ($debug)
echo "<p>Looking for topic with parent id of " . $parenttopic->id . " and name
of " . $basename . "</p>";
$topicobj = mgd_get_topic_by_name($parenttopic->id,$basename);
}
if ($topicobj) {
echo "<dl><dt>Topic: " . $topicobj->name . "<dd> " . $topicobj->description .
"</dl>";
$article = mgd_list_topic_articles( $topicobj->id, "score" );
if ( $article->N == 0 ) {
if ($debug)
echo "<p>No articles for this topic</p>";
} else {
echo "<ul>\n";
while( $article->fetch() ) {
echo "<li><a href='$PHP_SELF?article=" . $article->id . "'>" . $article->title
. "</a></li>\n";
}
echo "</ul>\n";
echo "$article->N article", ( $article->N == 1 ) ? '' : 's';
}
echo "<h4>Subtopics</h4>\n";
// Now let's try to find any subtopics
if ($debug) {
echo "<p>Looking for subtopics under id " . $topicobj->id . "</p>";
}
$lst = mgd_list_topics($topicobj->id);
if ($lst) {
while($lst->fetch()) {
echo "<a href='$PHP_SELF" . $lst->name . "'>" . $lst->name . "</a><br>\n";
}
}
}
}
?>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]