check for the existence of a case befor opening a new one

This commit is contained in:
Marc Michalsky forumZFD 2021-02-01 10:04:58 +01:00
parent 68f187c03a
commit 247874f380
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9

View file

@ -76,19 +76,32 @@ class CRM_TwingleCampaign_BAO_TwingleEvent extends Campaign {
throw new Exception($result['error_message']); throw new Exception($result['error_message']);
} }
// Start a case for event initiator // Open a case for event initiator if it does not yet exist
// TODO: save Case in Campaign and test if it already exists
// check for existence
$result = $result = civicrm_api3('Case', 'get', [
'contact_id' => $formattedValues['contact_id'],
'case_type_id' => Configuration::get('twinglecampaign_start_case'),
'subject' => $formattedValues['title'] . ' | Event-ID: ' . $formattedValues['id'],
]);
// Open a case
if ( if (
Configuration::get('twinglecampaign_start_case') Configuration::get('twinglecampaign_start_case') &&
$result['count'] == 0
) { ) {
$result = civicrm_api3('Case', 'create', [ $result = civicrm_api3('Case', 'create', [
'contact_id' => $formattedValues['contact_id'], 'contact_id' => $formattedValues['contact_id'],
'case_type_id' => Configuration::get('twinglecampaign_start_case'), 'case_type_id' => Configuration::get('twinglecampaign_start_case'),
'subject' => $formattedValues['title'], 'subject' => $formattedValues['title'] . ' | Event-ID: ' . $formattedValues['id'],
'start_date' => $formattedValues['created_at'], 'start_date' => $formattedValues['created_at'],
'status_id' => "Open", 'status_id' => "Open",
]); ]);
} }
if ($result['is_error'] != 0) {
throw new Exception('Could not create case');
}
return TRUE; return TRUE;
} }
@ -105,7 +118,8 @@ class CRM_TwingleCampaign_BAO_TwingleEvent extends Campaign {
* *
* @throws Exception * @throws Exception
*/ */
public static function formatValues(array &$values, string $direction) { public
static function formatValues(array &$values, string $direction) {
if ($direction == self::IN) { if ($direction == self::IN) {
@ -170,9 +184,9 @@ class CRM_TwingleCampaign_BAO_TwingleEvent extends Campaign {
throw new Exception( throw new Exception(
"Invalid Parameter $direction for formatValues()" "Invalid Parameter $direction for formatValues()"
); );
}
}
}
}
/** /**
* Translate array keys between CiviCRM Campaigns and Twingle * Translate array keys between CiviCRM Campaigns and Twingle
@ -188,7 +202,8 @@ class CRM_TwingleCampaign_BAO_TwingleEvent extends Campaign {
* *
* @throws Exception * @throws Exception
*/ */
public static function translateKeys(array &$values, string $direction) { public
static function translateKeys(array &$values, string $direction) {
// Get translations for fields // Get translations for fields
$field_translations = Cache::getInstance() $field_translations = Cache::getInstance()
@ -224,7 +239,8 @@ class CRM_TwingleCampaign_BAO_TwingleEvent extends Campaign {
* Returns a response array that contains title, id, event_id, project_id and * Returns a response array that contains title, id, event_id, project_id and
* status * status
*/ */
public function getResponse(string $status = NULL): array { public
function getResponse(string $status = NULL): array {
$response = [ $response = [
'title' => $this->values['description'], 'title' => $this->values['description'],
'id' => (int) $this->id, 'id' => (int) $this->id,
@ -249,7 +265,8 @@ class CRM_TwingleCampaign_BAO_TwingleEvent extends Campaign {
* @return int|null * @return int|null
* Returns a contact id * Returns a contact id
*/ */
private static function matchContact(string $names, string $email): ?int { private
static function matchContact(string $names, string $email): ?int {
$names = StringOps::split_names($names); // Hopefully just a temporary solution $names = StringOps::split_names($names); // Hopefully just a temporary solution
$firstnames = $names['firstnames']; $firstnames = $names['firstnames'];
$lastname = $names['lastname']; $lastname = $names['lastname'];
@ -269,13 +286,15 @@ class CRM_TwingleCampaign_BAO_TwingleEvent extends Campaign {
} }
} }
/** /**
* Gets the campaign id of the parent TwingleProject campaign. * Gets the campaign id of the parent TwingleProject campaign.
* *
* @return int|null * @return int|null
* @throws CiviCRM_API3_Exception * @throws CiviCRM_API3_Exception
*/ */
private function getParentCampaignId() { private
function getParentCampaignId() {
$cf_project_id = Cache::getInstance() $cf_project_id = Cache::getInstance()
->getCustomFieldMapping()['twingle_project_id']; ->getCustomFieldMapping()['twingle_project_id'];
$parentCampaign = civicrm_api3('Campaign', 'get', [ $parentCampaign = civicrm_api3('Campaign', 'get', [
@ -295,7 +314,8 @@ class CRM_TwingleCampaign_BAO_TwingleEvent extends Campaign {
* *
* @return int|string|null * @return int|string|null
*/ */
public function lastUpdate() { public
function lastUpdate() {
return self::getTimestamp($this->values['updated_at']); return self::getTimestamp($this->values['updated_at']);
} }
@ -306,7 +326,8 @@ class CRM_TwingleCampaign_BAO_TwingleEvent extends Campaign {
* *
* @return int * @return int
*/ */
public function getProjectId() { public
function getProjectId() {
return (int) $this->values['project_id']; return (int) $this->values['project_id'];
} }
@ -316,8 +337,8 @@ class CRM_TwingleCampaign_BAO_TwingleEvent extends Campaign {
* *
* @return int * @return int
*/ */
public function getEventId() { public
function getEventId() {
return (int) $this->values['id']; return (int) $this->values['id'];
} }
} }