From 946389fbb640eb2cafc13531dffc0b3bc2407b27 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 27 Aug 2018 11:54:56 +1200 Subject: [PATCH] Towards rationalising settings form & preferences form --- CRM/Admin/Form/Setting.php | 42 ++++----------- CRM/Admin/Form/SettingTrait.php | 90 +++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 31 deletions(-) create mode 100644 CRM/Admin/Form/SettingTrait.php diff --git a/CRM/Admin/Form/Setting.php b/CRM/Admin/Form/Setting.php index 695f6f097d..f10182b864 100644 --- a/CRM/Admin/Form/Setting.php +++ b/CRM/Admin/Form/Setting.php @@ -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 index 0000000000..a651c47c89 --- /dev/null +++ b/CRM/Admin/Form/SettingTrait.php @@ -0,0 +1,90 @@ +_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]); + } + } + +} -- 2.25.1