From d4a31b4c7b9cfd5c2b48d4a95ead3bd73e35d284 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 27 Oct 2023 18:08:43 +1300 Subject: [PATCH] Update Contribution Preferences form to interact with the 'real' settings (only) --- CRM/Admin/Form/Preferences/Contribute.php | 102 ++---------------- CRM/Admin/Form/SettingTrait.php | 40 ++++--- settings/Contribute.setting.php | 21 ++-- .../CRM/Admin/Form/Preferences/Contribute.tpl | 2 +- 4 files changed, 48 insertions(+), 117 deletions(-) diff --git a/CRM/Admin/Form/Preferences/Contribute.php b/CRM/Admin/Form/Preferences/Contribute.php index 6c2edd7ebc..db4ffc3ac4 100644 --- a/CRM/Admin/Form/Preferences/Contribute.php +++ b/CRM/Admin/Form/Preferences/Contribute.php @@ -15,109 +15,23 @@ * @copyright CiviCRM LLC https://civicrm.org/licensing */ +use Civi\Core\SettingsMetadata; + /** * This class generates form components for the display preferences. */ class CRM_Admin_Form_Preferences_Contribute extends CRM_Admin_Form_Preferences { - /** - * Our standards for settings are to have a setting per value with defined metadata. - * - * Unfortunately the 'contribution_invoice_settings' has been added in non-compliance. - * We use this array to hack-handle. - * - * These are now stored as individual settings but this form still does weird & wonderful things. - * - * Note the 'real' settings on this form are added via metadata definition - ie - * 'settings_pages' => ['contribute' => ['weight' => 1]], in their metadata. - * - * @var array - */ - protected $invoiceSettings = []; - /** * Build the form object. - * - * @throws \CRM_Core_Exception */ - public function buildQuickForm() { + public function buildQuickForm(): void { parent::buildQuickForm(); - $metadata = \Civi\Core\SettingsMetadata::getMetadata(['name' => ['invoice_prefix', 'tax_term', 'invoice_notes', 'invoice_due_date', 'invoice_is_email_pdf', 'invoice_due_date_period', 'tax_display_settings']], NULL, TRUE); - $this->invoiceSettings = [ - 'invoice_prefix' => $metadata['invoice_prefix'], - 'due_date' => $metadata['invoice_due_date'], - 'due_date_period' => $metadata['invoice_due_date_period'], - 'notes' => $metadata['invoice_notes'], - 'is_email_pdf' => $metadata['invoice_is_email_pdf'], - 'tax_term' => $metadata['tax_term'], - 'tax_display_settings' => $metadata['tax_display_settings'], - ]; - - // @todo this is a faux metadata approach - we should be honest & add them correctly or find a way to make this - // compatible with our settings standards. - foreach ($this->invoiceSettings as $fieldName => $fieldValue) { - switch ($fieldValue['html_type']) { - case 'text': - $this->addElement('text', - $fieldName, - $fieldValue['title'], - [ - 'maxlength' => 64, - 'size' => 32, - ] - ); - break; - - case 'checkbox': - $this->add($fieldValue['html_type'], - $fieldName, - $fieldValue['title'] - ); - break; - - case 'select': - $this->addElement('select', - $fieldName, - $fieldValue['title'], - $fieldValue['options'], - CRM_Utils_Array::value('attributes', $fieldValue) - ); - break; - - case 'wysiwyg': - $this->add('wysiwyg', $fieldName, $fieldValue['title'], $fieldValue['attributes']); - break; - } - } - - $this->assign('htmlFields', $this->invoiceSettings); - } - - /** - * Set default values for the form. - * - * default values are retrieved from the database - */ - public function setDefaultValues() { - $defaults = parent::setDefaultValues(); - $defaults = array_merge($defaults, Civi::settings()->get('contribution_invoice_settings')); - return $defaults; - } - - /** - * Process the form after the input has been submitted and validated. - */ - public function postProcess() { - // store the submitted values in an array - $params = $this->controller->exportValues($this->_name); - $invoiceParams = array_intersect_key($params, $this->invoiceSettings); - // This is a hack - invoicing is it's own setting but it is being used from invoice params - // too. This means that saving from api will not have the desired core effect. - // but we should fix that elsewhere - ie. stop abusing the settings - // and fix the code repetition associated with invoicing - $invoiceParams['invoicing'] = $params['invoicing'] ?? 0; - Civi::settings()->set('contribution_invoice_settings', $invoiceParams); - parent::postProcess(); + $invoiceSettings = SettingsMetadata::getMetadata(['name' => ['invoice_prefix', 'tax_term', 'invoice_notes', 'invoice_due_date', 'invoice_is_email_pdf', 'invoice_due_date_period', 'tax_display_settings']], NULL, TRUE); + // Let the main template file deal with the main setting & then Contribute.tpl + // can stick the invoice settings in a div that can show-hide-toggle if invoicing is enabled. + $this->assign('fields', $this->filterMetadataByWeight(array_diff_key($this->getSettingsMetaData(), $invoiceSettings))); + $this->assign('invoiceDependentFields', $invoiceSettings); } } diff --git a/CRM/Admin/Form/SettingTrait.php b/CRM/Admin/Form/SettingTrait.php index 705e0ab600..eb8d592cf7 100644 --- a/CRM/Admin/Form/SettingTrait.php +++ b/CRM/Admin/Form/SettingTrait.php @@ -150,21 +150,7 @@ trait CRM_Admin_Form_SettingTrait { */ protected function getSettingsOrderedByWeight() { $settingMetaData = $this->getSettingsMetaData(); - $filter = $this->getSettingPageFilter(); - - usort($settingMetaData, function ($a, $b) use ($filter) { - // Handle cases in which a comparison is impossible. Such will be considered ties. - if ( - // A comparison can't be made unless both setting weights are declared. - !isset($a['settings_pages'][$filter]['weight'], $b['settings_pages'][$filter]['weight']) - // A pair of settings might actually have the same weight. - || $a['settings_pages'][$filter]['weight'] === $b['settings_pages'][$filter]['weight'] - ) { - return 0; - } - - return $a['settings_pages'][$filter]['weight'] > $b['settings_pages'][$filter]['weight'] ? 1 : -1; - }); + $settingMetaData = $this->filterMetadataByWeight($settingMetaData); return $settingMetaData; } @@ -412,4 +398,28 @@ trait CRM_Admin_Form_SettingTrait { } } + /** + * @param array $settingMetaData + * + * @return array + */ + protected function filterMetadataByWeight(array $settingMetaData): array { + $filter = $this->getSettingPageFilter(); + + usort($settingMetaData, function ($a, $b) use ($filter) { + // Handle cases in which a comparison is impossible. Such will be considered ties. + if ( + // A comparison can't be made unless both setting weights are declared. + !isset($a['settings_pages'][$filter]['weight'], $b['settings_pages'][$filter]['weight']) + // A pair of settings might actually have the same weight. + || $a['settings_pages'][$filter]['weight'] === $b['settings_pages'][$filter]['weight'] + ) { + return 0; + } + + return $a['settings_pages'][$filter]['weight'] > $b['settings_pages'][$filter]['weight'] ? 1 : -1; + }); + return $settingMetaData; + } + } diff --git a/settings/Contribute.setting.php b/settings/Contribute.setting.php index a03643f4e0..5f3cd604ab 100644 --- a/settings/Contribute.setting.php +++ b/settings/Contribute.setting.php @@ -68,11 +68,12 @@ return [ 'html_type' => 'text', 'name' => 'invoice_prefix', 'add' => '5.23', - 'type' => CRM_Utils_Type::T_STRING, + 'type' => 'String', 'title' => ts('Invoice Prefix'), 'description' => ts('Enter prefix to be be pre-pended when creating an invoice number'), 'is_domain' => 1, 'is_contact' => 0, + 'settings_pages' => ['contribute' => ['weight' => 100]], ], 'invoice_due_date' => [ 'default' => '10', @@ -81,9 +82,10 @@ return [ 'title' => ts('Due Date'), 'description' => '', 'add' => '5.23', - 'type' => CRM_Utils_Type::T_INT, + 'type' => 'Integer', 'is_domain' => 1, 'is_contact' => 0, + 'settings_pages' => ['contribute' => ['weight' => 110]], ], 'invoice_due_date_period' => [ 'default' => 'days', @@ -92,7 +94,7 @@ return [ 'title' => ts('For transmission'), 'weight' => 4, 'add' => '5.23', - 'type' => CRM_Utils_Type::T_STRING, + 'type' => 'String', 'is_domain' => 1, 'is_contact' => 0, 'description' => ts('Select the interval for due date.'), @@ -102,28 +104,31 @@ return [ 'months' => ts('Months'), 'years' => ts('Years'), ], + 'settings_pages' => ['contribute' => ['weight' => 120]], ], 'invoice_notes' => [ 'default' => '', 'name' => 'invoice_notes', 'html_type' => 'wysiwyg', 'title' => ts('Notes or Standard Terms'), - 'type' => CRM_Utils_Type::T_STRING, + 'type' => 'String', 'add' => '5.23', 'is_domain' => 1, 'is_contact' => 0, 'description' => ts('Enter note or message to be displayed on PDF invoice or credit notes '), 'attributes' => ['rows' => 2, 'cols' => 40], + 'settings_pages' => ['contribute' => ['weight' => 130]], ], 'invoice_is_email_pdf' => [ 'name' => 'invoice_is_email_pdf', 'html_type' => 'checkbox', 'add' => '5.23', - 'type' => CRM_Utils_Type::T_BOOLEAN, + 'type' => 'Boolean', 'is_domain' => 1, 'is_contact' => 0, 'title' => ts('Automatically email invoice when user purchases online'), 'description' => ts('Should a pdf invoice be emailed automatically?'), + 'settings_pages' => ['contribute' => ['weight' => 140]], ], 'tax_term' => [ 'default' => 'Sales Tax', @@ -131,22 +136,24 @@ return [ 'html_type' => 'text', 'add' => '5.23', 'title' => ts('Tax Term'), - 'type' => CRM_Utils_Type::T_STRING, + 'type' => 'String', 'is_domain' => 1, 'is_contact' => 0, 'description' => '', + 'settings_pages' => ['contribute' => ['weight' => 150]], ], 'tax_display_settings' => [ 'default' => 'Inclusive', 'html_type' => 'select', 'name' => 'tax_display_settings', - 'type' => CRM_Utils_Type::T_STRING, + 'type' => 'String', 'add' => '5.23', 'title' => ts('Tax Display Settings'), 'is_domain' => 1, 'is_contact' => 0, 'description' => '', 'pseudoconstant' => ['callback' => 'CRM_Core_SelectValues::taxDisplayOptions'], + 'settings_pages' => ['contribute' => ['weight' => 160]], ], 'deferred_revenue_enabled' => [ 'group_name' => 'Contribute Preferences', diff --git a/templates/CRM/Admin/Form/Preferences/Contribute.tpl b/templates/CRM/Admin/Form/Preferences/Contribute.tpl index 4d5a3da18f..20ed5234f4 100644 --- a/templates/CRM/Admin/Form/Preferences/Contribute.tpl +++ b/templates/CRM/Admin/Form/Preferences/Contribute.tpl @@ -11,7 +11,7 @@ {include file="CRM/Form/basicFormFields.tpl"} - {foreach from=$htmlFields item=fieldSpec key=htmlField} + {foreach from=$invoiceDependentFields item=fieldSpec key=htmlField} {if $form.$htmlField} {assign var=n value=$htmlField|cat:'_description'} -- 2.25.1