diff --git a/CRM/TwingleCampaign/BAO/TwingleCampaign.php b/CRM/TwingleCampaign/BAO/TwingleCampaign.php index 1c035b9..2cbc865 100644 --- a/CRM/TwingleCampaign/BAO/TwingleCampaign.php +++ b/CRM/TwingleCampaign/BAO/TwingleCampaign.php @@ -2,6 +2,7 @@ use CRM_TwingleCampaign_Utils_ExtensionCache as ExtensionCache; use CRM_TwingleCampaign_ExtensionUtil as E; +use CRM_TwingleCampaign_Exceptions_TwingleCampaignException as TwingleCampaignException; class CRM_TwingleCampaign_BAO_TwingleCampaign { @@ -23,6 +24,7 @@ class CRM_TwingleCampaign_BAO_TwingleCampaign { * @param array $values * * @throws \CiviCRM_API3_Exception + * @throws \CRM_TwingleCampaign_Exceptions_TwingleCampaignException */ public function __construct(array $values = []) { @@ -30,15 +32,30 @@ class CRM_TwingleCampaign_BAO_TwingleCampaign { $this->id = $values['id'] ?? NULL; $this->values['campaign_type_id'] = 'twingle_campaign'; + // If there is already an ID for this TwingleProject, get its values from + // the database if ($this->id != NULL) { $this->fetch($this->id); } + + // Update the campaign values $this->update($values); + // Get the parent TwingleProject + // (it doesn't matter how many levels above in the campaign tree it is) $this->getParentProject(); - if (!isset($this->values['cid'])) { + + // If this is a new TwingleCampaign or if it is a cloned TwingleCampaign, + // calculate a cid + if ( + !isset($this->values['cid']) || + (isset($values['clone']) && $values['clone']) + ) { $this->createCid(); } + + // Create an url from the parent TwingleProject url and the cid of this + // TwingleCampaign $this->createUrl(); } @@ -86,6 +103,7 @@ class CRM_TwingleCampaign_BAO_TwingleCampaign { * deleted. * * @throws \CiviCRM_API3_Exception + * @throws \CRM_TwingleCampaign_Exceptions_TwingleCampaignException */ private function getParentProject(): void { @@ -96,8 +114,7 @@ class CRM_TwingleCampaign_BAO_TwingleCampaign { // Determine the parent project id by looping through the campaign tree // until the parent campaign type is a TwingleProject - $parent_id = $this->values['parent_id']; - $parent_id = $parent_id ?? civicrm_api3( + $parent_id = $this->values['parent_id'] ?? civicrm_api3( 'TwingleCampaign', 'getsingle', ['id' => $this->id] @@ -117,7 +134,7 @@ class CRM_TwingleCampaign_BAO_TwingleCampaign { } $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']; } else { @@ -167,7 +184,7 @@ class CRM_TwingleCampaign_BAO_TwingleCampaign { ' could not determine parent TwingleProject URL.', $this->getResponse() ); - $this->delete(); + throw new TwingleCampaignException('Parent project URL missing'); } } @@ -180,7 +197,7 @@ class CRM_TwingleCampaign_BAO_TwingleCampaign { ts('No parent TwingleProject found'), 'alert' ); - $this->delete(); + throw new TwingleCampaignException('No parent TwingleProject found'); } } @@ -288,7 +305,7 @@ class CRM_TwingleCampaign_BAO_TwingleCampaign { } catch (CiviCRM_API3_Exception $e) { Civi::log()->error( E::LONG_NAME . - ' could delete TwingleCampaign: ' . + ' could not delete TwingleCampaign: ' . $e->getMessage(), $this->getResponse() ); @@ -349,20 +366,6 @@ class CRM_TwingleCampaign_BAO_TwingleCampaign { } } - /** - * ## Clone this TwingleProject - * - * This method removes the id from this instance and in the next step it - * creates the clone as a new TwingleCampaign with the same values to - * Twingle. - * - * @throws \CiviCRM_API3_Exception - */ - public - function clone() { - // TODO: implement cloning - } - /** * ## Get ID * diff --git a/CRM/TwingleCampaign/Exceptions/TwingleCampaignException.php b/CRM/TwingleCampaign/Exceptions/TwingleCampaignException.php new file mode 100644 index 0000000..0f99555 --- /dev/null +++ b/CRM/TwingleCampaign/Exceptions/TwingleCampaignException.php @@ -0,0 +1,8 @@ + 1, 'description' => E::ts('Optional parent id for this Campaign'), ]; + $spec['clone'] = [ + 'name' => 'clone', + 'title' => E::ts('Clone'), + 'type' => CRM_Utils_Type::T_BOOLEAN, + 'api.required' => 0, + 'description' => E::ts('Set this value to true if this campaign is about to be cloned to recreate cid'), + ]; } @@ -61,11 +68,10 @@ function civicrm_api3_twingle_campaign_Create(array $params): array { _civicrm_api3_twingle_campaign_Create_spec($allowed_params); $params = array_intersect_key($params, $allowed_params); - // instantiate TwingleCampaign - $campaign = new TwingleCampaign($params); - - // Try to create the TwingleCampaign try { + // instantiate TwingleCampaign + $campaign = new TwingleCampaign($params); + // try to create the TwingleCampaign $campaign->create(TRUE); return civicrm_api3_create_success( $campaign->getResponse('TwingleCampaign created'), @@ -74,15 +80,8 @@ function civicrm_api3_twingle_campaign_Create(array $params): array { 'Create' ); } catch(Exception $e){ - Civi::log()->error( - E::LONG_NAME . - ' could not create TwingleCampaign: ' . - $e->getMessage(), - $campaign->getResponse() - ); return civicrm_api3_create_error( - 'Could not create TwingleCampaign: ' . $e->getMessage(), - $campaign->getResponse() + 'Could not create TwingleCampaign: ' . $e->getMessage() ); } diff --git a/l10n/de_DE/LC_MESSAGES/de_DE.mo b/l10n/de_DE/LC_MESSAGES/de_DE.mo index b69812f..7a64892 100644 Binary files a/l10n/de_DE/LC_MESSAGES/de_DE.mo and b/l10n/de_DE/LC_MESSAGES/de_DE.mo differ diff --git a/l10n/de_DE/LC_MESSAGES/de_DE.po b/l10n/de_DE/LC_MESSAGES/de_DE.po index 5736cb0..5f83de4 100644 --- a/l10n/de_DE/LC_MESSAGES/de_DE.po +++ b/l10n/de_DE/LC_MESSAGES/de_DE.po @@ -3,14 +3,14 @@ msgstr "" "Project-Id-Version: \n" "POT-Creation-Date: \n" "PO-Revision-Date: \n" +"Last-Translator: \n" "Language-Team: \n" +"Language: de_DE\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -"X-Generator: Poedit 2.4.3\n" -"Last-Translator: \n" +"X-Generator: Poedit 3.0\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n" -"Language: de_DE\n" #: CRM/TwingleCampaign/BAO/CustomField.php msgid "" @@ -907,6 +907,17 @@ msgstr "Elternkampagne" msgid "Optional parent id for this Campaign" msgstr "Optionale Eltern-ID für diese Kampagne" +#: api/v3/TwingleCampaign/Create.php +msgid "Clone" +msgstr "Klonen" + +#: api/v3/TwingleCampaign/Create.php +msgid "" +"Set this value to true if this campaign is about to be cloned to recreate cid" +msgstr "" +"Setze diesen Wert auf true, wenn diese Kampagne gerade geklont wird, damit " +"die cid neu berechnet wird" + #: api/v3/TwingleCampaign/Get.php api/v3/TwingleCampaign/Getsingle.php msgid "Twingle Campaign CID" msgstr "Twingle Campaign CID" @@ -1197,6 +1208,10 @@ msgstr "Twingle Event Einstellungen" msgid "Campaign cloning failed" msgstr "Klonen der Kampagne fehlgeschlagen" +#: twinglecampaign.php +msgid "TwingleCampaign was cloned." +msgstr "TwingleCampaign wurde geklont" + #: twinglecampaign.php msgid "TwingleCampaign update failed" msgstr "TwingleCampaign-Update fehlgeschlagen" diff --git a/l10n/pot/twinglecampaign.pot b/l10n/pot/twinglecampaign.pot index 156eafd..62ac159 100644 --- a/l10n/pot/twinglecampaign.pot +++ b/l10n/pot/twinglecampaign.pot @@ -806,6 +806,14 @@ msgstr "" msgid "Optional parent id for this Campaign" msgstr "" +#: ./api/v3/TwingleCampaign/Create.php +msgid "Clone" +msgstr "" + +#: ./api/v3/TwingleCampaign/Create.php +msgid "Set this value to true if this campaign is about to be cloned to recreate cid" +msgstr "" + #: ./api/v3/TwingleCampaign/Get.php ./api/v3/TwingleCampaign/Getsingle.php msgid "Twingle Campaign CID" msgstr "" @@ -1038,6 +1046,10 @@ msgstr "" msgid "Campaign cloning failed" msgstr "" +#: ./twinglecampaign.php +msgid "TwingleCampaign was cloned." +msgstr "" + #: ./twinglecampaign.php msgid "TwingleCampaign update failed" msgstr "" diff --git a/twinglecampaign.php b/twinglecampaign.php index a6a9b31..bda4f1d 100644 --- a/twinglecampaign.php +++ b/twinglecampaign.php @@ -82,7 +82,11 @@ function twinglecampaign_civicrm_postSave_civicrm_campaign($dao) { $_SESSION['CiviCRM']['de.forumzfd.twinglecampaign']['no_hook'] != TRUE) { // If request is not an API-Call - if ($_GET['action'] != 'create' && $_POST['action'] != 'create') { + if ( + ((isset($_GET['action']) && $_GET['action'] != 'create') || + (isset($_POST['action']) && $_POST['action'] != 'create')) || + (!isset($_GET['action']) && !isset($_POST['action'])) + ) { // If the db transaction is still running, add a function to it that will // be called afterwards @@ -180,6 +184,19 @@ function twinglecampaign_postSave_campaign_update_callback( ); } } + if ($_POST['action'] == 'clone' && $entity == 'TwingleCampaign') { + unset($_POST['action']); + try { + civicrm_api3('TwingleCampaign', 'create', + ['id' => $campaign_id, 'clone' => true] + ); + CRM_Utils_System::setUFMessage(E::ts('TwingleCampaign was cloned.')); + } catch (CiviCRM_API3_Exception $e) { + Civi::log()->error( + 'twinglecampaign_postSave_callback ' . $e->getMessage() + ); + } + } } // If a TwingleProject is getting saved