Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2007-11-28 05:30:22

mhulse
Plugin Author
From: Eugene Oregon
Registered: 2005-01-21
Posts: 200

[v.0.2.5] mah_lib_category_array: Return array of non-empty categories

mah_lib_category_array()

Library function that returns an array of non-empty “article”, “link”, “image”, or “file” categories.

Note: This code is just a hacked/modified version of cbs_category_list v0.9 by Christophe Beyls http://www.digitalia.be. A BIG thanks goes out to Christophe.

ABOUT

  • mah_lib_category_array() is a section-sensitive function returns an array of non-empty “article”, “link”, “image”, or “file” categories.
  • Categories are returned using their Title or Name and sorted alphabetically by Title.
  • For articles: If no article is found in the section for a category, this category will not be included.
  • For links, images, and files: If a category contains no data, this category will not be included.

SYNTAX

  • mah_lib_category_array()

ATTRIBUTES

REQUIRED
  • type: Category types, you must choose between “article”, “link”, “image”, or “file”.
OPTIONAL
  • format: Determines the return value type. “name” will return the categories in an array by name. The default is “title”, and will return the categories in an array by title.
  • parent: The parent category name. If specified, will return only the direct children categories for this parent. It will not show children of children. Default is any category.
  • posted: (“article” and “file” categories only) Either “past”, “future” or “all”. Will only count articles with the specified publication date. “all” is slightly faster because MySQL will be able to cache the query. Default is “past”.
  • sticky: (“article” categories only) When set to true, will display the category list of “sticky” articles instead of “live” articles. Default is false.
  • section: (“article” categories only) The section to display the related article categories. When “default” (the frontpage) is specified, it shows all categories related to sections which are part of the frontpage. Default is the current section.
  • order: “asc” = sort the categories in alphabetical ascending order. (Alphabetical descending, based on category title, is default.)
  • entry_order: (“article” categories only) “true” = sort categories based on entry date of last modified article in said category. (Alphabetical descending, based on title, is default.)

EXAMPLE USAGE:

<txp:php>
$mah_lib_category_array_atts = array('type' => 'article', 'posted' => 'all', 'format' => 'name', 'entry_order' => 'true'); // Setup attributes.
$category_list = mah_lib_category_array($mah_lib_category_array_atts); // Call method.
# Loop through array:
for($i = 0; $i < count($category_list); $i++) {
	$category = $category_list[$i];
	echo '<h2><txp:category name="'.$category.'" link="1" title="1" /></h2>'."\n";
	echo article_custom(array('category' => $category))."\n";
    echo '<hr>';
}
</txp:php>

DOWNLOAD

CHANGE LOG

  • 12/01/07 — Added the ability to specify the type of categories to return.
  • 03/26/08 — 1) Added: safe_pfx() to queries; 2) Ability to order categories in ASC order; 3) Option to sort categories based on latest article entry date in said category. 4) Cleaned-up code.

THANK YOU’S

RELATED LINKS

Last edited by mhulse (2008-03-26 08:59:26)

Offline

#2 2007-11-28 08:12:10

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: [v.0.2.5] mah_lib_category_array: Return array of non-empty categories

It’s a bit unusual as a plugin, because the <txp:mh_category_array /> doesn’t really work. Having an array used in string context is not useful. You may want to turn this into a library plugin instead, so it doesn’t offer a tag.

Offline

#3 2007-11-28 08:30:22

mhulse
Plugin Author
From: Eugene Oregon
Registered: 2005-01-21
Posts: 200

Re: [v.0.2.5] mah_lib_category_array: Return array of non-empty categories

Yah, very good point. My (hacked) plugin would only be helpful when using <txp:php>.

Hmmm, what would be the best way to convert this to a library plugin (not sure where to look for heading that direction)?

Thanks for the feedback Ruud. :D

Also, maybe I could add some functionality to this plugin that would make it more relevant as a tag. I was thinking of adding a parameter that would allow some sort of return value that is actually useful via a simple tag call… So that way one could switch between a return value of an array or direct HTML output? I have to think about this… getting late around my parts. :)

Lol, I am such a noob.

Thanks!
Cheers,
M

Last edited by mhulse (2007-11-28 08:31:25)

Offline

#4 2007-11-28 08:51:58

net-carver
Archived Plugin Author
Registered: 2006-03-08
Posts: 1,648

Re: [v.0.2.5] mah_lib_category_array: Return array of non-empty categories

Micky,

just changing the plugin’s type to ‘2’ should make it a library plugin. Other people would then be able to include your plugin and just call your mh_category_array function directly.

If you wanted to provide a tag that returned valid xhtml then I suggest you write another function that simply calls your original one and then converts the array it gets to some useful xhtml structure — a list of some kind (ordered/unordered/definition as a parameter to the new mh_category_list tag perhaps?)

Good to see someone starting to write plugins!


Steve

Offline

#5 2007-11-28 09:20:55

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: [v.0.2.5] mah_lib_category_array: Return array of non-empty categories

If you turn it into a library, there’s no need to have a tag function, because libraries are meant to be used by other plugins (which would then use your library for their own tags).

Libraries shouldn’t contain TXP tag functions, but it might be a good idea to give an example in the plugin help on how other plugin developers can use your library functions. Posting example tag function code there would be a nice addition.

To indicate that it’s a library, you may want to use a name like mh_lib_category_array

Offline

#6 2007-11-28 10:11:46

mhulse
Plugin Author
From: Eugene Oregon
Registered: 2005-01-21
Posts: 200

Re: [v.0.2.5] mah_lib_category_array: Return array of non-empty categories

Great ideas Steve and Ruud, thanks for the tips and guidance. I really appreciate the help. I am learning a lot. :)

So, I did re-generate the plugin as #2 via the template… I also changed the name… Although, I kinda feel silly calling it a library when it is only one function. :D

It is getting late around these parts, so I think I will hit hay soon and re-visit this tomorrow.

I am planning on doing the things that both of you suggest (writing another function and showing example of how to use via the ‘help’.)

Ruud and/or Steve, just to clarify… Now that I have generated my plugin as type “library” (#2), am I on the right track? In other words, do I now have a valid library plugin or do I have to alter my code further?

Pheeew… Ok, time for sleep. Hehe, I am sure my thoughts will be less cloudy in the morning.

Thanks again Steve and Ruud!

Cheers,
Micky

Offline

#7 2007-11-28 11:27:00

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: [v.0.2.5] mah_lib_category_array: Return array of non-empty categories

There’s no difference in plugin code between a library and a non-library plugin. The difference is mainly that library plugins are not loaded by default, so plugins that rely on have to load the library themselves (TXP has some built in function that plugins can use to do that).

The fact that your library contains just one function is not a problem. Quantity doesn’t matter, quality does ;)

Offline

#8 2007-11-29 18:17:43

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: [v.0.2.5] mah_lib_category_array: Return array of non-empty categories

Something I didn’t notice until now, the plugin prefix should be 3 characters (not 2). There’s a list of registered/claimed prefixes in Textbook.

Offline

#9 2007-12-02 05:05:11

mhulse
Plugin Author
From: Eugene Oregon
Registered: 2005-01-21
Posts: 200

Re: [v.0.2.5] mah_lib_category_array: Return array of non-empty categories

Hey Ruud, thanks for the replies. :)

Sorry to not get back to you sooner… Have been busily working the last few days… I haven’t had a chance to touch my plugin until tonight.

I will change the name very soon. My middle name is Alexander… I think I will change the prefix to MAH.

Also, I have been learning more SQL… I am really starting to enjoy it.

At my current job we use a database called Caché, and I have spent the last few days modifying and creating queries for that… Now, coming back to my TXP database, everything seems much less complex! Haha. :D

Anyway, for my plugin, I am going to add the ability for the user to specify the category type (article, image, link, file)… I figure that will be a nice addition.

Bbs. :)

Thanks again for all of your help.

Cheers,
Micky

Last edited by mhulse (2007-12-02 05:05:50)

Offline

#10 2007-12-02 09:46:40

mhulse
Plugin Author
From: Eugene Oregon
Registered: 2005-01-21
Posts: 200

Re: [v.0.2.5] mah_lib_category_array: Return array of non-empty categories

Plugin updated… Changed name and added the ability to obtain “link”, “image”, and “file” categories.

Now I just need to include an example of how to access my function from other plugins. Will try to do that soon. :)

Cheers,
Micky

Offline

#11 2008-03-26 09:00:41

mhulse
Plugin Author
From: Eugene Oregon
Registered: 2005-01-21
Posts: 200

Re: [v.0.2.5] mah_lib_category_array: Return array of non-empty categories

Plugin (slightly) updated: v.0.2.5

New features:

  • order: “asc” = sort the categories in alphabetical ascending order. (Alphabetical descending, based on category title, is default.)
  • entry_order: (“article” categories only) “true” = sort categories based on entry date of last modified article in said category. (Alphabetical descending, based on title, is default.)

Thanks!
M

Last edited by mhulse (2008-03-26 09:02:40)

Offline

#12 2008-11-27 13:09:11

roelof
Member
Registered: 2005-03-27
Posts: 647

Re: [v.0.2.5] mah_lib_category_array: Return array of non-empty categories

Hello,

I try your plugin to make a menu for my website. I don’t know anything about php so don’t be hard on me.

I have made this :

<div class="pro_linedrop">
<ul class="select">
<txp:php>
$mah_lib_category_array_atts = array('type' => 'article', 'posted' => 'all', 'format' => 'name', 'entry_order' => 'true'); // Setup attributes.
$category_list = mah_lib_category_array($mah_lib_category_array_atts); // Call method.
# Loop through array:
for($i = 0; $i < count($category_list); $i++) {
	$category = $category_list[$i];
	echo '<ul class='select'>'
        echo '<li class='line'>'
        echo '< a href="#nogo"> <b>'.category.'</b>'
}
</txp:php>

But now i get a message that Parse error: syntax error, unexpected T_STRING, expecting ‘,’ or ‘;’ in /home/tamarawobben.nl/test2/textpattern/publish/taghandlers.php(3075) : eval()’d code on line 7.

Can you help me ?

Roelof

Edit : do i understand it right that this must be in the place of category_list and not being used in a form ?

Last edited by roelof (2008-11-27 15:02:45)

Offline

Board footer

Powered by FluxBB