Towards rationalising settings form & preferences form
authoreileen <emcnaughton@wikimedia.org>
Sun, 26 Aug 2018 23:54:56 +0000 (11:54 +1200)
committereileen <emcnaughton@wikimedia.org>
Sun, 26 Aug 2018 23:54:56 +0000 (11:54 +1200)
CRM/Admin/Form/Setting.php
CRM/Admin/Form/SettingTrait.php [new file with mode: 0644]

index 695f6f097df4bcf63c494d270d9bafe460934f46..f10182b864a54df53a51ca9b8ec3ca0ed92405d0 100644 (file)
@@ -36,6 +36,8 @@
  */
 class CRM_Admin_Form_Setting extends CRM_Core_Form {
 
+  use CRM_Admin_Form_SettingTrait;
+
   protected $_settings = array();
 
   /**
@@ -160,15 +162,6 @@ class CRM_Admin_Form_Setting extends CRM_Core_Form {
     $this->assign('settings_fields', $settingMetaData);
   }
 
-  /**
-   * Get default entity.
-   *
-   * @return string
-   */
-  public function getDefaultEntity() {
-    return 'Setting';
-  }
-
   /**
    * Process the form submission.
    */
@@ -185,6 +178,7 @@ class CRM_Admin_Form_Setting extends CRM_Core_Form {
    * @todo Document what I do.
    *
    * @param array $params
+   * @throws \CRM_Core_Exception
    */
   public function commonProcess(&$params) {
 
@@ -216,20 +210,19 @@ class CRM_Admin_Form_Setting extends CRM_Core_Form {
         unset($params[$name]);
       }
     }
-    $settings = array_intersect_key($params, $this->_settings);
-    $result = civicrm_api('setting', 'create', $settings + array('version' => 3));
-    foreach ($settings as $setting => $settingGroup) {
-      //@todo array_diff this
-      unset($params[$setting]);
+    try {
+      $settings = $this->getSettingsToSetByMetadata($params);
+      civicrm_api3('setting', 'create', $settings);
     }
-    if (!empty($result['error_message'])) {
-      CRM_Core_Session::setStatus($result['error_message'], ts('Save Failed'), 'error');
+    catch (CiviCRM_API3_Exception $e) {
+      CRM_Core_Session::setStatus($e->getMessage(), ts('Save Failed'), 'error');
     }
 
-    //CRM_Core_BAO_ConfigSetting::create($params);
+    $this->filterParamsSetByMetadata($params);
+
     $params = CRM_Core_BAO_ConfigSetting::filterSkipVars($params);
     if (!empty($params)) {
-      CRM_Core_Error::fatal('Unrecognized setting. This may be a config field which has not been properly migrated to a setting. (' . implode(', ', array_keys($params)) . ')');
+      throw new CRM_Core_Exception('Unrecognized setting. This may be a config field which has not been properly migrated to a setting. (' . implode(', ', array_keys($params)) . ')');
     }
 
     CRM_Core_Config::clearDBCache();
@@ -299,17 +292,4 @@ class CRM_Admin_Form_Setting extends CRM_Core_Form {
     ) + $autoSearchFields;
   }
 
-  /**
-   * Get the metadata relating to the settings on the form, ordered by the keys in $this->_settings.
-   *
-   * @return array
-   */
-  protected function getSettingsMetaData() {
-    $allSettingMetaData = civicrm_api3('setting', 'getfields', array());
-    $settingMetaData = array_intersect_key($allSettingMetaData['values'], $this->_settings);
-    // This array_merge re-orders to the key order of $this->_settings.
-    $settingMetaData = array_merge($this->_settings, $settingMetaData);
-    return $settingMetaData;
-  }
-
 }
diff --git a/CRM/Admin/Form/SettingTrait.php b/CRM/Admin/Form/SettingTrait.php
new file mode 100644 (file)
index 0000000..a651c47
--- /dev/null
@@ -0,0 +1,90 @@
+<?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
+ */
+
+/**
+ * This trait allows us to consolidate Preferences & Settings forms.
+ *
+ * It is intended mostly as part of a refactoring process to get rid of having 2.
+ */
+trait CRM_Admin_Form_SettingTrait {
+
+  /**
+   * @var array
+   */
+  protected $settingsMetadata;
+
+  /**
+   * Get default entity.
+   *
+   * @return string
+   */
+  public function getDefaultEntity() {
+    return 'Setting';
+  }
+
+  /**
+   * Get the metadata relating to the settings on the form, ordered by the keys in $this->_settings.
+   *
+   * @return array
+   */
+  protected function getSettingsMetaData() {
+    if (empty($this->settingsMetadata)) {
+      $allSettingMetaData = civicrm_api3('setting', 'getfields', []);
+      $this->settingsMetadata = array_intersect_key($allSettingMetaData['values'], $this->_settings);
+      // This array_merge re-orders to the key order of $this->_settings.
+      $this->settingsMetadata = array_merge($this->_settings, $this->settingsMetadata);
+    }
+    return $this->settingsMetadata;
+  }
+
+  /**
+   * Get the settings which can be stored based on metadata.
+   *
+   * @param array $params
+   * @return array
+   */
+  protected function getSettingsToSetByMetadata($params) {
+    return array_intersect_key($params, $this->_settings);
+  }
+
+  /**
+   * @param $params
+   */
+  protected function filterParamsSetByMetadata(&$params) {
+    foreach ($this->getSettingsToSetByMetadata($params) as $setting => $settingGroup) {
+      //@todo array_diff this
+      unset($params[$setting]);
+    }
+  }
+
+}