Skip to content

Commit

Permalink
Use onToggle feature for setting user_dashboard_options when toggling…
Browse files Browse the repository at this point in the history
… invoicing, remove from form
  • Loading branch information
eileenmcnaughton committed Nov 21, 2018
1 parent 9886779 commit 8d33433
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 27 deletions.
25 changes: 0 additions & 25 deletions CRM/Admin/Form/Preferences/Contribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

}
80 changes: 80 additions & 0 deletions CRM/Invoicing/Utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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);
}

}
5 changes: 3 additions & 2 deletions settings/Contribute.setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 8d33433

Please sign in to comment.