Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-03-03 20:56:46

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

A question for PHP or Javascript masters: generate clean category info

Howdy! Using Manfre’s mem_simple_form plugin, I’ve created a public-side input form that posts a new article category into Textpattern on submit.

Here’s the code that it uses:

<txp:mem_simple_form table="txp_category" id_field="id" id_insert="0">

<txp:mem_form_text name="string_title" label="Name the Category" />

<txp:mem_form_secret name="string_type" value="article" />
<txp:mem_form_secret name="string_parent" value="root" />

<txp:mem_form_secret name="randuid_name" />
<txp:mem_form_secret name="randuid_lft" />
<txp:mem_form_secret name="randuid_rgt" />

<txp:mem_form_submit />
</txp:mem_simple_form>

This form creates an entry in the txp_category table— it gets the category title from user input; type and parent are pre-set.

Right now the name, lft, and rgt fields are generated by MySQL’s random unique id (md5 hash) function. That works for getting the category entered and functional, but it’s a messy way of doing business.

  • Having random string for lft and rgt creates a unique entry for each category posted through the form, but it messes up the category tree in Textpattern’s “Categories” tab under “Content”. This has no effect on the front-end site at this point, but I’d prefer for the new categories to be placed correctly in the tree.
  • Generating the name field randomly creates meaningless category names. This issue isn’t of great concern to me, but I’d love to get generate name as a sanitized version of title.

Is there a way to get javascript or php to generate this information?

Last edited by johnstephens (2009-07-17 18:34:42)

Offline

#2 2009-03-03 23:42:39

rsilletti
Moderator
From: Spokane WA
Registered: 2004-04-28
Posts: 707

Re: A question for PHP or Javascript masters: generate clean category info

lft & rgt are used to organize the display sequence on the admin side by type and parent relationships. I’m a little fuzzy on the details of exactly how it’s done from looking at the code, but I think it would require database info from the category table as a source to add to when you are creating rows.
No 2 numbers for lft or rgt are repeated within a given type so simply appending to what is already present may be a workable solution. The admin side uses them with all display code, so they are required for that.

Last edited by rsilletti (2009-03-04 01:52:25)

Offline

#3 2009-03-04 03:41:16

Mary
Sock Enthusiast
Registered: 2004-06-27
Posts: 6,236

Re: A question for PHP or Javascript masters: generate clean category info

Txp runs rebuild_tree_full('article'); after it inserts a new article category. You can view this function inside txplib_db.php.

Offline

#4 2009-07-17 18:45:32

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: A question for PHP or Javascript masters: generate clean category info

Thanks for your feedback on this! From what I gleaned in your messages, I came up with a temporary solution that worked:

<txp:mem_form_secret name="randuid_name" />
<txp:mem_form_secret name="randuid_lft" />
<txp:mem_form_secret name="randuid_rgt" />

This uses mem_simple_form’s randuid form type to generate a random unique ID. This solution has worked for prototyping the site, but it makes a huge mess of the category tree, as you can imagine.

I revised my initial post to make the question a little more clear— how can I generate clean category info? I’d love any feedback or guidance you could offer. Since php and javascript are foreign languages to me, code examples would be especially helpful!

Offline

#5 2009-07-17 19:42:26

jsoo
Plugin Author
From: NC, USA
Registered: 2004-11-15
Posts: 1,793
Website

Re: A question for PHP or Javascript masters: generate clean category info

I’ll throw in tuppence worth even though I don’t meet the criterion put forth in the topic’s subject line :)

Were I to try to crack such a problem myself, I’d:

  • tread carefully — you aren’t protected by admin-side login so it’s doubly important to validate all input
  • duplicate as much as feasible from txp_category.php, especially cat_event_category_list() and cat_event_category_create()

Code is topiary

Offline

#6 2009-07-17 20:03:27

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,454
Website GitHub

Re: A question for PHP or Javascript masters: generate clean category info

johnstephens wrote:

I’d prefer for the new categories to be placed correctly in the tree.

Nice bit of tag-fu there, Sir. As Mary says, you won’t be able to make the tree work without calling rebuild_tree_full() after every insertion. I think the official name for it is a Modified Preorder Tree (I don’t think it’s a true red-black tree implementation) but I’m a bit fuzzy on the technicalities. All I know is that the left and right values must be regenerated programmatically whenever anything is inserted to keep the tree balanced. Otherwise all category hell will break loose at some point.

I’ve done the auto-generation of articles entirely in PHP before, but not the category thing. I’ve done extensive category work elsewhere (smd_tags) so if you want a hand with knowing what to call in the core, drop me a line and I’ll see what I can do.

EDIT: oh, and heed jsoo’s warning. All user input is tainted by default!

Last edited by Bloke (2009-07-17 20:05:27)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#7 2009-07-18 02:10:07

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: A question for PHP or Javascript masters: generate clean category info

jsoo, Bloke, I thank you both for your thoughtful responses!

Nice bit of tag-fu there, Sir.

Thanks! The real tag-fu was chaining two input forms together — which I only started getting right this morning.

tread carefully — you aren’t protected by admin-side login so it’s doubly important to validate all input

oh, and heed jsoo’s warning. All user input is tainted by default!

Thanks for the warning! I forgot to mention that this input is hidden behind a login by ign_password_protect, but if more protection is possible, I’d like to consider it. I have mem_akismet, but I’m not sure if content can be filtered through that with mem_simple_form.

As Mary says, you won’t be able to make the tree work without calling rebuild_tree_full() after every insertion. I think the official name for it is a Modified Preorder Tree (I don’t think it’s a true red-black tree implementation) but I’m a bit fuzzy on the technicalities. All I know is that the left and right values must be regenerated programmatically whenever anything is inserted to keep the tree balanced. Otherwise all category hell will break loose at some point.

Well, I’ve added a couple hundred categories in testing, and I’ve seen what a mess it makes of Textpattern’s category tree— luckily they are easy to identify and delete in phpMyAdmin.

I’ve done the auto-generation of articles entirely in PHP before, but not the category thing. I’ve done extensive category work elsewhere (smd_tags) so if you want a hand with knowing what to call in the core, drop me a line and I’ll see what I can do.

Very cool— thanks! I’m reading over your eMail now and planning some experiments.

Offline

#8 2009-08-03 18:25:46

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: A question for PHP or Javascript masters: generate clean category info

If anybody is curious about this, placing this code in the success_form specified in mem_simple_form worked for getting the article tree rebuilt:

<txp:php>
include_once txpath.'/lib/txplib_db.php';
rebuild_tree_full('article');
</txp:php>

This didn’t resolve the issue of using a random string for the category-name, but I decided to reconfigure my database to use a custom field for this information— eliminating Textpattern’s need to rebuild the article tree and my need to chain two forms together in one fell swoop.

Offline

Board footer

Powered by FluxBB