From fceac703363a85496fcbcebe62fd708d90b8c6a9 Mon Sep 17 00:00:00 2001 From: Marc Michalsky forumZFD Date: Fri, 13 Nov 2020 17:17:33 +0100 Subject: [PATCH] start a case for event initiator --- CRM/TwingleCampaign/BAO/TwingleEvent.php | 32 ++++++++++++++++++------ api/v3/TwingleSync/Post.php | 11 +++++++- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/CRM/TwingleCampaign/BAO/TwingleEvent.php b/CRM/TwingleCampaign/BAO/TwingleEvent.php index d4325a6..f6e39c3 100644 --- a/CRM/TwingleCampaign/BAO/TwingleEvent.php +++ b/CRM/TwingleCampaign/BAO/TwingleEvent.php @@ -6,11 +6,11 @@ namespace CRM\TwingleCampaign\BAO; use Civi; use CRM_TwingleCampaign_ExtensionUtil as E; use CRM\TwingleCampaign\Utils\ExtensionCache as Cache; -use CRM\TwingleCampaign\BAO\Campaign; use Exception; use CiviCRM_API3_Exception; include_once E::path() . '/CRM/TwingleCampaign/BAO/Campaign.php'; +include_once E::path() . '/CRM/TwingleCampaign/BAO/Configuration.php'; include_once E::path() . '/CRM/TwingleCampaign/Utils/ExtensionCache.php'; class TwingleEvent extends Campaign { @@ -46,6 +46,7 @@ class TwingleEvent extends Campaign { * * @param array $values * @param TwingleApiCall $twingleApi + * @param int $user * @param bool $is_test * If TRUE, don't do any changes * @@ -53,11 +54,12 @@ class TwingleEvent extends Campaign { * Returns a response array that contains title, id, event_id, project_id * and status or NULL if $values is not an array * - * @throws CiviCRM_API3_Exception + * @throws \CiviCRM_API3_Exception */ public static function sync( array $values, TwingleApiCall &$twingleApi, + int $user, bool $is_test = FALSE ) { @@ -93,7 +95,7 @@ class TwingleEvent extends Campaign { // ... if not, get embed data and create event try { - $result = $event->create($is_test); + $result = $event->create($user, $is_test); } catch (Exception $e) { $errorMessage = $e->getMessage(); @@ -154,16 +156,16 @@ class TwingleEvent extends Campaign { /** * Create the Event as a campaign in CiviCRM if it does not exist * + * @param int $user * @param bool $is_test * If true: don't do any changes * * @return array * Returns a response array that contains title, id, project_id and status * - * @throws CiviCRM_API3_Exception - * @throws Exception + * @throws \CiviCRM_API3_Exception */ - public function create(bool $is_test = FALSE) { + public function create(int $user, bool $is_test = FALSE) { // Create campaign only if it does not already exist if (!$is_test) { @@ -178,6 +180,7 @@ class TwingleEvent extends Campaign { $values_prepared_for_import, self::IN ); + $formattedValues = $values_prepared_for_import; $this->translateCustomFields( $values_prepared_for_import, self::IN @@ -200,6 +203,21 @@ class TwingleEvent extends Campaign { $response = $this->getResponse("$this->className creation failed"); } + // Start a case for event initiator + if ( + $result['is_error'] == 0 && + Configuration::get('twinglecampaign_start_case') + ) { + $result = civicrm_api3('Case', 'create', [ + 'contact_id' => $formattedValues['contact_id'], + 'creator_id' => $user, + 'case_type_id' => Configuration::get('twinglecampaign_start_case'), + 'subject' => $formattedValues['title'], + 'start_date' => $formattedValues['created_at'], + 'status_id' => "Open", + ]); + } + } // If this is a test, do not create campaign else { @@ -247,7 +265,7 @@ class TwingleEvent extends Campaign { } if ($values['user_name']) { - $values['user_name'] = $this->matchContact( + $values['contact_id'] = $this->matchContact( $values['user_name'], $values['user_email'] ); diff --git a/api/v3/TwingleSync/Post.php b/api/v3/TwingleSync/Post.php index b2c6eb4..f31264c 100644 --- a/api/v3/TwingleSync/Post.php +++ b/api/v3/TwingleSync/Post.php @@ -58,6 +58,15 @@ function _civicrm_api3_twingle_sync_Post_spec(array &$spec) { function civicrm_api3_twingle_sync_Post(array $params) { $result_values = []; + // Who is calling? + $api_key = $_REQUEST['api_key']; + $user = CRM_Core_DAO::getFieldValue( + 'CRM_Contact_DAO_Contact', + $api_key, + 'id', + 'api_key' + ); + // Is this call a test? $is_test = (boolean) $params['is_test']; @@ -93,7 +102,7 @@ function civicrm_api3_twingle_sync_Post(array $params) { foreach ($events as $event) { if ($event) { $result_values['sync']['events'][$j++] = - TwingleEvent::sync($event, $twingleApi, $is_test); + TwingleEvent::sync($event, $twingleApi, $user, $is_test); } } }