Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
Manipulating Content Article List
I am trying under 4.9.0 beta.2 to manipulate the list under Content -> Articles.
My goal is to show only articles to the author that he/she did write. Obviously the admin can see everything.
I managed to hook in with a little plugin.
register_callback('restrict_author_articles', 'admin_criteria', 'list_list');
But the problem is that the query that comes from txp always ends with ( 1 ) and I am failing to get around that.
Am I using the right hook here for what I want to achieve? Is there a better way or does anybody know how to add something to the exsiting txp query without running into the ( 1 ) problem?
I tried already to regex out the (1) or ( 1 )
if ($privs == 3) {
$new_criteria = "AuthorID = '" . doSlash($txp_user) . "'";
$crit_clean = preg_replace('/\s+/', '', $criteria);
if ($crit_clean === '' || $crit_clean === '1' || $crit_clean === '(1)') {
$criteria = $new_criteria;
} else {
$criteria = "($criteria) AND $new_criteria";
}
}
but no success here. It seems that after my cleaning atempt txp is again adding the (1) somehow.
Plan B was to try to hide articles with javascript but then the pagination would not work anymore.
So I hope that the only way is not to hack txp and get rid of the (1).
Offline
Re: Manipulating Content Article List
You shouldn’t have to scrub the existing criteria, just append anything you want to the (1) and it will apply your filter. WHERE (1) AND AuthorID = 'some_user'
will limit it just fine. Try this approach, from a plugin I wrote for someone that limits content on all 4 content panels to author-only:
register_callback('mrd_for_your_eyes_only', 'admin_criteria');
function mrd_for_your_eyes_only($evt, $stp, $crit)
{
global $txp_user;
$user = doSlash($txp_user);
if ($stp === 'list_list') {
return " AND AuthorID = '$user'";
} else if (in_array($stp, array('link_list', 'file_list', 'image_list'))) {
return " AND author = '$user'";
}
}
You may wish to exclude certain priv levels (e.g. Publishers) or specific user accounts from the above, so you can skip those before applying the filter.
My plugin went a little further and actively checked people weren’t hacking the URL by changing the ID to access/edit other content:
register_callback('mrd_for_your_resource_only', 'article', '', 1);
register_callback('mrd_for_your_resource_only', 'image', 'image_edit', 1);
register_callback('mrd_for_your_resource_only', 'file', 'file_edit', 1);
register_callback('mrd_for_your_resource_only', 'link', 'link_edit', 1);
function mrd_for_your_resource_only($evt, $stp)
{
global $txp_user;
$resourceAuthor = null;
$resourceMap = array(
'txp_image' => 'image_edit',
'txp_file' => 'file_edit',
'txp_link' => 'link_edit',
);
if ($evt === 'article') {
$resourceID = gps('ID');
if ($resourceID) {
$resourceAuthor = safe_field('AuthorID', 'textpattern', "ID=".assert_int($resourceID));
}
} else {
$resourceID = gps('id');
$resourceType = array_search($stp, $resourceMap);
if ($resourceID && $resourceType) {
$resourceAuthor = safe_field('author', $resourceType, "id=".assert_int($resourceID));
}
}
if ($resourceAuthor && ($resourceAuthor !== $txp_user)) {
require_privs();
}
}
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: Manipulating Content Article List
Beautiful. Thank you so much. I kinda over complicated it.
This is the final plugin code for my use case. It should effect every user that has lesser rights than “Managing Editor”.
register_callback('restrict_author_articles', 'article', '', 1);
register_callback('restrict_author_articles', 'image', 'image_edit', 1);
register_callback('restrict_author_articles', 'file', 'file_edit', 1);
register_callback('restrict_author_articles', 'link', 'link_edit', 1);
function restrict_author_articles($evt, $stp)
{
global $txp_user;
$resourceAuthor = null;
$resourceMap = array(
'txp_image' => 'image_edit',
'txp_file' => 'file_edit',
'txp_link' => 'link_edit',
);
$privs = safe_field('privs', 'txp_users', "name = '" . doSlash($txp_user) . "'");
if($privs > 2) {
if ($evt === 'article') {
$resourceID = gps('ID');
if ($resourceID) {
$resourceAuthor = safe_field('AuthorID', 'textpattern', "ID=".assert_int($resourceID));
}
} else {
$resourceID = gps('id');
$resourceType = array_search($stp, $resourceMap);
if ($resourceID && $resourceType) {
$resourceAuthor = safe_field('author', $resourceType, "id=".assert_int($resourceID));
}
}
if ($resourceAuthor && ($resourceAuthor !== $txp_user)) {
require_privs();
}
}
}
register_callback('mrd_for_your_eyes_only', 'admin_criteria');
function mrd_for_your_eyes_only($evt, $stp, $crit)
{
global $txp_user;
$user = doSlash($txp_user);
$privs = safe_field('privs', 'txp_users', "name = '" . doSlash($txp_user) . "'");
if($privs > 2) {
if ($stp === 'list_list') {
return " AND AuthorID = '$user'";
} else if (in_array($stp, array('link_list', 'file_list', 'image_list'))) {
return " AND author = '$user'";
}
}
}
Thanks again Stev!
PS: Unfortunately I did not manage to get this fancy colored highlighting of code =(
Last edited by demoncleaner (2025-09-04 08:57:42)
Offline
Re: Manipulating Content Article List
No problem. Glad it worked out.
You can do code highlighting by adding the type of hinting after the bc.
or bc..
then put your code starting on the next line. e.g.
bc.. php
There are a few available options. I think you can use html
and txp
. Not sure what else (maybe js
or javascript
or css
, dunno).
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: Manipulating Content Article List
I now would like to have a new column in the article list showing the excerpt.
In my case it is just a reference number which I wanted to be searchable.
I did not manage to do that via a plugin and just wanted to check if that is doable at all or worth keeping on trying it.
I did manage it with hardcoding it in the txp_list.php though.
But thats not the ideal way, of course.
… and thanks for the info about the code highlighting.
Last edited by demoncleaner (Yesterday 07:16:11)
Offline
Re: Manipulating Content Article List
It’s doable easily on things like the Users panel because I’ve extended the callbacks to allow plugins to hook in and extend things. From what I recall, it’s not been rolled out to (m)any other panels yet.
The tricky thing is that the articles are tied to the Write panel which has very specific layout requirements at the moment. So we’re delaying that level of integration until later.
If you’d like me to look at just the Articles panel, I could extend it now (as in 4.9.0) for you, like the Users panel. I think we’ve proved the concept works, and is established well enough.
Last edited by Bloke (Yesterday 09:24:39)
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: Manipulating Content Article List
Thank you for your answer and the offer to help. That sounds great.
Although I am not sure if I understood all completely. =)
In my case, showing the excerpt (or maybe a custom_field or maybe any field of the textpattern table) should not effect the Write panel I guess.
(For me) in a perfect scenario I could choose which fields I want to show in the articles panel and which of them I would want to be searched through.
In my use case I would want to hide the list options. At least for users with lower rights.
Offline
Re: Manipulating Content Article List
I guess the Write panel can be dealt with separately. The ‘list’ and ‘edit’ steps are more closely aligned in the other panels so it’s easier to control them.
For example, if you had a plugin that added a column to the User table, it becomes automatically searchable and filterable from the on-screen furniture in the ‘list’ (table) view and also shows up in the ‘edit’ step because they’re governed by he same event.
That’s not the case on the ‘articles’ and ‘write’ panels. They’re totally separate. So if we give the ability to add fields to the table they need to be reflected in both panels. Two sets of callbacks. Layout rules. Etc
In your (specific) case you only want to show a column that already exists. So it ought to be doable without adding columns per se. I’ll give it some thought. Might be easily doable now.
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
Pages: 1