Преглед на файлове

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
frytimo преди 2 години
родител
ревизия
edee5ff3ef
променени са 1 файла, в които са добавени 45 реда и са изтрити 52 реда
  1. 45 52
      resources/classes/settings.php

+ 45 - 52
resources/classes/settings.php

@@ -80,66 +80,59 @@ class 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) {
-
-		//find the table
-			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';
-			}
-
+	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
+		$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
-			$array[$table_name][0][$table_prefix.'_setting_uuid'] = $setting_array['setting_uuid'];
-			$array[$table_name][0][$table_prefix.'_setting_category'] = $setting_array['setting_category'];
-			$array[$table_name][0][$table_prefix.'_setting_subcategory'] = $setting_array['setting_subcategory'];
-			$array[$table_name][0][$table_prefix.'_setting_name'] = $setting_array['setting_name'];
-			$array[$table_name][0][$table_prefix.'_setting_value'] = $setting_array['setting_value'];
-			$array[$table_name][0][$table_prefix.'_setting_enabled'] = $setting_array['setting_enabled'];
-			$array[$table_name][0][$table_prefix.'_setting_description'] = $setting_array['setting_description'];
-			
+		$record[$table_name][0][$table_prefix.'_setting_uuid'       ] = $uuid;
+		$record[$table_name][0][$table_prefix.'_setting_category'   ] = $category;
+		$record[$table_name][0][$table_prefix.'_setting_subcategory'] = $subcategory;
+		$record[$table_name][0][$table_prefix.'_setting_name'       ] = $type;
+		$record[$table_name][0][$table_prefix.'_setting_value'      ] = $value;
+		$record[$table_name][0][$table_prefix.'_setting_enabled'    ] = $enabled;
+		$record[$table_name][0][$table_prefix.'_setting_description'] = $description;
+
 		//grant temporary permissions
-			$p = new permissions;
-			$p->add($table_prefix.'_setting_add', 'temp');
-			$p->add($table_prefix.'_setting_edit', 'temp');
+		$p = new permissions;
+		$p->add($table_prefix.'_setting_add', 'temp');
+		$p->add($table_prefix.'_setting_edit', 'temp');
 
 		//execute insert
-			$this->database->app_name = $table_prefix.'_settings';
-			$result = $this->database->save($array);
-			unset($array);
+		$this->database->app_name = $table_name;
+		$this->database->save($record);
 
 		//revoke temporary permissions
-			$p->delete($table_prefix.'_setting_add', 'temp');
-			$p->delete($table_prefix.'_setting_edit', 'temp');
-
+		$p->delete($table_prefix.'_setting_add', 'temp');
+		$p->delete($table_prefix.'_setting_edit', 'temp');
 	}
 
 	/**
@@ -267,4 +260,4 @@ class settings {
 
 }
 
-?>
+?>