Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#37 2018-08-29 11:52:13

lythande
Member
Registered: 2009-09-08
Posts: 202
Website

Re: Outputting a valid .ics file from an Event for download

Hi! :-)

a lot of months was ago, since I have implemented the ICS-Calendars. This works good.

I try to make my workaround for new events better and ask me, is there possible to make the ics for more <txp:article_id />? The generates an .ics file for a single event and more single events works good. Now I wish to make it for more single events – without categories (because the categories from event was using for another site and can’t using for this site on the same website). So I can only work with article_id's.

I try it in form for 4 events, form name=“generate-ics-single-4”:

<txp:smd_wrap_all><txp:rah_gps name="event, event2, event3, event4" />
<txp:php>
  global $variable;
  if( !ctype_digit($variable['event']) ){
    $variable['event'] = '';
  }
  if( !ctype_digit($variable['event2']) ){
    $variable['event2'] = '';
  }
  if( !ctype_digit($variable['event3']) ){
    $variable['event3'] = '';
  }
  if( !ctype_digit($variable['event4']) ){
    $variable['event4'] = '';
  }
</txp:php>
<txp:if_variable name="event" value=""><txp:else />
<txp:if_variable name="event2" value=""><txp:else />
<txp:if_variable name="event3" value=""><txp:else />
<txp:if_variable name="event4" value=""><txp:else />
BEGIN:VCALENDAR...
<txp:article_custom id='<txp:variable name="event" />, <txp:variable name="event2" />, <txp:variable name="event3" />, <txp:variable name="event4" />' section="naechste-fuehrungen" form="ics-item" sortdir="asc" limit="4" expired="0" break="
" />
END:VCALENDAR
</txp:if_variable>
</txp:if_variable>
</txp:if_variable>
</txp:if_variable>
</txp:smd_wrap_all>

and in the article-body is this link:

<a class=“ics” href=”/?rah_external_output=generate-ics-single-4&event=83&event2=86&event3=91&event4=92”>Termine speichern</a>

How I can make this workaround better, shorter – then If I deleting e.g. &event4=92 so the .ics outputting empty, nothings. Mostly I have maximum 4 Events for one exhibition.
Some way for making this better or txp-like how <txp:article_custom id=" 83, 86, 91, 92"?
So that I can work with the same code for 2, 3 or 4 (or more) events with article_id, without to change the "/?rah_external_output=generate-ics-single-4&..." code for 1, 2, 3 or more events. Only to leave empty the follow events.
e.g.: for 3 events: <a href="/?rah_external_output=generate-ics-single-4&event=83&event2=86&event3=91">

Last edited by lythande (2018-08-29 11:55:09)

Offline

#38 2018-08-29 18:31:18

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,577
Website

Re: Outputting a valid .ics file from an Event for download

Hi lythande,

I replied to your email with a similar but more flexible approach to dealing with multiple events that should cater for one or more events using just one url parameter.


TXP Builders – finely-crafted code, design and txp

Offline

#39 2018-08-29 20:49:30

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

Re: Outputting a valid .ics file from an Event for download

I guess jakob approach will suffice, but if there is a hierarchical dependency between the events (i.e. event3 should be ignored if event2 is not set), you can use the recursive forms parsing of txp 4.7.

Create a misc type form called events:

<txp:variable name="level"><txp:evaluate query='<txp:variable name="level" escape="integer" />+1' /></txp:variable>
<txp:variable name="label" value='event<txp:if_variable name="level" not value="1"><txp:variable name="level" /></txp:if_variable>' />

<txp:evaluate test="page_url">
    <txp:page_url type='<txp:variable name="label" />' /><txp::events escape="trim" wraptag=",<+>" />
</txp:evaluate>

Now, if you call <txp::events escape="trim" />, you will get 1,3,2 on ?event=1&event3=2&event2=3 page, but only 1 on ?event=1&event3=2 page.

Offline

#40 2018-08-29 21:27:37

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,577
Website

Re: Outputting a valid .ics file from an Event for download

Oleg, that’s also an interesting approach. My suggestion was to go back a step and just allow any number of events using an url variable ?event=12-34-56-78 with 12, 34, 56, 78 being the article ID#s:

<txp:smd_wrap_all>
    <txp:rah_gps name="event" />
    <txp:php>
        global $variable;
        // remove anything that's not a number or dash
        $tmp = preg_replace("/[^0-9-]/", "", $variable['event']);
        // change every "-" into a "," and remove any preceding or trailing commas
        $variable['event'] = trim(str_replace("-", ",", $tmp), ",");
    </txp:php>
    <txp:if_variable name="event" value=""><txp:else />
        <txp:article_custom id='<txp:variable name="event" />' section="naechste-fuehrungen" form="ics-item" />
    </txp:if_variable>
</txp:smd_wrap_all>

The limit attribute must also be removed (or made a high number if expecting more than 10 dates) from article_custom and the form ics-item has to use if_first_article and if_last_article for the ICS header and footer infos. There’s no inter-article dependency there, just one or more articles.


TXP Builders – finely-crafted code, design and txp

Offline

#41 2018-08-29 21:48:21

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

Re: Outputting a valid .ics file from an Event for download

jakob wrote #313704:

There’s no inter-article dependency there, just one or more articles.

Then why wouldn’t you directly use comma-separated event=12,34,56,78?

<txp:smd_wrap_all>
    <txp:variable name="event" value='<txp:page_url type="event" />' />
    <txp:if_variable name="event" value>
        <txp:article_custom id='<txp:variable name="event" />' section="naechste-fuehrungen" form="ics-item" />
    </txp:if_variable>
</txp:smd_wrap_all>

Note that <txp:page_url /> and <txp:article_custom /> sanitize their attributes enough for this case.

Edit: or even more 4.7-ish

<txp:page_url type="event" wraptag="<txp:article_custom id='<+>' section='naechste-fuehrungen' form='ics-item' />" />

Offline

#42 2018-08-30 07:20:56

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,577
Website

Re: Outputting a valid .ics file from an Event for download

Lovely how you’ve reduced it successively to a one-liner. Always learn something new from you.

Then why wouldn’t you directly use comma-separated event=12,34,56,78?

I agree, that was my first thought too. It would certainly be simplest but I read somewhere that commas are a reserved character in URL parameters and that, while they often work in practice, there can be encoding problems when they get retrieved as %2C. I haven’t tested that, though.

Note that <txp:page_url /> and <txp:article_custom /> sanitize their attributes enough for this case.

That’s good to know too. While that might prevent illegal stuff in the parameters from getting to the tag, will that also eradicate anything that’s not a comma or a number? Could some wrong entry still cause article_custom to fail?

@lythande: Thinking about that, it would be advisable to add a conditional in ics-form that skips any article that is not an event article and does not have the date custom field. Otherwise, a manually-input URL could be used to output content from any article ID number in your installation.


TXP Builders – finely-crafted code, design and txp

Offline

#43 2018-08-30 07:28:54

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

Re: Outputting a valid .ics file from an Event for download

Ah, perhaps, though event=12%2C34 in URL seems to work for me.

Offline

#44 2018-08-30 10:03:51

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

Re: Outputting a valid .ics file from an Event for download

jakob wrote #313710:

While that might prevent illegal stuff in the parameters from getting to the tag, will that also eradicate anything that’s not a comma or a number?

No, it just encodes '"<> characters to prevent attributes injections. But we could enhance the global escape attribute in such a way that, say, escape="list, integer, filter" removes all invalid values from comma-separated lists.

Could some wrong entry still cause article_custom to fail?

No, anything that’s not an integer is removed from id list.

Offline

#45 2018-08-31 09:09:46

lythande
Member
Registered: 2009-09-08
Posts: 202
Website

Re: Outputting a valid .ics file from an Event for download

Hi,

much thanks for your discussions. Its for me too hard for following it. ^^

I have now try the code from Jacob what he posted first, but I am not sure, what exactly I should write in the body of my article.
At moment I have there written in this: <a class="ics" href="/?rah_external_output=generate-ics-event=83-86-91-92">Termine speichern</a> but on the webpage this link is a normal link and outputting an 404 error: http://mydomain.de/?rah_external_output=generate-ics-event=83-86-91-92 instead a linked .ics-file

In my form for event I written follow:

<txp:smd_wrap_all>
    <txp:rah_gps name="event" />
    <txp:php>
        global $variable;
        // remove anything that's not a number or dash
        $tmp = preg_replace("/[^0-9-]/", "", $variable['event']);
        // change every "-" into a "," and remove any preceding or trailing commas
        $variable['event'] = trim(str_replace("-", ",", $tmp), ",");
    </txp:php>
    <txp:if_variable name="event" value=""><txp:else />
(code for vcalendar is here)
<txp:article_custom id='<txp:variable name="event" />' section="naechste-fuehrungen" time="any" form="ics-item" sortdir="asc" limit="10000" expired="0" break="
" />
END:VCALENDAR
</txp:if_variable>
</txp:smd_wrap_all>

Maybe I have misinterpreted something? :-)

Last edited by lythande (2018-08-31 09:12:24)

Offline

#46 2018-08-31 21:46:15

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,577
Website

Re: Outputting a valid .ics file from an Event for download

lythande wrote #313735:

At moment I have there written in this: <a class="ics" href="/?rah_external_output=generate-ics-event=83-86-91-92">Termine speichern</a> but …

No, you need your link to look like this:

<a class="ics" href="/?rah_external_output=generate-ics&event=83-86-91-92">Termine speichern</a>

Note the & in there.

I’m not sure how you’re pulling in your articles with the deadlines, but you should be able to do something like this:

<a href="/?rah_external_output=generate-ics&event=<txp:article_custom name="my_section" category="my_cat" wraptag="" break="-"><txp:article_id /></txp:article_custom>">Termine speichern</a>

Or use break="," if you go straight to commas using Oleg/etc’s method.


TXP Builders – finely-crafted code, design and txp

Offline

#47 2018-09-03 12:40:57

lythande
Member
Registered: 2009-09-08
Posts: 202
Website

Re: Outputting a valid .ics file from an Event for download

Hi Jacob,

much thanks.

Note the & in there.

Of course, why I having not see that…
Now the code works (with “-”).

For with comma-separated id’s I havent understand, how I should do it. The code from etc doesn’t works.

Offline

Board footer

Powered by FluxBB