Deprecate two single-use option-value functions
authorEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 18 Aug 2022 20:34:36 +0000 (08:34 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 18 Aug 2022 20:34:47 +0000 (08:34 +1200)
CRM/Admin/Form/Setting/Localization.php
CRM/Core/OptionGroup.php
tests/phpunit/CRM/Admin/Form/Setting/LocalizationTest.php

index d18e75ab718fdcd4bd03708e1873466056a3b6ca..06e8c560396152bb8b099e92052979a346950098 100644 (file)
@@ -15,6 +15,9 @@
  * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
 
+use Civi\Api4\OptionGroup;
+use Civi\Api4\OptionValue;
+
 /**
  * This class generates form components for Localization.
  */
@@ -231,29 +234,45 @@ class CRM_Admin_Form_Setting_Localization extends CRM_Admin_Form_Setting {
    *
    * @param string[] $currencies array of currencies ['USD', 'CAD']
    * @param string $default default currency
+   *
+   * @throws \CRM_Core_Exception
    */
-  public static function updateEnabledCurrencies($currencies, $default) {
+  public static function updateEnabledCurrencies(array $currencies, string $default): void {
 
     // sort so that when we display drop down, weights have right value
     sort($currencies);
-
     // get labels for all the currencies
     $options = [];
 
     $currencySymbols = CRM_Admin_Form_Setting_Localization::getCurrencySymbols();
-    for ($i = 0; $i < count($currencies); $i++) {
+    foreach ($currencies as $i => $currency) {
       $options[] = [
-        'label' => $currencySymbols[$currencies[$i]],
-        'value' => $currencies[$i],
+        'label' => $currencySymbols[$currency],
+        'value' => $currency,
         'weight' => $i + 1,
         'is_active' => 1,
-        'is_default' => $currencies[$i] == $default,
+        'is_default' => $currency === $default,
       ];
     }
+    $optionGroupID = OptionGroup::get(FALSE)->addSelect('id')
+      ->addWhere('name', '=', 'currencies_enabled')
+      ->execute()->first()['id'];
+    // @TODO: This causes a problem in multilingual
+    // (https://github.com/civicrm/civicrm-core/pull/17228), but is needed in
+    // order to be able to remove currencies once added.
+    if (!CRM_Core_I18n::isMultiLingual()) {
+      CRM_Core_DAO::executeQuery("
+        DELETE
+        FROM civicrm_option_value
+        WHERE option_group_id = $optionGroupID
+      ");
+    }
 
-    $dontCare = NULL;
-    CRM_Core_OptionGroup::createAssoc('currencies_enabled', $options, $dontCare);
-
+    OptionValue::save(FALSE)
+      ->setRecords($options)
+      ->setDefaults(['is_active' => 1, 'option_group_id' => $optionGroupID])
+      ->setMatch(['option_group_id', 'value'])
+      ->execute();
   }
 
   /**
index 0b2b3653d5477804db367163004692b7005f0f61..ccbe32f8adcc0076825a647f2bd5f3569ff229c0 100644 (file)
@@ -476,6 +476,7 @@ WHERE  v.option_group_id = g.id
    *   the option group ID
    */
   public static function createAssoc($groupName, &$values, &$defaultID, $groupTitle = NULL) {
+    CRM_Core_Error::deprecatedFunctionWarning('use the api');
     // @TODO: This causes a problem in multilingual
     // (https://github.com/civicrm/civicrm-core/pull/17228), but is needed in
     // order to be able to remove currencies once added.
@@ -568,8 +569,12 @@ ORDER BY v.weight
   /**
    * @param string $groupName
    * @param string $operator
+   *
+   * @deprecated
    */
   public static function deleteAssoc($groupName, $operator = "=") {
+    CRM_Core_Error::deprecatedFunctionWarning('use the api');
+
     $query = "
 DELETE g, v
   FROM civicrm_option_group g,
index d92baf18f2c88c43faef0b11a18471e5530fc1e4..b7ae39ef6eeb7c16cbff0939a86c17918c2da66a 100644 (file)
@@ -7,22 +7,20 @@ class CRM_Admin_Form_Setting_LocalizationTest extends CiviUnitTestCase {
 
   /**
    * Test adding and removing a currency.
+   *
+   * @throws \CRM_Core_Exception
    */
-  public function testUpdateCurrencies() {
+  public function testUpdateCurrencies(): void {
     CRM_Admin_Form_Setting_Localization::updateEnabledCurrencies(['USD', 'CAD'], 'USD');
-    CRM_Core_OptionGroup::flushAll();
+
     $currencies = array_keys(CRM_Core_OptionGroup::values('currencies_enabled'));
     $this->assertEquals(['CAD', 'USD'], $currencies, 'Unable to add a currency.');
 
     // Now try to remove it.
     CRM_Admin_Form_Setting_Localization::updateEnabledCurrencies(['USD'], 'USD');
-    CRM_Core_OptionGroup::flushAll();
+
     $currencies = array_keys(CRM_Core_OptionGroup::values('currencies_enabled'));
     $this->assertEquals(['USD'], $currencies, 'Unable to remove a currency.');
-
-    // Note in the form there's code to prevent removing the default. The
-    // function we're testing here isn't the full form so we're not testing
-    // that.
   }
 
 }