Classes for category plugin

Creating and modifying plugins.
Post Reply
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Classes for category plugin

Post by Don Chambers »

Any chance there could be classes for categories by depth in the sidebar category plugin?? I just put together a custom tpl for the categories, but would rather see s9y crank out classes as follows:

a) category_depth0 (parent), category_depth1 (first child), etc...
b) a last and first item class, possibly for each depth level (margin and padding issue, particularly for child classes).

I did the above with smarty in a tpl, but thought it might be nice to have this generated by s9y so I do get the performance hit from the tpl.

You ARE looking for ideas for 1.3, right Garvin??!! :lol: :lol: :lol:
=Don=
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Classes for category plugin

Post by garvinhicking »

Hi!

IMHO this just unnecessarily bumps the HTML volume. I don't see a huge usability inrease for that - if templates need it, they could specify their own categories smarty template file?

What would be the benfit of it? I'm willing to debate :)

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post by Don Chambers »

Because I want to do something like this:

Parent0 Category
Child 1 category
Child 2 category
Child 3 category

//space - accomplished with bottom margin or padding on last Child item

Parent1 Category
Child 1 category
Child 2 category
Child 3 category


I didn't think it would be that big of a deal - the depth is already being calculated as it then sets left padding to 6px for each increment in depth. So left padding is 0 for parent, 6px for Child 1, 12px for child 2, etc. "All categories" already has its own class.
=Don=
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

Would you maybe have the time and motivation to provide a patch for this to the include/plugin_internal.inc.php file? You can commit that to the 1.2 and 1.3 branch, I'd appreciate it a lot. And yes, I do now see the importance of it. :)

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post by Don Chambers »

I have the category depth worked out in the plugin as follows:

Code: Select all

$html .= '<li class="category_depth'. $cat['depth']. '" style="display: block;">';
No big mystery there.

I also added this:

Code: Select all

$categories[$cid]['catdepth']       = $cat['depth'];
So I can do this in the tpl:

Code: Select all

<li class="category_depth{$plugin_category.catdepth}" style="display: block;">
What I cannot seem to figure out is the last item for the child categories so I can emit a second class, like category_depth1_last. I would also need a smarty {if $plugin_category.last_item}.....

Ideas?
=Don=
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post by Don Chambers »

It dawned on me that isolating a specific category might be handy in some instances too, and for maximum understanding if styled in css, I used the category name (not the id). So the php and tpl codes, respectively, look like this now:

Code: Select all

$html .= '<li class="category_depth'. $cat['depth']. ' category_' . $cat['category_name'] .'" style="display: block;">';

<li class="category_depth{$plugin_category.catdepth} category_{$plugin_category.category_name}" style="display: block;">
Still have not come up with a solution for the last <li> per depth..... assuming that idea even has relevance.
=Don=
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

Fetching the last depth in PHP is hard, because you would need to iterate over the resultset all over again. If you do that, you have nearly the same performance impact as when using Smarty templating. So this cannot be done...

Code: Select all

$html .= '<li class="category_depth'. $cat['depth']. ' category_' . $cat['category_name'] .'" style="display: block;">';[/quote]

You can't use that because category_name can contain invalid characters. You would need to strip out all invalid characters like , . " ! in the category name. I can't really recommend that, so you should use categoryid instead.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post by Don Chambers »

What about htmlspecialchars($cat['category_name'] ???

or do you think the id is still better for some reason, and if so, am I correct in assuming I would need to add something like this to use it in the tpl?

$categories[$cid]['category_id'] = $cat['categoryid'], or can I already fetch it in smarty without adding that?
=Don=
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

HI!
Don Chambers wrote:What about htmlspecialchars($cat['category_name'] ???
That would introduce blanks and % signs, both invalid for CSS classnames.

BTW: The foreach loop wouldn't work for category childs, because "islast" is only set for the last category of all categories. There are no subloops for parent categories!
or do you think the id is still better for some reason, and if so, am I correct in assuming I would need to add something like this to use it in the tpl?
The ID would always only be a number :)
$categories[$cid]['category_id'] = $cat['categoryid'], or can I already fetch it in smarty without adding that?
you should be able to retrieve that in smarty already, yes. Check <pre>{$category|@print_r}</pre>

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post by Don Chambers »

It also occurs to me that if 2 otherwise different category names each had the same positioned character stripped, they would return the same name after htmlspecialchars.....

Anyway, I committed it to both 1.2 branch and trunk.
=Don=
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post by Don Chambers »

As long as we are on the subject, how could I select, from a drop down list, a category id# and set it to a template option variable?
=Don=
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi Don!
Don Chambers wrote:As long as we are on the subject, how could I select, from a drop down list, a category id# and set it to a template option variable?
This should work something like that (config.inc.php snippet):

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'];
  }
}

...

$template_config = array(
 ...
    array(
      'var'           => 'specialcat',
      'name'          => 'Category',
      'type'          => 'select',
      'default'       => '',
      'select_values' => $catsel
    )
...
);
...
Then you should have a {$template_option.specialcat} smarty variable.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post by Don Chambers »

Thanks Garvin - and if I want to set 3 different template variables to cagegories, I only need the GET once, correct?
=Don=
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

Yes, you can simply put $catsel to the other config variable items where you want a selector.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Post Reply