Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2023-04-03 12:28:58

Dragondz
Moderator
From: Algérie
Registered: 2005-06-12
Posts: 1,536
Website GitHub Twitter

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

#2 2023-04-03 14:29:09

etc
Developer
Registered: 2010-11-11
Posts: 5,134
Website GitHub

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

#3 2023-04-03 14:36:07

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

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

#4 2023-04-03 14:44:02

etc
Developer
Registered: 2010-11-11
Posts: 5,134
Website GitHub

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

#5 2023-04-03 15:22:17

etc
Developer
Registered: 2010-11-11
Posts: 5,134
Website GitHub

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

#6 2023-04-03 15:32:09

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

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

#7 2023-04-03 15:34:35

etc
Developer
Registered: 2010-11-11
Posts: 5,134
Website GitHub

Re: Use parse inside an admin plugin

The performance gains are not spectacular. Needs further testing.

Offline

#8 2023-04-03 17:22:47

Dragondz
Moderator
From: Algérie
Registered: 2005-06-12
Posts: 1,536
Website GitHub Twitter

Re: Use parse inside an admin plugin

Thanks for explanation,

Right now the parse is not recognized at all on the admin plugin!

Offline

#9 2023-04-03 18:31:36

etc
Developer
Registered: 2010-11-11
Posts: 5,134
Website GitHub

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

#10 2023-04-04 08:20:24

Dragondz
Moderator
From: Algérie
Registered: 2005-06-12
Posts: 1,536
Website GitHub Twitter

Re: Use parse inside an admin plugin

Thanks Oleg,

I completely forgot about get_tree !

Offline

#11 2023-04-04 15:01:17

etc
Developer
Registered: 2010-11-11
Posts: 5,134
Website GitHub

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

#12 2023-04-04 15:15:04

Dragondz
Moderator
From: Algérie
Registered: 2005-06-12
Posts: 1,536
Website GitHub Twitter

Re: Use parse inside an admin plugin

Perfect Oleg

Thanks 🙏

Offline

Board footer

Powered by FluxBB