Skip to content

Commit

Permalink
Make checkboxes save
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Oct 8, 2018
1 parent 601361a commit c1f7302
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
10 changes: 10 additions & 0 deletions CRM/Admin/Form/Preferences.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ public function setDefaultValues() {
}

/**
* @todo deprecate in favour of setting using metadata.
*
* @param $defaults
*/
public function cbsDefaultValues(&$defaults) {
Expand Down Expand Up @@ -257,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
2 changes: 1 addition & 1 deletion CRM/Admin/Form/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,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
45 changes: 44 additions & 1 deletion 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 @@ -176,9 +198,30 @@ protected function addFieldsDefinedInSettingsMetadata() {
*/
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);
}

}
1 change: 1 addition & 0 deletions settings/Core.setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
'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',
Expand Down

0 comments on commit c1f7302

Please sign in to comment.