From f9cbe9db6cb11c727f5dcb06ce1c3599f10475a5 Mon Sep 17 00:00:00 2001 From: Marc Michalsky forumZFD Date: Wed, 30 Sep 2020 15:27:55 +0200 Subject: [PATCH] implement test mode for TwingleSync --- api/v3/TwingleSync/Get.php | 8 +++++-- api/v3/TwingleSync/models/TwingleApiCall.php | 9 ++++---- api/v3/TwingleSync/models/TwingleProject.php | 23 ++++++++++++++------ 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/api/v3/TwingleSync/Get.php b/api/v3/TwingleSync/Get.php index b149b94..81ef21d 100644 --- a/api/v3/TwingleSync/Get.php +++ b/api/v3/TwingleSync/Get.php @@ -22,7 +22,7 @@ function _civicrm_api3_twingle_sync_Get_spec(&$spec) { 'description' => E::ts('The key to access the Twingle API'), ]; $spec['test'] = [ - 'name' => 'test', + 'name' => 'is_test', 'title' => E::ts('Test'), 'type' => CRM_Utils_Type::T_BOOLEAN, 'api.required' => 0, @@ -45,6 +45,9 @@ function _civicrm_api3_twingle_sync_Get_spec(&$spec) { function civicrm_api3_twingle_sync_Get($params) { $result_values = []; + // Is this call a test? + $is_test = (boolean) $params['is_test']; + // If function call provides an API key, use it instead of the API key set // on the extension settings page $apiKey = empty($params['twingle_api_key']) @@ -60,7 +63,8 @@ function civicrm_api3_twingle_sync_Get($params) { $i = 0; foreach ($projects as $project) { if (is_array($project)) { - $result_values['sync']['projects'][$i++] = $twingleApi->syncProject($project); + $result_values['sync']['projects'][$i++] = $twingleApi + ->syncProject($project, $is_test); } } diff --git a/api/v3/TwingleSync/models/TwingleApiCall.php b/api/v3/TwingleSync/models/TwingleApiCall.php index 3debc10..95bd442 100644 --- a/api/v3/TwingleSync/models/TwingleApiCall.php +++ b/api/v3/TwingleSync/models/TwingleApiCall.php @@ -106,18 +106,18 @@ class TwingleApiCall { * @throws \CiviCRM_API3_Exception * @throws \Exception */ - public function syncProject($values) { + public function syncProject(array $values, bool $is_test = FALSE) { if (is_array($values)) { $project = new TwingleProject($values); - + $result = $project->create($is_test); $result = $project->create(); if ( $result['state'] == 'exists' && $values['last_update'] > $project->getTimestamp() ) { - $result = $project->update(); + $result = $project->update($is_test); } elseif ( $result['state'] == 'exists' && @@ -134,7 +134,8 @@ class TwingleApiCall { } - public function updateProject(array $values) { + public function updateProject(array $values, bool $is_test = FALSE) { + // TODO: Implement $is_test $url = $this->protocol . 'project' . $this->baseUrl . $values['id']; return $this->curlPost($url, $values); } diff --git a/api/v3/TwingleSync/models/TwingleProject.php b/api/v3/TwingleSync/models/TwingleProject.php index ed5966e..1a38c7c 100644 --- a/api/v3/TwingleSync/models/TwingleProject.php +++ b/api/v3/TwingleSync/models/TwingleProject.php @@ -92,15 +92,18 @@ class TwingleProject { // Create project if it does not exist yet and give back the result if (!$this->exists()) { - $result = civicrm_api3('Campaign', 'create', $translatedValues); + if (!$is_test) { + $result = civicrm_api3('Campaign', 'create', $translatedValues); $this->id = $result['id']; $this->timestamp = $result['last_update']; return [ - 'title' => $this->values['title'], - 'id' => $this->id, - 'project_id' => $this->values['id'], + } + // If this is a test, do not create campaign + else { 'state' => 'created', - ]; + } + } + else { } // Give information back if project already exists return [ @@ -114,14 +117,20 @@ class TwingleProject { /** * Update an existing project * + * If true: don't do any changes + * + * @param bool $is_test + * * @return array * @throws \CiviCRM_API3_Exception */ - public function update() { + public function update(bool $is_test = FALSE) { + // Translate $value keys to custom field names $translatedValues = $this->translateValues(); - $result = civicrm_api3('Campaign', 'create', $translatedValues); + if (!$is_test) { + $result = civicrm_api3('Campaign', 'create', $translatedValues); return [ 'title' => $this->values['title'], 'id' => $this->id,