Update database.php

Update the database class delete method.
This commit is contained in:
FusionPBX
2016-11-23 13:39:20 -07:00
committed by GitHub
parent a7b76b7a84
commit f8d40ccc6d
+55 -126
View File
@@ -350,7 +350,7 @@ include "root.php";
} }
// Use this function to execute complex queries // Use this function to execute complex queries
public function execute(){ public function execute() {
//connect to the database if needed //connect to the database if needed
if (!$this->db) { if (!$this->db) {
@@ -368,7 +368,7 @@ include "root.php";
} }
} }
public function add(){ public function add() {
//connect to the database if needed //connect to the database if needed
if (!$this->db) { if (!$this->db) {
$this->connect(); $this->connect();
@@ -530,6 +530,28 @@ include "root.php";
//echo "</pre>\n"; //echo "</pre>\n";
//exit; //exit;
//get the $apps array from the installed apps from the core and mod directories
//$config_list = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/$schema_name/app_config.php");
/*
$x = 0;
foreach ($config_list as &$config_path) {
include($config_path);
$x++;
}
$tables = $apps[0]['db'];
foreach ($tables as &$row) {
//print_r($row);
$table = $row['table'];
echo $table."\n";
foreach ($row['fields'] as &$field) {
if (isset($field['key']['type'])) {
print_r($field);
}
}
}
*/
//loop through the array //loop through the array
foreach ($new_array as $schema_name => $schema_array) { foreach ($new_array as $schema_name => $schema_array) {
@@ -568,7 +590,7 @@ include "root.php";
//get the data before the delete //get the data before the delete
if ($parent_key_exists) { if ($parent_key_exists) {
$sql = "SELECT ".implode(", ", $parent_field_names)." FROM ".$table_name." "; $sql = "SELECT * FROM ".$table_name." ";
$sql .= "WHERE ".$parent_key_name." = '".$this->uuid."' "; $sql .= "WHERE ".$parent_key_name." = '".$this->uuid."' ";
$prep_statement = $this->db->prepare($sql); $prep_statement = $this->db->prepare($sql);
if ($prep_statement) { if ($prep_statement) {
@@ -601,23 +623,25 @@ include "root.php";
//delete a specific uuid //delete a specific uuid
if ($action == "delete") { if ($action == "delete") {
if (permission_exists($this->singular($this->name).'_delete') if (permission_exists($this->singular($this->name).'_delete') && strlen($parent_key_value) > 0
&& ($parent_key_exists) && is_uuid($parent_key_value)) { && ($parent_key_exists) && is_uuid($parent_key_value)) {
//set the table name //set the table name
$table_name = 'v_'.$this->name; $table_name = 'v_'.$this->name;
//parent data //parent data
//$sql = "DELETE FROM $table_name "; $sql = "DELETE FROM $table_name ";
//$sql .= "WHERE $parent_key_name = '$parent_key_value' "; $sql .= "WHERE $parent_key_name = '$parent_key_value' ;";
$sql = "DELETE FROM :table_name "; //echo $sql;
$sql .= "WHERE :parent_key_name = ':parent_key_value' "; //$sql = "DELETE FROM :table_name ";
$statement = $this->db->prepare($sql); //$sql .= "WHERE :parent_key_name = ':parent_key_value'; ";
$statement->bindParam(':table_name', $table_name); //$statement = $this->db->prepare($sql);
$statement->bindParam(':parent_key_name', $parent_key_name); //$statement->bindParam(':table_name', $table_name);
$statement->bindParam(':parent_key_value', $parent_key_value); //$statement->bindParam(':parent_key_name', $parent_key_name);
//$statement->bindParam(':parent_key_value', $parent_key_value);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try { try {
//$this->db->query(check_sql($sql)); $this->db->query(check_sql($sql));
$statement->execute(); //$statement->execute();
$message["message"] = "OK"; $message["message"] = "OK";
$message["code"] = "200"; $message["code"] = "200";
$message["uuid"] = $parent_key_value; $message["uuid"] = $parent_key_value;
@@ -631,6 +655,7 @@ include "root.php";
$this->message = $message; $this->message = $message;
$m++; $m++;
unset($sql); unset($sql);
unset($statement);
} }
catch(PDOException $e) { catch(PDOException $e) {
$message["message"] = "Bad Request"; $message["message"] = "Bad Request";
@@ -700,7 +725,7 @@ include "root.php";
//get the child data //get the child data
if ($uuid_exists) { if ($uuid_exists) {
$sql = "SELECT ". implode(", ", $child_field_names)." FROM ".$table_name." "; $sql = "SELECT * FROM ".$table_name." ";
$sql .= "WHERE ".$child_key_name." = '".$child_key_value."' "; $sql .= "WHERE ".$child_key_name." = '".$child_key_value."' ";
$prep_statement = $this->db->prepare($sql); $prep_statement = $this->db->prepare($sql);
if ($prep_statement) { if ($prep_statement) {
@@ -728,22 +753,22 @@ include "root.php";
//delete the child data //delete the child data
if ($action == "delete") { if ($action == "delete") {
if (permission_exists($child_name.'_delete')) { if (permission_exists($child_name.'_delete')) {
//$sql = "DELETE FROM ".$table_name." "; $sql = "DELETE FROM ".$table_name." ";
//$sql .= "WHERE ".$parent_key_name." = '".$this->uuid."' "; $sql .= "WHERE ".$child_key_name." = '".$child_key_value."' ";
//$sql .= "AND ".$child_key_name." = '".$child_key_value."' "; if (strlen($this->uuid) > 0) { $sql .= "AND ".$parent_key_name." = '".$this->uuid."' "; }
$sql = "DELETE FROM :table_name "; //$sql = "DELETE FROM :table_name ";
$sql .= "WHERE :parent_key_name = ':parent_key_value' "; //$sql .= "WHERE :child_key_name = ':child_key_value' ";
$sql .= "AND :child_key_name = ':child_key_value' "; //if (strlen($this->uuid) > 0) { $sql .= "AND :parent_key_name = ':parent_key_value' }";
$statement = $this->db->prepare($sql); //$statement = $this->db->prepare($sql);
$statement->bindParam(':table_name', $table_name); //$statement->bindParam(':table_name', $table_name);
$statement->bindParam(':parent_key_name', $parent_key_name); //$statement->bindParam(':parent_key_name', $parent_key_name);
$statement->bindParam(':parent_key_value', $this->uuid); //$statement->bindParam(':parent_key_value', $this->uuid);
$statement->bindParam(':child_key_name', $child_key_name); //$statement->bindParam(':child_key_name', $child_key_name);
$statement->bindParam(':child_key_value', $child_key_value); //$statement->bindParam(':child_key_value', $child_key_value);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try { try {
//$this->db->query(check_sql($sql)); $this->db->query(check_sql($sql));
$statement->execute(); //$statement->execute();
$message["details"][$m]["name"] = $key; $message["details"][$m]["name"] = $key;
$message["details"][$m]["message"] = "OK"; $message["details"][$m]["message"] = "OK";
$message["details"][$m]["code"] = "200"; $message["details"][$m]["code"] = "200";
@@ -850,7 +875,7 @@ include "root.php";
$this->db->exec(check_sql($sql)); $this->db->exec(check_sql($sql));
unset($sql); unset($sql);
} }
} } //delete
public function count() { public function count() {
//connect to the database if needed //connect to the database if needed
@@ -1006,102 +1031,6 @@ include "root.php";
} }
} }
public function delete_new($uuid = null, $array = null) {
//connect to the database if needed
if (!$this->db) {
$this->connect();
}
//delete a specific uuid
if (permission_exists($this->singular($this->name).'_delete')) {
if (isset($api_uuid)) {
//start the atomic transaction
$this->db->beginTransaction();
//delete the primary data
$primary_key_name = $this->singular($this->name)."_uuid";
$sql = "DELETE FROM v_".$this->name." ";
$sql .= "WHERE ".$this->singular($this->name)."_uuid = '".$uuid."' ";
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$this->db->query(check_sql($sql));
$message["message"] = "OK";
$message["code"] = "200";
$message["details"][$m]["name"] = $this->name;
$message["details"][$m]["message"] = "OK";
$message["details"][$m]["code"] = "200";
if ($this->debug["sql"]) {
$message["details"][$m]["sql"] = $sql;
}
$this->message = $message;
$m++;
}
catch(PDOException $e) {
$message["message"] = "Bad Request";
$message["code"] = "400";
$message["details"][$m]["name"] = $this->name;
$message["details"][$m]["message"] = $e->getMessage();
$message["details"][$m]["code"] = "400";
if ($this->debug["sql"]) {
$message["details"][$m]["sql"] = $sql;
}
$this->message = $message;
$m++;
}
//delete the related data
$relations = $this->get_relations($this->name);
foreach ($relations as &$row) {
$schema_name = $row['table'];
if (substr($schema_name, 0,2) == "v_") {
$schema_name = substr($schema_name, 2);
}
if (permission_exists($this->singular($schema_name).'_delete')) {
$sql = "DELETE FROM ".$row['table']." ";
$sql .= "WHERE ".$row['key']['field']." = '".$uuid."' ";
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$this->db->query(check_sql($sql));
$message["details"][$m]["name"] = $schema_name;
$message["details"][$m]["message"] = "OK";
$message["details"][$m]["code"] = "200";
if ($this->debug["sql"]) {
$message["details"][$m]["sql"] = $sql;
}
$this->message = $message;
$m++;
}
catch(PDOException $e) {
if ($message["code"] = "200") {
$message["message"] = "Bad Request";
$message["code"] = "400";
}
$message["details"][$m]["name"] = $schema_name;
$message["details"][$m]["message"] = $e->getMessage();
$message["details"][$m]["code"] = "400";
if ($this->debug["sql"]) {
$message["details"][$m]["sql"] = $sql;
}
$this->message = $message;
$m++;
}
unset ($sql);
}
}
//commit the atomic transaction
if ($message["code"] == "200") {
$this->db->commit();
}
}
}
else {
$message["name"] = $this->name;
$message["message"] = "Forbidden";
$message["code"] = "403";
$message["line"] = __line__;
$this->message = $message;
$m++;
}
}
private function normalize_array($array, $name) { private function normalize_array($array, $name) {
//get the depth of the array //get the depth of the array
$depth = $this->array_depth($array); $depth = $this->array_depth($array);