update the settings set method to use params instead of array (#6804)

* update the settings set method to use params instead of array

* remove test code
This commit is contained in:
frytimo
2023-09-20 17:59:37 -06:00
committed by GitHub
parent 518c32efe6
commit 9a982edc2e
+44 -51
View File
@@ -80,66 +80,59 @@ class settings {
} }
/** /**
* set the default, domain, user, device or device profile settings * set the default, domain, user, device or device profile settings
* * @param string $table_prefix prefix for the table.
* @param string $uuid uuid of the setting if available. If set to an empty string then a new uuid will be created.
* @param string $category Category of the setting.
* @param string $subcategory Subcategory of the setting.
* @param string $type Type of the setting (array, numeric, text, etc)
* @param string $value (optional) Value to set. Default is empty string.
* @param bool $enabled (optional) True or False. Default is True.
* @param string $description (optional) Description. Default is empty string.
*/ */
public function set($setting_array) { public function set(string $table_prefix, string $uuid, string $category, string $subcategory, string $type = 'text', string $value = "", bool $enabled = true, string $description = "") {
//set the table name
//find the table $table_name = $table_prefix.'_settings';
if (!empty($setting_array['user_uuid']) && is_uuid($setting_array['user_uuid'])) {
$table_prefix = 'user';
$table_name = $table_prefix.'_settings';
$array[$table_name][0]['user_uuid'] = $setting_array['user_uuid'];
$array[$table_name][0]['domain_uuid'] = $setting_array['domain_uuid'];
}
elseif (!empty($setting_array['device_uuid']) && is_uuid($setting_array['device_uuid'])) {
$table_prefix = 'device';
$table_name = $table_prefix.'_settings';
$array[$table_name][0]['user_uuid'] = $setting_array['user_uuid'];
$array[$table_name][0]['domain_uuid'] = $setting_array['domain_uuid'];
}
elseif (!empty($setting_array['device_profile_uuid']) && is_uuid($setting_array['device_profile_uuid'])) {
$table_prefix = 'device_profile';
$table_name = $table_prefix.'_settings';
$array[$table_name][0]['device_profile_uuid'] = $setting_array['device_profile_uuid'];
if (!empty($setting_array['domain_uuid']) && is_uuid($setting_array['domain_uuid'])) {
$array[$table_name][0]['domain_uuid'] = $setting_array['domain_uuid'];
}
}
elseif (!empty($setting_array['domain_uuid']) && is_uuid($setting_array['domain_uuid'])) {
$table_prefix = 'domain';
$table_name = $table_prefix.'_settings';
}
else {
$table_prefix = 'default';
$table_name = $table_prefix.'_settings';
}
//init record as an array
$record = [];
if(!empty($this->domain_uuid)) {
$record[$table_name][0]['domain_uuid'] = $this->domain_uuid;
}
if(!empty($this->user_uuid)) {
$record[$table_name][0]['user_uuid'] = $this->user_uuid;
}
if(!empty($this->device_uuid)) {
$record[$table_name][0]['device_uuid'] = $this->device_uuid;
}
if(!empty($this->device_profile_uuid)) {
$record[$table_name][0]['device_profile_uuid'] = $this->device_profile_uuid;
}
if(!is_uuid($uuid)) {
$uuid = uuid();
}
//build the array //build the array
$array[$table_name][0][$table_prefix.'_setting_uuid'] = $setting_array['setting_uuid']; $record[$table_name][0][$table_prefix.'_setting_uuid' ] = $uuid;
$array[$table_name][0][$table_prefix.'_setting_category'] = $setting_array['setting_category']; $record[$table_name][0][$table_prefix.'_setting_category' ] = $category;
$array[$table_name][0][$table_prefix.'_setting_subcategory'] = $setting_array['setting_subcategory']; $record[$table_name][0][$table_prefix.'_setting_subcategory'] = $subcategory;
$array[$table_name][0][$table_prefix.'_setting_name'] = $setting_array['setting_name']; $record[$table_name][0][$table_prefix.'_setting_name' ] = $type;
$array[$table_name][0][$table_prefix.'_setting_value'] = $setting_array['setting_value']; $record[$table_name][0][$table_prefix.'_setting_value' ] = $value;
$array[$table_name][0][$table_prefix.'_setting_enabled'] = $setting_array['setting_enabled']; $record[$table_name][0][$table_prefix.'_setting_enabled' ] = $enabled;
$array[$table_name][0][$table_prefix.'_setting_description'] = $setting_array['setting_description']; $record[$table_name][0][$table_prefix.'_setting_description'] = $description;
//grant temporary permissions //grant temporary permissions
$p = new permissions; $p = new permissions;
$p->add($table_prefix.'_setting_add', 'temp'); $p->add($table_prefix.'_setting_add', 'temp');
$p->add($table_prefix.'_setting_edit', 'temp'); $p->add($table_prefix.'_setting_edit', 'temp');
//execute insert //execute insert
$this->database->app_name = $table_prefix.'_settings'; $this->database->app_name = $table_name;
$result = $this->database->save($array); $this->database->save($record);
unset($array);
//revoke temporary permissions //revoke temporary permissions
$p->delete($table_prefix.'_setting_add', 'temp'); $p->delete($table_prefix.'_setting_add', 'temp');
$p->delete($table_prefix.'_setting_edit', 'temp'); $p->delete($table_prefix.'_setting_edit', 'temp');
} }
/** /**
@@ -267,4 +260,4 @@ class settings {
} }
?> ?>