Quellcode durchsuchen

Use the database more efficiently

FusionPBX vor 1 Jahr
Ursprung
Commit
0167145be5
1 geänderte Dateien mit 51 neuen und 52 gelöschten Zeilen
  1. 51 52
      resources/classes/domains.php

+ 51 - 52
resources/classes/domains.php

@@ -46,18 +46,31 @@ if (!class_exists('domains')) {
 		private $toggle_values;
 		private $location;
 
+		/**
+		 * Set in the constructor. Must be a database object and cannot be null.
+		 * @var database Database Object
+		 */
+		private $database;
+
 		/**
 		 * called when the object is created
 		 */
-		public function __construct() {
+		public function __construct($setting_array = []) {
 			//assign the variables
-				$this->app_name = 'domains';
-				$this->app_uuid = '8b91605b-f6d2-42e6-a56d-5d1ded01bb44';
-				$this->name = 'domain';
-				$this->table = 'domains';
-				$this->toggle_field = 'domain_enabled';
-				$this->toggle_values = ['true','false'];
-				$this->location = 'domains.php';
+			$this->app_name = 'domains';
+			$this->app_uuid = '8b91605b-f6d2-42e6-a56d-5d1ded01bb44';
+			$this->name = 'domain';
+			$this->table = 'domains';
+			$this->toggle_field = 'domain_enabled';
+			$this->toggle_values = ['true','false'];
+			$this->location = 'domains.php';
+
+			//open a database connection
+			if (empty($setting_array['database'])) {
+				$this->database = database::new();
+			} else {
+				$this->database = $setting_array['database'];
+			}
 		}
 
 		/**
@@ -91,8 +104,7 @@ if (!class_exists('domains')) {
 											$sql = "select domain_name from v_domains ";
 											$sql .= "where domain_uuid = :domain_uuid ";
 											$parameters['domain_uuid'] = $id;
-											$database = new database;
-											$domain_name = $database->select($sql, $parameters, 'column');
+											$domain_name = $this->database->select($sql, $parameters, 'column');
 											unset($sql, $parameters);
 
 										//get the domain settings
@@ -100,8 +112,7 @@ if (!class_exists('domains')) {
 											$sql .= "where domain_uuid = :domain_uuid ";
 											$sql .= "and domain_setting_enabled = 'true' ";
 											$parameters['domain_uuid'] = $id;
-											$database = new database;
-											$result = $database->select($sql, $parameters, 'all');
+											$result = $this->database->select($sql, $parameters, 'all');
 											unset($sql, $parameters);
 
 											if (is_array($result) && sizeof($result) != 0) {
@@ -155,10 +166,9 @@ if (!class_exists('domains')) {
 															if ($field['name'] == 'domain_uuid' && $table_name != 'v_domains') {
 																$sql = "delete from ".$table_name." where domain_uuid = :domain_uuid ";
 																$parameters['domain_uuid'] = $id;
-																$database = new database;
-																$database->app_name = 'domain_settings';
-																$database->app_uuid = 'b31e723a-bf70-670c-a49b-470d2a232f71';
-																$database->execute($sql, $parameters);
+																$this->database->app_name = 'domain_settings';
+																$this->database->app_uuid = 'b31e723a-bf70-670c-a49b-470d2a232f71';
+																$this->database->execute($sql, $parameters);
 																unset($sql, $parameters);
 															}
 														}
@@ -265,10 +275,9 @@ if (!class_exists('domains')) {
 						//delete the checked rows
 							if (is_array($domain_array) && @sizeof($domain_array) != 0) {
 								//execute delete
-									$database = new database;
-									$database->app_name = $this->app_name;
-									$database->app_uuid = $this->app_uuid;
-									$database->delete($domain_array);
+									$this->database->app_name = $this->app_name;
+									$this->database->app_uuid = $this->app_uuid;
+									$this->database->delete($domain_array);
 
 								//set message
 									message::add($text['message-delete']);
@@ -310,8 +319,7 @@ if (!class_exists('domains')) {
 							if (is_array($uuids) && @sizeof($uuids) != 0) {
 								$sql = "select ".$this->name."_uuid as uuid, ".$this->toggle_field." as toggle from v_".$this->table." ";
 								$sql .= "where ".$this->name."_uuid in (".implode(', ', $uuids).") ";
-								$database = new database;
-								$rows = $database->select($sql, $parameters ?? null, 'all');
+								$rows = $this->database->select($sql, $parameters ?? null, 'all');
 								if (is_array($rows) && @sizeof($rows) != 0) {
 									foreach ($rows as $row) {
 										$states[$row['uuid']] = $row['toggle'];
@@ -334,10 +342,9 @@ if (!class_exists('domains')) {
 						//save the changes
 							if (is_array($array) && @sizeof($array) != 0) {
 								//save the array
-									$database = new database;
-									$database->app_name = $this->app_name;
-									$database->app_uuid = $this->app_uuid;
-									$database->save($array);
+									$this->database->app_name = $this->app_name;
+									$this->database->app_uuid = $this->app_uuid;
+									$this->database->save($array);
 									unset($array);
 
 								//set message
@@ -380,8 +387,7 @@ if (!class_exists('domains')) {
 							if (is_array($uuids) && @sizeof($uuids) != 0) {
 								$sql = "select * from v_".$this->table." ";
 								$sql .= "where ".$this->name."_uuid in (".implode(', ', $uuids).") ";
-								$database = new database;
-								$rows = $database->select($sql, $parameters, 'all');
+								$rows = $this->database->select($sql, $parameters, 'all');
 								if (is_array($rows) && @sizeof($rows) != 0) {
 									$x = 0;
 									foreach ($rows as $row) {
@@ -402,10 +408,9 @@ if (!class_exists('domains')) {
 						//save the changes and set the message
 							if (is_array($array) && @sizeof($array) != 0) {
 								//save the array
-									$database = new database;
-									$database->app_name = $this->app_name;
-									$database->app_uuid = $this->app_uuid;
-									$database->save($array);
+									$this->database->app_name = $this->app_name;
+									$this->database->app_uuid = $this->app_uuid;
+									$this->database->save($array);
 									unset($array);
 
 								//set message
@@ -428,8 +433,7 @@ if (!class_exists('domains')) {
 					$sql .= "and domain_setting_enabled = 'true' ";
 					$sql .= " order by domain_setting_order asc ";
 					$parameters['previous_domain_uuid'] = $_SESSION["previous_domain_uuid"];
-					$database = new database;
-					$result = $database->select($sql, $parameters, 'all');
+					$result = $this->database->select($sql, $parameters, 'all');
 					unset($sql, $parameters);
 
 					//unset previous domain settings
@@ -444,8 +448,7 @@ if (!class_exists('domains')) {
 			//get the default settings
 				$sql = "select * from v_default_settings ";
 				$sql .= "order by default_setting_order asc ";
-				$database = new database;
-				$result = $database->select($sql, null, 'all');
+				$result = $this->database->select($sql, null, 'all');
 				unset($sql, $parameters);
 
 				//unset all settings
@@ -495,8 +498,7 @@ if (!class_exists('domains')) {
 					$sql .= "and domain_setting_enabled = 'true' ";
 					$sql .= " order by domain_setting_order asc ";
 					$parameters['domain_uuid'] = $_SESSION["domain_uuid"];
-					$database = new database;
-					$result = $database->select($sql, $parameters, 'all');
+					$result = $this->database->select($sql, $parameters, 'all');
 					unset($sql, $parameters);
 
 					//unset the arrays that domains are overriding
@@ -545,8 +547,7 @@ if (!class_exists('domains')) {
 					$sql .= " order by user_setting_order asc ";
 					$parameters['domain_uuid'] = $_SESSION["domain_uuid"];
 					$parameters['user_uuid'] = $_SESSION["user_uuid"];
-					$database = new database;
-					$result = $database->select($sql, $parameters, 'all');
+					$result = $this->database->select($sql, $parameters, 'all');
 					if (is_array($result)) {
 						foreach ($result as $row) {
 							if ($row['user_setting_enabled'] == 'true') {
@@ -609,6 +610,9 @@ if (!class_exists('domains')) {
 			//add missing default settings
 				$this->settings();
 
+			//save the database object to be used by app_defaults.php
+				$database = $this->database;
+
 			//get the variables
 				$config = new config;
 				$config_path = $config->config_file;
@@ -628,8 +632,7 @@ if (!class_exists('domains')) {
 
 			//get the domains
 				$sql = "select * from v_domains ";
-				$database = new database;
-				$domains = $database->select($sql, null, 'all');
+				$domains = $this->database->select($sql, null, 'all');
 				unset($sql);
 
 			//loop through all domains
@@ -676,8 +679,7 @@ if (!class_exists('domains')) {
 
 			//get an array of the default settings UUIDs
 				$sql = "select * from v_default_settings ";
-				$database = new database;
-				$result = $database->select($sql, null, 'all');
+				$result = $this->database->select($sql, null, 'all');
 				foreach($result as $row) {
 					$setting[$row['default_setting_uuid']] = 1;
 				}
@@ -710,10 +712,9 @@ if (!class_exists('domains')) {
 						$p->add('default_setting_add', 'temp');
 
 					//execute insert
-						$database = new database;
-						$database->app_name = 'default_settings';
-						$database->app_uuid = '2c2453c0-1bea-4475-9f44-4d969650de09';
-						$database->save($array, false);
+						$this->database->app_name = 'default_settings';
+						$this->database->app_uuid = '2c2453c0-1bea-4475-9f44-4d969650de09';
+						$this->database->save($array, false);
 						unset($array);
 
 					//revoke temporary permissions
@@ -727,11 +728,9 @@ if (!class_exists('domains')) {
 		 */
 		public function all() {
 			//get the domains from the database
-				$database = new database;
-				if ($database->table_exists('v_domains')) {
+				if ($this->database->table_exists('v_domains')) {
 					$sql = "select * from v_domains order by domain_name asc;";
-					$database = new database;
-					$result = $database->select($sql, null, 'all');
+					$result = $this->database->select($sql, null, 'all');
 					foreach($result as $row) {
 						$domain_names[] = $row['domain_name'];
 					}