From fc515de26d473a3d4f66caecfdd9e1c4a2a296bb Mon Sep 17 00:00:00 2001 From: Marc Michalsky forumZFD Date: Tue, 29 Sep 2020 14:09:18 +0200 Subject: [PATCH] add exists() function to check if a project exists --- api/v3/TwingleSync/models/TwingleProject.php | 48 ++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/api/v3/TwingleSync/models/TwingleProject.php b/api/v3/TwingleSync/models/TwingleProject.php index 571cffa..36e9d9f 100644 --- a/api/v3/TwingleSync/models/TwingleProject.php +++ b/api/v3/TwingleSync/models/TwingleProject.php @@ -116,6 +116,54 @@ class TwingleProject { ]; } + /** + * Check if a project already exists + * + * @return bool + * @throws \CiviCRM_API3_Exception + */ + public function exists() { + + // Get custom field names + $cf_project_id = TwingleProject::$customFieldMapping['twingle_project_id']; + $cf_last_update = TwingleProject::$customFieldMapping['twingle_project_last_update']; + + $count = FALSE; + $result = []; + + // If there is more than one campaign for a project, handle the duplicates + while (!$count) { + $result = civicrm_api3('Campaign', 'get', [ + 'sequential' => 1, + 'return' => ['id', $cf_last_update], + 'is_active' => '1', + $cf_project_id => $this->values['id'], + ]); + + if ($result['count'] > 1) { + TwingleProject::handleDuplicates($result); + } + else { + $count = TRUE; + } + } + + if ($result['count'] == 1) { + return TRUE; + } + else { + return FALSE; + } + } + + public static function fetch($id) { + $result = civicrm_api3('Campaign', 'getsingle', [ + 'sequential' => 1, + 'id' => $id, + ]); + + return new TwingleProject($result, TRUE); + } /** * Translate $value keys to custom field names *