Automatically archive pages by date

Hey,

we have an events page on our webpage with subpages for every event.

Each event has a date.

We want to archive events if they have past. I.e. If the current date is lager than the event date, the event page should be moved to another directory.

Is there an easy way to implement this?

Thank you, anton

1 Like

is moving directory necessary?

it’s not difficult either way.

you could just check your date field and use the move page command… it’s actually a few lines of code, so it doesn’t need to be a full fledged plugin… you could use it within site/plugins/ to call it every time a page is accessed though.

https://getkirby.com/docs/cheatsheet/page/move

foreach($page->children() as $child){
  if(strtotime("-2 weeks") > strtotime($child->date()){
     try {
        $child->move('a-new-uid');
        } catch(Exception $e) {
               echo $e->getMessage();
            }
  }
}

but i’d always recommend sorting by date, and while the place will not change, you could filter them in your list just like that…

There are several questions to consider:

  • should the page still be available at the old URL? If not, then you can simply move the page, e.g. using a cron job every night. Moving a page only when it is accessed is not very reliable.
  • if yes, you could move the page but use a route to make it available at the old URL
  • but: You might end up with the wrong content under the same URL if a user creates a new page with the same UID in the posts folder

So the question is: why do you need an archives page in the form of a new folder?

I’d use a filter to only show current articles in the frontend. And if you want to create an archives page, you can do that using a route or with a new folder (and then also filter posts by date), but without actually moving the posts into that folder.

This is pretty much what i did with my event calendar:


<?php
$events = $site->find("events")->children()->visible();
$currentdatetime = date('Y-m-d');
foreach ($events as $event) {
$eventdatetime = date('Y-m-d', strtotime($event->startdate()));

  if ($eventdatetime >= $currentdatetime)
  {
    // your event list
  }
}
?>

This way any bookmarks people have made still work, and you don’t have to move pages. If you wanted to do an old event list (aka an archive) just flip the if to

if ($eventdatetime <= $currentdatetime)

This will list articles on the page that are older or newer than todays date, depending on what you want.

1 Like

Thank you for the help.
I tried this out and can’t figure out how to actually move the page into a subdirectory.
The move()-function just changes the title of the page.
Any thoughts?

move() does not move the page to a new directory, you can only change the UID. You would have to use the file methods](https://getkirby.com/docs/toolkit/api#) from the toolkit, in particular f::move(). But what about the questions I asked above?

yeah, mostly also using this as well…

one thing i like doing, turning everything a bit more flexible is using:

$pages->index()->filterBy('template','event.list')->children()->visible() 

instead of hardcoding find(“events”) … let’s say you have multiple access towards your event.list (parent template of whereever your events are children of) you’d need to change several files manually.

but obviously this would only work if you are using the event.list template once.

Sure, however I tend to use snippets and pass the desired
root page through as a snippet variable. I stripped this out above for brevity. That way you can use the same snippet with a news section for example.

I would love to turn my solution into a plugin because it’s pretty cool and includes support for Google calendar events alongside Kirby articles, and uses jQuery fullcalendar.js to show events on a visual calendar, but my PHP is pretty young because I’m learning it and I don’t know how to make a plugin. I know there are docs for that. I guess I could set it up on starter kit and open it up to the community for refinement via github.

@anton:

I would not move any page for this purpose, but build new events within the directory for old events.

Then I would build your logic in the blueprints, snippets and templates as needed, e.g. look at the homepage of the Kirby starterkit. There you find the “Latest Projects”, their content is saved as pages in the projects directory.

I see no need to move pages, if you design the content directory of your website like you would do this with the data of all pages if you use a database. Then you can get the data of the new events on every webpage you want (e.g. on a “New events page”).

This cookbook article I wrote some time ago might help with filtering by date.

If you want to get some order into the Panel, this field might help: https://github.com/lukaskleinschmidt/kirby-sortable-events.

If you are afraid that you might end up with too many subfolders in the events folder, you might consider structuring your events by year or even month subfolders.

Thank you for the thoughts, texnixe.

The page will not have to be accessible at the url after archiving, since we are not using the event-pages as real pages but merely displaying their content on the main events-page.

Does Kirby provide an interface to implement cron jobs? Since we’re not decided on a server yet.

Thank you for the link to f::move(). I’ll use dir::move($old, $new) in my case.

Could someone provide an example for the old and new path that move accepts? I can’t get it to work. I also experimented with is_dir(), but can’t figure out where the root is, nor how I can get to /content/events/…

You can get the path to the content root like this:

$path = kirby()->roots()->content() . '/rest_of_path';

All works great now :slight_smile:

Thank you for your time & effort!