👾 bug fix: TwingleCampaign cannot find parent campaign

This commit is contained in:
Marc Michalsky forumZFD 2021-04-12 14:40:04 +02:00
parent a658190a68
commit cc6777e206
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9
5 changed files with 36 additions and 9 deletions

View file

@ -32,7 +32,7 @@ class CRM_TwingleCampaign_BAO_TwingleCampaign {
$this->id = $id ?? NULL;
$this->values['campaign_type_id'] = 'twingle_campaign';
if ($this->id) {
if (!isset($this->id)) {
$this->update($values);
$this->getParentProject();
$this->createCid();
@ -341,10 +341,11 @@ class CRM_TwingleCampaign_BAO_TwingleCampaign {
*/
private
function update(array $values) {
$filter = ExtensionCache::getInstance()->getTemplates()['TwingleCampaign'];
$filter = ExtensionCache::getInstance()
->getTemplates()['TwingleCampaign']['campaign_data'];
foreach ($values as $key => $value) {
if (in_array($key, $filter)) {
$this->values[$key] = $values[$key];
$this->values[$key] = $value;
}
}
}

View file

@ -27,7 +27,7 @@ class CRM_TwingleCampaign_BAO_TwingleEvent extends Campaign {
$this->id_custom_field = Cache::getInstance()
->getCustomFieldMapping()['twingle_event_id'];
if (isset($this->values['parent_id'])) {
if (!isset($this->values['parent_id'])) {
try {
$this->values['parent_id'] = $this->getParentCampaignId();
} catch (CiviCRM_API3_Exception $e) {

View file

@ -62,7 +62,7 @@ function civicrm_api3_twingle_campaign_Create(array $params): array {
$params = array_intersect_key($params, $allowed_params);
// instantiate TwingleCampaign
$campaign = new TwingleCampaign($params, $params['id']);
$campaign = new TwingleCampaign($params);
// Try to create the TwingleCampaign
try {

View file

@ -33,6 +33,13 @@ function _civicrm_api3_twingle_campaign_Get_spec(array &$spec) {
'api.required' => 0,
'description' => E::ts('Twingle ID of the parent TwingleProject'),
];
$spec['parent_id'] = [
'name' => 'parent_id',
'title' => E::ts('Parent Project ID'),
'type' => CRM_Utils_Type::T_INT,
'api.required' => 0,
'description' => E::ts('ID of the parent TwingleProject'),
];
$spec['name'] = [
'name' => 'name',
'title' => E::ts('Campaign Name'),
@ -122,6 +129,8 @@ function civicrm_api3_twingle_campaign_Get(array $params): array {
// Include parent TwingleProject id in $params
$params['parent_id'] = $project['id'];
}
elseif (isset($params['parent_id'])) {
// Include campaign type ot TwingleCampaigns in $params
$params['campaign_type_id'] = $twingle_campaign_campaign_type_id;
@ -157,7 +166,7 @@ function civicrm_api3_twingle_campaign_Get(array $params): array {
}
// Translate custom fields
if (!empty($campaigns)) {
if (!empty($campaigns['values'])) {
$custom_field_mapping_reverse =
array_flip(Cache::getInstance()->getCustomFieldMapping());
@ -171,7 +180,7 @@ function civicrm_api3_twingle_campaign_Get(array $params): array {
$returnValues[$campaign['id']][$key] = $value;
}
}
foreach($returnValues[$campaign['id']] as $key => $value) {
foreach ($returnValues[$campaign['id']] as $key => $value) {
if ($key != 'twingle_campaign_id' && strpos($key, 'twingle_campaign_') === 0) {
$returnValues[$campaign['id']][str_replace('twingle_campaign_', '', $key)]
= $value;

View file

@ -20,12 +20,19 @@ function _civicrm_api3_twingle_campaign_Sync_spec(array &$spec) {
'description' => E::ts('The Twingle Campaign ID'),
];
$spec['project_id'] = [
'name' => 'project_id',
'name' => 'parent_project_id',
'title' => E::ts('Parent Twingle Project ID'),
'type' => CRM_Utils_Type::T_INT,
'api.required' => 0,
'description' => E::ts('Twingle ID of the parent TwingleProject'),
];
$spec['parent_id'] = [
'name' => 'parent_id',
'title' => E::ts('Parent Project ID'),
'type' => CRM_Utils_Type::T_INT,
'api.required' => 0,
'description' => E::ts('ID of the parent TwingleProject'),
];
}
/**
@ -52,7 +59,17 @@ function civicrm_api3_twingle_campaign_Sync(array $params): array {
$returnValues = [];
$errors_occurred = 0;
if ($campaigns['is_error'] == 0 && $campaigns['count'] > 0) {
// Abort if TwingleProject does not have TingleCampaign children
if ($campaigns['count'] == 0) {
return civicrm_api3_create_success(
$returnValues,
$params,
'TwingleCampaign',
'Sync'
);
}
if ($campaigns['is_error'] == 0) {
// Instantiate and re-create TwingleCampaigns
foreach ($campaigns['values'] as $campaign) {