Use onToggle feature for setting user_dashboard_options when toggling invoicing,...
authoreileen <emcnaughton@wikimedia.org>
Wed, 21 Nov 2018 00:49:09 +0000 (13:49 +1300)
committereileen <emcnaughton@wikimedia.org>
Wed, 21 Nov 2018 00:49:09 +0000 (13:49 +1300)
CRM/Admin/Form/Preferences/Contribute.php
CRM/Invoicing/Utils.php [new file with mode: 0644]
settings/Contribute.setting.php

index 111111d5145cc4199f88c31e74a13b56199338c0..6fc6b26d50164f7fdd9d2d9a476624291e2bc410 100644 (file)
@@ -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);
-    }
   }
 
 }
diff --git a/CRM/Invoicing/Utils.php b/CRM/Invoicing/Utils.php
new file mode 100644 (file)
index 0000000..22994f4
--- /dev/null
@@ -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);
+  }
+
+}
index 213271e2a3fb56f737b4bbf97862c11e8e6f7743..277133f24efef81fbb548d0e7ab8cf584d34e39e 100644 (file)
@@ -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',