Merge pull request #12890 from dereklewis123/CW-905-2
[civicrm-core.git] / CRM / Admin / Form / SettingTrait.php
index a651c47c89fa43914131075344cdfdab73d5ce95..4ecd397a459127fdf6404e0aac11e8e87b7d8744 100644 (file)
@@ -87,4 +87,141 @@ 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.
+   */
+  protected function addFieldsDefinedInSettingsMetadata() {
+    $settingMetaData = $this->getSettingsMetaData();
+    $descriptions = [];
+    foreach ($settingMetaData as $setting => $props) {
+      if (isset($props['quick_form_type'])) {
+        if (isset($props['pseudoconstant'])) {
+          $options = civicrm_api3('Setting', 'getoptions', [
+            'field' => $setting,
+          ]);
+        }
+        else {
+          $options = NULL;
+        }
+        //Load input as readonly whose values are overridden in civicrm.settings.php.
+        if (Civi::settings()->getMandatory($setting)) {
+          $props['html_attributes']['readonly'] = TRUE;
+          $this->includesReadOnlyFields = TRUE;
+        }
+
+        $add = 'add' . $props['quick_form_type'];
+        if ($add == 'addElement') {
+          $this->$add(
+            $props['html_type'],
+            $setting,
+            ts($props['title']),
+            ($options !== NULL) ? $options['values'] : CRM_Utils_Array::value('html_attributes', $props, []),
+            ($options !== NULL) ? CRM_Utils_Array::value('html_attributes', $props, []) : NULL
+          );
+        }
+        elseif ($add == 'addSelect') {
+          $this->addElement('select', $setting, ts($props['title']), $options['values'], CRM_Utils_Array::value('html_attributes', $props));
+        }
+        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']),
+          ]);
+        }
+        elseif ($add == 'addMonthDay') {
+          $this->add('date', $setting, ts($props['title']), CRM_Core_SelectValues::date(NULL, 'M d'));
+        }
+        else {
+          $this->$add($setting, ts($props['title']));
+        }
+        // Migrate to using an array as easier in smart...
+        $descriptions[$setting] = ts($props['description']);
+        $this->assign("{$setting}_description", ts($props['description']));
+        if ($setting == 'max_attachments') {
+          //temp hack @todo fix to get from metadata
+          $this->addRule('max_attachments', ts('Value should be a positive number'), 'positiveInteger');
+        }
+        if ($setting == 'maxFileSize') {
+          //temp hack
+          $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger');
+        }
+
+      }
+    }
+    // setting_description should be deprecated - see Mail.tpl for metadata based tpl.
+    $this->assign('setting_descriptions', $descriptions);
+    $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);
+  }
+
 }