👾 bug fix: wrong parent TwingleProject id's

This commit is contained in:
Marc Michalsky 2021-10-05 17:20:37 +02:00
parent e23e0d7e8d
commit f059abf5a8
3 changed files with 20 additions and 18 deletions

View file

@ -2,6 +2,7 @@
use CRM_TwingleCampaign_Utils_ExtensionCache as ExtensionCache; use CRM_TwingleCampaign_Utils_ExtensionCache as ExtensionCache;
use CRM_TwingleCampaign_ExtensionUtil as E; use CRM_TwingleCampaign_ExtensionUtil as E;
use CRM_TwingleCampaign_Exceptions_TwingleCampaignException as TwingleCampaignException;
class CRM_TwingleCampaign_BAO_TwingleCampaign { class CRM_TwingleCampaign_BAO_TwingleCampaign {
@ -23,6 +24,7 @@ class CRM_TwingleCampaign_BAO_TwingleCampaign {
* @param array $values * @param array $values
* *
* @throws \CiviCRM_API3_Exception * @throws \CiviCRM_API3_Exception
* @throws \CRM_TwingleCampaign_Exceptions_TwingleCampaignException
*/ */
public function __construct(array $values = []) { public function __construct(array $values = []) {
@ -86,6 +88,7 @@ class CRM_TwingleCampaign_BAO_TwingleCampaign {
* deleted. * deleted.
* *
* @throws \CiviCRM_API3_Exception * @throws \CiviCRM_API3_Exception
* @throws \CRM_TwingleCampaign_Exceptions_TwingleCampaignException
*/ */
private function getParentProject(): void { private function getParentProject(): void {
@ -96,8 +99,7 @@ class CRM_TwingleCampaign_BAO_TwingleCampaign {
// Determine the parent project id by looping through the campaign tree // Determine the parent project id by looping through the campaign tree
// until the parent campaign type is a TwingleProject // until the parent campaign type is a TwingleProject
$parent_id = $this->values['parent_id']; $parent_id = $this->values['parent_id'] ?? civicrm_api3(
$parent_id = $parent_id ?? civicrm_api3(
'TwingleCampaign', 'TwingleCampaign',
'getsingle', 'getsingle',
['id' => $this->id] ['id' => $this->id]
@ -117,7 +119,7 @@ class CRM_TwingleCampaign_BAO_TwingleCampaign {
} }
$parent_campaign_type_id = $parent_campaign['campaign_type_id']; $parent_campaign_type_id = $parent_campaign['campaign_type_id'];
if (isset($parent_campaign['parent_id'])) { if ($parent_campaign_type_id != $twingle_project_campaign_type_id && isset($parent_campaign['parent_id'])) {
$parent_id = $parent_campaign['parent_id']; $parent_id = $parent_campaign['parent_id'];
} }
else { else {
@ -167,7 +169,7 @@ class CRM_TwingleCampaign_BAO_TwingleCampaign {
' could not determine parent TwingleProject URL.', ' could not determine parent TwingleProject URL.',
$this->getResponse() $this->getResponse()
); );
$this->delete(); throw new TwingleCampaignException('Parent project URL missing');
} }
} }
@ -180,7 +182,7 @@ class CRM_TwingleCampaign_BAO_TwingleCampaign {
ts('No parent TwingleProject found'), ts('No parent TwingleProject found'),
'alert' 'alert'
); );
$this->delete(); throw new TwingleCampaignException('No parent TwingleProject found');
} }
} }
@ -288,7 +290,7 @@ class CRM_TwingleCampaign_BAO_TwingleCampaign {
} catch (CiviCRM_API3_Exception $e) { } catch (CiviCRM_API3_Exception $e) {
Civi::log()->error( Civi::log()->error(
E::LONG_NAME . E::LONG_NAME .
' could delete TwingleCampaign: ' . ' could not delete TwingleCampaign: ' .
$e->getMessage(), $e->getMessage(),
$this->getResponse() $this->getResponse()
); );

View file

@ -0,0 +1,8 @@
<?php
/*
* A simple custom exception that indicates a problem with the TwingleCampaign class
*/
class CRM_TwingleCampaign_Exceptions_TwingleCampaignException extends Exception {
}

View file

@ -61,11 +61,10 @@ function civicrm_api3_twingle_campaign_Create(array $params): array {
_civicrm_api3_twingle_campaign_Create_spec($allowed_params); _civicrm_api3_twingle_campaign_Create_spec($allowed_params);
$params = array_intersect_key($params, $allowed_params); $params = array_intersect_key($params, $allowed_params);
try {
// instantiate TwingleCampaign // instantiate TwingleCampaign
$campaign = new TwingleCampaign($params); $campaign = new TwingleCampaign($params);
// try to create the TwingleCampaign
// Try to create the TwingleCampaign
try {
$campaign->create(TRUE); $campaign->create(TRUE);
return civicrm_api3_create_success( return civicrm_api3_create_success(
$campaign->getResponse('TwingleCampaign created'), $campaign->getResponse('TwingleCampaign created'),
@ -74,15 +73,8 @@ function civicrm_api3_twingle_campaign_Create(array $params): array {
'Create' 'Create'
); );
} catch(Exception $e){ } catch(Exception $e){
Civi::log()->error(
E::LONG_NAME .
' could not create TwingleCampaign: ' .
$e->getMessage(),
$campaign->getResponse()
);
return civicrm_api3_create_error( return civicrm_api3_create_error(
'Could not create TwingleCampaign: ' . $e->getMessage(), 'Could not create TwingleCampaign: ' . $e->getMessage()
$campaign->getResponse()
); );
} }