Page 1 of 1
Category selection in config.inc.php
Posted: Mon Mar 02, 2009 11:03 am
by yellowled
I've been using this code by Judebert for quite some time to be able to select a or more categories in the theme options:
Code: Select all
if ($serendipity['GET']['adminModule'] == 'templates') {
$all_cats = serendipity_fetchCategories('all');
$categories = serendipity_walkRecursive($categories, 'categoryid', 'parentid', VIEWMODE_THREADED);
$catsel = array();
foreach($all_cats AS $cat) {
$catsel[$cat['categoryid']] = str_repeat(' ', $cat['depth']) . $cat['category_name'];
}
}
Can (and if so, how?) this be modified to not only emit the name of a category, but also the name of its parent category, if any? Right now, it just emits "catname" - I'd like it to emit at least "parent/catname", maybe even "parent/child/catname", if possible.
YL
Re: Category selection in config.inc.php
Posted: Mon Mar 02, 2009 11:53 am
by garvinhicking
Hi!
I can not supply you with code right now, but basically you can iterate the $all_cats array once again and store the parentid there, and later access it.
Sorry I can't be of much more help right now.
Regards,
Garvin
Re: Category selection in config.inc.php
Posted: Mon Mar 02, 2009 12:03 pm
by yellowled
garvinhicking wrote:I can not supply you with code right now, but basically you can iterate the $all_cats array once again and store the parentid there, and later access it.
Sorry I can't be of much more help right now.
Take your time, this is not exactly urgent since I have already set up some kind of workaround.
YL
Re: Category selection in config.inc.php
Posted: Mon Mar 02, 2009 5:28 pm
by judebert
Okay, that array should have a 'depth' item for each category. We might be able to do this without recursing. It'll cost us a few variables, though.

I've added "fullname" to keep track of all the category names up to this point; "lastdepth" to check for backing up one or more categories; and "catdepth" for convenience and performance.
I also assume you don't want the repeated spaces any more.
Code: Select all
if ($serendipity['GET']['adminModule'] == 'templates') {
$all_cats = serendipity_fetchCategories('all');
$categories = serendipity_walkRecursive($categories, 'categoryid', 'parentid', VIEWMODE_THREADED);
$catsel = array();
$fullname = array();
$lastdepth = 0;
foreach($all_cats AS $cat) {
$catdepth = $cat['depth'];
$fullname[$catdepth] = $cat['category_name'];
if ($lastdepth > $catdepth) {
// We've just backed out of a child category; we don't need such a long fullname
$fullname = array_slice($fullname, 0, $catdepth + 1);
}
$lastdepth = $catdepth;
$catsel[$cat['categoryid']] = implode('/', $fullname);
}
}
Let me see, did I miss any semicolons? No? Good. Give that a shot, tell me if it works.
Re: Category selection in config.inc.php
Posted: Mon Mar 02, 2009 6:44 pm
by yellowled
judebert wrote:Give that a shot, tell me if it works.
I'm not saying it doesn't work - maybe it just doesn't do what it's supposed to do
I'm not sure I've been clear about what I need here. Let's assume you have a category structute like this:
Code: Select all
* Category A
* News
* Dates
* Photos
* Category B
* News
* Dates
* Photos
which is, more or less, what I actually have in this case. Now you want to use said code to select a category in the theme options from a select box to emit articles from a specific category without having to hardcode the category id.
Both the old and new code give me a select box like this:
Code: Select all
Category A
Category B
Dates
Dates
News
News
Photos
Photos
which makes it next to impossible to select a category. What I'd need would be something like this in the select box:
Code: Select all
Category A
Category A/Dates
Category A/News
Category A/Photos
Category B
Category B/Dates
Category B/News
Category B/Photos
I don't really care about formatting or ordering here - all I need is a way to distinguish one Dates category from the others, since I'm dealing with about 20 of each in the "real" site.
YL
Re: Category selection in config.inc.php
Posted: Mon Mar 02, 2009 10:15 pm
by Don Chambers
FWIW - I seem to remember looking into something like this before, and the logic falls apart at the grandchild level... ie, each category knows only ITS parent.
Re: Category selection in config.inc.php
Posted: Mon Mar 02, 2009 10:35 pm
by yellowled
Don Chambers wrote:each category knows only ITS parent.
That would do the trick.
YL
Re: Category selection in config.inc.php
Posted: Tue Mar 03, 2009 9:17 pm
by judebert
yellowled wrote:judebert wrote:Give that a shot, tell me if it works.
I'm not saying it doesn't work - maybe it just doesn't do what it's supposed to do
<snip>
What I'd need would be something like this in the select box:
Code: Select all
Category A
Category A/Dates
Category A/News
Category A/Photos
Category B
Category B/Dates
Category B/News
Category B/Photos
That's pretty much what the new code should've given you. The old code should've given you a listing without the parent category names but with indentation.
I'm finding it especially odd that you're getting the categories in alphabetical order.
Unfortunately, to track this down, we're going to need to do some debugging. Can you tell me a plugin or template where the code fails, so I can have a look at it locally? (I suppose I could jam it in an existing location, but I'm just so busy/lazy...)
Meanwhile, let's add some debugging output to liven things up:
Code: Select all
if ($serendipity['GET']['adminModule'] == 'templates') {
$all_cats = serendipity_fetchCategories('all');
$categories = serendipity_walkRecursive($categories, 'categoryid', 'parentid', VIEWMODE_THREADED);
$catsel = array();
$fullname = array();
$lastdepth = 0;
foreach($all_cats AS $cat) {
$catdepth = $cat['depth'];
$fullname[$catdepth] = $cat['category_name'];
if ($lastdepth > $catdepth) {
// We've just backed out of a child category; we don't need such a long fullname
$fullname = array_slice($fullname, 0, $catdepth + 1);
}
$lastdepth = $catdepth;
$catsel[$cat['categoryid']] = implode('/', $fullname) . '@' . $catdepth;
}
}
I just added "@$catdepth" to the end of the output, because I'm guessing that there is no category depth assigned. If depths are assigned, your output should be otherwise unaltered, so you should see:
Code: Select all
Category A@0
Category B@0
Dates@1
Dates@1
News@1
News@1
Photos@1
Photos@1
Re: Category selection in config.inc.php
Posted: Tue Mar 03, 2009 9:34 pm
by yellowled
judebert wrote:That's pretty much what the new code should've given you. The old code should've given you a listing without the parent category names but with indentation.
In fact, both give me all categories in alphabetical order without any indentation.
judebert wrote:Can you tell me a plugin or template where the code fails, so I can have a look at it locally?
I'm pretty sure I used the old code in the Mimbo template to be able to select categories for lead and featured articles in the theme options, which is exactly the same purpose I'm using it for now.
judebert wrote:Meanwhile, let's add some debugging output to liven things up:
Thnaks, I'll see what I can do to check that code asap. Might take some time, though, it's already pretty late over here
YL
Re: Category selection in config.inc.php
Posted: Tue Mar 03, 2009 9:47 pm
by yellowled
judebert wrote:I just added "@$catdepth" to the end of the output, because I'm guessing that there is no category depth assigned. If depths are assigned, your output should be otherwise unaltered, so you should see:
Code: Select all
Category A@0
Category B@0
Dates@1
Dates@1
News@1
News@1
Photos@1
Photos@1
Hm.
I'm getting a list of all categories in alphabetical order without any indentation, but with an @ at the end.
YL
Re: Category selection in config.inc.php
Posted: Tue Mar 03, 2009 10:07 pm
by judebert
Sorry; indentation was unintentional (stupid copy-and-paste!).
So there's an @ sign, but no depth number at the end? Sounds like serendipityWalkRecursive isn't assigning a depth, and returning items in alphabetical order. I'll start checking there.
Re: Category selection in config.inc.php
Posted: Tue Mar 03, 2009 11:27 pm
by yellowled
judebert wrote:So there's an @ sign, but no depth number at the end?
That's exactly what I'm getting here. Thanks again for looking into it
YL
Re: Category selection in config.inc.php
Posted: Wed Mar 04, 2009 4:14 pm
by judebert
Oh, I just don't believe I am so dense. But there it is in the code; there's no denying it.
YL, I apologize for leading you astray all this time. The problem is simple: I never actually walked the categories, and I never actually used the result anyway. The categories are in $all_cats, while the walking is performed on (and stored in) $categories, which is never populated and never used.
I tested this on Mimbo running in a 1.4.2 sandbox. It works; the   is non-functional in drop-downs, so I did the path/separator thing. Let me know if it works the way you like.
Code: Select all
if ($serendipity['GET']['adminModule'] == 'templates') {
$all_cats = serendipity_fetchCategories('all');
$all_cats = serendipity_walkRecursive($all_cats, 'categoryid', 'parentid', VIEWMODE_THREADED);
$catsel = array();
$fullname = array();
$lastdepth = 0;
foreach($all_cats AS $cat) {
$catdepth = $cat['depth'];
$fullname[$catdepth] = $cat['category_name'];
if ($lastdepth > $catdepth) {
// We've just backed out of a child category; we don't need such a long fullname
$fullname = array_slice($fullname, 0, $catdepth + 1);
}
$lastdepth = $catdepth;
$catsel[$cat['categoryid']] = implode('/', $fullname);
}
}
If you'd rather have a different separator, just change the '/' to ':' or whatever you like.
If you'd rather have an indented version, this works in drop-downs:
Code: Select all
if ($serendipity['GET']['adminModule'] == 'templates') {
$all_cats = serendipity_fetchCategories('all');
$all_cats = serendipity_walkRecursive($all_cats, 'categoryid', 'parentid', VIEWMODE_THREADED);
$catsel = array();
foreach($all_cats AS $cat) {
$catsel[$cat['categoryid']] = str_repeat('-', $cat['depth']) . $cat['category_name'];
}
}
I used a '-' for the repeating character because some browsers render HTML (and HTML escaped characters) in their drop-downs, and others don't. The dash should provide some indentation in all browsers, won't be collapsed by HTML, and will even allow users to count the indentation in most browsers. If you were using this same thing in a sidebar, an ' ' would provide invisible indentation.
Let me know if this works for you.
Re: Category selection in config.inc.php
Posted: Wed Mar 04, 2009 6:36 pm
by Don Chambers
I tested both of these - either version works just fine... I prefer the nested display as the select/dropdown input box can otherwise grow quite wide with grandparent/parent/child names listed on a single line.
I have never personally encountered being collapsed in a drop down, but have never tested it across numerous browsers either.
Re: Category selection in config.inc.php
Posted: Wed Mar 04, 2009 11:39 pm
by yellowled
judebert wrote:YL, I apologize for leading you astray all this time.
No need for an apology, Jude. The old code worked perfect in my test scenario for Mimbo because I usually don't nest categories myself. I only came across this because I have to use nested categories in a client's site now.
And the code works perfectly fine, thanks a lot
YL