Make checkboxes save
authoreileen <emcnaughton@wikimedia.org>
Sun, 26 Aug 2018 23:34:00 +0000 (11:34 +1200)
committereileen <emcnaughton@wikimedia.org>
Mon, 8 Oct 2018 15:25:11 +0000 (16:25 +0100)
CRM/Admin/Form/Preferences.php
CRM/Admin/Form/Setting.php
CRM/Admin/Form/SettingTrait.php
settings/Core.setting.php

index 00810d6001b91332ed67c5686cc26790ba0d5ffa..28b4cf191a465dbac44c9e35d5e002c109de6bd2 100644 (file)
@@ -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']) {
index 93010df5aec947c905ed345c872731ed37c5d722..2bc9abe2467647c60692d5e551f6688f6794ac4c 100644 (file)
@@ -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');
index baa312298fa9f0b16a59f155c0cced4f52583b5a..4ecd397a459127fdf6404e0aac11e8e87b7d8744 100644 (file)
@@ -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);
   }
 
 }
index a7adf3719397ac7bc38f3671d69ad59c5e8a3aa3..382e55497844e0a135fc19571d19665974599746 100644 (file)
@@ -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',