Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Towards rationalising settings form & preferences form (partial of 12731) #12732

Merged
merged 1 commit into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 11 additions & 31 deletions CRM/Admin/Form/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
*/
class CRM_Admin_Form_Setting extends CRM_Core_Form {

use CRM_Admin_Form_SettingTrait;

protected $_settings = array();

/**
Expand Down Expand Up @@ -160,15 +162,6 @@ public function buildQuickForm() {
$this->assign('settings_fields', $settingMetaData);
}

/**
* Get default entity.
*
* @return string
*/
public function getDefaultEntity() {
return 'Setting';
}

/**
* Process the form submission.
*/
Expand All @@ -185,6 +178,7 @@ public function postProcess() {
* @todo Document what I do.
*
* @param array $params
* @throws \CRM_Core_Exception
*/
public function commonProcess(&$params) {

Expand Down Expand Up @@ -216,20 +210,19 @@ public function commonProcess(&$params) {
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();
Expand Down Expand Up @@ -299,17 +292,4 @@ public static function getAutocompleteContactSearch() {
) + $autoSearchFields;
}

/**
* Get the metadata relating to the settings on the form, ordered by the keys in $this->_settings.
*
* @return array
*/
protected function getSettingsMetaData() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to the trait

$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;
}

}
90 changes: 90 additions & 0 deletions CRM/Admin/Form/SettingTrait.php
Original file line number Diff line number Diff line change
@@ -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]);
}
}

}