improve method inheritance

This commit is contained in:
Marc Michalsky forumZFD 2021-02-08 14:00:26 +01:00
parent 6affd53c02
commit 2a1952313e
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9
3 changed files with 83 additions and 86 deletions

View file

@ -20,6 +20,8 @@ abstract class CRM_TwingleCampaign_BAO_Campaign {
protected $prefix = NULL; protected $prefix = NULL;
protected $formattedValues;
/** /**
* ## Campaign constructor. * ## Campaign constructor.
@ -37,6 +39,56 @@ abstract class CRM_TwingleCampaign_BAO_Campaign {
$this->update($values); $this->update($values);
} }
/**
* ## Create this entity as campaign in CiviCRM
*
* Returns _TRUE_ if creation was successful or _FALSE_ if it creation failed.
*
* @param bool $no_hook
* Do not trigger postSave hook to prevent recursion
*
* @return bool
* @throws \Exception
*/
public function create(bool $no_hook = FALSE): bool {
// Prepare project values for import into database
$values_prepared_for_import = $this->values;
$this->formatValues(
$values_prepared_for_import,
self::IN
);
$this->translateKeys(
$values_prepared_for_import,
self::IN
);
$this->formattedValues = $values_prepared_for_import;
$this->translateCustomFields(
$values_prepared_for_import,
self::IN
);
// // Set id
// $values_prepared_for_import['id'] = $this->id;
// Set a flag to not trigger the hook
if ($no_hook) {
$_SESSION['CiviCRM']['de.forumzfd.twinglecampaign']['no_hook'] = TRUE;
}
// Create campaign
$result = civicrm_api3('Campaign', 'create', $values_prepared_for_import);
// Update id
$this->id = $result['id'];
// Check if campaign was created successfully
if ($result['is_error'] != 0) {
throw new Exception($result['error_message']);
}
return TRUE;
}
/** /**
* ## Update instance values * ## Update instance values
@ -137,10 +189,8 @@ abstract class CRM_TwingleCampaign_BAO_Campaign {
* ## Translate field names and custom field names * ## Translate field names and custom field names
* *
* Constants for **$direction**:<br> * Constants for **$direction**:<br>
* **Campaign::IN** translate array keys from Twingle format into * **Campaign::IN** Translate field name to custom field name <br>
* CiviCRM format <br> * **Campaign::OUT** Translate from custom field name to field name
* **Campaign::OUT** translate array keys from CiviCRM format into
* Twingle format
* *
* @param array $values * @param array $values
* array of keys to translate * array of keys to translate

View file

@ -40,66 +40,41 @@ class CRM_TwingleCampaign_BAO_TwingleEvent extends Campaign {
* ## Create the Event as a campaign in CiviCRM if it does not exist * ## Create the Event as a campaign in CiviCRM if it does not exist
* Returns _TRUE_ if creation was successful or _FALSE if it creation failed. * Returns _TRUE_ if creation was successful or _FALSE if it creation failed.
* *
* @param bool $no_hook
* @return bool * @return bool
* @throws CiviCRM_API3_Exception * @throws \CiviCRM_API3_Exception
* @throws Exception * @throws \Exception
*/ */
public function create(): bool { public function create(bool $no_hook = FALSE): bool {
// Prepare project values for import into database if (parent::create()) {
$values_prepared_for_import = $this->values;
$this::formatValues(
$values_prepared_for_import,
self::IN
);
$this->translateKeys(
$values_prepared_for_import,
self::IN
);
$formattedValues = $values_prepared_for_import;
$this->translateCustomFields(
$values_prepared_for_import,
self::IN
);
// Create campaign // check for existence
$result = civicrm_api3('Campaign', 'create', $values_prepared_for_import); $result = civicrm_api3('Case', 'get', [
'contact_id' => $this->formattedValues['contact_id'],
// Update id
$this->id = $result['id'];
// Check if campaign was created successfully
if ($result['is_error'] != 0) {
throw new Exception($result['error_message']);
}
// Open a case for event initiator if it does not yet exist
// 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 (
Configuration::get('twinglecampaign_start_case') &&
$result['count'] == 0
) {
$result = civicrm_api3('Case', 'create', [
'contact_id' => $formattedValues['contact_id'],
'case_type_id' => Configuration::get('twinglecampaign_start_case'), 'case_type_id' => Configuration::get('twinglecampaign_start_case'),
'subject' => $formattedValues['title'] . ' | Event-ID: ' . $formattedValues['id'], 'subject' => $this->formattedValues['title'] . ' | Event-ID: ' . $formattedValues['id'],
'start_date' => $formattedValues['created_at'],
'status_id' => "Open",
]); ]);
}
if ($result['is_error'] != 0) {
throw new Exception('Could not create case');
}
return TRUE; // Open a case
if (
Configuration::get('twinglecampaign_start_case') &&
$result['count'] == 0
) {
$result = civicrm_api3('Case', 'create', [
'contact_id' => $this->formattedValues['contact_id'],
'case_type_id' => Configuration::get('twinglecampaign_start_case'),
'subject' => $formattedValues['title'] . ' | Event-ID: ' . $formattedValues['id'],
'start_date' => $formattedValues['created_at'],
'status_id' => "Open",
]);
}
if ($result['is_error'] != 0) {
throw new Exception('Could not create case');
}
return TRUE;
}
} }
@ -204,7 +179,6 @@ class CRM_TwingleCampaign_BAO_TwingleEvent extends Campaign {
'id' => (int) $this->id, 'id' => (int) $this->id,
'event_id' => (int) $this->values['id'], 'event_id' => (int) $this->values['id'],
'project_id' => (int) $this->values['project_id'], 'project_id' => (int) $this->values['project_id'],
'status' => $status,
]; ];
if ($status) { if ($status) {
$response['status'] = $status; $response['status'] = $status;

View file

@ -69,34 +69,7 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign {
*/ */
public function create(bool $no_hook = FALSE): bool { public function create(bool $no_hook = FALSE): bool {
// Prepare project values for import into database $result = parent::create($no_hook);
$values_prepared_for_import = $this->values;
$this->formatValues(
$values_prepared_for_import,
self::IN
);
$this->translateKeys(
$values_prepared_for_import,
self::IN
);
$this->translateCustomFields(
$values_prepared_for_import,
self::IN
);
// Set id
$values_prepared_for_import['id'] = $this->id;
// Set a flag to not trigger the hook
if ($no_hook) {
$_SESSION['CiviCRM']['de.forumzfd.twinglecampaign']['no_hook'] = TRUE;
}
// Create campaign
$result = civicrm_api3('Campaign', 'create', $values_prepared_for_import);
// Update id
$this->id = $result['id'];
// Check if campaign was created successfully // Check if campaign was created successfully
if ($result['is_error'] == 0) { if ($result['is_error'] == 0) {