From 50b0ff7da8fa8ee5be7a3022d3e75dd2fbe08ec0 Mon Sep 17 00:00:00 2001 From: Marc Michalsky forumZFD Date: Tue, 29 Sep 2020 21:47:03 +0200 Subject: [PATCH] preparation for updating via Twingle API --- api/v3/TwingleSync/models/TwingleApiCall.php | 32 ++++++++++++++++++-- api/v3/TwingleSync/models/TwingleProject.php | 9 ++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/api/v3/TwingleSync/models/TwingleApiCall.php b/api/v3/TwingleSync/models/TwingleApiCall.php index 0d95f42..3debc10 100644 --- a/api/v3/TwingleSync/models/TwingleApiCall.php +++ b/api/v3/TwingleSync/models/TwingleApiCall.php @@ -119,15 +119,24 @@ class TwingleApiCall { ) { $result = $project->update(); } + elseif ( + $result['state'] == 'exists' && + $values['last_update'] < $project->getTimestamp() + ) { + $result = $this->updateProject($project->export()); + } return $result; - } else { - return null; + } + else { + return NULL; } } - public function updateProject() { + public function updateProject(array $values) { + $url = $this->protocol . 'project' . $this->baseUrl . $values['id']; + return $this->curlPost($url, $values); } public function updateEvent() { @@ -168,4 +177,21 @@ class TwingleApiCall { return $response; } + private function curlPost($url, $data) { + $curl = curl_init($url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_HTTPHEADER, [ + "x-access-code: $this->apiKey", + 'Content-Type: application/json', + ]); + curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); + $response = json_decode(curl_exec($curl), TRUE); + if (empty($response)) { + $response = curl_error($curl); + } + curl_close($curl); + return $response; + } + } \ No newline at end of file diff --git a/api/v3/TwingleSync/models/TwingleProject.php b/api/v3/TwingleSync/models/TwingleProject.php index a8dfd12..ed5966e 100644 --- a/api/v3/TwingleSync/models/TwingleProject.php +++ b/api/v3/TwingleSync/models/TwingleProject.php @@ -130,6 +130,15 @@ class TwingleProject { ]; } + /** + * Export values + * + * @return array + */ + public function export() { + return $this->values; + } + /** * Check if a project already exists *