Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Re: smd_if: Generic multiple if condition tests
NeilA wrote:
You programmers need twits like me to do things like use single quotes in names, and overwrite program variables!
Hehehe, we need normal people like you to make us see the blindingly obvious :-)
…will also drop the single quotes (I seem to recall them causing problems elsewhere too…).
Fine by me. The only reason I never spotted it is ‘cos I just don’t use them as a matter of course. Same with spaces. Old programming habit I guess. However, the latest version has better support for quotes insofar as I went the other way: single quotes are fine now to allow for apostrophes in words but double quotes will be converted to alleviate the errors you encountered.
So if you use double quotes in things and encounter stuff that fails to match any more, try not comparing against double quotes, or use a less restrictive comparison such as operator="contains" and search for a known bit within the string.
Try v0.4 (or the compressed version) with the official question-mark-in-value extension/patch mentioned above and more robust quote support. Shout if anything goes sideways, and thanks again for finding the bugs.
Last edited by Bloke (2008-01-06 03:44:50)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Offline
#17 2008-01-06 13:40:42
- peterj
- Member
- From: Melbourne, Australia
- Registered: 2005-06-02
- Posts: 110
Re: smd_if: Generic multiple if condition tests
Hi bloke,
I’ve had one instance where this nice plugin threw up errors that I couldn’t figure. I have very possibly done something silly but thought I’d post just in case.
I’m using the plugin in a form to check whether two custom fields are being used:
<txp:smd_if field="URL, URL_name" operator="isused, isused" value=",,">
<p><a href='<txp:custom_field name="URL" />'><txp:custom_field name="URL_name" /></a></p>
</txp:smd_if>
When a relevant article is accessed it returns one of these for each custom field queried (can’t seem to format it):
Tag error: <txp:smd_if field=“URL, URL_name” operator=“isused, isused” value=”,,” logic=“and”> -> Notice: Undefined index: URL on line 1
textpattern/lib/txplib_misc.php(534) : eval()’d code(138) : eval()’d code:1 unknown()
textpattern/lib/txplib_misc.php(534) : eval()’d code:138 eval()
textpattern/publish.php:963 smd_if()
processtags()
textpattern/publish.php:922 preg_replace_callback()
textpattern/lib/txplib_misc.php:1383 parse()
textpattern/publish.php:782 parse_form()
textpattern/publish.php:808 doarticle()
textpattern/publish.php:509 parsearticles()
textpattern/publish.php:963 article()
the debug goes like this, despite both fields being used:
<txp:smd_if field="URL, URL_name" operator="isused, isused" value=",," logic="and">
[<txp:smd_if field="URL, URL_name" operator="isused, isused" value=",," logic="and">: false]
</txp:smd_if>
Any tips much appreciated.
(I have installed v0.4)
Thanks,
Peter
Offline
Re: smd_if: Generic multiple if condition tests
peterj wrote:
I have very possibly done something silly
Hehe, unlikely, it’ll probably be the code!
I can immediately see one problem that I overlooked: custom field names are always stored in the database in lower case. I’ll fix the code in the next revision so that even if you give upper case names they will be converted to lower case before I ask TXP about them.
To get round it for now, just change to this: field="url, url_name".
But don’t do that just yet because it doesn’t explain the error message: I can’t replicate it here. I’ve named two custom fields the same as yours (same capitalisation too) and used the same tag — incidentally you now don’t need the two commas in the value field if you are wholly using operators that don’t require a value, you can either use value="" or miss it out altogether.
So before you change your field attribute to lower case, would you confirm a few things please:
- what sort of stuff is in each of your custom fields (I can guess(!) but want to be sure, post some examples if you can). It may be some odd characters are screwing up my code and I need to check for them
- add to your smd_if tag the attribute
debug="1"and post the output. Just wanna check it’s comparing the right stuff.
Oh, and your version of TXP, thanks. I’m a bit stumped at the moment but I’ll carry on playing with it.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Offline
#19 2008-01-06 14:39:21
- peterj
- Member
- From: Melbourne, Australia
- Registered: 2005-06-02
- Posts: 110
Re: smd_if: Generic multiple if condition tests
I’m working to late..
1. URL has no quotes, just something like http://www.theage.com.au/
2. The optional friendly name for the link ie The Age
Idea being if no friendly name is put in then LINK is shown.
Hope that helps.
The plugin is working fine on pages, although I’m having some problems there (I think) with the ‘isused’ operator – not 100% sure yet, will have to go to sleep before I break something,
Peter
Offline
#20 2008-01-06 14:40:17
- peterj
- Member
- From: Melbourne, Australia
- Registered: 2005-06-02
- Posts: 110
Re: smd_if: Generic multiple if condition tests
Sorry misread the end of your post, will do as instructed.
Offline
#21 2008-01-06 14:45:58
- peterj
- Member
- From: Melbourne, Australia
- Registered: 2005-06-02
- Posts: 110
Re: smd_if: Generic multiple if condition tests
2. With debug=“1” I get:
$out[] = ($thisarticle[“URL”] != “”) ? ‘true’ : ‘false’;
Tag error: <txp:smd_if field=“URL, URL_name” operator=“isused, isused” logic=“and” debug=“1”> -> Notice: Undefined index: URL on line 1
AND in the txp debug I get:
<txp:smd_if field=“URL, URL_name” operator=“isused, isused” logic=“and” debug=“1”> [<txp:smd_if field=“URL, URL_name” operator=“isused, isused” logic=“and” debug=“1”>: false] </txp:smd_if>
I am using textpattern 4.0.5
Thanks.
Offline
#22 2008-01-06 14:52:10
- peterj
- Member
- From: Melbourne, Australia
- Registered: 2005-06-02
- Posts: 110
Re: smd_if: Generic multiple if condition tests
The diagnostic might help as well if you’re a bit stumped:
Textpattern version: 4.0.5 (r2466)
Last Update: 2007-12-29 08:44:50/2007-12-29 08:29:46
…
Permanent link mode: section_id_title
…
PHP version: 4.4.7
Register globals: 1
GD Image Library: 2.0 or higher; supported formats: GIF, JPG, PNG.
Server Local Time: 2008-01-07 01:48:28
MySQL: 4.1.21-standard-log
Locale: en_GB.UTF-8
Server: Apache
Apache version: Apache
PHP Server API: apache
RFC 2616 headers:
Server OS: Linux 2.6.21-1.3194.fc7
Active plugins: zem_ir-0.5, asy_wondertag-0.5, stm_image_uploader-0.3, glx_admin_image-2007.1, tru_tags-3.1, rss_admin_show_adv_opts-0.3, smd_if-0.4
.htaccess file contents:
————————————
#DirectoryIndex index.php index.html
#Options +FollowSymLinks
#Options -Indexes
<IfModule mod_rewrite.c> RewriteEngine On #RewriteBase /relative/web/path/
RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^(.+) – [PT,L]
RewriteRule ^(.*) index.php
</IfModule>
#php_value register_globals 0
————————————
I’d put the site address up but I don’t want google getting anywhere near it for a while.
Offline
#23 2008-01-06 15:02:33
- peterj
- Member
- From: Melbourne, Australia
- Registered: 2005-06-02
- Posts: 110
Re: smd_if: Generic multiple if condition tests
I just spotted something else – here is a larger version of the debug above with an array message at the end, and an undefined offset error I hadn’t noticed:
$out[] = ($thisarticle[“URL”] != “”) ? ‘true’ : ‘false’;
Tag error: <txp:smd_if field=“URL, URL_name” operator=“isused, isused” logic=“and” debug=“1”> -> Notice: Undefined index: URL on line 1
textpattern/lib/txplib_misc.php(534) : eval()’d code(138) : eval()’d code:1 unknown()
textpattern/lib/txplib_misc.php(534) : eval()’d code:138 eval()
textpattern/publish.php:963 smd_if()
processtags()
textpattern/publish.php:922 preg_replace_callback()
textpattern/lib/txplib_misc.php:1383 parse()
textpattern/publish.php:782 parse_form()
textpattern/publish.php:808 doarticle()
textpattern/publish.php:509 parsearticles()
textpattern/publish.php:963 article()
Tag error: <txp:smd_if field=“URL, URL_name” operator=“isused, isused” logic=“and” debug=“1”> -> Notice: Undefined offset: 1 on line 51
textpattern/lib/txplib_misc.php(534) : eval()’d code:51 smd_if()
textpattern/publish.php:963 smd_if()
processtags()
textpattern/publish.php:922 preg_replace_callback()
textpattern/lib/txplib_misc.php:1383 parse()
textpattern/publish.php:782 parse_form()
textpattern/publish.php:808 doarticle()
textpattern/publish.php:509 parsearticles()
textpattern/publish.php:963 article()
processtags()
$out[] = ($thisarticle[“URL_name”] != “”) ? ‘true’ : ‘false’;
Tag error: <txp:smd_if field=“URL, URL_name” operator=“isused, isused” logic=“and” debug=“1”> -> Notice: Undefined index: URL_name on line 1
textpattern/lib/txplib_misc.php(534) : eval()’d code(138) : eval()’d code:1 unknown()
textpattern/lib/txplib_misc.php(534) : eval()’d code:138 eval()
textpattern/publish.php:963 smd_if()
processtags()
textpattern/publish.php:922 preg_replace_callback()
textpattern/lib/txplib_misc.php:1383 parse()
textpattern/publish.php:782 parse_form()
textpattern/publish.php:808 doarticle()
textpattern/publish.php:509 parsearticles()
textpattern/publish.php:963 article()
array (
0 => ‘false’,
1 => ‘false’,
)
Offline
Re: smd_if: Generic multiple if condition tests
Thanks for the debug and diagnostics. Your last one led me to it. I’m such a doughnut sometimes and contravened the immutable programming law: don’t code at 3:30am and expect it to be good :-)
Try v0.41 (compressed) which now treats field names as lower case and should fix the undefined index warning. I’m kinda flying blind because my version of PHP doesn’t ouput the warnings like yours does, but I’m pretty sure I nailed it.
See how you get on and let me know. And thanks for spotting the bug.
Last edited by Bloke (2008-01-06 15:37:27)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Offline
Re: smd_if: Generic multiple if condition tests
Bloke wrote:
Try v0.4 (or the compressed version) with the official question-mark-in-value extension/patch mentioned above and more robust quote support. Shout if anything goes sideways, and thanks again for finding the bugs.
You’re a legend Stef! v0.4 seems to be working fine. Thanks so much…
Cheers
Last edited by NeilA (2008-01-06 19:58:19)
Offline
#26 2008-01-06 21:10:40
- peterj
- Member
- From: Melbourne, Australia
- Registered: 2005-06-02
- Posts: 110
Re: smd_if: Generic multiple if condition tests
Stef, v 0.4.1 is working perfectly, thank you.
Offline
Re: smd_if: Generic multiple if condition tests
Something is wrong.
For ex. i have the following category structure:- metro
- metro1
- metro2
- metro3
My code:
<txp:smd_if field="parent" value="metro">
the list of metro stations <txp:category title="1" />
</txp:smd_if>
And if we are at global category metro1 (url looks like http://uromax.ru/category/metro1) plugin should return true, but:
<txp:smd_if field="parent" value="metro">
[SQL (0,000463): select lft as l, rgt as r from uro_txp_category as txp_category where name='avtozavodskaya' and type = 'article']
[SQL (0,000469): select * from uro_txp_category as txp_category where lft <= 21 and rgt >= 22 and type = 'article' order by lft asc]
[<txp:smd_if field="parent" value="metro">: false]
</txp:smd_if>
At the same time i have another plugin nfe_if_category:
<txp:nfe_if_category parent="METRO">
</txp:nfe_if_category>
And it works fine at the same page:
<txp:nfe_if_category parent="METRO">
[SQL (0,027634): select lft, rgt from uro_txp_category as txp_category where name = 'METRO']
[SQL (0,001715): select name, title from uro_txp_category as txp_category where (lft between 2 and 309) and type = 'article' and name != 'default' order by lft asc]
[<txp:nfe_if_category parent="METRO">: true]
Some information:
- the whole tagtrace is here
- Diagnostic is here
Last edited by the_ghost (2008-01-13 13:38:47)
Providing help in hacking ATM! Come to courses and don’t forget to bring us notebook and hammer! What for notebook? What a kind of hacker you are without notebok?
Offline
Re: smd_if: Generic multiple if condition tests
Thanks for the detailed diagnostics, the_ghost. I originally thought this had something to do with the URL structure (using /category/metro1 instead of ?c=metro1) but I tried a similar setup on my server and it worked fine. Out of curiosity, does it work if you use:
http://uromax.ru/?c=metro1
I don’t think it’ll make any difference, but I’d like to find out.
The next puzzling thing is that the plugin thinks the name you supplied in the value parameter is “avtozavodskaya” instead of “metro1”. Any idea where “avtozavodskaya” comes from? Is it the name of a section or an article or something? Do your categories have a different name and title? (the plugin uses the name)
Can you try a couple of things please and post the results:
- add
debug="1"to the smd_if call. The plugin will output the whole category tree it tries to match, and a few other variables that might help me track the problem down - add
<txp:php>global $pretext; dmp($pretext);</txp:php>somewhere near the smd_if call and tell me what it outputs
If you’d rather not post these values publicly you can email them to me instead or use the forum e-mail system.
Sorry it’s not working: I can’t see what the problem is at the moment but clearly it should work so I’ll keep poking at the code and see if anything leaps out at me.
Last edited by Bloke (2008-01-13 15:11:17)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Offline
Re: smd_if: Generic multiple if condition tests
metro1 was provided like example to make my example clear. Аctually ш have the following tree of categories:
- metro
- http://uromax.ru/category/AVIAMOTORNAYA
- http://uromax.ru/category/BRATISLAVSKAYA
- and so on…
And i have glossary were childs are the letters:
- slovar (russian translation of glossary)
- http://uromax.ru/category/A/
- http://uromax.ru/category/B/
- http://uromax.ru/category/V/
- and so on…
Providing help in hacking ATM! Come to courses and don’t forget to bring us notebook and hammer! What for notebook? What a kind of hacker you are without notebok?
Offline
Re: smd_if: Generic multiple if condition tests
Aha, right, I think it might be a conceptual difficulty with the way I implemented the parent option. I will revisit the help to try and make it clearer, sorry!
First, a few observations:
- None of your examples contain an
operatorparameter, therefore they default to always compare exact equality (eq). - The searches are always case sensitive (looking at this again, I will actually go back and add a parameter to allow you to turn this off in the next revision of the plugin)
- Usually with the parent option you would use
operator="contains"oroperator="begins", and here is why:
Unlike nfe_if_category, the parent parameter does not directly compare just the “top level” parent with the value you supply. What it does is look at the entire tree from the current category all the way “up” to the root. Internally, the plugin concatenates that hierarchy into one long string and compares against that.
So if you had categories:
- metro (level 1)
- AVIAMOTORNAYA (level 2)
- BRATISLAVSKAYA (level 2)
- INFO (level3)
- DUBROVKA (level2)
And you were currently browsing the ‘INFO’ category, its parents would be both “metro” and “BRATISLAVSKAYA”. By default, the plugin makes this into one string like this “metro BRATISLAVSKAYA” and tests for a match against that.
Thus, to see if the current category is a descendent of metro you could use:
<txp:smd_if field="parent" operator="begins" value="metro">
//something
</txp:smd_if>
Now, in your case, this is too general and you probably want to see if the current category is somewhere below the ‘level 1’ category called ‘metro’. The parent field can be told to only look at a particular tree level by adding the LVL modifier to the end. e.g.
<txp:smd_if field="parent:LVL1" value="metro">
//something
</txp:smd_if>
Notice now you may omit the operator parameter because you are always directly comparing (eq) the value to a single category level which can of course only contain one element.
btw, in your second case you would have to use value="SLOVAR" because your category name is in capital letters. I will fix that in the next revision by adding a case_sensitive attribute.
Sorry if all that sounds confusing but I thought it was the most flexible solution. And sorry that the documentation neglects to mention this adequately! I will also fix the help with better examples and make it obvious that by default you should use contains or begins with the parent, but adding a :LVL allows you to use eq.
Does that (sort of) make sense? If you can think of a better way of implementing it without sacrificing the ability to search the whole tree, then please let me know :-)
Last edited by Bloke (2008-01-13 17:40:25)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Offline