1
0
mirror of https://github.com/dwaxweiler/connector-mobilizon synced 2025-06-05 21:59:25 +02:00
This commit is contained in:
Daniel Waxweiler
2022-05-19 07:50:18 +02:00
parent 367a1c97b2
commit 7322f196e9
12 changed files with 2412 additions and 5 deletions

View File

@ -0,0 +1,33 @@
<?php
final class DateTimeWrapper {
private $dateTime;
private $locale;
private $timeZone;
public function __construct(string $text, string $locale = 'en-GB', string $timeZone = 'utc') {
if (!$locale) {
$locale = 'en-GB';
}
if (!$timeZone) {
$timeZone = 'utc';
}
$this->dateTime = new DateTime($text);
$this->locale = $locale;
$this->timeZone = new DateTimeZone($timeZone);
}
public function getShortDate(): string {
$formatter = IntlDateFormatter::create($this->locale, IntlDateFormatter::SHORT, IntlDateFormatter::NONE, $this->timeZone);
return $formatter->format($this->dateTime);
}
public function getOffset(): string {
return $this->timeZone->getOffset($this->dateTime);
}
public function get24Time(): string {
$formatter = IntlDateFormatter::create($this->locale, IntlDateFormatter::NONE, IntlDateFormatter::SHORT, $this->timeZone);
return $formatter->format($this->dateTime);
}
}

View File

@ -27,12 +27,18 @@ class EventsListWidget extends \WP_Widget {
$classNamePrefix = NAME;
$eventsCount = $options['eventsCount'];
$locale = str_replace('_', '-', get_locale());
$locale = str_replace('_', '-', get_locale()); // TODO _ is okay too.
$groupName = isset($options['groupName']) ? $options['groupName'] : '';
$url = Settings::getUrl();
$timeZone = wp_timezone_string();
$isShortOffsetNameShown = Settings::isShortOffsetNameShown();
if ($groupName) {
$data = GraphQlClient::get_upcoming_events_by_group_name($url, (int) $eventsCount, $groupName); // TODO wrap and put into shortcut as well
} else {
$data = GraphQlClient::get_upcoming_events($url, (int) $eventsCount);
}
require dirname(__DIR__) . '/view/events-list.php';
echo $args['after_widget'];

View File

@ -0,0 +1,40 @@
<?php
final class Formatter
{
public static function format_date(string $locale, string $timeZone, string $start, ?string $end, bool $isShortOffsetNameShown): string {
$startDateTime = new DateTimeWrapper($start, $locale, $timeZone);
$dateText = $startDateTime->getShortDate();
$dateText .= ' ' . $startDateTime->get24Time();
if (!$end && $isShortOffsetNameShown) {
$dateText .= ' (' . $startDateTime->getOffset() . ')';
}
if ($end) {
$endDateTime = new DateTimeWrapper($end, $locale, $timeZone);
if ($startDateTime->getShortDate() != $endDateTime->getShortDate()) {
$dateText .= ' - ';
$dateText .= $endDateTime->getShortDate() . ' ';
} else {
$dateText .= ' - ';
}
$dateText .= $endDateTime->get24Time();
if ($isShortOffsetNameShown) {
$dateText .= ' (' . $endDateTime->getOffset() . ')';
}
}
return $dateText;
}
public static function format_location(string $description, string $locality): string {
$location = '';
if ($description && trim($description)) {
$location .= trim($description);
}
if ($location && $locality) {
$location .= ', ';
}
if ($locality) {
$location .= $locality;
}
return $location;
}
}

View File

@ -0,0 +1,116 @@
<?php
namespace MobilizonConnector;
// Exit if this file is called directly.
if (!defined('ABSPATH')) {
exit;
}
final class GraphQlClient {
public static function query(string $endpoint, string $query, array $variables = [], ?string $token = null): array
{
$headers = ['Content-Type: application/json'];
if ($token !== null) {
$headers[] = "Authorization: bearer $token";
}
$context = stream_context_create([
'http' => [
'method' => 'POST',
'header' => $headers,
'content' => json_encode(['query' => $query, 'variables' => $variables]),
]
]);
$data = @file_get_contents($endpoint, false, $context);
if ($data === false) {
$error = error_get_last();
throw new \ErrorException($error['message'], $error['type']);
}
return json_decode($data, true);
}
public static function get_upcoming_events(string $url, int $limit): array {
$query = <<<'GRAPHQL'
query ($limit: Int) {
events(limit: $limit) {
elements {
id,
title,
url,
beginsOn,
endsOn,
physicalAddress {
description,
locality
}
},
total
}
}
GRAPHQL;
$endpoint = $url . '/api';
// const dataInCache = SessionCache.get(sessionStorage, {
// url,
// query,
// variables: { limit },
// })
// if (dataInCache !== null) {
// return Promise.resolve(dataInCache)
// }
$data = self::query($endpoint, $query, ['limit' => $limit]);
// SessionCache.add(sessionStorage, { url, query, variables: { limit } }, data)
return $data;
}
public static function get_upcoming_events_by_group_name(string $url, int $limit, string $groupName): array {
$query = <<<'GRAPHQL'
query ($afterDatetime: DateTime, $groupName: String, $limit: Int) {
group(preferredUsername: $groupName) {
organizedEvents(afterDatetime: $afterDatetime, limit: $limit) {
elements {
id,
title,
url,
beginsOn,
endsOn,
physicalAddress {
description,
locality
}
},
total
}
}
}
GRAPHQL;
$endpoint = $url . '/api';
// const afterDatetime = DateTimeWrapper.getCurrentDatetimeAsString()
// const dataInCache = SessionCache.get(sessionStorage, {
// url,
// query,
// variables: { afterDatetime, groupName, limit },
// })
// if (dataInCache !== null) {
// return Promise.resolve(dataInCache)
// }
$afterDatetime = date(DateTime::ISO8601);
$data = self::query($endpoint, $query, ['afterDatetime'=> $afterDatetime, 'groupName' => $groupName, 'limit' => $limit]);
// return request(url, query, { afterDatetime, groupName, limit }).then(
// (data) => {
// SessionCache.add(
// sessionStorage,
// { url, query, variables: { afterDatetime, groupName, limit } },
// data
// )
// return Promise.resolve(data)
// }
// )
return $data;
}
}