diff --git a/CRM/Admin/Form/Preferences/Contribute.php b/CRM/Admin/Form/Preferences/Contribute.php index 111111d5145c..6fc6b26d5016 100644 --- a/CRM/Admin/Form/Preferences/Contribute.php +++ b/CRM/Admin/Form/Preferences/Contribute.php @@ -194,31 +194,6 @@ public function postProcess() { $invoiceParams['invoicing'] = CRM_Utils_Array::value('invoicing', $params, 0); Civi::settings()->set('contribution_invoice_settings', $invoiceParams); parent::postProcess(); - - // @todo - all this should be handled by defining an on change action in the metadata. - // to set default value for 'Invoices / Credit Notes' checkbox on display preferences - $values = CRM_Core_BAO_Setting::getItem("CiviCRM Preferences"); - $optionValues = CRM_Core_OptionGroup::values('user_dashboard_options', FALSE, FALSE, FALSE, NULL, 'name'); - $setKey = array_search('Invoices / Credit Notes', $optionValues); - - if (isset($params['invoicing'])) { - $value = array($setKey => $optionValues[$setKey]); - $setInvoice = CRM_Core_DAO::VALUE_SEPARATOR . - implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($value)) . - CRM_Core_DAO::VALUE_SEPARATOR; - Civi::settings()->set('user_dashboard_options', $values['user_dashboard_options'] . $setInvoice); - } - else { - $setting = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($values['user_dashboard_options'], 1, -1)); - $invoiceKey = array_search($setKey, $setting); - if ($invoiceKey !== FALSE) { - unset($setting[$invoiceKey]); - } - $settingName = CRM_Core_DAO::VALUE_SEPARATOR . - implode(CRM_Core_DAO::VALUE_SEPARATOR, array_values($setting)) . - CRM_Core_DAO::VALUE_SEPARATOR; - Civi::settings()->set('user_dashboard_options', $settingName); - } } } diff --git a/CRM/Invoicing/Utils.php b/CRM/Invoicing/Utils.php new file mode 100644 index 000000000000..22994f4b2819 --- /dev/null +++ b/CRM/Invoicing/Utils.php @@ -0,0 +1,80 @@ + 'user_dashboard_options'])['values'][CRM_Core_Config::domainID()]['user_dashboard_options']; + $optionValues= civicrm_api3('Setting', 'getoptions', ['field' => 'user_dashboard_options'])['values']; + $invoiceKey = array_search('Invoices / Credit Notes', $optionValues); + $existingIndex = in_array($invoiceKey, $existingUserViewOptions); + + if ($newValue && $existingIndex === FALSE) { + $existingUserViewOptions[] = $invoiceKey; + } + elseif (!$newValue && $existingIndex !== FALSE) { + unset($existingUserViewOptions[$existingIndex]); + } + civicrm_api3('Setting', 'create', ['user_dashboard_options' => $existingUserViewOptions]); + } + + /** + * Function to call to determine if invoicing is enabled. + * + * Historically the invoicing was declared as a setting but actually + * set within contribution_invoice_settings (which stores multiple settings + * as an array in a non-standard way). + * + * We check both here. + */ + public static function isInvoicingEnabled() { + if (Civi::settings()->get('invoicing')) { + return TRUE; + } + $invoiceSettings = Civi::settings()->get('contribution_invoice_settings'); + return CRM_Utils_Array::value('invoicing', $invoiceSettings); + } + +} diff --git a/settings/Contribute.setting.php b/settings/Contribute.setting.php index 213271e2a3fb..277133f24efe 100644 --- a/settings/Contribute.setting.php +++ b/settings/Contribute.setting.php @@ -84,8 +84,9 @@ 'title' => 'Enable Tax and Invoicing', 'is_domain' => 1, 'is_contact' => 0, - 'description' => NULL, - 'help_text' => NULL, + 'on_change' => array( + 'CRM_Invoicing_Utils::onToggle', + ), ), 'acl_financial_type' => array( 'group_name' => 'Contribute Preferences',