Add a new object relational model class and make the dialplan edit use for add and edit.

This commit is contained in:
Mark Crane
2014-01-19 13:09:17 +00:00
parent 1316745483
commit be442661c9
3 changed files with 861 additions and 182 deletions

View File

@@ -81,7 +81,6 @@
$dialplan_order = 0;
}
//dialplan class
require_once "app/dialplan/resources/classes/dialplan.php";
$dialplan = new dialplan;
$dialplan->domain_uuid = $domain_uuid;
$dialplan->dialplan_order = $dialplan_order;
@@ -93,4 +92,45 @@
$dialplan->import();
}
//add the global dialplan to inbound routes
if ($domains_processed == 1) {
$sql = "select count(*) as num_rows from v_dialplans ";
$sql .= "where dialplan_uuid = 'd4e06654-e394-444a-b3af-4c3d54aebbec' ";
$prep_statement = $db->prepare(check_sql($sql));
if ($prep_statement) {
$prep_statement->execute();
$row = $prep_statement->fetch(PDO::FETCH_ASSOC);
if ($row['num_rows'] == 0) {
//create the dialplan array
$array["app_uuid"] = "c03b422e-13a8-bd1b-e42b-b6b9b4d27ce4";
$array["dialplan_context"] = "public";
$array["dialplan_name"] = "global";
$array["dialplan_continue"] = "true";
$array["dialplan_order"] = "0";
$array["dialplan_enabled"] = "true";
$y = 0;
$array["dialplan_details"][$y]["dialplan_detail_uuid"] = "5e1062d8-6842-4890-a78a-388e8dd5bbaf";
$array["dialplan_details"][$y]["dialplan_detail_tag"] = "condition";
$array["dialplan_details"][$y]["dialplan_detail_type"] = "context";
$array["dialplan_details"][$y]["dialplan_detail_data"] = "public";
$array["dialplan_details"][$y]["dialplan_detail_order"] = "10";
$y++;
$array["dialplan_details"][$y]["dialplan_detail_uuid"] = "bdafd4aa-6633-48fc-970e-bc2778f3f022";
$array["dialplan_details"][$y]["dialplan_detail_tag"] = "action";
$array["dialplan_details"][$y]["dialplan_detail_type"] = "lua";
$array["dialplan_details"][$y]["dialplan_detail_data"] = "app.lua dialplan";
$array["dialplan_details"][$y]["dialplan_detail_order"] = "20";
//save the dialplan with a specific uuid
$orm = new orm;
$orm->domain_uuid = $domain_uuid;
$orm->uuid('d4e06654-e394-444a-b3af-4c3d54aebbec');
$orm->name('dialplans');
$orm->save($array);
//$message = $orm->message;
unset($array);
}
}
}
?>

View File

@@ -103,192 +103,63 @@ if (count($_POST) > 0 && strlen($_POST["persistformvar"]) == 0) {
}
//remove the invalid characters from the extension name
$dialplan_name = str_replace(" ", "_", $dialplan_name);
$dialplan_name = str_replace("/", "", $dialplan_name);
foreach ($_POST as $key => $value) {
if ($key == "dialplan_name") {
$dialplan_name = str_replace(" ", "_", $value);
$dialplan_name = str_replace("/", "", $dialplan_name);
$_POST["dialplan_name"] = $dialplan_name;
}
}
//array cleanup
$x = 0;
foreach ($_POST["dialplan_details"] as $row) {
//unset the empty row
if (strlen($row["dialplan_detail_tag"]) == 0) {
unset($_POST["dialplan_details"][$x]);
}
//unset dialplan_detail_uuid if the field has no value
if (strlen($row["dialplan_detail_uuid"]) == 0) {
unset($_POST["dialplan_details"][$x]["dialplan_detail_uuid"]);
}
//increment the row
$x++;
}
//add or update the database
if ($_POST["persistformvar"] != "true") {
//add the data into the database
if ($action == "add" && permission_exists('dialplan_add')) {
$dialplan_context = $_SESSION['context'];
$dialplan_uuid = uuid();
$sql = "insert into v_dialplans ";
$sql .= "(";
$sql .= "domain_uuid, ";
$sql .= "dialplan_uuid, ";
$sql .= "app_uuid, ";
$sql .= "dialplan_name, ";
$sql .= "dialplan_number, ";
$sql .= "dialplan_order, ";
$sql .= "dialplan_continue, ";
$sql .= "dialplan_context, ";
$sql .= "dialplan_enabled, ";
$sql .= "dialplan_description ";
$sql .= ")";
$sql .= "values ";
$sql .= "(";
$sql .= "'".$_SESSION['domain_uuid']."', ";
$sql .= "'$dialplan_uuid', ";
$sql .= "'742714e5-8cdf-32fd-462c-cbe7e3d655db', ";
$sql .= "'$dialplan_name', ";
$sql .= "'$dialplan_number', ";
$sql .= "'$dialplan_order', ";
$sql .= "'$dialplan_continue', ";
$sql .= "'$dialplan_context', ";
$sql .= "'$dialplan_enabled', ";
$sql .= "'$dialplan_description' ";
$sql .= ")";
$db->exec(check_sql($sql));
unset($sql);
}
$orm = new orm;
$orm->name('dialplans');
$orm->uuid($dialplan_uuid);
$orm->save($_POST);
//$message = $orm->message;
}
//update the dialplan
if ($action == "update" && permission_exists('dialplan_edit')) {
$sql = "update v_dialplans set ";
$sql .= "dialplan_name = '$dialplan_name', ";
$sql .= "dialplan_number = '$dialplan_number', ";
$sql .= "dialplan_order = '$dialplan_order', ";
$sql .= "dialplan_continue = '$dialplan_continue', ";
$sql .= "dialplan_context = '$dialplan_context', ";
$sql .= "dialplan_enabled = '$dialplan_enabled', ";
$sql .= "dialplan_description = '$dialplan_description' ";
$sql .= "where domain_uuid = '".$_SESSION['domain_uuid']."' ";
$sql .= "and dialplan_uuid = '$dialplan_uuid'";
$db->exec(check_sql($sql));
unset($sql);
}
//delete the cache
$fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
if ($fp) {
$switch_cmd = "memcache delete dialplan:".$dialplan_context;
$switch_result = event_socket_request($fp, 'api '.$switch_cmd);
}
//add or update the dialplan details
foreach ($dialplan_details as $row) {
//set the variables
$dialplan_detail_uuid = check_str($row["dialplan_detail_uuid"]);
$dialplan_detail_tag = check_str($row["dialplan_detail_tag"]);
$dialplan_detail_order = check_str($row["dialplan_detail_order"]);
$dialplan_detail_type = check_str($row["dialplan_detail_type"]);
$dialplan_detail_data = check_str($row["dialplan_detail_data"]);
$dialplan_detail_break = check_str($row["dialplan_detail_break"]);
$dialplan_detail_inline = check_str($row["dialplan_detail_inline"]);
$dialplan_detail_group = check_str($row["dialplan_detail_group"]);
//synchronize the xml config
save_dialplan_xml();
//add the details
if (strlen($dialplan_detail_uuid) == 0 && permission_exists('dialplan_detail_add')) {
$dialplan_detail_uuid = uuid();
$sql = "insert into v_dialplan_details ";
$sql .= "(";
$sql .= "dialplan_uuid, ";
$sql .= "dialplan_detail_uuid, ";
$sql .= "dialplan_detail_tag, ";
$sql .= "dialplan_detail_order, ";
$sql .= "dialplan_detail_type, ";
$sql .= "dialplan_detail_data, ";
$sql .= "dialplan_detail_break, ";
$sql .= "dialplan_detail_inline, ";
$sql .= "dialplan_detail_group, ";
$sql .= "domain_uuid ";
$sql .= ")";
$sql .= "values ";
$sql .= "(";
$sql .= "'$dialplan_uuid', ";
$sql .= "'$dialplan_detail_uuid', ";
$sql .= "'$dialplan_detail_tag', ";
$sql .= "'$dialplan_detail_order', ";
$sql .= "'$dialplan_detail_type', ";
$sql .= "'$dialplan_detail_data', ";
$sql .= "'$dialplan_detail_break', ";
$sql .= "'$dialplan_detail_inline', ";
if (strlen($dialplan_detail_group) == 0) {
$sql .= "null, ";
}
else {
$sql .= "'$dialplan_detail_group', ";
}
$sql .= "'".$_SESSION['domain_uuid']."' ";
$sql .= ")";
$db->exec(check_sql($sql));
unset($sql);
}
//set the message
if ($action == "add") {
$_SESSION['message'] = $text['message-add'];
}
if ($action == "update") {
$_SESSION['message'] = $text['message-update'];
}
//update the details
if (strlen($dialplan_detail_uuid) > 0 && permission_exists('dialplan_detail_edit')) {
$sql = "update v_dialplan_details set ";
$sql .= "dialplan_uuid = '".$dialplan_uuid."', ";
$sql .= "dialplan_detail_tag = '".$dialplan_detail_tag."', ";
$sql .= "dialplan_detail_order = '".$dialplan_detail_order."', ";
$sql .= "dialplan_detail_type = '".$dialplan_detail_type."', ";
$sql .= "dialplan_detail_data = '".$dialplan_detail_data."', ";
$sql .= "dialplan_detail_break = '".$dialplan_detail_break."', ";
$sql .= "dialplan_detail_inline = '".$dialplan_detail_inline."', ";
if (strlen($dialplan_detail_group) == 0) {
$sql .= "dialplan_detail_group = null ";
}
else {
$sql .= "dialplan_detail_group = '".$dialplan_detail_group."' ";
}
$sql .= "where dialplan_detail_uuid = '".$dialplan_detail_uuid."'";
$sql .= "and domain_uuid = '".$_SESSION['domain_uuid']."' ";
$db->exec(check_sql($sql));
unset($sql);
}
}
//delete the dialplan context from memcache
$fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
if ($fp) {
$switch_cmd = "memcache delete dialplan:".$dialplan_context;
$switch_result = event_socket_request($fp, 'api '.$switch_cmd);
}
//synchronize the xml config
save_dialplan_xml();
//redirect the user
/*
require_once "resources/header.php";
switch ($app_uuid) {
case "c03b422e-13a8-bd1b-e42b-b6b9b4d27ce4":
//inbound routes
echo "<meta http-equiv=\"refresh\" content=\"0;url=".PROJECT_PATH."/app/dialplan/dialplan_edit.php?id=".$dialplan_uuid."&app_uuid=$app_uuid\">\n";
break;
case "8c914ec3-9fc0-8ab5-4cda-6c9288bdc9a3":
//outbound routes
echo "<meta http-equiv=\"refresh\" content=\"0;url=".PROJECT_PATH."/app/dialplan/dialplan_edit.php?id=".$dialplan_uuid."&app_uuid=$app_uuid\">\n";
break;
case "4b821450-926b-175a-af93-a03c441818b1":
//time conditions
echo "<meta http-equiv=\"refresh\" content=\"0;url=".PROJECT_PATH."/app/dialplan/dialplan_edit.php?id=".$dialplan_uuid."&app_uuid=$app_uuid\">\n";
break;
default:
echo "<meta http-equiv=\"refresh\" content=\"0;url=".PROJECT_PATH."/app/dialplan/dialplan_edit.php?id=".$dialplan_uuid."\">\n";
break;
}
echo "<div align='center'>\n";
if ($action == "add") {
echo $text['message-add']."\n";
}
if ($action == "update") {
echo $text['message-update']."\n";
}
echo "</div>\n";
require_once "resources/footer.php";
return;
*/
//set the message
if ($action == "add") {
$_SESSION['message'] = $text['message-add'];
}
if ($action == "update") {
$_SESSION['message'] = $text['message-update'];
}
} //if ($_POST["persistformvar"] != "true")
} //(count($_POST)>0 && strlen($_POST["persistformvar"]) == 0)
//pre-populate the form
if (count($_GET)>0 && $_POST["persistformvar"] != "true") {
$dialplan_uuid = $_GET["id"];
$sql = "select * from v_dialplans ";
$sql .= "where domain_uuid = '".$_SESSION['domain_uuid']."' ";
$sql .= "and dialplan_uuid = '$dialplan_uuid' ";
// $sql .= "where domain_uuid = '".$_SESSION['domain_uuid']."' ";
$sql .= "where dialplan_uuid = '$dialplan_uuid' ";
$prep_statement = $db->prepare(check_sql($sql));
$prep_statement->execute();
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
@@ -307,8 +178,8 @@ if (count($_POST) > 0 && strlen($_POST["persistformvar"]) == 0) {
//get the dialplan details in an array
$sql = "select * from v_dialplan_details ";
$sql .= "where domain_uuid = '".$_SESSION['domain_uuid']."' ";
$sql .= "and dialplan_uuid = '$dialplan_uuid' ";
// $sql .= "where domain_uuid = '".$_SESSION['domain_uuid']."' ";
$sql .= "where dialplan_uuid = '$dialplan_uuid' ";
$sql .= "order by dialplan_detail_group asc, dialplan_detail_order asc";
$prep_statement = $db->prepare(check_sql($sql));
$prep_statement->execute();
@@ -372,7 +243,7 @@ if (count($_POST) > 0 && strlen($_POST["persistformvar"]) == 0) {
}
}
//increment the highest order by 5
$dialplan_detail_order = $dialplan_detail_order + 5;
$dialplan_detail_order = $dialplan_detail_order + 10;
//set the rest of the empty array
//$details[$group][$x]['domain_uuid'] = '';
//$details[$group][$x]['dialplan_uuid'] = '';
@@ -418,9 +289,7 @@ if (count($_POST) > 0 && strlen($_POST["persistformvar"]) == 0) {
obj[0].parentNode.removeChild(obj[1]);
obj[0].parentNode.removeChild(obj[2]);
}
</script>
<?php
//show the content
@@ -439,7 +308,7 @@ if (count($_POST) > 0 && strlen($_POST["persistformvar"]) == 0) {
echo" <span class=\"title\">".$text['title-dialplan_edit']."</span><br />\n";
echo " </td>\n";
echo " <td width='70%' align='right'>\n";
echo " <input type='submit' name='submit' class='btn' value='".$text['button-save']."'>\n";
echo " <input type='submit' class='btn' value='".$text['button-save']."'>\n";
echo " <input type='button' class='btn' name='' alt='".$text['button-copy']."' onclick=\"if (confirm('".$text['confirm-copy']."')){window.location='dialplan_copy.php?id=".$row['dialplan_uuid']."';}\" value='".$text['button-copy']."'>\n";
if (strlen($app_uuid) > 0) {
echo " <input type='button' class='btn' name='' alt='".$text['button-back']."' onclick=\"window.location='dialplans.php?app_uuid=$app_uuid'\" value='".$text['button-back']."'>\n";
@@ -629,7 +498,9 @@ if (count($_POST) > 0 && strlen($_POST["persistformvar"]) == 0) {
$element['visibility'] = "visibility:hidden;";
}
//add the primary key uuid
echo " <input name='dialplan_details[".$x."][dialplan_detail_uuid]' type='hidden' value=\"".$dialplan_detail_uuid."\">\n";
if (strlen($dialplan_detail_uuid) > 0) {
echo " <input name='dialplan_details[".$x."][dialplan_detail_uuid]' type='hidden' value=\"".$dialplan_detail_uuid."\">\n";
}
//tag
$selected = "selected=\"selected\" ";
if ($element['hidden']) { $element['width'] = '0'; } else { $element['width'] = '97'; }
@@ -892,7 +763,7 @@ if (count($_POST) > 0 && strlen($_POST["persistformvar"]) == 0) {
if ($action == "update") {
echo " <input type='hidden' name='dialplan_uuid' value='$dialplan_uuid'>\n";
}
echo " <input type='submit' name='submit' class='btn' value='".$text['button-save']."'>\n";
echo " <input type='submit' class='btn' value='".$text['button-save']."'>\n";
echo " </td>\n";
echo " </tr>";
echo "</table>";

768
resources/classes/orm.php Normal file
View File

@@ -0,0 +1,768 @@
<?php
/*
FusionPBX
Version: MPL 1.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is FusionPBX
The Initial Developer of the Original Code is
Mark J Crane <markjcrane@fusionpbx.com>
Copyright (C) 2014
All Rights Reserved.
Contributor(s):
Mark J Crane <markjcrane@fusionpbx.com>
*/
include "root.php";
//define the orm class
if (!class_exists('orm')) {
class orm extends database {
//factory - sets the model_name
//set - sets the array
public $name;
//get - get the results
public $result;
//find
public $uuid;
//public $name;
public $where;
public $limit;
public $offset;
//save
//public $uuid;
//public $name;
public $message;
public $debug;
//delete
//public $uuid;
//public $name;
//public $where;
//public $message;
public function factory($name) {
$this->name = $name;
return $this;
}
public function name($name) {
$this->name = $name;
return $this;
}
public function uuid($uuid) {
$this->uuid = $uuid;
return $this;
}
public function set($array) {
foreach ($array as $key => $value) {
//public $this->$$key = $value;
}
return $this;
}
public function get() {
return $this->result;
}
public function find($array) {
//connect to the database if needed
if (!$this->db) {
$this->connect();
}
//set the name
if (isset($array['name'])) {
$this->name = $array['name'];
}
//set the uuid
if (isset($array['uuid'])) {
$this->uuid = $array['uuid'];
}
//build the query
$sql = "SELECT * FROM v_".$this->name." ";
if (isset($this->uuid)) {
//get the specific uuid
$sql .= "WHERE ".$this->singular($this->name)."_uuid = '".$this->uuid."' ";
}
else {
//where
if (is_array($array['where'])) {
$i = 0;
foreach($array['where'] as $row) {
if ($i == 0) {
$sql .= "WHERE ".$row['name']." ".$row['operator']." '".$row['value']."' ";
}
else {
$sql .= "AND ".$row['name']." ".$row['operator']." '".$row['value']."' ";
}
$i++;
}
}
//order by
if (is_array($array['order_by'])) {
$sql .= "order by ".$array['order_by']." ";
}
//limit
if (isset($array['limit'])) {
$sql .= "LIMIT ".$array['limit']." ";
}
//offset
if (isset($array['offset'])) {
$sql .= "OFFSET ".$array['offset']." ";
}
}
//execute the query, and return the results
try {
$prep_statement = $this->db->prepare(check_sql($sql));
$prep_statement->execute();
$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;
$this->result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
$m++;
return $this;
}
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;
$this->result = '';
$m++;
return $this;
}
}
public function delete($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 {
$prep_statement = $this->db->prepare(check_sql($sql));
$prep_statement->execute();
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
$this->result = $result;
$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 {
$sub_prep_statement = $this->db->prepare($sql);
$sub_prep_statement->execute();
$sub_result = $sub_prep_statement->fetchAll(PDO::FETCH_NAMED);
unset ($sub_prep_statement, $sub_result);
$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";
$this->message = $message;
$m++;
}
}
public function save($array) {
//connect to the database if needed
if (!$this->db) {
$this->connect();
}
//debug sql
$this->debug["sql"] = true;
//set the variables
$table_name = "v_".$this->name;
$parent_key_name = $this->singular($this->name)."_uuid";
//get the number of rows
if (isset($this->uuid)) {
$sql = "SELECT count(*) AS num_rows 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 {
$action = "add";
}
}
unset($prep_statement);
}
else {
$action = "add";
}
//add a record
if ($action == "add") {
if (permission_exists($this->singular($this->name).'_add')) {
//start the atomic transaction
$this->db->beginTransaction();
//set the message index
$m = 0;
//parent data
if (isset($this->uuid)) {
$parent_key_value = $this->uuid;
}
else {
$parent_key_value = uuid();
}
$sql = "INSERT INTO v_".$this->name." ";
$sql .= "(";
$sql .= $parent_key_name.", ";
foreach ($array as $key => $value) {
if (!is_array($value)) {
$sql .= check_str($key).", ";
}
}
$sql .= ") ";
$sql .= "VALUES ";
$sql .= "(";
$sql .= "'".$parent_key_value."', ";
foreach ($array as $key => $value) {
if (!is_array($value)) {
if (strlen($value) == 0) {
$sql .= "null, ";
}
else {
$sql .= "'".check_str($value)."', ";
}
}
}
$sql .= ");";
$sql = str_replace(", )", ")", $sql);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$prep_statement = $this->db->prepare(check_sql($sql));
$prep_statement->execute();
$message["message"] = "OK";
$message["code"] = "200";
$message["details"][$m]["name"] = $this->name;
$message["details"][$m]["message"] = "OK";
$message["details"][$m]["code"] = "200";
$message["details"][$m]["uuid"] = $parent_key_value;
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++;
}
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;
}
}
//add the data
$sql = "INSERT INTO ".$table_name." ";
$sql .= "(";
$sql .= $parent_key_name.", ";
$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 .= "(";
$sql .= "'".$parent_key_value."', ";
$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 {
$prep_statement = $this->db->prepare(check_sql($sql));
$prep_statement->execute();
//$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
$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;
$message["message"] = "Forbidden";
$message["code"] = "403";
$this->message = $message;
$m++;
}
}
//edit a specific uuid
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) {
if (strlen($value) == 0) {
$sql .= check_str($key)." = null, ";
}
else {
$sql .= check_str($key)." = '".check_str($value)."', ";
}
}
}
$sql .= "WHERE ".$parent_key_name." = '".$parent_key_value."' ";
$sql = str_replace(", WHERE", " WHERE", $sql);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$prep_statement = $this->db->prepare(check_sql($sql));
$prep_statement->execute();
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
$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++;
unset($sql);
}
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++;
}
//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";
//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;
}
}
//update the data
if ($uuid_exists) {
//if (permission_exists($child_name.'_edit')) {
$sql = "UPDATE ".$table_name." SET ";
foreach ($row as $k => $v) {
if (!is_array($v) && $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);
// if (strlen($child_key_value) > 0) {
try {
$prep_statement = $this->db->prepare(check_sql($sql));
$prep_statement->execute();
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
$message["details"][$m]["name"] = $key;
$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"] = $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')) {
$sql = "INSERT INTO ".$table_name." ";
$sql .= "(";
$sql .= $this->singular($parent_key_name).", ";
$sql .= $this->singular($child_key_name).", ";
foreach ($row as $k => $v) {
if (!is_array($v)) {
$sql .= check_str($k).", ";
}
}
$sql .= ") ";
$sql .= "VALUES ";
$sql .= "(";
$sql .= "'".$parent_key_value."', ";
$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 {
$prep_statement = $this->db->prepare(check_sql($sql));
$prep_statement->execute();
$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;
$message["message"] = "Forbidden";
$message["code"] = "403";
$this->message = $message;
$m++;
}
}
}
//define singular function to convert a word in english to singular
private function singular($word) {
//"-es" is used for words that end in "-x", "-s", "-z", "-sh", "-ch" in which case you add
if (substr($word, -2) == "es") {
if (substr($word, -3, 1) == "x") {
return substr($word,0,-2);
}
if (substr($word, -3, 1) == "s") {
return substr($word,0,-2);
}
elseif (substr($word, -3, 1) == "z") {
return substr($word,0,-2);
}
elseif (substr($word, -4, 2) == "sh") {
return substr($word,0,-2);
}
elseif (substr($word, -4, 2) == "ch") {
return substr($word,0,-2);
}
else {
return rtrim($word, "s");
}
}
else {
return rtrim($word, "s");
}
}
public function get_apps() {
//get the $apps array from the installed apps from the core and mod directories
$config_list = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_config.php");
$x = 0;
foreach ($config_list as &$config_path) {
include($config_path);
$x++;
}
$_SESSION['apps'] = $apps;
}
public function domain_uuid_exists($name) {
//get the $apps array from the installed apps from the core and mod directories
if (!is_array($_SESSION['apps'])) {
$this->get_apps();
}
//search through all fields to see if domain_uuid exists
foreach ($_SESSION['apps'] as $x => &$app) {
foreach ($app['db'] as $y => &$row) {
if ($row['table'] == $name) {
foreach ($row['fields'] as $z => $field) {
if ($field['name'] == "domain_uuid") {
return true;
}
}
}
}
}
//not found
return false;
}
}
}
//examples
/*
//get records
$orm = new orm();
$result = $orm->name('dialplans')->find()->get();
print_r($result);
//get a single record
$orm = new orm();
$orm->name('dialplans')
$orm->uuid('a8363085-8318-4dee-b87f-0818be0d6318');
$orm->find();
$result = $orm->get();
print_r($result);
//get a single record
$array['name'] = "dialplans";
$array['uuid'] = "2d27e4a4-c954-4f8a-b734-88b0e1054b86";
$orm = new orm();
$result = $orm->find($array)->get();
print_r($result);
//get limited records with limit and offset
$array['name'] = "dialplans";
$array['limit'] = "10";
$array['offset'] = "2";
$orm = new orm();
$result = $orm->find($array)->get();
print_r($result);
*/
?>