use array from POST to save

This commit is contained in:
Tim Fry
2024-11-23 11:06:10 -04:00
parent b8ec86671a
commit ba64e7b6b2
2 changed files with 206 additions and 206 deletions
+202 -202
View File
@@ -25,245 +25,245 @@
*/ */
//includes files //includes files
require_once dirname(__DIR__, 2) . "/resources/require.php"; require_once dirname(__DIR__, 2) . "/resources/require.php";
require_once "resources/check_auth.php"; require_once "resources/check_auth.php";
//check permissions //check permissions
if (permission_exists('phrase_add') || permission_exists('phrase_edit')) { if (permission_exists('phrase_add') || permission_exists('phrase_edit')) {
//access granted //access granted
} }
else { else {
echo "access denied"; echo "access denied";
exit; exit;
} }
//add multi-lingual support //add multi-lingual support
$language = new text; $language = new text;
$text = $language->get(); $text = $language->get();
//ensure we have a database object //ensure we have a database object
if (!($database instanceof database)) { if (!($database instanceof database)) {
$database = database::new(); $database = database::new();
} }
//add the defaults //add the defaults
$phrase_name = ''; $phrase_name = '';
$phrase_language = ''; $phrase_language = '';
$phrase_description = ''; $phrase_description = '';
//set the action as an add or an update //set the action as an add or an update
if (!empty($_REQUEST["id"])) { if (!empty($_REQUEST["id"])) {
$action = "update"; $action = "update";
$phrase_uuid = $_REQUEST["id"]; $phrase_uuid = $_REQUEST["id"];
} }
else { else {
$action = "add"; $action = "add";
} }
//get the form value and set to php variables //get the form value and set to php variables
if (count($_POST) > 0) { if (count($_POST) > 0) {
//process the http post data by submitted action //process the http post data by submitted action
if (!empty($_POST['action']) && is_uuid($_POST['phrase_uuid'])) { if (!empty($_POST['action']) && is_uuid($_POST['phrase_uuid'])) {
$array[0]['checked'] = 'true'; $array[0]['checked'] = 'true';
$array[0]['uuid'] = $_POST['phrase_uuid']; $array[0]['uuid'] = $_POST['phrase_uuid'];
switch ($_POST['action']) { switch ($_POST['action']) {
case 'delete': case 'delete':
if (permission_exists('phrase_delete')) { if (permission_exists('phrase_delete')) {
$obj = new phrases; $obj = new phrases;
$obj->delete($array); $obj->delete($array);
} }
break; break;
}
header('Location: phrases.php');
exit;
} }
if (permission_exists('phrase_domain')) { header('Location: phrases.php');
$domain_uuid = $_POST["domain_uuid"]; exit;
} }
$phrase_name = $_POST["phrase_name"];
$phrase_language = $_POST["phrase_language"];
$phrase_enabled = $_POST["phrase_enabled"] ?? 'false';
$phrase_description = $_POST["phrase_description"];
$phrase_details_delete = $_POST["phrase_details_delete"] ?? '';
//clean the name if (permission_exists('phrase_domain')) {
$phrase_name = str_replace(" ", "_", $phrase_name); $domain_uuid = $_POST["domain_uuid"];
$phrase_name = str_replace("'", "", $phrase_name);
} }
$phrase_name = $_POST["phrase_name"];
$phrase_language = $_POST["phrase_language"];
$phrase_enabled = $_POST["phrase_enabled"] ?? 'false';
$phrase_description = $_POST["phrase_description"];
$phrase_details_delete = $_POST["phrase_details_delete"] ?? '';
//clean the name
$phrase_name = str_replace(" ", "_", $phrase_name);
$phrase_name = str_replace("'", "", $phrase_name);
}
//process the changes from the http post //process the changes from the http post
if (count($_POST) > 0 && empty($_POST["persistformvar"])) { if (count($_POST) > 0 && empty($_POST["persistformvar"])) {
//get the uuid //get the uuid
if ($action == "update") { if ($action == "update") {
$phrase_uuid = $_POST["phrase_uuid"]; $phrase_uuid = $_POST["phrase_uuid"];
} }
//validate the token //validate the token
$token = new token; $token = new token;
if (!$token->validate($_SERVER['PHP_SELF'])) { if (!$token->validate($_SERVER['PHP_SELF'])) {
message::add($text['message-invalid_token'],'negative'); message::add($text['message-invalid_token'],'negative');
header('Location: phrases.php'); header('Location: phrases.php');
exit;
}
//check for all required data
$msg = '';
if (empty($phrase_name)) { $msg .= $text['message-required']." ".$text['label-name']."<br>\n"; }
if (empty($phrase_language)) { $msg .= $text['message-required']." ".$text['label-language']."<br>\n"; }
if (!empty($msg) && empty($_POST["persistformvar"])) {
require_once "resources/header.php";
require_once "resources/persist_form_var.php";
echo "<div align='center'>\n";
echo "<table><tr><td>\n";
echo $msg."<br />";
echo "</td></tr></table>\n";
persistformvar($_POST);
echo "</div>\n";
require_once "resources/footer.php";
return;
}
//add the phrase
if (empty($_POST["persistformvar"]) || $_POST["persistformvar"] != "true") {
if ($action == "add" && permission_exists('phrase_add')) {
//build data array
$phrase_uuid = uuid();
$array['phrases'][0]['domain_uuid'] = $domain_uuid;
$array['phrases'][0]['phrase_uuid'] = $phrase_uuid;
$array['phrases'][0]['phrase_name'] = $phrase_name;
$array['phrases'][0]['phrase_language'] = $phrase_language;
$array['phrases'][0]['phrase_enabled'] = $phrase_enabled;
$array['phrases'][0]['phrase_description'] = $phrase_description;
if ($_POST['phrase_detail_function'] != '') {
if ($_POST['phrase_detail_function'] == 'execute' && substr($_POST['phrase_detail_data'], 0,5) != "sleep" && !permission_exists("phrase_execute")) {
header("Location: phrase_edit.php");
exit;
}
$_POST['phrase_detail_tag'] = 'action'; // default, for now
$_POST['phrase_detail_group'] = "0"; // one group, for now
if ($_POST['phrase_detail_data'] != '') {
$phrase_detail_uuid = uuid();
$array['phrase_details'][0]['phrase_detail_uuid'] = $phrase_detail_uuid;
$array['phrase_details'][0]['phrase_uuid'] = $phrase_uuid;
$array['phrase_details'][0]['domain_uuid'] = $domain_uuid;
$array['phrase_details'][0]['phrase_detail_order'] = $_POST['phrase_detail_order'];
$array['phrase_details'][0]['phrase_detail_tag'] = $_POST['phrase_detail_tag'];
$array['phrase_details'][0]['phrase_detail_pattern'] = $_POST['phrase_detail_pattern'] ?? null;
$array['phrase_details'][0]['phrase_detail_function'] = $_POST['phrase_detail_function'];
$array['phrase_details'][0]['phrase_detail_data'] = $_POST['phrase_detail_data'];
$array['phrase_details'][0]['phrase_detail_method'] = $_POST['phrase_detail_method'] ?? null;
$array['phrase_details'][0]['phrase_detail_type'] = $_POST['phrase_detail_type'] ?? null;
$array['phrase_details'][0]['phrase_detail_group'] = $_POST['phrase_detail_group'];
}
}
//execute insert
$p = new permissions;
$p->add('phrase_detail_add', 'temp');
$database->app_name = 'phrases';
$database->app_uuid = '5c6f597c-9b78-11e4-89d3-123b93f75cba';
$database->save($array);
unset($array);
$p->delete('phrase_detail_add', 'temp');
//save the xml to the file system if the phrase directory is set
//save_phrases_xml();
//clear the cache
$cache = new cache;
$cache->delete("languages:".$phrase_language.".".$phrase_uuid);
//clear the destinations session array
if (isset($_SESSION['destinations']['array'])) {
unset($_SESSION['destinations']['array']);
}
//send a redirect
message::add($text['message-add']);
header("Location: phrase_edit.php?id=".$phrase_uuid);
exit; exit;
} }
//check for all required data
$msg = '';
if (empty($phrase_name)) { $msg .= $text['message-required']." ".$text['label-name']."<br>\n"; }
if (empty($phrase_language)) { $msg .= $text['message-required']." ".$text['label-language']."<br>\n"; }
if (!empty($msg) && empty($_POST["persistformvar"])) {
require_once "resources/header.php";
require_once "resources/persist_form_var.php";
echo "<div align='center'>\n";
echo "<table><tr><td>\n";
echo $msg."<br />";
echo "</td></tr></table>\n";
persistformvar($_POST);
echo "</div>\n";
require_once "resources/footer.php";
return;
}
//add the phrase
if (empty($_POST["persistformvar"]) || $_POST["persistformvar"] != "true") {
if ($action == "add" && permission_exists('phrase_add')) {
//build data array
$phrase_uuid = uuid();
$array['phrases'][0]['domain_uuid'] = $domain_uuid;
$array['phrases'][0]['phrase_uuid'] = $phrase_uuid;
$array['phrases'][0]['phrase_name'] = $phrase_name;
$array['phrases'][0]['phrase_language'] = $phrase_language;
$array['phrases'][0]['phrase_enabled'] = $phrase_enabled;
$array['phrases'][0]['phrase_description'] = $phrase_description;
if ($_POST['phrase_detail_function'] != '') {
if ($_POST['phrase_detail_function'] == 'execute' && substr($_POST['phrase_detail_data'], 0,5) != "sleep" && !permission_exists("phrase_execute")) {
header("Location: phrase_edit.php");
exit;
}
$_POST['phrase_detail_tag'] = 'action'; // default, for now
$_POST['phrase_detail_group'] = "0"; // one group, for now
if ($_POST['phrase_detail_data'] != '') {
$phrase_detail_uuid = uuid();
$array['phrase_details'][0]['phrase_detail_uuid'] = $phrase_detail_uuid;
$array['phrase_details'][0]['phrase_uuid'] = $phrase_uuid;
$array['phrase_details'][0]['domain_uuid'] = $domain_uuid;
$array['phrase_details'][0]['phrase_detail_order'] = $_POST['phrase_detail_order'];
$array['phrase_details'][0]['phrase_detail_tag'] = $_POST['phrase_detail_tag'];
$array['phrase_details'][0]['phrase_detail_pattern'] = $_POST['phrase_detail_pattern'] ?? null;
$array['phrase_details'][0]['phrase_detail_function'] = $_POST['phrase_detail_function'];
$array['phrase_details'][0]['phrase_detail_data'] = $_POST['phrase_detail_data'];
$array['phrase_details'][0]['phrase_detail_method'] = $_POST['phrase_detail_method'] ?? null;
$array['phrase_details'][0]['phrase_detail_type'] = $_POST['phrase_detail_type'] ?? null;
$array['phrase_details'][0]['phrase_detail_group'] = $_POST['phrase_detail_group'];
}
}
//execute insert
$p = new permissions;
$p->add('phrase_detail_add', 'temp');
$database->app_name = 'phrases';
$database->app_uuid = '5c6f597c-9b78-11e4-89d3-123b93f75cba';
$database->save($array);
unset($array);
$p->delete('phrase_detail_add', 'temp');
//save the xml to the file system if the phrase directory is set
//save_phrases_xml();
//clear the cache
$cache = new cache;
$cache->delete("languages:".$phrase_language.".".$phrase_uuid);
//clear the destinations session array
if (isset($_SESSION['destinations']['array'])) {
unset($_SESSION['destinations']['array']);
}
//send a redirect
message::add($text['message-add']);
header("Location: phrase_edit.php?id=".$phrase_uuid);
exit;
}
//update the phrase //update the phrase
if ($action == "update" && permission_exists('phrase_edit')) { if ($action == "update" && permission_exists('phrase_edit')) {
//build data array $array = [];
$array['phrases'][0]['domain_uuid'] = $domain_uuid; if (!empty($_POST['phrase_detail_function'])) {
$array['phrases'][0]['phrase_uuid'] = $phrase_uuid; for ($i = 0; $i < count($_POST['phrase_detail_function']); $i++) {
$array['phrases'][0]['phrase_name'] = $phrase_name; //build data array
$array['phrases'][0]['phrase_language'] = $phrase_language; $array['phrases'][$i]['domain_uuid'] = $domain_uuid;
$array['phrases'][0]['phrase_enabled'] = $phrase_enabled; $array['phrases'][$i]['phrase_uuid'] = $phrase_uuid;
$array['phrases'][0]['phrase_description'] = $phrase_description; $array['phrases'][$i]['phrase_name'] = $phrase_name;
$array['phrases'][$i]['phrase_language'] = $phrase_language;
if ($_POST['phrase_detail_function'] != '') { $array['phrases'][$i]['phrase_enabled'] = $phrase_enabled;
if ($_POST['phrase_detail_function'] == 'execute' && substr($_POST['phrase_detail_data'], 0,5) != "sleep" && !permission_exists("phrase_execute")) { $array['phrases'][$i]['phrase_description'] = $phrase_description;
header("Location: phrase_edit.php?id=".$phrase_uuid); if ($_POST['phrase_detail_function'][$i] == 'execute' && substr($_POST['phrase_detail_data'][$i], 0,5) != "sleep" && !permission_exists("phrase_execute")) {
exit; header("Location: phrase_edit.php?id=".$phrase_uuid);
} exit;
$_POST['phrase_detail_tag'] = 'action'; // default, for now
$_POST['phrase_detail_group'] = "0"; // one group, for now
if ($_POST['phrase_detail_data'] != '') {
$phrase_detail_uuid = uuid();
$array['phrase_details'][0]['phrase_detail_uuid'] = $phrase_detail_uuid;
$array['phrase_details'][0]['phrase_uuid'] = $phrase_uuid;
$array['phrase_details'][0]['domain_uuid'] = $domain_uuid;
$array['phrase_details'][0]['phrase_detail_order'] = $_POST['phrase_detail_order'];
$array['phrase_details'][0]['phrase_detail_tag'] = $_POST['phrase_detail_tag'];
$array['phrase_details'][0]['phrase_detail_pattern'] = $_POST['phrase_detail_pattern'] ?? null;
$array['phrase_details'][0]['phrase_detail_function'] = $_POST['phrase_detail_function'];
$array['phrase_details'][0]['phrase_detail_data'] = $_POST['phrase_detail_data'];
$array['phrase_details'][0]['phrase_detail_method'] = $_POST['phrase_detail_method'] ?? null;
$array['phrase_details'][0]['phrase_detail_type'] = $_POST['phrase_detail_type'] ?? null;
$array['phrase_details'][0]['phrase_detail_group'] = $_POST['phrase_detail_group'];
}
} }
$_POST['phrase_detail_tag'] = 'action'; // default, for now
$_POST['phrase_detail_group'] = "0"; // one group, for now
if (!empty($_POST['phrase_detail_data'][$i])) {
$phrase_detail_uuid = uuid();
$array['phrase_details'][$i]['phrase_detail_uuid'] = $phrase_detail_uuid;
$array['phrase_details'][$i]['phrase_uuid'] = $phrase_uuid;
$array['phrase_details'][$i]['domain_uuid'] = $domain_uuid;
$array['phrase_details'][$i]['phrase_detail_order'] = $i;
$array['phrase_details'][$i]['phrase_detail_tag'] = $_POST['phrase_detail_tag'];
$array['phrase_details'][$i]['phrase_detail_pattern'] = $_POST['phrase_detail_pattern'] ?? null;
$array['phrase_details'][$i]['phrase_detail_function'] = $_POST['phrase_detail_function'][$i];
$array['phrase_details'][$i]['phrase_detail_data'] = $_POST['phrase_detail_data'][$i];
$array['phrase_details'][$i]['phrase_detail_method'] = $_POST['phrase_detail_method'] ?? null;
$array['phrase_details'][$i]['phrase_detail_type'] = $_POST['phrase_detail_type'] ?? null;
$array['phrase_details'][$i]['phrase_detail_group'] = $_POST['phrase_detail_group'];
}
}
//execute update/insert //execute update/insert
$p = new permissions; $p = new permissions;
$p->add('phrase_detail_add', 'temp'); $p->add('phrase_detail_add', 'temp');
$database->app_name = 'phrases'; $database->app_name = 'phrases';
$database->app_uuid = '5c6f597c-9b78-11e4-89d3-123b93f75cba'; $database->app_uuid = '5c6f597c-9b78-11e4-89d3-123b93f75cba';
if (count($array) > 0) {
$database->save($array); $database->save($array);
unset($array); }
$p->delete('phrase_detail_add', 'temp');
//remove checked phrase details
if (
is_array($phrase_details_delete)
&& @sizeof($phrase_details_delete) != 0
) {
$obj = new phrases;
$obj->phrase_uuid = $phrase_uuid;
$obj->delete_details($phrase_details_delete);
}
//clear the cache
$cache = new cache;
$cache->delete("languages:".$phrase_language.".".$phrase_uuid);
//clear the destinations session array
if (isset($_SESSION['destinations']['array'])) {
unset($_SESSION['destinations']['array']);
}
//send a redirect
message::add($text['message-update']);
header("Location: phrase_edit.php?id=".$phrase_uuid);
exit;;
} }
$p->delete('phrase_detail_add', 'temp');
//remove checked phrase details
if (
is_array($phrase_details_delete)
&& @sizeof($phrase_details_delete) != 0
) {
$obj = new phrases;
$obj->phrase_uuid = $phrase_uuid;
$obj->delete_details($phrase_details_delete);
}
//clear the cache
$cache = new cache;
$cache->delete("languages:".$phrase_language.".".$phrase_uuid);
//clear the destinations session array
if (isset($_SESSION['destinations']['array'])) {
unset($_SESSION['destinations']['array']);
}
//send a redirect
message::add($text['message-update']);
header("Location: phrase_edit.php?id=".$phrase_uuid);
exit;
} }
}
} }
//pre-populate the form //pre-populate the form
@@ -93,11 +93,11 @@ function add_row() {
const select_recording = select_list[1]; const select_recording = select_list[1];
//set the new id and name for action //set the new id and name for action
select_action.id = 'phrase_detail_function[' + tbody.childElementCount - 1 + ']' select_action.id = 'phrase_detail_function[' + tbody.childElementCount + ']'
select_action.setAttribute('name', 'phrase_detail_function[' + tbody.childElementCount - 1 + ']'); select_action.setAttribute('name', 'phrase_detail_function[' + tbody.childElementCount + ']');
//set the new id and name for recording //set the new id and name for recording
select_recording.id = 'phrase_detail_data[' + tbody.childElementCount - 1 + ']' select_recording.id = 'phrase_detail_data[' + tbody.childElementCount + ']'
select_recording.setAttribute('name', 'phrase_detail_data[' + tbody.childElementCount - 1 + ']'); select_recording.setAttribute('name', 'phrase_detail_data[' + tbody.childElementCount + ']');
//add the row to the table body //add the row to the table body