From 2eb24a3ef2b9e996cca8aac36b346fd8c72c3ed1 Mon Sep 17 00:00:00 2001 From: Tim Fry Date: Mon, 25 Nov 2024 17:19:15 -0400 Subject: [PATCH] edit finally working with recordings --- app/phrases/phrase_edit.php | 190 +++++------------- .../resources/javascript/phrase_edit.js | 14 +- 2 files changed, 63 insertions(+), 141 deletions(-) diff --git a/app/phrases/phrase_edit.php b/app/phrases/phrase_edit.php index 7c3f19e4b2..dd0212145c 100644 --- a/app/phrases/phrase_edit.php +++ b/app/phrases/phrase_edit.php @@ -210,25 +210,43 @@ if (count($_POST) > 0) { //update the phrase if ($action == "update" && permission_exists('phrase_edit')) { $array = []; + $drop_rows = []; + $drop_row_count = 0; if (!empty($_POST['phrase_detail_function'])) { $recording_files = phrases::get_all_domain_recordings($settings); + //update the phrase information + $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; for ($i = 0; $i < count($_POST['phrase_detail_function']); $i++) { - //build data array - $array['phrases'][$i]['domain_uuid'] = $domain_uuid; - $array['phrases'][$i]['phrase_uuid'] = $phrase_uuid; - $array['phrases'][$i]['phrase_name'] = $phrase_name; - $array['phrases'][$i]['phrase_language'] = $phrase_language; - $array['phrases'][$i]['phrase_enabled'] = $phrase_enabled; - $array['phrases'][$i]['phrase_description'] = $phrase_description; - if ($_POST['phrase_detail_function'][$i] == 'execute' && substr($_POST['phrase_detail_data'][$i], 0,5) != "sleep" && !permission_exists("phrase_execute")) { - header("Location: phrase_edit.php?id=".$phrase_uuid); - exit; + //check for valid uuids + if (!empty($_POST['phrase_detail_uuid'][$i]) && !is_uuid($_POST['phrase_detail_uuid'][$i])) { + continue; } - $_POST['phrase_detail_tag'] = 'action'; // default, for now - $_POST['phrase_detail_group'] = "0"; // one group, for now - + //check for the empty rows to delete + if (empty($_POST['phrase_detail_data'][$i]) && !empty($_POST['phrase_detail_uuid'][$i])) { + $drop_rows['phrase_details'][$drop_row_count++]['phrase_detail_uuid'] = $_POST['phrase_detail_uuid'][$i]; + continue; + } + //only save rows with data if (!empty($_POST['phrase_detail_data'][$i])) { - $phrase_detail_uuid = uuid(); + //build data array + if ($_POST['phrase_detail_function'][$i] == 'execute' && substr($_POST['phrase_detail_data'][$i], 0,5) != "sleep" && !permission_exists("phrase_execute")) { + 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 + + //update existing records in the database + if (!empty($_POST['phrase_detail_uuid'][$i])) { + $phrase_detail_uuid = $_POST['phrase_detail_uuid'][$i]; + } else { + $phrase_detail_uuid = uuid(); + } $recording_uuid = $_POST['phrase_detail_data'][$i]; $array['phrase_details'][$i]['phrase_detail_uuid'] = $phrase_detail_uuid; $array['phrase_details'][$i]['phrase_uuid'] = $phrase_uuid; @@ -247,14 +265,19 @@ if (count($_POST) > 0) { //execute update/insert $p = new permissions; $p->add('phrase_detail_add', 'temp'); + $p->add('phrase_detail_edit', 'temp'); + $p->add('phrase_detail_delete', 'temp'); $database->app_name = 'phrases'; $database->app_uuid = '5c6f597c-9b78-11e4-89d3-123b93f75cba'; if (count($array) > 0) { - //$database->save($array); + $database->save($array); } + if (count($drop_rows) > 0) { + $database->delete($drop_rows); + } + $p->delete('phrase_detail_add', 'temp'); } - $p->delete('phrase_detail_add', 'temp'); //remove checked phrase details if ( @@ -354,12 +377,18 @@ if (count($_POST) > 0) { $row['phrase_detail_display_name'] = ucfirst(str_replace('_', ' ', $basename)); } echo "window.phrase_details = " . json_encode($phrase_details, true) . ";\n"; + } else { + //send an empty array + echo "window.phrase_details = [];\n"; } //recording files if ($recordings !== false) { //recordings echo "window.phrase_recordings = " . json_encode($recordings, true) . ";\n"; + } else { + //send an empty array + echo "window.phrase_recordings = [];\n"; } //sound files @@ -374,124 +403,6 @@ if (count($_POST) > 0) { //js to control action form input echo "\n"; -/* old section - echo " \n"; -//*/ //show the content echo "
\n"; @@ -566,7 +477,7 @@ if (count($_POST) > 0) { echo "\n"; echo "
"; echo " \n"; - echo " \n"; echo " \n"; echo " \n"; if (if_group("superadmin")) { @@ -575,10 +486,11 @@ if (count($_POST) > 0) { echo " \n"; echo " \n"; echo " \n"; - echo " "; - if (if_group("superadmin")) { - echo " \n"; - } + echo " "; +// if (if_group("superadmin")) { +// echo " \n"; +// } + echo " "; echo " \n"; echo " \n"; echo ""; diff --git a/app/phrases/resources/javascript/phrase_edit.js b/app/phrases/resources/javascript/phrase_edit.js index d060051155..da9de59f2e 100644 --- a/app/phrases/resources/javascript/phrase_edit.js +++ b/app/phrases/resources/javascript/phrase_edit.js @@ -1,6 +1,6 @@ document.addEventListener("DOMContentLoaded", function () { // Initialize the select options - const select = document.getElementById('phrase_detail_data[0]'); + const select = document.getElementById('phrase_detail_data_empty'); const grp_rec = document.createElement('optgroup'); const grp_snd = document.createElement('optgroup'); @@ -46,15 +46,21 @@ function add_existing() { //get the select boxes const select_list = newRow.querySelectorAll('td select'); //action and recording select dropdown boxes - //play, pause, execute select box const select_action = select_list[0]; select_by_text(select_action, 'Play'); //recording select box const select_recording = select_list[1]; + select_recording.setAttribute('tag', window.phrase_details[i]['phrase_detail_uuid']); select_by_text(select_recording, window.phrase_details[i]['phrase_detail_display_name']); + const input_fields = newRow.querySelectorAll('td input'); + const uuid_field = input_fields[0]; + uuid_field.setAttribute('id' , 'phrase_detail_uuid[' + i +']'); + uuid_field.setAttribute('name', 'phrase_detail_uuid[' + i +']'); + uuid_field.value = window.phrase_details[i]['phrase_detail_uuid']; + //add the row to the table body tbody.appendChild(newRow); } @@ -81,6 +87,10 @@ function select_by_text(selectElement, textToFind) { console.warn('Text not found in select options'); } +function update_id(element) { + index = element.index; + const hidden_input_field = document.getElementById('hidden_input_field[' + index +']'); +} // Add draggable functionality to rows function add_draggable_rows() {