Commit 8d334338 authored by eileen's avatar eileen

Use onToggle feature for setting user_dashboard_options when toggling invoicing, remove from form

parent 98867798
......@@ -194,31 +194,6 @@ class CRM_Admin_Form_Preferences_Contribute extends CRM_Admin_Form_Preferences {
$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);
}
}
}
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2018 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2018
*/
class CRM_Invoicing_Utils {
/**
* Function to call when invoicing is toggled on or off.
*
* We add or remove invoicing from the user dashboard here.
*
* @param bool $oldValue
* @param bool $newValue
* @param array $metadata
*/
public static function onToggle($oldValue, $newValue, $metadata) {
if ($oldValue == $newValue) {
return;
}
$existingUserViewOptions = civicrm_api3('Setting', 'get', ['return' => '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);
}
}
......@@ -84,8 +84,9 @@ return array(
'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',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment