improve method inheritance
This commit is contained in:
parent
6affd53c02
commit
2a1952313e
3 changed files with 83 additions and 86 deletions
|
@ -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
|
||||||
|
|
|
@ -40,46 +40,20 @@ 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
|
|
||||||
$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']);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open a case for event initiator if it does not yet exist
|
|
||||||
|
|
||||||
// check for existence
|
// check for existence
|
||||||
$result = $result = civicrm_api3('Case', 'get', [
|
$result = civicrm_api3('Case', 'get', [
|
||||||
'contact_id' => $formattedValues['contact_id'],
|
'contact_id' => $this->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'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Open a case
|
// Open a case
|
||||||
|
@ -88,7 +62,7 @@ class CRM_TwingleCampaign_BAO_TwingleEvent extends Campaign {
|
||||||
$result['count'] == 0
|
$result['count'] == 0
|
||||||
) {
|
) {
|
||||||
$result = civicrm_api3('Case', 'create', [
|
$result = civicrm_api3('Case', 'create', [
|
||||||
'contact_id' => $formattedValues['contact_id'],
|
'contact_id' => $this->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' => $formattedValues['title'] . ' | Event-ID: ' . $formattedValues['id'],
|
||||||
'start_date' => $formattedValues['created_at'],
|
'start_date' => $formattedValues['created_at'],
|
||||||
|
@ -101,6 +75,7 @@ class CRM_TwingleCampaign_BAO_TwingleEvent extends Campaign {
|
||||||
|
|
||||||
return TRUE;
|
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;
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue