Documentation, format class, no modification. (#7629)

This commit is contained in:
frytimo
2025-11-19 12:48:36 -04:00
committed by GitHub
parent 0ea256fce8
commit 34821bed7e
36 changed files with 12982 additions and 11551 deletions

View File

@@ -3,8 +3,8 @@
/**
* The settings class is used to load settings using hierarchical overriding
*
* The settings are loaded from the database tables default_settings, domain_settings, and user_settings in that order with
* Each setting overrides the setting from the previous table.
* The settings are loaded from the database tables default_settings, domain_settings, and user_settings in that order
* with Each setting overrides the setting from the previous table.
*
* @access public
* @author Mark Crane <mark@fusionpbx.com>
@@ -12,49 +12,59 @@
class settings implements clear_cache {
/**
* Set in the constructor. String used to load a specific domain. Must be a value domain UUID before sending to the constructor.
* Set in the constructor. String used to load a specific domain. Must be a value domain UUID before sending to the
* constructor.
*
* @var string
*/
private $domain_uuid;
/**
* Set in the constructor. String used to load a specific user. Must be a valid user UUID before sending to the constructor.
* Set in the constructor. String used to load a specific user. Must be a valid user UUID before sending to the
* constructor.
*
* @var string
*/
private $user_uuid;
/**
* Set in the constructor. String used for loading a specific device UUID
*
* @var string
*/
private $device_uuid;
/**
* Set in the constructor. String used for loading device profile
*
* @var string
*/
private $device_profile_uuid;
/**
* Set in the constructor. Current category set to load
*
* @var string
*/
private $category;
/**
* Internal array structure that is populated from the database
*
* @var array Array of settings loaded from Default Settings
*/
private $settings;
/**
* Set in the constructor. Must be a database object and cannot be null.
*
* @var database Database Object
*/
private $database;
/**
* Tracks if the APCu extension is loaded for database queries
*
* @var bool
*/
private $apcu_enabled;
@@ -63,9 +73,11 @@ class settings implements clear_cache {
* Create a settings object using key/value pairs in the $setting_array.
*
* Valid values are: database, domain_uuid, user_uuid, device_uuid, device_profile_uuid, and category.
*
* @param array setting_array
*
* @depends database::new()
* @access public
* @access public
*/
public function __construct($setting_array = []) {
@@ -89,7 +101,7 @@ class settings implements clear_cache {
//trap passing a PDO object instead of the required database object
if (!($this->database instanceof database)) {
//should never happen but will trap it here just-in-case
throw new \InvalidArgumentException("Database object passed in settings class constructor is not a valid database object");
throw new InvalidArgumentException("Database object passed in settings class constructor is not a valid database object");
}
//set the values from the array
@@ -102,14 +114,6 @@ class settings implements clear_cache {
$this->reload();
}
/**
* Returns the database object used in the settings
* @return database Object
*/
public function database(): database {
return $this->database;
}
/**
* Reloads the settings from the database
*/
@@ -144,107 +148,9 @@ class settings implements clear_cache {
}
}
/**
* Get the value utilizing the hierarchical overriding technique
* @param string|null $category Returns all settings when empty or the default value if the settings array is null
* @param string|null $subcategory Returns the array of category items when empty or the default value if the category array is null
* @param mixed $default_value allows default value returned if category and subcategory not found
*/
public function get(?string $category = null, ?string $subcategory = null, $default_value = null) {
//incremental refinement from all settings to a single setting
if (empty($category)) {
return $this->settings ?? $default_value;
}
elseif (empty($subcategory)) {
return $this->settings[$category] ?? $default_value;
}
else {
return $this->settings[$category][$subcategory] ?? $default_value;
}
}
/**
* Returns the domain_uuid in this object used to load the settings
* @return string UUID of the domain used to load the object or an empty string
*/
public function get_domain_uuid(): string {
if (!empty($this->domain_uuid)) {
return $this->domain_uuid;
}
return "";
}
/**
* Returns the user_uuid in this object used to load the settings
* @return string UUID of the user used to load the object or an empty string
*/
public function get_user_uuid(): string {
if (!empty($this->user_uuid)) {
return $this->user_uuid;
}
return "";
}
/**
* 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 $value (optional) Value to set. Default is empty string.
* @param string $type Type of the setting (array, numeric, text, etc)
* @param bool $enabled (optional) True or False. Default is True.
* @param string $description (optional) Description. Default is empty string.
*/
public function set(string $table_prefix, string $uuid, string $category, string $subcategory, string $value = "", string $type = 'text', 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
$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 = permissions::new();
$p->add($table_prefix.'_setting_add', 'temp');
$p->add($table_prefix.'_setting_edit', 'temp');
//execute insert
$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');
}
/**
* Update the internal settings array with the default settings from the database
*
* @access private
*/
private function default_settings() {
@@ -275,14 +181,12 @@ class settings implements clear_cache {
if (isset($row['default_setting_value']) && $row['default_setting_value'] !== '') {
if ($name == "boolean") {
$this->settings[$category][$subcategory] = filter_var($row['default_setting_value'], FILTER_VALIDATE_BOOLEAN);
}
elseif ($name == "array") {
} elseif ($name == "array") {
if (!isset($this->settings[$category][$subcategory]) || !is_array($this->settings[$category][$subcategory])) {
$this->settings[$category][$subcategory] = array();
$this->settings[$category][$subcategory] = [];
}
$this->settings[$category][$subcategory][] = $row['default_setting_value'];
}
else {
} else {
$this->settings[$category][$subcategory] = $row['default_setting_value'];
}
}
@@ -297,6 +201,7 @@ class settings implements clear_cache {
/**
* Update the internal settings array with the domain settings from the database
*
* @access private
*/
private function domain_settings($domain_uuid = '', $i = 0) {
@@ -312,7 +217,7 @@ class settings implements clear_cache {
$this->domain_settings($uuid, $i++);
}
$key = 'settings_domain_'.$domain_uuid;
$key = 'settings_domain_' . $domain_uuid;
$result = '';
//if the apcu extension is loaded get the cached database result
@@ -336,7 +241,7 @@ class settings implements clear_cache {
$category = $row['domain_setting_category'];
$subcategory = $row['domain_setting_subcategory'];
if ($name == "array") {
$this->settings[$category][$subcategory] = array();
$this->settings[$category][$subcategory] = [];
}
}
@@ -351,11 +256,10 @@ class settings implements clear_cache {
}
if ($name == "array") {
if (!isset($this->settings[$category][$subcategory]) || !is_array($this->settings[$category][$subcategory])) {
$this->settings[$category][$subcategory] = array();
$this->settings[$category][$subcategory] = [];
}
$this->settings[$category][$subcategory][] = $row['domain_setting_value'];
}
else {
} else {
$this->settings[$category][$subcategory] = $row['domain_setting_value'];
}
}
@@ -366,11 +270,12 @@ class settings implements clear_cache {
/**
* Update the internal settings array with the user settings from the database
* @access private
*
* @access private
* @depends $this->domain_uuid
*/
private function user_settings() {
$key = 'settings_user_'.$this->user_uuid;
$key = 'settings_user_' . $this->user_uuid;
$result = '';
//if the apcu extension is loaded get the cached database result
if ($this->apcu_enabled && apcu_exists($key)) {
@@ -397,11 +302,9 @@ class settings implements clear_cache {
if (isset($row['user_setting_value']) && $row['user_setting_value'] !== '') {
if ($name == "boolean") {
$this->settings[$category][$subcategory] = filter_var($row['user_setting_value'], FILTER_VALIDATE_BOOLEAN);
}
elseif ($name == "array") {
} elseif ($name == "array") {
$this->settings[$category][$subcategory][] = $row['user_setting_value'];
}
else {
} else {
$this->settings[$category][$subcategory] = $row['user_setting_value'];
}
@@ -413,7 +316,8 @@ class settings implements clear_cache {
/**
* Update the internal settings array with the device profile settings from the database
* @access private
*
* @access private
* @depends $this->domain_uuid
*/
private function device_profile_settings() {
@@ -438,7 +342,8 @@ class settings implements clear_cache {
/**
* Update the internal settings array with the device settings from the database
* @access private
*
* @access private
* @depends $this->domain_uuid
*/
private function device_settings() {
@@ -461,6 +366,15 @@ class settings implements clear_cache {
}
}
/**
* Clears the APC cache and reloads the settings object.
*
* This method checks if APC is enabled on the server and if so, it clears
* any cached entries that start with "settings_". It then recreates the
* settings object to load all settings from the database.
*
* @return void
*/
public static function clear_cache() {
if (function_exists('apcu_enabled') && apcu_enabled()) {
$cache = apcu_cache_info(false);
@@ -484,4 +398,126 @@ class settings implements clear_cache {
}
}
}
/**
* Returns the database object used in the settings
*
* @return database Object
*/
public function database(): database {
return $this->database;
}
/**
* Returns the domain_uuid in this object used to load the settings
*
* @return string UUID of the domain used to load the object or an empty string
*/
public function get_domain_uuid(): string {
if (!empty($this->domain_uuid)) {
return $this->domain_uuid;
}
return "";
}
/**
* Returns the user_uuid in this object used to load the settings
*
* @return string UUID of the user used to load the object or an empty string
*/
public function get_user_uuid(): string {
if (!empty($this->user_uuid)) {
return $this->user_uuid;
}
return "";
}
/**
* Get the value utilizing the hierarchical overriding technique
*
* @param string|null $category Returns all settings when empty or the default value if the settings array is
* null
* @param string|null $subcategory Returns the array of category items when empty or the default value if the
* category array is null
* @param mixed $default_value allows default value returned if category and subcategory not found
*/
public function get(?string $category = null, ?string $subcategory = null, $default_value = null) {
//incremental refinement from all settings to a single setting
if (empty($category)) {
return $this->settings ?? $default_value;
} elseif (empty($subcategory)) {
return $this->settings[$category] ?? $default_value;
} else {
return $this->settings[$category][$subcategory] ?? $default_value;
}
}
/**
* Create or update an in-memory setting for a domain, user, device,
* or device profile.
*
* This method assembles a settings record, generates a UUID when needed,
* assigns ownership context (domain, user, device, or device profile),
* grants temporary permissions, and commits the record using the database
* abstraction layer. Only in-memory values are modified; this does not
* reload or apply settings system-wide.
*
* @param string $table_prefix Prefix used to build the settings table name.
* @param string $uuid UUID of the setting. A new UUID is generated
* if the provided value is empty or invalid.
* @param string $category Setting category.
* @param string $subcategory Setting subcategory.
* @param string $value Optional value for the setting. Defaults to an empty string.
* @param string $type Setting type (e.g. text, numeric, array).
* @param bool $enabled Whether the setting is enabled. Defaults to true.
* @param string $description Optional description for the setting.
*
* @return void
*/
public function set(string $table_prefix, string $uuid, string $category, string $subcategory, string $value = "", string $type = 'text', 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
$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 = permissions::new();
$p->add($table_prefix . '_setting_add', 'temp');
$p->add($table_prefix . '_setting_edit', 'temp');
//execute insert
$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');
}
}