Update orm.php
Updated code to be more robust and handle more situations such as primary key and child key in the array but doesn't exist in the database.
This commit is contained in:
parent
185417c45a
commit
d190f6a30d
|
|
@ -260,82 +260,60 @@
|
||||||
//debug sql
|
//debug sql
|
||||||
$this->debug["sql"] = true;
|
$this->debug["sql"] = true;
|
||||||
|
|
||||||
|
//start the atomic transaction
|
||||||
|
$this->db->beginTransaction();
|
||||||
|
|
||||||
//set the variables
|
//set the variables
|
||||||
$table_name = "v_".$this->name;
|
$table_name = "v_".$this->name;
|
||||||
$parent_key_name = $this->singular($this->name)."_uuid";
|
$parent_key_name = $this->singular($this->name)."_uuid";
|
||||||
|
|
||||||
//set the uuid
|
//if the uuid is set then set parent key exists and value
|
||||||
if (isset($array[$parent_key_name])) {
|
if (isset($this->uuid)) {
|
||||||
$this->uuid = $array[$parent_key_name];
|
$parent_key_exists = true;
|
||||||
|
$parent_key_value = $this->uuid;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
//determine if the parent_key_exists
|
||||||
|
$parent_key_exists = false;
|
||||||
|
if (isset($array[$parent_key_name])) {
|
||||||
|
$this->uuid = $array[$parent_key_name];
|
||||||
|
$parent_key_value = $this->uuid;
|
||||||
|
$parent_key_exists = true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$parent_key_value = uuid();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//determine action update or delete
|
//determine action update or delete and get the original data
|
||||||
if (isset($this->uuid)) {
|
if ($parent_key_exists) {
|
||||||
$sql = "SELECT count(*) AS num_rows 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) {
|
||||||
$prep_statement->execute();
|
//get the data
|
||||||
$row = $prep_statement->fetch(PDO::FETCH_ASSOC);
|
$prep_statement->execute();
|
||||||
if ($row['num_rows'] > 0) {
|
$parent_array = $prep_statement->fetch(PDO::FETCH_ASSOC);
|
||||||
$action = "update";
|
//set the action
|
||||||
}
|
if (is_array($parent_array)) {
|
||||||
else {
|
$action = "update";
|
||||||
//set the action
|
}
|
||||||
$action = "add";
|
else {
|
||||||
|
$action = "add";
|
||||||
//unset the primary key if it doesn't exist
|
|
||||||
unset($array[$parent_key_name]);
|
|
||||||
unset($this->uuid);
|
|
||||||
|
|
||||||
//remove parent and child keys from the data if the parent key doesn't exist
|
|
||||||
foreach ($array as $key => $value) {
|
|
||||||
if (is_array($value)) {
|
|
||||||
$child_key_name = $this->singular($key)."_uuid";
|
|
||||||
$i = 0;
|
|
||||||
foreach ($value as $row) {
|
|
||||||
foreach ($row as $k => $v) {
|
|
||||||
if ($k == $parent_key_name) {
|
|
||||||
unset($array[$key][$i][$parent_key_name]);
|
|
||||||
}
|
|
||||||
if ($k == $child_key_name) {
|
|
||||||
unset($array[$key][$i][$child_key_name]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
unset($prep_statement);
|
unset($prep_statement);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$action = "add";
|
$action = "add";
|
||||||
}
|
}
|
||||||
|
//echo "action: ".$action."\n";
|
||||||
|
|
||||||
//add a record
|
//add a record
|
||||||
//set the message index
|
|
||||||
$m = 0;
|
$m = 0;
|
||||||
if ($action == "add") {
|
if ($action == "add") {
|
||||||
if (permission_exists($this->singular($this->name).'_add')) {
|
if (permission_exists($this->singular($this->name).'_add')) {
|
||||||
//start the atomic transaction
|
|
||||||
$this->db->beginTransaction();
|
|
||||||
|
|
||||||
//determine if child or parent key exists
|
|
||||||
$parent_key_exists = false;
|
|
||||||
foreach ($array as $k => $v) {
|
|
||||||
if ($k == $parent_key_name) { $parent_key_exists = true; }
|
|
||||||
}
|
|
||||||
|
|
||||||
//parent data
|
|
||||||
if (isset($this->uuid)) {
|
|
||||||
$parent_key_value = $this->uuid;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$parent_key_value = uuid();
|
|
||||||
}
|
|
||||||
$sql = "INSERT INTO v_".$this->name." ";
|
$sql = "INSERT INTO v_".$this->name." ";
|
||||||
$sql .= "(";
|
$sql .= "(";
|
||||||
if (!$parent_key_exists) {
|
if (!$parent_key_exists) {
|
||||||
|
|
@ -367,7 +345,6 @@
|
||||||
$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));
|
||||||
|
|
||||||
$message["message"] = "OK";
|
$message["message"] = "OK";
|
||||||
$message["code"] = "200";
|
$message["code"] = "200";
|
||||||
$message["uuid"] = $parent_key_value;
|
$message["uuid"] = $parent_key_value;
|
||||||
|
|
@ -394,112 +371,6 @@
|
||||||
$m++;
|
$m++;
|
||||||
}
|
}
|
||||||
unset($sql);
|
unset($sql);
|
||||||
|
|
||||||
//child data
|
|
||||||
foreach ($array as $key => $value) {
|
|
||||||
if (is_array($value)) {
|
|
||||||
if (permission_exists($this->singular($key).'_add')) {
|
|
||||||
$table_name = "v_".$key;
|
|
||||||
foreach ($value as $row) {
|
|
||||||
//prepare the variables
|
|
||||||
$child_key_name = $this->singular($key)."_uuid";
|
|
||||||
//uuid_exists true / false
|
|
||||||
$uuid_exists = false;
|
|
||||||
$child_key_value = uuid();
|
|
||||||
foreach ($row as $k => $v) {
|
|
||||||
if ($child_key_name == $k) {
|
|
||||||
if (strlen($v) > 0) {
|
|
||||||
$child_key_value = $v;
|
|
||||||
$uuid_exists = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$uuid_exists = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//determine if child or parent key exists
|
|
||||||
$parent_key_exists = false;
|
|
||||||
$child_key_exists = false;
|
|
||||||
foreach ($row as $k => $v) {
|
|
||||||
if ($k == $parent_key_name) { $parent_key_exists = true; }
|
|
||||||
if ($k == $child_key_name) { $child_key_exists = true; }
|
|
||||||
}
|
|
||||||
//add the data
|
|
||||||
$sql = "INSERT INTO ".$table_name." ";
|
|
||||||
$sql .= "(";
|
|
||||||
if (!$parent_key_exists) {
|
|
||||||
$sql .= $parent_key_name.", ";
|
|
||||||
}
|
|
||||||
if (!$child_key_exists) {
|
|
||||||
$sql .= $child_key_name.", ";
|
|
||||||
}
|
|
||||||
foreach ($row as $k => $v) {
|
|
||||||
if (!is_array($v)) {
|
|
||||||
//if ($k != $child_key_name) {
|
|
||||||
$sql .= check_str($k).", ";
|
|
||||||
//}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$sql .= ") ";
|
|
||||||
$sql .= "VALUES ";
|
|
||||||
$sql .= "(";
|
|
||||||
if (!$parent_key_exists) {
|
|
||||||
$sql .= "'".$parent_key_value."', ";
|
|
||||||
}
|
|
||||||
if (!$child_key_exists) {
|
|
||||||
$sql .= "'".$child_key_value."', ";
|
|
||||||
}
|
|
||||||
foreach ($row as $k => $v) {
|
|
||||||
if (!is_array($v)) {
|
|
||||||
//if ($k != $child_key_name) {
|
|
||||||
if (strlen($v) == 0) {
|
|
||||||
$sql .= "null, ";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$sql .= "'".check_str($v)."', ";
|
|
||||||
}
|
|
||||||
//}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$sql .= ");";
|
|
||||||
$sql = str_replace(", )", ")", $sql);
|
|
||||||
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
||||||
try {
|
|
||||||
$this->db->query(check_sql($sql));
|
|
||||||
$message["details"][$m]["name"] = $key;
|
|
||||||
$message["details"][$m]["message"] = "OK";
|
|
||||||
$message["details"][$m]["code"] = "200";
|
|
||||||
$message["details"][$m]["uuid"] = $child_key_value;
|
|
||||||
if ($this->debug["sql"]) {
|
|
||||||
$message["details"][$m]["sql"] = $sql;
|
|
||||||
}
|
|
||||||
$this->message = $message;
|
|
||||||
$m++;
|
|
||||||
unset($sql);
|
|
||||||
}
|
|
||||||
catch(PDOException $e) {
|
|
||||||
if ($message["code"] = "200") {
|
|
||||||
$message["message"] = "Bad Request";
|
|
||||||
$message["code"] = "400";
|
|
||||||
}
|
|
||||||
$message["details"][$m]["name"] = $key;
|
|
||||||
$message["details"][$m]["message"] = $e->getMessage();
|
|
||||||
$message["details"][$m]["code"] = "400";
|
|
||||||
if ($this->debug["sql"]) {
|
|
||||||
$message["details"][$m]["sql"] = $sql;
|
|
||||||
}
|
|
||||||
$this->message = $message;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//commit the atomic transaction
|
|
||||||
if ($message["code"] == "200") {
|
|
||||||
$this->db->commit();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$message["name"] = $this->name;
|
$message["name"] = $this->name;
|
||||||
|
|
@ -514,11 +385,7 @@
|
||||||
if ($action == "update") {
|
if ($action == "update") {
|
||||||
if (permission_exists($this->singular($this->name).'_edit')) {
|
if (permission_exists($this->singular($this->name).'_edit')) {
|
||||||
|
|
||||||
//start the atomic transaction
|
|
||||||
$this->db->beginTransaction();
|
|
||||||
|
|
||||||
//parent data
|
//parent data
|
||||||
$parent_key_value = $this->uuid;
|
|
||||||
$sql = "UPDATE v_".$this->name." SET ";
|
$sql = "UPDATE v_".$this->name." SET ";
|
||||||
foreach ($array as $key => $value) {
|
foreach ($array as $key => $value) {
|
||||||
if (!is_array($value) && $key != $parent_key_name) {
|
if (!is_array($value) && $key != $parent_key_name) {
|
||||||
|
|
@ -561,183 +428,6 @@
|
||||||
$this->message = $message;
|
$this->message = $message;
|
||||||
$m++;
|
$m++;
|
||||||
}
|
}
|
||||||
|
|
||||||
//child data
|
|
||||||
foreach ($array as $key => $value) {
|
|
||||||
if (is_array($value)) {
|
|
||||||
$table_name = "v_".$key;
|
|
||||||
foreach ($value as $row) {
|
|
||||||
//prepare the variables
|
|
||||||
$child_name = $this->singular($key);
|
|
||||||
$child_key_name = $child_name."_uuid";
|
|
||||||
|
|
||||||
//determine if the parent key exists
|
|
||||||
$parent_key_exists = false;
|
|
||||||
if (!isset($array[$parent_key_name])) {
|
|
||||||
$parent_key_exists = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
//determine if the uuid exists
|
|
||||||
$uuid_exists = false;
|
|
||||||
$child_key_value = uuid();
|
|
||||||
foreach ($row as $k => $v) {
|
|
||||||
if ($child_key_name == $k) {
|
|
||||||
if (strlen($v) > 0) {
|
|
||||||
$child_key_value = $v;
|
|
||||||
$uuid_exists = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$uuid_exists = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//update the data
|
|
||||||
if ($uuid_exists) {
|
|
||||||
|
|
||||||
$sql = "UPDATE ".$table_name." SET ";
|
|
||||||
foreach ($row as $k => $v) {
|
|
||||||
//if (!is_array($v) && $k != $child_key_name) { //original
|
|
||||||
if (!is_array($v) && ($k != $parent_key_name || $k != $child_key_name)) {
|
|
||||||
if (strlen($v) == 0) {
|
|
||||||
$sql .= check_str($k)." = null, ";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$sql .= check_str($k)." = '".check_str($v)."', ";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$sql .= "WHERE ".$parent_key_name." = '".$this->uuid."' ";
|
|
||||||
$sql .= "AND ".$child_key_name." = '".$child_key_value."' ";
|
|
||||||
$sql = str_replace(", WHERE", " WHERE", $sql);
|
|
||||||
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
||||||
try {
|
|
||||||
$this->db->query(check_sql($sql));
|
|
||||||
$message["details"][$m]["name"] = $key;
|
|
||||||
$message["details"][$m]["message"] = "OK";
|
|
||||||
$message["details"][$m]["code"] = "200";
|
|
||||||
$message["details"][$m]["uuid"] = $child_key_value;
|
|
||||||
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"] = $key;
|
|
||||||
$message["details"][$m]["message"] = $e->getMessage();
|
|
||||||
$message["details"][$m]["code"] = "400";
|
|
||||||
if ($this->debug["sql"]) {
|
|
||||||
$message["details"][$m]["sql"] = $sql;
|
|
||||||
}
|
|
||||||
$this->message = $message;
|
|
||||||
$m++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//add the data
|
|
||||||
if (!$uuid_exists) {
|
|
||||||
if (permission_exists($child_name.'_add')) {
|
|
||||||
//determine if child or parent key exists
|
|
||||||
$child_key_name = $this->singular($child_name).'_uuid';
|
|
||||||
$parent_key_exists = false;
|
|
||||||
$child_key_exists = false;
|
|
||||||
foreach ($row as $k => $v) {
|
|
||||||
if ($k == $parent_key_name) {
|
|
||||||
$parent_key_exists = true;
|
|
||||||
$parent_key_value = $v;
|
|
||||||
}
|
|
||||||
if ($k == $child_key_name) {
|
|
||||||
$child_key_exists = true;
|
|
||||||
$child_key_value = $v;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!$parent_key_name) {
|
|
||||||
$parent_key_value = uuid();
|
|
||||||
}
|
|
||||||
if (!$child_key_name) {
|
|
||||||
$child_key_value = uuid();
|
|
||||||
}
|
|
||||||
//build the insert
|
|
||||||
$sql = "INSERT INTO ".$table_name." ";
|
|
||||||
$sql .= "(";
|
|
||||||
if (!$parent_key_exists) {
|
|
||||||
$sql .= $this->singular($parent_key_name).", ";
|
|
||||||
}
|
|
||||||
if (!$child_key_exists) {
|
|
||||||
$sql .= $this->singular($child_key_name).", ";
|
|
||||||
}
|
|
||||||
foreach ($row as $k => $v) {
|
|
||||||
if (!is_array($v)) {
|
|
||||||
$sql .= check_str($k).", ";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$sql .= ") ";
|
|
||||||
$sql .= "VALUES ";
|
|
||||||
$sql .= "(";
|
|
||||||
if (!$parent_key_exists) {
|
|
||||||
$sql .= "'".$parent_key_value."', ";
|
|
||||||
}
|
|
||||||
if (!$child_key_exists) {
|
|
||||||
$sql .= "'".$child_key_value."', ";
|
|
||||||
}
|
|
||||||
foreach ($row as $k => $v) {
|
|
||||||
if (!is_array($v)) {
|
|
||||||
if (strlen($v) == 0) {
|
|
||||||
$sql .= "null, ";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$sql .= "'".check_str($v)."', ";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$sql .= ");";
|
|
||||||
$sql = str_replace(", )", ")", $sql);
|
|
||||||
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
||||||
try {
|
|
||||||
$this->db->query(check_sql($sql));
|
|
||||||
$message["details"][$m]["name"] = $key;
|
|
||||||
$message["details"][$m]["message"] = "OK";
|
|
||||||
$message["details"][$m]["code"] = "200";
|
|
||||||
$message["details"][$m]["uuid"] = $child_key_value;
|
|
||||||
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"] = $key;
|
|
||||||
$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 the sql variable
|
|
||||||
unset($sql);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//commit the atomic transaction
|
|
||||||
if ($message["code"] == "200") {
|
|
||||||
$this->db->commit();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$message["name"] = $this->name;
|
$message["name"] = $this->name;
|
||||||
|
|
@ -747,7 +437,232 @@
|
||||||
$m++;
|
$m++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
//unset the variables
|
||||||
|
unset($sql, $action);
|
||||||
|
|
||||||
|
//child data
|
||||||
|
foreach ($array as $key => $value) {
|
||||||
|
if (is_array($value)) {
|
||||||
|
$table_name = "v_".$key;
|
||||||
|
foreach ($value as $row) {
|
||||||
|
//prepare the variables
|
||||||
|
$child_name = $this->singular($key);
|
||||||
|
$child_key_name = $child_name."_uuid";
|
||||||
|
|
||||||
|
//determine if the parent key exists in the child array
|
||||||
|
$parent_key_exists = false;
|
||||||
|
if (!isset($array[$parent_key_name])) {
|
||||||
|
$parent_key_exists = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//determine if the uuid exists
|
||||||
|
$uuid_exists = false;
|
||||||
|
foreach ($row as $k => $v) {
|
||||||
|
if ($child_key_name == $k) {
|
||||||
|
if (strlen($v) > 0) {
|
||||||
|
$child_key_value = $v;
|
||||||
|
$uuid_exists = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$uuid_exists = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//determine sql update or delete and get the original data
|
||||||
|
if ($uuid_exists) {
|
||||||
|
$sql = "SELECT * FROM ".$table_name." ";
|
||||||
|
$sql .= "WHERE ".$child_key_name." = '".$child_key_value."' ";
|
||||||
|
$prep_statement = $this->db->prepare($sql);
|
||||||
|
if ($prep_statement) {
|
||||||
|
//get the data
|
||||||
|
$prep_statement->execute();
|
||||||
|
$child_array = $prep_statement->fetch(PDO::FETCH_ASSOC);
|
||||||
|
//set the action
|
||||||
|
if (is_array($child_array)) {
|
||||||
|
$action = "update";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$action = "add";
|
||||||
|
}
|
||||||
|
//add to the parent array
|
||||||
|
if (is_array($child_array)) {
|
||||||
|
$parent_array[][$child_name] = $child_array;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unset($prep_statement);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$action = "add";
|
||||||
|
}
|
||||||
|
|
||||||
|
//update the data
|
||||||
|
if ($action == "update") {
|
||||||
|
if (permission_exists($child_name.'_edit')) {
|
||||||
|
$sql = "UPDATE ".$table_name." SET ";
|
||||||
|
foreach ($row as $k => $v) {
|
||||||
|
//if (!is_array($v) && $k != $child_key_name) { //original
|
||||||
|
if (!is_array($v) && ($k != $parent_key_name || $k != $child_key_name)) {
|
||||||
|
if (strlen($v) == 0) {
|
||||||
|
$sql .= check_str($k)." = null, ";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$sql .= check_str($k)." = '".check_str($v)."', ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$sql .= "WHERE ".$parent_key_name." = '".$this->uuid."' ";
|
||||||
|
$sql .= "AND ".$child_key_name." = '".$child_key_value."' ";
|
||||||
|
$sql = str_replace(", WHERE", " WHERE", $sql);
|
||||||
|
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
|
try {
|
||||||
|
$this->db->query(check_sql($sql));
|
||||||
|
$message["details"][$m]["name"] = $key;
|
||||||
|
$message["details"][$m]["message"] = "OK";
|
||||||
|
$message["details"][$m]["code"] = "200";
|
||||||
|
$message["details"][$m]["uuid"] = $child_key_value;
|
||||||
|
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"] = $key;
|
||||||
|
$message["details"][$m]["message"] = $e->getMessage();
|
||||||
|
$message["details"][$m]["code"] = "400";
|
||||||
|
if ($this->debug["sql"]) {
|
||||||
|
$message["details"][$m]["sql"] = $sql;
|
||||||
|
}
|
||||||
|
$this->message = $message;
|
||||||
|
$m++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$message["name"] = $child_name;
|
||||||
|
$message["message"] = "Forbidden";
|
||||||
|
$message["code"] = "403";
|
||||||
|
$this->message = $message;
|
||||||
|
$m++;
|
||||||
|
}
|
||||||
|
} //action update
|
||||||
|
|
||||||
|
//add the data
|
||||||
|
if ($action == "add") {
|
||||||
|
if (permission_exists($child_name.'_add')) {
|
||||||
|
//determine if child or parent key exists
|
||||||
|
$child_key_name = $this->singular($child_name).'_uuid';
|
||||||
|
$parent_key_exists = false;
|
||||||
|
$child_key_exists = false;
|
||||||
|
foreach ($row as $k => $v) {
|
||||||
|
if ($k == $parent_key_name) {
|
||||||
|
$parent_key_exists = true;
|
||||||
|
}
|
||||||
|
if ($k == $child_key_name) {
|
||||||
|
$child_key_exists = true;
|
||||||
|
$child_key_value = $v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!$child_key_value) {
|
||||||
|
$child_key_value = uuid();
|
||||||
|
}
|
||||||
|
//build the insert
|
||||||
|
$sql = "INSERT INTO ".$table_name." ";
|
||||||
|
$sql .= "(";
|
||||||
|
if (!$parent_key_exists) {
|
||||||
|
$sql .= $this->singular($parent_key_name).", ";
|
||||||
|
}
|
||||||
|
if (!$child_key_exists) {
|
||||||
|
$sql .= $this->singular($child_key_name).", ";
|
||||||
|
}
|
||||||
|
foreach ($row as $k => $v) {
|
||||||
|
if (!is_array($v)) {
|
||||||
|
$sql .= check_str($k).", ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$sql .= ") ";
|
||||||
|
$sql .= "VALUES ";
|
||||||
|
$sql .= "(";
|
||||||
|
if (!$parent_key_exists) {
|
||||||
|
$sql .= "'".$parent_key_value."', ";
|
||||||
|
}
|
||||||
|
if (!$child_key_exists) {
|
||||||
|
$sql .= "'".$child_key_value."', ";
|
||||||
|
}
|
||||||
|
foreach ($row as $k => $v) {
|
||||||
|
if (!is_array($v)) {
|
||||||
|
if (strlen($v) == 0) {
|
||||||
|
$sql .= "null, ";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$sql .= "'".check_str($v)."', ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$sql .= ");";
|
||||||
|
$sql = str_replace(", )", ")", $sql);
|
||||||
|
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
|
try {
|
||||||
|
$this->db->query(check_sql($sql));
|
||||||
|
$message["details"][$m]["name"] = $key;
|
||||||
|
$message["details"][$m]["message"] = "OK";
|
||||||
|
$message["details"][$m]["code"] = "200";
|
||||||
|
$message["details"][$m]["uuid"] = $child_key_value;
|
||||||
|
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"] = $key;
|
||||||
|
$message["details"][$m]["message"] = $e->getMessage();
|
||||||
|
$message["details"][$m]["code"] = "400";
|
||||||
|
if ($this->debug["sql"]) {
|
||||||
|
$message["details"][$m]["sql"] = $sql;
|
||||||
|
}
|
||||||
|
$this->message = $message;
|
||||||
|
$m++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$message["name"] = $child_name;
|
||||||
|
$message["message"] = "Forbidden";
|
||||||
|
$message["code"] = "403";
|
||||||
|
$this->message = $message;
|
||||||
|
$m++;
|
||||||
|
}
|
||||||
|
|
||||||
|
} //action add
|
||||||
|
|
||||||
|
//unset the variables
|
||||||
|
unset($sql, $action, $child_key_name, $child_key_value);
|
||||||
|
} // foreach value
|
||||||
|
} //is array
|
||||||
|
} //foreach array
|
||||||
|
|
||||||
|
//return the before and after data
|
||||||
|
//log this in the future
|
||||||
|
//if (is_array($parent_array)) {
|
||||||
|
// $message["old"] = $parent_array;
|
||||||
|
//}
|
||||||
|
//$message["new"] = $array;
|
||||||
|
//$this->message = $message;
|
||||||
|
|
||||||
|
//commit the atomic transaction
|
||||||
|
$this->db->commit();
|
||||||
|
|
||||||
|
} //save method
|
||||||
|
|
||||||
//define singular function to convert a word in english to singular
|
//define singular function to convert a word in english to singular
|
||||||
private function singular($word) {
|
private function singular($word) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue