Automatically create file from form fields

Hey there.
I didn’t find anything on this, so here’s my question:

Let’s say I got a page & want to auto-generate a file containing some of the values entered into the page’s form fields (think of a quick summary, title & (last modified) date). How could I achieve this (without plugin/subpage)?

Best regards,
S1SYPHOS

You could use a panel.page.update hook: https://getkirby.com/docs/developer-guide/advanced/hooks

1 Like

Hey there again,
thanks for the reply. If I may, let me bother you once again with a similar request:

I installed the calendar plugin and everything’s just working fine, what I need is to generate an .ics file from my page input. I can modify the ical-snippet, but how to make a file out of it?

Maybe I am asking for too much, just say the word and I’ll try to figure it out. I see how this maybe goes too far :sweat:

There are two plugins with .ical support https://github.com/moritzz/Kirby-Calendar/tree/master/lib, and https://github.com/mzur/kirby-calendar-plugin/, which might be a starting point.

Could you give me an example how to convert the content of a snippet into a file with panel.page.update?

Could you please let us know what you got until now? What do you mean with “content of a snippet”?

I’m using the ics-template from mzur’s calendar-plugin.
First thing I tried was using this code as page template, so linking to the page would output the ics information:

<?php header('Content-type: text/calendar'); ?>
<?php $calpage = page('calendar'); $calendar = calendar($calpage->calendar()->yaml()); ?>
BEGIN:VCALENDAR
VERSION:2.0
METHOD:PUBLISH
<?php foreach ($calendar->getAllEvents() as $event): ?>
BEGIN:VEVENT
DTSTART:<?php echo gmdate('Ymd\THis\Z', $event->getBeginTimestamp()); ?>
DTEND:<?php echo gmdate('Ymd\THis\Z', $event->getEndTimestamp()); ?>
SUMMARY:<?php echo $event->getField('summary') ?>
DESCRIPTION:<?php echo $event->getField('description') ?>
LOCATION:<?php echo $event->getField('location') ?>
END:VEVENT
<?php endforeach; ?>
END:VCALENDAR

I don’t like I can’t append a file extension that way, but the header makes sure calendar apps detect it as ics information BUT it’s a dirty workaround. I want to resolve it from within the page (calendar) template and just tell kirby to encapsulate this ical snippet and save it as ics (for all events, extra cherries for single events as well, with a snippet that’s not looping).

Maybe like this (this is an example using subpages and without calendar board):

kirby()->hook('panel.page.update', function($page) {
if($page->isChildOf(page('events')):
  foreach ($page->siblings() as $event):
      $properties[] = array(
        'BEGIN:VEVENT',
        'DTSTART:' . gmdate('Ymd\THis\Z', $event->from()->int()),
        'DTEND:' . gmdate('Ymd\THis\Z', $event->to()->int()),
        'SUMMARY:' . $event->summary(),
        'DESCRIPTION:' . $event->description(),
        'LOCATION:' . $event->location(),
        'END:VEVENT'
      );

  endforeach;
  // call buildICS function
  $props = buildICS($properties);
  // write to file
  f::write(kirby()->roots()->index() .'/events.ics', $props);
endif;
});

function:

function buildICS($properties) {
   // Build ICS properties - add header
   $ics_props = array(
     'BEGIN:VCALENDAR',
     'VERSION:2.0',
   );
     // Build ICS properties - add header
   $props = array();
     foreach($properties as $key => $value) {
      foreach($value as $k => $v) {
        $props[] = $v;
      }
     }
   // Append properties
   foreach ($props as $key => $value) {
     foreach($value as $k => $v) {
       $ics_props[] = $v;
     }


   }
  // add footer
  $ics_props[] = 'END:VCALENDAR';

  return implode("\r\n", $ics_props);
}

For a single event, reduce the double foreach loops within the function.

Thank you so much, I’ll have a look right away!