/** * Create a set of settings. * * @param array $settings * @param string $settingType */ public function installSettings(array $settings, $settingType) { // Make sure we have proper settings if (!is_array($settings)) { return false; } // Add settings foreach ($settings as $setting) { // Only install if we have proper keys if (!isset($setting['name']) || !isset($setting['value'])) { continue; } // Add new setting! $settingRecord = new AmForms_SettingRecord(); $settingRecord->type = $settingType; $settingRecord->name = $setting['name']; $settingRecord->handle = $this->_camelCase($setting['name']); $settingRecord->value = $setting['value']; $settingRecord->save(); } return true; }
/** * Save settings. * * @param AmForms_SettingModel * * @return bool */ public function saveSettings(AmForms_SettingModel $settings) { if (!$settings->id) { return false; } $settingsRecord = AmForms_SettingRecord::model()->findById($settings->id); if (!$settingsRecord) { throw new Exception(Craft::t('No settings exists with the ID “{id}”.', array('id' => $settings->id))); } // Set attributes $properSettings = $settings->value; if (is_array($properSettings)) { $properSettings = json_encode($settings->value); } $settingsRecord->setAttributes($settings->getAttributes(), false); $settingsRecord->setAttribute('value', $properSettings); // Validate $settingsRecord->validate(); $settings->addErrors($settingsRecord->getErrors()); // Save settings if (!$settings->hasErrors()) { // Save in database return $settingsRecord->save(); } return false; }