X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FAdmin%2FForm%2FSettingTrait.php;h=4ecd397a459127fdf6404e0aac11e8e87b7d8744;hb=36e4d1a775a0f1420c7dfc10e708e6a455225e6a;hp=734eccc83098fd3eaa1df04efde53e0c6f7e3b11;hpb=616d4c6db0b45ade6ddc08f95baf1a6777c11e6a;p=civicrm-core.git diff --git a/CRM/Admin/Form/SettingTrait.php b/CRM/Admin/Form/SettingTrait.php index 734eccc830..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. */ @@ -125,6 +147,19 @@ trait CRM_Admin_Form_SettingTrait { elseif ($add == 'addCheckBox') { $this->addCheckBox($setting, ts($props['title']), $options['values'], NULL, CRM_Utils_Array::value('html_attributes', $props), NULL, NULL, ['  ']); } + elseif ($add == 'addCheckBoxes') { + $options = array_flip($options['values']); + $newOptions = []; + foreach ($options as $key => $val) { + $newOptions[$key] = $val; + } + $this->addCheckBox($setting, + $props['title'], + $newOptions, + NULL, NULL, NULL, NULL, + ['  ', '  ', '
'] + ); + } elseif ($add == 'addChainSelect') { $this->addChainSelect($setting, [ 'label' => ts($props['title']), @@ -155,4 +190,38 @@ trait CRM_Admin_Form_SettingTrait { $this->assign('settings_fields', $settingMetaData); } + + /** + * Get the defaults for all fields defined in the metadata. + * + * All others are pending conversion. + */ + protected function setDefaultsForMetadataDefinedFields() { + CRM_Core_BAO_ConfigSetting::retrieve($this->_defaults); + 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); + } + }