From d190f6a30d8a9955e96cbbf2f154b7b4a568d97f Mon Sep 17 00:00:00 2001 From: FusionPBX Date: Sun, 18 Sep 2016 15:27:40 -0600 Subject: [PATCH] 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. --- resources/classes/orm.php | 601 ++++++++++++++++---------------------- 1 file changed, 258 insertions(+), 343 deletions(-) diff --git a/resources/classes/orm.php b/resources/classes/orm.php index 801efc40bb..a327b26221 100644 --- a/resources/classes/orm.php +++ b/resources/classes/orm.php @@ -260,82 +260,60 @@ //debug sql $this->debug["sql"] = true; + //start the atomic transaction + $this->db->beginTransaction(); + //set the variables $table_name = "v_".$this->name; $parent_key_name = $this->singular($this->name)."_uuid"; - //set the uuid - if (isset($array[$parent_key_name])) { - $this->uuid = $array[$parent_key_name]; + //if the uuid is set then set parent key exists and value + if (isset($this->uuid)) { + $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 - if (isset($this->uuid)) { - $sql = "SELECT count(*) AS num_rows FROM ".$table_name." "; + //determine action update or delete and get the original data + if ($parent_key_exists) { + $sql = "SELECT * FROM ".$table_name." "; $sql .= "WHERE ".$parent_key_name." = '".$this->uuid."' "; $prep_statement = $this->db->prepare($sql); if ($prep_statement) { - $prep_statement->execute(); - $row = $prep_statement->fetch(PDO::FETCH_ASSOC); - if ($row['num_rows'] > 0) { - $action = "update"; - } - else { - //set the action - $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++; - } - } + //get the data + $prep_statement->execute(); + $parent_array = $prep_statement->fetch(PDO::FETCH_ASSOC); + //set the action + if (is_array($parent_array)) { + $action = "update"; + } + else { + $action = "add"; } - - } } unset($prep_statement); } else { $action = "add"; } + //echo "action: ".$action."\n"; //add a record - //set the message index $m = 0; if ($action == "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 .= "("; if (!$parent_key_exists) { @@ -367,7 +345,6 @@ $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); try { $this->db->query(check_sql($sql)); - $message["message"] = "OK"; $message["code"] = "200"; $message["uuid"] = $parent_key_value; @@ -394,112 +371,6 @@ $m++; } 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 { $message["name"] = $this->name; @@ -514,11 +385,7 @@ if ($action == "update") { if (permission_exists($this->singular($this->name).'_edit')) { - //start the atomic transaction - $this->db->beginTransaction(); - //parent data - $parent_key_value = $this->uuid; $sql = "UPDATE v_".$this->name." SET "; foreach ($array as $key => $value) { if (!is_array($value) && $key != $parent_key_name) { @@ -561,183 +428,6 @@ $this->message = $message; $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 { $message["name"] = $this->name; @@ -747,7 +437,232 @@ $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 private function singular($word) {