Skip to content

Commit

Permalink
Merge pull request #12731 from eileenmcnaughton/setting_form
Browse files Browse the repository at this point in the history
Setting form - start to sync preferences with Setting, using trait
  • Loading branch information
eileenmcnaughton authored Oct 8, 2018
2 parents 5ec0ddd + 6821aa1 commit 2e9a385
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 22 deletions.
22 changes: 19 additions & 3 deletions CRM/Admin/Form/Preferences.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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']) {
Expand Down
10 changes: 5 additions & 5 deletions CRM/Admin/Form/Preferences/Display.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
15 changes: 3 additions & 12 deletions CRM/Admin/Form/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down
69 changes: 69 additions & 0 deletions CRM/Admin/Form/SettingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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,
['&nbsp;&nbsp;', '&nbsp;&nbsp;', '<br/>']
);
}
elseif ($add == 'addChainSelect') {
$this->addChainSelect($setting, [
'label' => ts($props['title']),
Expand Down Expand Up @@ -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);
}

}
6 changes: 4 additions & 2 deletions settings/Core.setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 2e9a385

Please sign in to comment.