diff --git a/CRM/Admin/Form/Preferences.php b/CRM/Admin/Form/Preferences.php index c379954b73e2..28b4cf191a46 100644 --- a/CRM/Admin/Form/Preferences.php +++ b/CRM/Admin/Form/Preferences.php @@ -35,6 +35,9 @@ * Base class for settings forms. */ class CRM_Admin_Form_Preferences extends CRM_Core_Form { + + use CRM_Admin_Form_SettingTrait; + protected $_system = FALSE; protected $_contactID = NULL; public $_action = NULL; @@ -85,7 +88,9 @@ public function preProcess() { $this->_config->contact_id = $this->_contactID; } + $this->addFieldsDefinedInSettingsMetadata(); $settings = Civi::settings(); + // @todo replace this by defining all in settings. foreach ($this->_varNames as $groupName => $settingNames) { foreach ($settingNames as $settingName => $options) { $this->_config->$settingName = $settings->get($settingName); @@ -98,18 +103,21 @@ public function preProcess() { * @return array */ public function setDefaultValues() { - $defaults = array(); + $this->_defaults = array(); + $this->setDefaultsForMetadataDefinedFields(); foreach ($this->_varNames as $groupName => $settings) { foreach ($settings as $settingName => $settingDetails) { - $defaults[$settingName] = isset($this->_config->$settingName) ? $this->_config->$settingName : CRM_Utils_Array::value('default', $settingDetails, NULL); + $this->_defaults[$settingName] = isset($this->_config->$settingName) ? $this->_config->$settingName : CRM_Utils_Array::value('default', $settingDetails, NULL); } } - return $defaults; + return $this->_defaults; } /** + * @todo deprecate in favour of setting using metadata. + * * @param $defaults */ public function cbsDefaultValues(&$defaults) { @@ -251,6 +259,14 @@ public function postProcess() { * 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/Preferences/Display.php b/CRM/Admin/Form/Preferences/Display.php index f8cb9af81828..1ad83572600f 100644 --- a/CRM/Admin/Form/Preferences/Display.php +++ b/CRM/Admin/Form/Preferences/Display.php @@ -35,17 +35,17 @@ * This class generates form components for the display preferences. */ class CRM_Admin_Form_Preferences_Display extends CRM_Admin_Form_Preferences { + + protected $_settings = array( + 'contact_view_options' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, + ); + public function preProcess() { CRM_Utils_System::setTitle(ts('Settings - Display Preferences')); $optionValues = CRM_Activity_BAO_Activity::buildOptions('activity_type_id'); $this->_varNames = array( CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME => array( - 'contact_view_options' => array( - 'html_type' => 'checkboxes', - 'title' => ts('Viewing Contacts'), - 'weight' => 1, - ), 'contact_smart_group_display' => array( 'html_type' => 'radio', 'title' => ts('Viewing Smart Groups'), diff --git a/CRM/Admin/Form/Setting.php b/CRM/Admin/Form/Setting.php index 09c0b0384229..2bc9abe24676 100644 --- a/CRM/Admin/Form/Setting.php +++ b/CRM/Admin/Form/Setting.php @@ -56,18 +56,9 @@ public function setDefaultValues() { $formMode = TRUE; } - CRM_Core_BAO_ConfigSetting::retrieve($this->_defaults); - - // we can handle all the ones defined in the metadata here. Others to be converted - foreach ($this->_settings as $setting => $group) { - $this->_defaults[$setting] = civicrm_api('setting', 'getvalue', array( - 'version' => 3, - 'name' => $setting, - 'group' => $group, - ) - ); - } + $this->setDefaultsForMetadataDefinedFields(); + // @todo thise should be retrievable from the above function. $this->_defaults['contact_autocomplete_options'] = self::getAutocompleteContactSearch(); $this->_defaults['contact_reference_options'] = self::getAutocompleteContactReference(); $this->_defaults['enableSSL'] = Civi::settings()->get('enableSSL'); @@ -154,7 +145,7 @@ public function commonProcess(&$params) { } 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 734eccc83098..4ecd397a4591 100644 --- a/CRM/Admin/Form/SettingTrait.php +++ b/CRM/Admin/Form/SettingTrait.php @@ -87,6 +87,28 @@ protected function filterParamsSetByMetadata(&$params) { } } + /** + * 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 @@ protected function addFieldsDefinedInSettingsMetadata() { 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 @@ protected function addFieldsDefinedInSettingsMetadata() { $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); + } + } diff --git a/settings/Core.setting.php b/settings/Core.setting.php index 9d91ed59c03a..382e55497844 100644 --- a/settings/Core.setting.php +++ b/settings/Core.setting.php @@ -42,17 +42,19 @@ 'group' => 'core', 'name' => 'contact_view_options', 'type' => 'String', + 'quick_form_type' => 'CheckBoxes', 'html_type' => 'checkboxes', 'pseudoconstant' => array( 'optionGroupName' => 'contact_view_options', ), 'default' => '123456789101113', 'add' => '4.1', - 'title' => 'Viewing Contacts', + 'title' => ts('Viewing Contacts'), 'is_domain' => '1', 'is_contact' => 0, - 'description' => NULL, + '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',