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 "