client and c3fl sample
This commit is contained in:
commit
13353cc4a5
8 changed files with 393 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
config.php
|
||||
vendor/
|
||||
.php-cs-fixer.cache
|
||||
composer.lock
|
16
composer.json
Normal file
16
composer.json
Normal file
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"name": "kekskurse/mobilizon-php",
|
||||
"description": "Simple API Client for some mobilizoin api functions",
|
||||
"type": "library",
|
||||
"license": "MIT",
|
||||
"minimum-stability": "dev",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"kekskurse\\mobilizon\\": "src/"
|
||||
}
|
||||
},
|
||||
"require": {
|
||||
"rdx/graphql-query": "dev-master",
|
||||
"guzzlehttp/guzzle": "^7.0"
|
||||
}
|
||||
}
|
96
sample/c3fl.php
Normal file
96
sample/c3fl.php
Normal file
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
|
||||
include("vendor/autoload.php");
|
||||
$token = "OVERWRITE IN CONFIG.php";
|
||||
include("config.php");
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use kekskurse\mobilizon\Mobilizon;
|
||||
use kekskurse\mobilizon\MobilizonAddress;
|
||||
use kekskurse\mobilizon\MobilizonEvent;
|
||||
|
||||
$token = "overwrite in config.php";
|
||||
include("config.php");
|
||||
|
||||
$client = new Client([
|
||||
"base_uri" => "https://events.gutes.team",
|
||||
'timeout' => 2.0,
|
||||
"headers" => [
|
||||
"Authorization" => "bearer ".$token,
|
||||
]
|
||||
]);
|
||||
|
||||
//Calculate the next 3 Events for OpenSpace
|
||||
$date = new DateTime('next tuesday');
|
||||
$dateList = [$date];
|
||||
for($i = 1; $i < 1; $i++) {
|
||||
$newDate = clone end($dateList);
|
||||
$newDate->modify('+7 days');
|
||||
$dateList[] = $newDate;
|
||||
}
|
||||
|
||||
//Remove Events in public holidays
|
||||
$dates = file_get_contents("https://get.api-feiertage.de/?states=sh");
|
||||
$holidayData = json_decode($dates, true);
|
||||
$listOfHolidays = [];
|
||||
foreach($holidayData["feiertage"] as $date) {
|
||||
if ($date["sh"]) {
|
||||
$listOfHolidays[$date["date"]] = true;
|
||||
}
|
||||
}
|
||||
|
||||
foreach($dateList as $key => $value) {
|
||||
if($listOfHolidays[$value->format("Y-m-d")]) {
|
||||
unset($dateList[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove if Date already exists in mobilizion
|
||||
$mobilizon = Mobilizon::getInstance($client);
|
||||
/** @var MobilizonEvent[] $events */
|
||||
$events = $mobilizon->getEventByGroup("c3fl", Mobilizon::EVENT_FIELDS);
|
||||
|
||||
foreach($dateList as $key => $value) {
|
||||
foreach($events as $event) {
|
||||
if($event->beginsOn->format("Y-m-d") == $value->format("Y-m-d") && $event->title = "Open Space") {
|
||||
unset($dateList[$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
echo "To creat:\r\n";
|
||||
var_dump($dateList);
|
||||
|
||||
|
||||
echo "Events:\r\n";
|
||||
var_dump($events);
|
||||
|
||||
|
||||
foreach($dateList as $event) {
|
||||
|
||||
$event->setTime(18, 00);
|
||||
$end = clone $event;
|
||||
$end->modify("+3 hours");
|
||||
$newEvent = new MobilizonEvent();
|
||||
$newEvent->beginsOn = $event;
|
||||
$newEvent->endsOn = $end;
|
||||
$newEvent->title = "Open Space";
|
||||
$newEvent->physicalAddress->description = "Chaostreff Flensburg";
|
||||
$newEvent->physicalAddress->street = "Apenrader Str. 49";
|
||||
$newEvent->physicalAddress->postalCode = "24939";
|
||||
$newEvent->physicalAddress->locality = "Flensburg";
|
||||
$newEvent->physicalAddress->country = "Germany";
|
||||
$newEvent->physicalAddress->region = "Schleswig-Holstein";
|
||||
$newEvent->physicalAddress->geom = "9.4235724;54.8045086";
|
||||
$newEvent->physicalAddress->type = "house";
|
||||
$newEvent->physicalAddress->timezone = "Europe/Berlin";
|
||||
$newEvent->picture->mediaId = 1;
|
||||
$newEvent->organizerActorId = 3;
|
||||
$newEvent->attributedToId = 7;
|
||||
|
||||
$newEvent->description = "Einmal in der Woche gibt es das sogenannte OpenSpace treffen. Dort sind alle, egal ob Mitglied oder nicht, eingeladen vorbeizukommen. Wir treffen uns jeden Dienstag ab 19 Uhr in unseren Räumen in der Apenrader Str. 49 in Flensburg.<br><br>Was dann passiert steht den Mitgliedern und Gästen frei. Es gibt aber immer die Möglichkeit sich mit anderen auszutauschen, an Projekten zu arbeiten oder einfach nur zu quatschen. Du kannst alles aus unserer Werkstatt benutzen wie: Prusa 3D Drucker, Lötkolben, Oszilloskop, Akkuschrauber, Sägen und vieles mehr. Zum austausch haben wir oben eine Launch, Sofa Ecke und einen Tisch mit Stühlen. Außerdem haben wir einen Beamer und eine Leinwand für Vorträge und Präsentationen.<br><br>Am besten kommst du einfach vorbei, wenn du fragen hast. Wir haben auch einen Public Matrix Channel <a href='https://matrix.to/#/#chaos-fl-public:fairydust.space'>#chaos-fl-public:fairydust.space</a>.<h3>Räumlichkeiten</h3>Unsere Räumlichkeiten befinden sich in der Apenrader Str. 49. Wir haben unten einen geteilten Gemeinschaftsraum mit einer Küche und Toiletten. Oben eine kleine Werkstatt und eine Lounge Raum sowie ein wenig Lagerfläche. Nur der Gemeinschaftsraum ist ohne Umwege barrierefrei erreichbar. Wir haben leider keine Barriefreien Toiletten. Das Haus gehört zum Sydelsvigsk Forening, dem kulturelle Dachverband der dänischen Minderheit in Südschleswig und wird Tønnsethuset genannt.";
|
||||
|
||||
$mobilizon->createEvent($newEvent);
|
||||
}
|
99
src/Mobilizon.php
Normal file
99
src/Mobilizon.php
Normal file
|
@ -0,0 +1,99 @@
|
|||
<?php
|
||||
|
||||
namespace kekskurse\mobilizon;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use rdx\graphqlquery\Query;
|
||||
|
||||
class Mobilizon
|
||||
{
|
||||
private static $_instance = null;
|
||||
private $guzzle;
|
||||
public const EVENT_FIELDS = ["id", "beginsOn", "category", "description", "draft", "endsOn", "externalParticipationUrl", "joinOptions", "language", "onlineAddress", "physicalAddress", "status", "title"];
|
||||
public const PHYSICAL_ADRESS_FIELDS = ["country", "description", "geom", "id", "locality", "originId", "postalCode", "region", "street", "timezone", "type", "url"];
|
||||
|
||||
public function __construct(Client $client)
|
||||
{
|
||||
$this->guzzle = $client;
|
||||
}
|
||||
|
||||
|
||||
public static function getInstance(Client $client): Mobilizon
|
||||
{
|
||||
if (self::$_instance === null) {
|
||||
self::$_instance = new self($client);
|
||||
}
|
||||
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
public function createEvent(MobilizonEvent $event): void
|
||||
{
|
||||
$query = Query::mutation("CreateEvent");
|
||||
$query->field("createEvent")->attributes($event->eventArray());
|
||||
$query->createEvent->fields('id');
|
||||
|
||||
$response = $this->guzzle->request("POST", "/api", [
|
||||
"http_errors" => true,
|
||||
"headers" => [
|
||||
"Content-Type" => "application/json",
|
||||
"Accept" => "application/json"
|
||||
],
|
||||
"body" => json_encode(["query" => $query->build()])
|
||||
]);
|
||||
|
||||
$responseData = (string) $response->getBody();
|
||||
var_dump($responseData);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function getEventByGroup(string $preferredUsername, array $field): array
|
||||
{
|
||||
$query = Query::query("Group");
|
||||
$query->fields(['group']);
|
||||
$query->group->fields("organizedEvents");
|
||||
$query->group->attribute("preferredUsername", $preferredUsername);
|
||||
$query->group->organizedEvents->fields("total", "elements");
|
||||
$query->group->organizedEvents->elements->fields($field);
|
||||
if(in_array("physicalAddress", $field)) {
|
||||
$query->group->organizedEvents->elements->physicalAddress->fields(self::PHYSICAL_ADRESS_FIELDS);
|
||||
}
|
||||
|
||||
$string = $query->build();
|
||||
var_dump($string);
|
||||
|
||||
$resData = $this->executeQuery($query->build());
|
||||
$eventList = [];
|
||||
|
||||
foreach($resData["data"]["group"]["organizedEvents"]["elements"] as $element) {
|
||||
$event = new MobilizonEvent();
|
||||
$event->load($element);
|
||||
$eventList[] = $event;
|
||||
}
|
||||
|
||||
return $eventList;
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function executeQuery(string $query): array
|
||||
{
|
||||
$response = $this->guzzle->request("POST", "/api", [
|
||||
"http_errors" => false,
|
||||
"headers" => [
|
||||
"Content-Type" => "application/json",
|
||||
"Accept" => "Application/json",
|
||||
],
|
||||
"body" => json_encode(["query" => $query])
|
||||
]);
|
||||
|
||||
$responseContent = (string) $response->getBody();
|
||||
|
||||
return json_decode($responseContent, true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
20
src/MobilizonAddress.php
Normal file
20
src/MobilizonAddress.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace kekskurse\mobilizon;
|
||||
|
||||
class MobilizonAddress extends MobilizonDataObject
|
||||
{
|
||||
public string $country = "";
|
||||
public string $street = "";
|
||||
public string $postalCode = "";
|
||||
public string $region = "";
|
||||
public string $description = "";
|
||||
public string $geom = "";
|
||||
public string $id = "";
|
||||
public string $locality = "";
|
||||
public string $originId = "";
|
||||
public string $timezone = "";
|
||||
public string $type = "";
|
||||
public string $url = "";
|
||||
|
||||
}
|
69
src/MobilizonDataObject.php
Normal file
69
src/MobilizonDataObject.php
Normal file
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
namespace kekskurse\mobilizon;
|
||||
|
||||
class MobilizonDataObject
|
||||
{
|
||||
public function load(array $data)
|
||||
{
|
||||
foreach($data as $key => $value) {
|
||||
if (is_null($value)) {
|
||||
continue;
|
||||
}
|
||||
$type = gettype($this->$key);
|
||||
if($type == "object") {
|
||||
$class = get_class($this->$key);
|
||||
if($class == "DateTime") {
|
||||
$this->$key = new \DateTime($value);
|
||||
continue;
|
||||
}
|
||||
if($class == "kekskurse\mobilizon\MobilizonAddress") {
|
||||
$address = new MobilizonAddress();
|
||||
$address->load($value);
|
||||
$this->$key = $address;
|
||||
continue;
|
||||
}
|
||||
|
||||
throw new \Exception('Unhandelt Object "'.$class.'" vor parsing');
|
||||
}
|
||||
$this->$key = $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function eventArray(): array
|
||||
{
|
||||
$fields = get_class_vars(get_class($this));
|
||||
$dataArray = [];
|
||||
foreach($fields as $key => $value) {
|
||||
$type = gettype($this->$key);
|
||||
if($type == "object") {
|
||||
$class = get_class($this->$key);
|
||||
if($class == "DateTime") {
|
||||
$dataArray[$key] = $this->$key->format("c");
|
||||
continue;
|
||||
}
|
||||
if($class == "kekskurse\mobilizon\MobilizonAddress") {
|
||||
$dataArray[$key] = $this->$key->eventArray();
|
||||
continue;
|
||||
}
|
||||
if($class == "kekskurse\mobilizon\MobilizonPicture") {
|
||||
$dataArray[$key] = $this->$key->eventArray();
|
||||
continue;
|
||||
}
|
||||
if($class == "rdx\graphqlquery\Enum") {
|
||||
$dataArray[$key] = $this->$key;
|
||||
continue;
|
||||
}
|
||||
|
||||
throw new \Exception("Unhandel Object ".$class);
|
||||
}
|
||||
if(!empty($this->$key)) {
|
||||
$dataArray[$key] = $this->$key;
|
||||
}
|
||||
}
|
||||
|
||||
return $dataArray;
|
||||
|
||||
}
|
||||
}
|
80
src/MobilizonEvent.php
Normal file
80
src/MobilizonEvent.php
Normal file
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
|
||||
namespace kekskurse\mobilizon;
|
||||
|
||||
use rdx\graphqlquery\Enum;
|
||||
use rdx\graphqlquery\Query;
|
||||
|
||||
class MobilizonEvent extends MobilizonDataObject
|
||||
{
|
||||
public const CATEGORY_ARTS = "ARTS";
|
||||
public const CATEGORY_AUTO_BOAT_AIR = "AUTO_BOAT_AIR";
|
||||
public const CATEGORY_BOOK_CLUBS = "BOOK_CLUBS";
|
||||
public const CATEGORY_BUSINESS = "BUSINESS";
|
||||
public const CATEGORY_CAUSES = "CAUSES";
|
||||
public const CATEGORY_COMEDY = "COMEDY";
|
||||
public const CATEGORY_COMMUNITY = "COMMUNITY";
|
||||
public const CATEGORY_CRAFTS = "CRAFTS";
|
||||
public const CATEGORY_FAMILY_EDUCATION = "FAMILY_EDUCATION";
|
||||
public const CATEGORY_FASHION_BEAUTY = "FASHION_BEAUTY";
|
||||
public const CATEGORY_FILM_MEDIA = "FILM_MEDIA";
|
||||
public const CATEGORY_FOOD_DRINK = "FOOD_DRINK";
|
||||
public const CATEGORY_GAMES = "GAMES";
|
||||
public const CATEGORY_HEALTH = "HEALTH";
|
||||
public const CATEGORY_LANGUAGE_CULTURE = "LANGUAGE_CULTURE";
|
||||
public const CATEGORY_LEARNING = "LEARNING";
|
||||
public const CATEGORY_LGBTQ = "LGBTQ";
|
||||
public const CATEGORY_MEETING = "MEETING";
|
||||
public const CATEGORY_MOVEMENTS_POLITICS = "MOVEMENTS_POLITICS";
|
||||
public const CATEGORY_MUSIC = "MUSIC";
|
||||
public const CATEGORY_NETWORKING = "NETWORKING";
|
||||
public const CATEGORY_OUTDOORS_ADVENTURE = "OUTDOORS_ADVENTURE";
|
||||
public const CATEGORY_PARTY = "PARTY";
|
||||
public const CATEGORY_PERFORMING_VISUAL_ARTS = "PERFORMING_VISUAL_ARTS";
|
||||
public const CATEGORY_PETS = "PETS";
|
||||
public const CATEGORY_PHOTOGRAPHY = "PHOTOGRAPHY";
|
||||
public const CATEGORY_SCIENCE_TECH = "SCIENCE_TECH";
|
||||
public const CATEGORY_SPIRITUALITY_RELIGION_BELIEFS = "SPIRITUALITY_RELIGION_BELIEFS";
|
||||
public const CATEGORY_SPORTS = "SPORTS";
|
||||
public const CATAGORY_THEATRE = "THEATRE";
|
||||
|
||||
public const JOIN_OPTION_EXTERNAL = "EXTERNAL";
|
||||
public const JOIN_OPTION_FREE = "FREE";
|
||||
public const JOIN_OPTION_INVITE = "INVITE";
|
||||
public const JOIN_OPTION_RESTRICTED = "RESTRICTED";
|
||||
|
||||
public const VISIBILITY_PRIVATE = "PRIVATE";
|
||||
public const VISIBILITY_PUBLIC = "PUBLIC";
|
||||
public const VISIBILITY_RESTRICTED = "RESTRICTED";
|
||||
public const VISIBILITY_UNLISTED = "UNLISTED";
|
||||
|
||||
|
||||
|
||||
public int $id = 0;
|
||||
public int $attributedToId = 0;
|
||||
public \DateTime $beginsOn;
|
||||
public string $category = "";
|
||||
public string $description = "";
|
||||
public bool $draft = false;
|
||||
public \DateTime $endsOn;
|
||||
public string $externalParticipationUrl = "";
|
||||
public string $joinOptions = "";
|
||||
public string $language = "";
|
||||
public string $onlineAddress = "";
|
||||
public string $organizerActorId = "";
|
||||
public MobilizonAddress $physicalAddress;
|
||||
public string $status = "";
|
||||
public string $title = "";
|
||||
public Enum $visibility;
|
||||
public MobilizonPicture $picture;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->beginsOn = new \DateTime();
|
||||
$this->endsOn = new \DateTime();
|
||||
$this->physicalAddress = new MobilizonAddress();
|
||||
$this->picture = new MobilizonPicture();
|
||||
$this->visibility = Query::enum(self::VISIBILITY_PUBLIC);
|
||||
}
|
||||
|
||||
}
|
9
src/MobilizonPicture.php
Normal file
9
src/MobilizonPicture.php
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace kekskurse\mobilizon;
|
||||
|
||||
class MobilizonPicture extends MobilizonDataObject
|
||||
{
|
||||
public string $mediaId = "";
|
||||
|
||||
}
|
Loading…
Reference in a new issue