*/
class CRM_Admin_Form_Setting extends CRM_Core_Form {
+ use CRM_Admin_Form_SettingTrait;
+
protected $_settings = array();
/**
$this->assign('settings_fields', $settingMetaData);
}
- /**
- * Get default entity.
- *
- * @return string
- */
- public function getDefaultEntity() {
- return 'Setting';
- }
-
/**
* Process the form submission.
*/
* @todo Document what I do.
*
* @param array $params
+ * @throws \CRM_Core_Exception
*/
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();
) + $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;
- }
-
}
--- /dev/null
+<?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]);
+ }
+ }
+
+}