Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Use parse inside an admin plugin
Hi
I am writing a plugin that convert Keywords input into a select2 multiselect list, i need to create a list from category and subcategories and inject it in the plugin code using javascript.
What i try to achieve is to have a list like that :
<opgroup label="cat Title">
<option value="cat name">Cat Title</option>
<option value="cat name">Cat Title</option>
.
.
</optgroup>
To create that i used parse as below:
$cat = parse('<txp:category_list parent="activites" exclude="activites" break="" children="0"><optgroup label="<txp:category title />"><txp:category_list parent=''<txp:category />'' exclude=''<txp:category />'' break=""><option value="<txp:category />"><txp:category title /></option></txp:category_list></optgroup></txp:category_list>);
But i am afread it’s too complicated to the parse function! any idea how to correct that or is there a better option ?
Offline
Re: Use parse inside an admin plugin
Why would such a short string be complicated to parse syntactically? The db queries behind <txp:category_list />
can be long of course if one has millions of categories, but that’s difficult to handle even in pure php, unless you add indexes on name/parent/type to txp_category
table (what we should probably do in core).
You can simplify a little the internal category list. Valueless exclude
and parent
are set to <txp:category />
, so
<txp:category_list parent=''<txp:category />'' exclude=''<txp:category />'' break="">
is equivalent to
<txp:category_list exclude parent break="">
Offline
Re: Use parse inside an admin plugin
etc wrote #335243:
add indexes on name/parent/type to
txp_category
table (what we should probably do in core).
I’m messing with indexes right now so I’ll add a combined index on these three columns while I’m at it. Or do you mean it’s better to add individual indexes to each column?
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
Re: Use parse inside an admin plugin
Bloke wrote #335245:
I’m messing with indexes right now so I’ll add a combined index on these three columns while I’m at it. Or do you mean it’s better to add individual indexes to each column?
Dunno, we should look at EXPLAIN
queries to see whether indexes are used. A common case is filtering by parent/type and sorting by name, I guess.
Offline
Re: Use parse inside an admin plugin
From fast testing, name_type
(UNIQUE) and parent_type
(INDEX) should do. SQL seems to prefer them to separate columns when it has choice. And the triple name_parent_type
seems to be of no use, unless we search for the whole combo, which is rare.
Offline
Re: Use parse inside an admin plugin
Makes sense. Feel free to add them if you have a mo. If not, I’ll get to it this evening.
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
Re: Use parse inside an admin plugin
The performance gains are not spectacular. Needs further testing.
Offline
Re: Use parse inside an admin plugin
Thanks for explanation,
Right now the parse is not recognized at all on the admin plugin!
Offline
Re: Use parse inside an admin plugin
Ah, ok. Then you will spare some memory by calling directly
get_tree(array('parent' => 'activites', 'children' => 0, 'exclude' => true, 'flatten' => false));
This will return an array of ‘activites’ like
array(
'danse' =>
array (
'id' => '52',
'name' => 'danse',
'parent' => 'activites',
'title' => 'Danse',
'description' => '',
'level' => 0,
),
...
)
that you can iterate over, calling get_tree()
for each activity to retrieve its children (tango, rock, etc) and constructing <options />
from its output.
Offline
Re: Use parse inside an admin plugin
Thanks Oleg,
I completely forgot about get_tree !
Offline
Re: Use parse inside an admin plugin
Even better: if you call the parent list with 'children' => true
, you should get the whole ‘activites’ tree (dump it to check), so no further calls to get_tree()
will be necessary.
Offline
Re: Use parse inside an admin plugin
Perfect Oleg
Thanks 🙏
Offline