Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Re: Sort articles based on multiple dates defined in one custom_field
Oh, one other thing. smd_calendar does not (at present) factor in times in the extrafield. Only dates. At the moment it assumes that any date you specify has the same time as the article’s posted date and will ignore any time you add to the datestamp in your custom field.
This is a deficiency in the system that I’ll try and fix. If I get a chance today I’ll look at it but I have a file due at the printers for a book, so I need to get that done first.
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: Sort articles based on multiple dates defined in one custom_field
Meanwhile I tried having the additional info that I need in an extra custom-field leading the info with the date it is ment for. This is not ideal because people will have to put in the date twice (or more) but it is the best solution I can think of at the moment.
It might be best to have one custom_field for each type of additional info so that it will not get too complicated in splitting and not too messy for the admin.
So for the “ticketlink” customfield it could look like:
2021-05-14#https://www.ticketseller.com/12345 2021-05-16#https://www.ticketseller.com/67890
Then I change the linebreak to a comma separated list with this:
<txp:variable name="additional_info">
<txp:pax_grep delimiter="|" from="/<\/?p>/|/\t/|/<br\s*\/?>/" to=","><txp:custom_field name="ticketlink" escape="textile"/></txp:pax_grep>
</txp:variable>
I am using then rah_repeat to do all the splitting and a trim regex at the end to “hide” the date and the #. Pretty ugly but it works. I am wondering if there is a cleaner/cleverer way? Any simplifying is welcome!
<txp:rah_repeat value='<txp:variable name="additional_info"/>'>
<txp:rah_repeat value='<txp:rah_repeat_value/>'>
<txp:variable name="test" value='<txp:rah_repeat_value/>'/>
<txp:if_variable name="test" value='<txp:posted format="%Y-%m-%d"/>' match="any">
<txp:variable name="output" trim='/\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])*[#]/' replace="" output><txp:rah_repeat_value/></txp:variable>
</txp:if_variable>
</txp:rah_repeat>
</txp:rah_repeat>
Then I´ll put all that into an output form called “eventinfo”.
Of course the appearance of custom_field has to be replaced with a yield.
I will then be able to create an individual ticketlink that reacts on the given date like this:
<a href='<txp::eventinfo customfield="ticketlink"/>'>Book now!</a>
One additional question:
I am wondering if performance-wise it would make a difference if I check at the beginning in my output-form if the variable additional_info has a value at all. Or if it makes sense to check up front if in the custom-field the current date is matched at all like:
<txp:if_custom_field name='<txp:yield name="ticketlink"/>' value='<txp:posted format="%Y-%m-%d"/>' match="any">
Last edited by demoncleaner (2021-05-13 11:57:21)
Offline
Re: Sort articles based on multiple dates defined in one custom_field
I think we are getting closer to an acceptable solution.
I did not see your previous post when I posted my last setup.
The only thing I do not understand with your last example is: How would I feed smd_calender with <txp:custom_field name="dates" escape="info" />
It only allows me to feed it like <txp:smd_article_event extrafield='custom_3'> right ?
So only the custom-field name can be set and no way to escape things there. Am I missing something again? That was the reason why I chose extra fields with the repetition of the date followed by the info/link needed.
Last edited by demoncleaner (2021-05-13 10:17:40)
Offline
Re: Sort articles based on multiple dates defined in one custom_field
demoncleaner wrote #330136:
It only allows me to feed it like
<txp:smd_article_event extrafield='custom_3'>right ?
Drat, you’re right. I’ll need to allow you to pass in custom content somehow so you can filter it. Leave that with me.
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: Sort articles based on multiple dates defined in one custom_field
I would suggest that you ask your clients to structure the data a bit better. For example, some events custom field could contain it in ini format:
[Day 05]
title=Title of event A
link=https://www.ticketseller.com/67890
[Day 08]
title=Title of event A
link=https://www.ticketseller.com/67890
...
This is not too much effort and is more robust. Then you could use etc_query to construct and reorder your output. Let me know if interested.
Offline
Re: Sort articles based on multiple dates defined in one custom_field
That looks interesting. If with this I can allow a more user and reader friendly input of the data I am more than happy. The client does not have an exact idea of how the input should be. So the more structured and logical it can be and at the same time easily feed smd_calendar the better.
Would it be too much asked if you could give an example on how exactly you would output the date (YYYY-mm-dd) on one hand and the title, link, whatever on the other? And maybe even keep the relation to the given date?
In my example earlier I just output the whole thing and hide the date to get the info needed. How would that work here?
Offline
Re: Sort articles based on multiple dates defined in one custom_field
The ini format is a great idea IF it can handle multiple events per day. The proposed structure by etc wouldn’t allow that unless ini sections with the same name are structured as arrays, which is extra burden on the inputter:
[2021-06-18]
event[1][time] = "18:00"
event[1][title] = "Title of event A"
event[1][link] = "https://example.com/67890"
event[2][time] = "12:00"
event[2][title] = "Title of matinee"
event[2][link] = "https://example.com/12354"
[2021-06-20]
event[1][time] = "14:00"
event[1][title] = "Some title"
event[1][link] = "https://example.com/abcdef"
...
Not even sure that works. Untested.
Keying off the date and time is possible but still has the same problem if two events are in different areas at the same time (e.g. a cinema multiplex with 10 screens).
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: Sort articles based on multiple dates defined in one custom_field
demoncleaner wrote #330139:
Would it be too much asked if you could give an example on how exactly you would output the date (YYYY-mm-dd) on one hand and the title, link, whatever on the other? And maybe even keep the relation to the given date?
In my example earlier I just output the whole thing and hide the date to get the info needed. How would that work here?
Sorry, I have not followed the entire thread and don’t know how smd_calendar works. The point is: the more the data is structured, the easier is the extraction. With etc_query it could be (you can try sorry, I must release the ini-version first)
<!-- data would come from a cf here -->
<txp:etc_query markup="ini" data="[Day 05]
title=Title of event A
link=https://www.ticketseller.com/67890
[Day 08]
title=Title of event A
link=https://www.ticketseller.com/67890">
<h4>{#}</h4><!-- date -->
<a href="{link?}">{title?}</a>
</txp:etc_query>
Bloke wrote #330140:
The ini format is a great idea IF it can handle multiple events per day.
It can not, the latest event takes it all. But I thought from the OP that events were unique per day? Otherwise you’d need more complex [sections] or switch to XML (but this would be too much to ask).
Another point to keep in mind is that it is possible to reorder HTML output (provided you get it somehow) with XSLT.
Offline
Re: Sort articles based on multiple dates defined in one custom_field
Sorry, I did not want to overcomplicate things at the beginning and was struggeling to find a good title for this thread anyway. In my specific case I would need to handle multiple events at a day, yes. But this still looks very interesting.
Offline
Re: Sort articles based on multiple dates defined in one custom_field
demoncleaner wrote #330142:
In my specific case I would need to handle multiple events at a day, yes.
That is not a problem, save (a little) for user convenience. You could structure your cf as (say)
[1]
date=2021-05-13
time=18:00
venue=Bolshoi Theatre
[2]
date=2021-05-13
time=21:00
venue=Bolshoi Theatre
...
The only requirement is the [key] uniqueness, up to you to choose it.
Offline
Re: Sort articles based on multiple dates defined in one custom_field
Sorry I am confused. So at the end it can work with multiple events a day? Like in your syntax above?
But you wrote:
sorry, I must release the ini-version first
So it cannot work, yet?
I tried
<txp:etc_query markup="ini" data="[Day 05]
title=Title of event A
link=https://www.ticketseller.com/12345
[Day 08]
title=Title of event B
link=https://www.ticketseller.com/67890">
<h4>{#}</h4><!-- date -->
<a href="{link?}">{title?}</a>
</txp:etc_query>
But I don´t get any ouput apart from a 0.
Maybe I am doing a major mistake here but I am barely understanding the concept and I am not sure if it is supposed to work or not.
Offline
Re: Sort articles based on multiple dates defined in one custom_field
I have just uploaded the ini version, please try again. Otherwise, you can store data in some <txp:variable name="events" /> and call
<txp:etc_query data="{?events||parse_ini_string.json_encode}" markup="json">...
Yes, you can work with multiple events. The main question is what format is acceptable for your client.
Offline
Re: Sort articles based on multiple dates defined in one custom_field
Yes it works and it looks very promising.
Just cannot figure out now how to sort it by date.
My cf looks like this:
[1]
date=2021-05-13
time=18:00
venue=Bolshoi Theatre
link=https://www.ticketlink.com/456
[2]
date=2021-05-26
time=21:00
venue=OtherTheater
link=https://www.ticketlink.com/1234
Another article might have
[1]
date=2021-05-14
time=18:00
venue=Bolshoi Theatre
link=https://www.ticketlink.com/xxx
[2]
date=2021-05-25
time=21:00
venue=OtherTheater
link=https://www.ticketlink.com/yyy
Output works all fine. Just not ordered by the date.
How can I achieve the desired order now mentioned at the beginning of this thread?
Do I have to use the “xsl stuff” of your example (<xsl:sort select="." order="descending" />) to get the ordering done?
Offline
Re: Sort articles based on multiple dates defined in one custom_field
Great! Yes, you must check first that xsl extension is enabled on your PHP server. Then you would loop over articles and structure the output, say:
<txp:variable name="eventslist">
<txp:article_custom wraptag="article"><!-- add attributes as needed -->
<txp:etc_query data='<txp:custom_field name="events" />' markup="ini">
<h4>{date?}</h4>
<div><a href="{link?}"><txp:title /></a> ({time?}, {venue?})</div>
</txp:etc_query>
</txp:article_custom>
</txp:variable>
Then you pass this variable to etc_query again, to sort and output events:
<txp:etc_query data='<txp:variable name="eventslist" />'>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:txp="https://www.textpattern.com">
<xsl:output method="html" encoding="utf-8" omit-xml-declaration="yes" />
<xsl:template match="article">
<xsl:for-each select="h4">
<xsl:sort select="." order="descending" />
<txp:if_different><xsl:copy-of select="." /></txp:if_different>
<xsl:copy-of select="following-sibling::div[1]" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
</txp:etc_query>
I have not tested it, but this should work.
Edit: splitting by [n] is a bit cumbersome, but you can replace them with something more human-readable, like ---. This would require some extra work, but is doable.
Offline
Re: Sort articles based on multiple dates defined in one custom_field
That’s really sweet. Love it.
Very soon we won’t need a CMS. It’ll just be Apache + etc_query :)
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