commit 13353cc4a58e5d9b3f59b51eef69a595fa52d068 Author: kekskurse Date: Tue Dec 17 20:52:19 2024 +0100 client and c3fl sample diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..41d7c1a --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +config.php +vendor/ +.php-cs-fixer.cache +composer.lock diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..151cf5b --- /dev/null +++ b/composer.json @@ -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" + } +} diff --git a/sample/c3fl.php b/sample/c3fl.php new file mode 100644 index 0000000..ea85ea6 --- /dev/null +++ b/sample/c3fl.php @@ -0,0 +1,96 @@ + "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.

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.

Am besten kommst du einfach vorbei, wenn du fragen hast. Wir haben auch einen Public Matrix Channel #chaos-fl-public:fairydust.space.

Räumlichkeiten

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); +} diff --git a/src/Mobilizon.php b/src/Mobilizon.php new file mode 100644 index 0000000..235b65e --- /dev/null +++ b/src/Mobilizon.php @@ -0,0 +1,99 @@ +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); + } + + + + +} diff --git a/src/MobilizonAddress.php b/src/MobilizonAddress.php new file mode 100644 index 0000000..739d4c6 --- /dev/null +++ b/src/MobilizonAddress.php @@ -0,0 +1,20 @@ + $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; + + } +} diff --git a/src/MobilizonEvent.php b/src/MobilizonEvent.php new file mode 100644 index 0000000..d082d8c --- /dev/null +++ b/src/MobilizonEvent.php @@ -0,0 +1,80 @@ +beginsOn = new \DateTime(); + $this->endsOn = new \DateTime(); + $this->physicalAddress = new MobilizonAddress(); + $this->picture = new MobilizonPicture(); + $this->visibility = Query::enum(self::VISIBILITY_PUBLIC); + } + +} diff --git a/src/MobilizonPicture.php b/src/MobilizonPicture.php new file mode 100644 index 0000000..4b6e712 --- /dev/null +++ b/src/MobilizonPicture.php @@ -0,0 +1,9 @@ +