From e4af927a5b5d210ba909f36fafe2c628014caf08 Mon Sep 17 00:00:00 2001 From: Marc Michalsky forumZFD Date: Thu, 12 Nov 2020 14:02:03 +0100 Subject: [PATCH] add select field for XCM profile to extension config page --- CRM/TwingleCampaign/BAO/TwingleEvent.php | 2 +- CRM/TwingleCampaign/Form/Settings.php | 63 +++++++++++++++++-- settings/twingle.setting.php | 26 -------- .../CRM/TwingleCampaign/Form/Settings.tpl | 37 +++++------ 4 files changed, 72 insertions(+), 56 deletions(-) delete mode 100644 settings/twingle.setting.php diff --git a/CRM/TwingleCampaign/BAO/TwingleEvent.php b/CRM/TwingleCampaign/BAO/TwingleEvent.php index 2c7bed9..486102c 100644 --- a/CRM/TwingleCampaign/BAO/TwingleEvent.php +++ b/CRM/TwingleCampaign/BAO/TwingleEvent.php @@ -318,7 +318,7 @@ class TwingleEvent extends Campaign { } try { $contact = civicrm_api3('Contact', 'getorcreate', [ - 'xcm_profile' => 'default', + 'xcm_profile' => Civi::settings()->get('twinglecampaign_xcm_profile'), 'first_name' => $names, 'last_name' => $lastname, 'email' => $email, diff --git a/CRM/TwingleCampaign/Form/Settings.php b/CRM/TwingleCampaign/Form/Settings.php index a532e77..29d5287 100644 --- a/CRM/TwingleCampaign/Form/Settings.php +++ b/CRM/TwingleCampaign/Form/Settings.php @@ -7,16 +7,67 @@ use CRM_TwingleCampaign_ExtensionUtil as E; * * @see https://docs.civicrm.org/dev/en/latest/framework/quickform/ */ -class CRM_TwingleCampaign_Form_Settings extends CRM_Admin_Form_Setting { +class CRM_TwingleCampaign_Form_Settings extends CRM_Core_Form { - protected $_settings = [ - 'twingle_api_key' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, - 'twingle_request_size' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, - ]; + protected $_settings = NULL; public function buildQuickForm() { + + $this->addElement('text', + 'twingle_api_key', + E::ts('Twingle API key') + ); + + $this->addElement('select', + 'twinglecampaign_xcm_profile', + E::ts('XCM Profile'), + $this->getXCMProfiles() + ); + + $this->addButtons([ + [ + 'type' => 'submit', + 'name' => E::ts('Save'), + 'isDefault' => TRUE, + ] + ]); + parent::buildQuickForm(); - $this->assign('elementNames', array_keys($this->_settings)); } + public function setDefaultValues() { + $defaultValues['twingle_api_key'] = + Civi::settings()->get('twingle_api_key'); + $defaultValues['twinglecampaign_xcm_profile'] = + Civi::settings()->get('twinglecampaign_xcm_profile'); + return $defaultValues; + } + + //TODO: validate Twingle API key + + public function postProcess() { + $values = $this->exportValues(); + Civi::settings()->set('twingle_api_key', $values['twingle_api_key']); + Civi::settings()->set('twinglecampaign_xcm_profile', $values['twinglecampaign_xcm_profile']); + parent::postProcess(); + } + + /** + * Retrieves XCM profiles (if supported). 'default' profile is always + * available + * + * @return array + */ + public function getXCMProfiles() { + $xcmProfiles = []; + if (!isset($this->_settings['twinglecampaign_xcm_profile'])) { + if (method_exists('CRM_Xcm_Configuration', 'getProfileList')) { + $profiles = CRM_Xcm_Configuration::getProfileList(); + foreach ($profiles as $profile_key => $profile_name) { + $xcmProfiles[$profile_key] = $profile_name; + } + } + } + return $xcmProfiles; + } } \ No newline at end of file diff --git a/settings/twingle.setting.php b/settings/twingle.setting.php deleted file mode 100644 index 95ab0a7..0000000 --- a/settings/twingle.setting.php +++ /dev/null @@ -1,26 +0,0 @@ - [ - 'name' => 'twingle_api_key', - 'type' => 'String', - 'default' => '', - 'html_type' => 'text', - 'title' => ts('Twingle API Key'), - 'is_domain' => 1, - 'is_contact' => 0, - 'description' => ts('The key that allows you to call the Twingle API'), - `settings_pages` => ['remote' => ['weight' => 10]], - ], - 'twingle_request_size' => [ - 'name' => 'twingle_request_size', - 'type' => 'Integer', - 'default' => '10', - 'html_type' => 'text', - 'title' => ts('Twingle Request Size'), - 'is_domain' => 1, - 'is_contact' => 0, - 'description' => ts('How many items should be requested from the Twingle API at once?'), - `settings_pages` => ['remote' => ['weight' => 11]], - ] -]; \ No newline at end of file diff --git a/templates/CRM/TwingleCampaign/Form/Settings.tpl b/templates/CRM/TwingleCampaign/Form/Settings.tpl index c99bb1f..147961f 100644 --- a/templates/CRM/TwingleCampaign/Form/Settings.tpl +++ b/templates/CRM/TwingleCampaign/Form/Settings.tpl @@ -1,27 +1,18 @@ {* HEADER *} -
-{include file="CRM/common/formButtons.tpl" location="top"} -
- -{* FIELD EXAMPLE: OPTION 1 (AUTOMATIC LAYOUT) *} - -{foreach from=$elementNames item=elementName} -
-
{$form.$elementName.label}
-
{$form.$elementName.html}
+
+
{$form.twingle_api_key.label}
+
{$form.twingle_api_key.html}
-
-{/foreach} - -{* FIELD EXAMPLE: OPTION 2 (MANUAL LAYOUT) - -
- {$form.favorite_color.label} - {$form.favorite_color.html} -
- -{* FOOTER *} -
-{include file="CRM/common/formButtons.tpl" location="bottom"} +
+ +
+
{$form.twinglecampaign_xcm_profile.label}
+
{$form.twinglecampaign_xcm_profile.html}
+
+
+ + +
+ {include file="CRM/common/formButtons.tpl" location="bottom"}