Skip to content

Commit

Permalink
Establish compatibility with Kirby 3
Browse files Browse the repository at this point in the history
  • Loading branch information
mzur committed Jun 21, 2021
1 parent 997b497 commit 35d5c45
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 160 deletions.
18 changes: 11 additions & 7 deletions calendar/lib/Calendar.php → Calendar.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

namespace Mzur\KirbyCalendar;

use Kirby\Toolkit\A;

/**
* A simple calendar object that mainly consists of a list of Event objects.
*/
Expand All @@ -20,11 +24,11 @@ class Calendar {
* @param array $events An array of 'raw' events. A raw event is an array
* of field => value pairs.
*/
function __construct($events = array()) {
function __construct($events = []) {
// intantiate all the given events to Event objects
$this->events = array_map('event::instantiate', $events);
$this->events = array_map('Mzur\\KirbyCalendar\\Event::instantiate', $events);
// sort the events from old to new
usort($this->events, 'event::compare');
usort($this->events, 'Mzur\\KirbyCalendar\\Event::compare');

$this->eventFields = self::findEventFields($this->events);
}
Expand All @@ -40,7 +44,7 @@ public function getAllEvents() {
* @return all future events.
*/
public function getEvents() {
return array_filter($this->events, 'event::filterPast');
return array_filter($this->events, 'Mzur\\KirbyCalendar\\Event::filterPast');
}

/**
Expand All @@ -55,13 +59,13 @@ public function getEventFields() {
* @param array $events An array of Event objects.
*/
private static function findEventFields($events) {
$fields = array();
$fields = [];

foreach ($events as $event) {
$fields = a::merge($fields, $event->getFieldKeys());
$fields = A::merge($fields, $event->getFieldKeys());
}

// make an associative array with the same keys as values
return array_combine($fields, $fields);
}
}
}
58 changes: 27 additions & 31 deletions calendar/lib/Event.php → Event.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
<?php

namespace Mzur\KirbyCalendar;

use Exception;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Str;

class Event {

/**
* The string for the beginning date field key of an event.
*/
const beginDateKey = '_begin_date';
const BEGIN_DATE_KEY = '_begin_date';

/**
* The string for the beginning time field key of an event.
*/
const beginTimeKey = '_begin_time';
const BEGIN_TIME_KEY = '_begin_time';

/**
* The string for the end date field key of an event.
*/
const endDateKey = '_end_date';
const END_DATE_KEY = '_end_date';

/**
* The string for the end time field key of an event.
*/
const endTimeKey = '_end_time';
const END_TIME_KEY = '_end_time';

/**
* An array of field keys that are required to create an event.
*/
private static $requiredKeys;
private static $requiredKeys = [
self::BEGIN_DATE_KEY,
];

/**
* The timestamp of the beginning of this event.
Expand Down Expand Up @@ -73,24 +81,24 @@ function __construct($event) {

$this->hasEnd = true;

$this->hasBeginTime = (bool) a::get($event, self::beginTimeKey);
$this->hasEndTime = (bool) a::get($event, self::endTimeKey);
$this->hasBeginTime = (bool) A::get($event, self::BEGIN_TIME_KEY);
$this->hasEndTime = (bool) A::get($event, self::END_TIME_KEY);

$this->beginTimestamp = self::getTimestamp(
a::get($event, self::beginDateKey),
a::get($event, self::beginTimeKey)
A::get($event, self::BEGIN_DATE_KEY),
A::get($event, self::BEGIN_TIME_KEY)
);

$this->endTimestamp = self::getTimestamp(
a::get($event, self::endDateKey),
a::get($event, self::endTimeKey)
A::get($event, self::END_DATE_KEY),
A::get($event, self::END_TIME_KEY)
);

// if there is no end date given, use the same as the beginning date
if (!$this->endTimestamp) {
$this->endTimestamp = self::getTimestamp(
a::get($event, self::beginDateKey),
a::get($event, self::endTimeKey)
A::get($event, self::BEGIN_DATE_KEY),
A::get($event, self::END_TIME_KEY)
);

// if there also is no end time given, there is no end at all
Expand All @@ -106,22 +114,13 @@ function __construct($event) {

// only use the full format, if there were times given for this event
$this->timeFormat = ($this->hasBeginTime || $this->hasEndTime)
? l::get('calendar-full-time-format')
: l::get('calendar-time-format');
? t('calendar-full-time-format')
: t('calendar-time-format');

// remove the 'private' fields
$this->fields = self::filterFields($event);
}

/**
* Static initializer.
*/
public static function __init() {
self::$requiredKeys = array(
self::beginDateKey
);
}

/**
* @param array $event A 'raw' event array.
* @return A new Event instance.
Expand Down Expand Up @@ -156,10 +155,10 @@ public static function filterPast($e) {
* @param array $event a 'raw' event array containing all fields
*/
private static function validate($event) {
$missingKeys = a::missing($event, self::$requiredKeys);
$missingKeys = A::missing($event, self::$requiredKeys);
if ($missingKeys) {
$message = "Event creation failed because of the following missing " .
"required fields:\n" . a::show($missingKeys, false);
"required fields:\n" . A::show($missingKeys, false);
throw new Exception($message, 1);
}
}
Expand All @@ -180,7 +179,7 @@ private static function getTimestamp($date, $time = '') {
*/
private static function filterFields($event) {
foreach (array_keys($event) as $key) {
if (str::startsWith($key, '_')) {
if (Str::startsWith($key, '_')) {
unset($event[$key]);
}
}
Expand Down Expand Up @@ -276,7 +275,7 @@ public function getFieldKeys() {
* in this event.
*/
public function getField($key) {
return a::get($this->fields, $key, '');
return A::get($this->fields, $key, '');
}

/**
Expand All @@ -295,6 +294,3 @@ public function hasEnd() {
return $this->hasEnd;
}
}

// initialize the static variables
event::__init();
53 changes: 18 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
# kirby-calendar-plugin

A plugin for the [Kirby 2 CMS](http://getkirby.com) to easily implement an event calendar.
A plugin for the [Kirby CMS](http://getkirby.com) to easily implement an event calendar.

The Calendar Plugin `v2` is **not** compatible with `v1`!

## Installation

1. Put the `calendar` directory to `site/plugins/`.
1. Clone this repository to `site/plugins/`.

2. Put those snippets from `snippets` that you like to use to `site/snippets/` (or make your own).
2. Make sure you have the language support of Kirby activated (even, if you only want to support one language).

3. Put the language files of the `language` directory to `site/languages/`. If you already have existing language files, simply append the content to them. You only need to choose those languages that you actually want to support.

4. Make sure you have the language support of Kirby activated (even, if you only want to support one language). Here is an example for activating the support with a single language in `site/config/config.php`:

```php
c::set('languages', array(
array(
'code' => 'en',
'name' => 'English',
'locale' => 'en_US.UTF-8',
'default' => true,
'url' => '/'
)
));
```

For more information on the multi language support of Kirby, see [the docs](http://getkirby.com/docs/languages/setup).
For more information on the multi language support of Kirby, see [the docs](https://getkirby.com/docs/guide/languages/introduction).

## Usage

Expand Down Expand Up @@ -54,14 +37,14 @@ Calendar:
_begin_date: 10/01/2014
```

The `Calendar` field contains all events in a [YAML](http://getkirby.com/blog/structured-field-content) list. Each event has several own fields, too, but of them only `_begin_date` is mandatory (see [event-fields](#event-fields)). You can define as many other fields as you like.
The `Calendar` field contains all events as a [structure](https://getkirby.com/docs/reference/panel/fields/structure). Each event has several own fields, too, but of them only `_begin_date` is mandatory (see [event-fields](#event-fields)). You can define as many other fields as you like.

### Calendar object

Now let's get to the `calendar` template. Setting up the calendar object is really simple; you only have to do this:

```php
<?php $calendar = calendar($page->calendar()->yaml()); ?>
<?php $calendar = new Mzur\KirbyCalendar\Calendar($page->calendar()->yaml()); ?>
```
### Calendar snippet

Expand All @@ -71,13 +54,13 @@ Typical usage of a snippet looks like this (here we use the [`table`](#table) sn

```php
<?php
snippet('calendar-table', array(
snippet('calendar-table', [
'calendar' => $calendar,
'fields' => array(
'summary' => l::get('title'),
'description' => l::get('description')
)
));
'fields' => [
'summary' => t('title'),
'description' => t('description')
]
]);
?>
```

Expand All @@ -87,7 +70,7 @@ So this is basically it. If you followed the instructions correctly, you should

### Panel

The calendar data is formatted to work perfectly with a [structure field](http://getkirby.com/docs/cheatsheet/panel-fields/structure). Take a look at the `blueprints` directory of this repo to see a full example.
The calendar data is formatted to work perfectly with a [structure field](https://getkirby.com/docs/reference/panel/fields/structure). Take a look at the `blueprints` directory of this repo to see a full example.

## Localisation

Expand Down Expand Up @@ -137,11 +120,11 @@ The behavior with different combinations of these fields is the following:

## Functions

There are two objects you can work with, `Calendar` and `Event`.
There are two classes you can work with, `Mzur\KirbyCalendar\Calendar` and `Mzur\KirbyCalendar\Event`.

### Calendar object
### Calendar class

The `Calendar` object is returned by the `calendar()` function of this plugin. It has the following functions:
The `Mzur\KirbyCalendar\Calendar` class has the following functions:

#### getAllEvents()

Expand All @@ -155,9 +138,9 @@ Returns an array of all future events of this calendar.

Returns an array of all the event fields occurring in the events of this calendar.

### Event object
### Event class

The `Event` objects are provided by the `Calendar` object. They have the following functions:
The `Mzur\KirbyCalendar\Event` class has the following functions:

#### getBeginTimestamp()

Expand Down
40 changes: 0 additions & 40 deletions blueprints/calendar.php

This file was deleted.

13 changes: 0 additions & 13 deletions calendar/calendar.php

This file was deleted.

37 changes: 37 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

load([
'Mzur\\KirbyCalendar\\Calendar' => 'Calendar.php',
'Mzur\\KirbyCalendar\\Event' => 'Event.php',
], __DIR__);

Kirby::plugin('mzur/kirby-calendar', [
'snippets' => [
'calendar-div' => __DIR__.'/snippets/calendar-div.php',
'calendar-ical' => __DIR__.'/snippets/calendar-ical.php',
'calendar-table' => __DIR__.'/snippets/calendar-table.php',
'calendar-teaser' => __DIR__.'/snippets/calendar-teaser.php',
],
'translations' => [
'de' => [
'calendar-full-time-format' => '%d %X',
'calendar-month-format' => '%B %Y',
'calendar-no-entry' => 'Zur Zeit gibt es keine Events.',
'calendar-time-format' => '%d',
'date' => 'Datum',
'description' => 'Beschreibung',
'title' => 'Titel',
'to' => 'bis',
],
'en' => [
'calendar-full-time-format' => '%d %X',
'calendar-month-format' => '%B %Y',
'calendar-no-entry' => 'There currently are no events.',
'calendar-time-format' => '%d',
'date' => 'Date',
'description' => 'Description',
'title' => 'Title',
'to' => 'to',
],
],
]);
Loading

0 comments on commit 35d5c45

Please sign in to comment.