From 6821aa1d29af928e5ef1c69caf04fc55afaba1cc Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 27 Aug 2018 11:34:00 +1200 Subject: [PATCH] Make checkboxes save --- CRM/Admin/Form/Preferences.php | 10 ++++++++ CRM/Admin/Form/Setting.php | 2 +- CRM/Admin/Form/SettingTrait.php | 45 ++++++++++++++++++++++++++++++++- settings/Core.setting.php | 1 + 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/CRM/Admin/Form/Preferences.php b/CRM/Admin/Form/Preferences.php index 00810d6001..28b4cf191a 100644 --- a/CRM/Admin/Form/Preferences.php +++ b/CRM/Admin/Form/Preferences.php @@ -116,6 +116,8 @@ class CRM_Admin_Form_Preferences extends CRM_Core_Form { } /** + * @todo deprecate in favour of setting using metadata. + * * @param $defaults */ public function cbsDefaultValues(&$defaults) { @@ -257,6 +259,14 @@ class CRM_Admin_Form_Preferences extends CRM_Core_Form { * Process the form submission. */ public function postProcessCommon() { + try { + $this->saveMetadataDefinedSettings($this->_params); + $this->filterParamsSetByMetadata($this->_params); + } + catch (CiviCRM_API3_Exception $e) { + CRM_Core_Session::setStatus($e->getMessage(), ts('Save Failed'), 'error'); + } + foreach ($this->_varNames as $groupName => $groupValues) { foreach ($groupValues as $settingName => $fieldValue) { switch ($fieldValue['html_type']) { diff --git a/CRM/Admin/Form/Setting.php b/CRM/Admin/Form/Setting.php index 93010df5ae..2bc9abe246 100644 --- a/CRM/Admin/Form/Setting.php +++ b/CRM/Admin/Form/Setting.php @@ -145,7 +145,7 @@ class CRM_Admin_Form_Setting extends CRM_Core_Form { } try { $settings = $this->getSettingsToSetByMetadata($params); - civicrm_api3('setting', 'create', $settings); + $this->saveMetadataDefinedSettings($params); } catch (CiviCRM_API3_Exception $e) { CRM_Core_Session::setStatus($e->getMessage(), ts('Save Failed'), 'error'); diff --git a/CRM/Admin/Form/SettingTrait.php b/CRM/Admin/Form/SettingTrait.php index baa312298f..4ecd397a45 100644 --- a/CRM/Admin/Form/SettingTrait.php +++ b/CRM/Admin/Form/SettingTrait.php @@ -87,6 +87,28 @@ trait CRM_Admin_Form_SettingTrait { } } + /** + * Get the metadata for a particular field. + * + * @param $setting + * @return mixed + */ + protected function getSettingMetadata($setting) { + return $this->getSettingsMetaData()[$setting]; + } + + /** + * Get the metadata for a particular field for a particular item. + * + * e.g get 'serialize' key, if exists, for a field. + * + * @param $setting + * @return mixed + */ + protected function getSettingMetadataItem($setting, $item) { + return CRM_Utils_Array::value($item, $this->getSettingsMetaData()[$setting]); + } + /** * Add fields in the metadata to the template. */ @@ -176,9 +198,30 @@ trait CRM_Admin_Form_SettingTrait { */ protected function setDefaultsForMetadataDefinedFields() { CRM_Core_BAO_ConfigSetting::retrieve($this->_defaults); - foreach ($this->_settings as $setting => $group) { + foreach (array_keys($this->_settings) as $setting) { $this->_defaults[$setting] = civicrm_api3('setting', 'getvalue', ['name' => $setting]); + $spec = $this->getSettingsMetadata()[$setting]; + if (!empty($spec['serialize'])) { + $this->_defaults[$setting] = CRM_Core_DAO::unSerializeField($this->_defaults[$setting], $spec['serialize']); + } + if ($spec['quick_form_type'] === 'CheckBoxes') { + $this->_defaults[$setting] = array_fill_keys($this->_defaults[$setting], 1); + } + } + } + + /** + * @param $params + * + */ + protected function saveMetadataDefinedSettings($params) { + $settings = $this->getSettingsToSetByMetadata($params); + foreach ($settings as $setting => $settingValue) { + if ($this->getSettingMetadataItem($setting, 'quick_form_type') === 'CheckBoxes') { + $settings[$setting] = array_keys($settingValue); + } } + civicrm_api3('setting', 'create', $settings); } } diff --git a/settings/Core.setting.php b/settings/Core.setting.php index a7adf37193..382e554978 100644 --- a/settings/Core.setting.php +++ b/settings/Core.setting.php @@ -54,6 +54,7 @@ return array( 'is_contact' => 0, 'description' => ts("Select the tabs that should be displayed when viewing a contact record. EXAMPLE: If your organization does not keep track of 'Relationships', then un-check this option to simplify the screen display. Tabs for Contributions, Pledges, Memberships, Events, Grants and Cases are also hidden if the corresponding component is not enabled. Go to Administer > System Settings > Enable Components to modify the components which are available for your site."), 'help_text' => NULL, + 'serialize' => CRM_Core_DAO::SERIALIZE_SEPARATOR_BOOKEND, ), 'contact_edit_options' => array( 'group_name' => 'CiviCRM Preferences', -- 2.25.1