* @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);
}
}
*/
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;
}
}
}
+ /**
+ * @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;
+ }
+
}
'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',
'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',
'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.'),
'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',
'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',
{include file="CRM/Form/basicFormFields.tpl"}
<table class="form-layout" id="invoicing_blocks">
- {foreach from=$htmlFields item=fieldSpec key=htmlField}
+ {foreach from=$invoiceDependentFields item=fieldSpec key=htmlField}
{if $form.$htmlField}
{assign var=n value=$htmlField|cat:'_description'}
<tr class="crm-preferences-form-block-{$htmlField}">