use renumbering

This commit is contained in:
Tim Fry 2024-11-24 09:08:28 -04:00
parent ba64e7b6b2
commit 66a1034ac0
2 changed files with 134 additions and 33 deletions

View File

@ -37,6 +37,16 @@ else {
exit;
}
//set default domain
if (empty($domain_uuid)) {
$domain_uuid = $_SESSION['domain_uuid'] ?? '';
}
//set default user
if (empty($user_uuid)) {
$user_uuid = $_SESSION['user_uuid'] ?? '';
}
//add multi-lingual support
$language = new text;
$text = $language->get();
@ -46,6 +56,11 @@ if (!($database instanceof database)) {
$database = database::new();
}
//ensure we have a settings object
if (!($settings instanceof settings)) {
$settings = new settings(['database' => $database, 'domain_uuid' => $domain_uuid, 'user_uuid' => $user_uuid]);
}
//add the defaults
$phrase_name = '';
$phrase_language = '';
@ -196,6 +211,7 @@ if (count($_POST) > 0) {
if ($action == "update" && permission_exists('phrase_edit')) {
$array = [];
if (!empty($_POST['phrase_detail_function'])) {
$recording_files = phrases::get_all_domain_recordings($settings);
for ($i = 0; $i < count($_POST['phrase_detail_function']); $i++) {
//build data array
$array['phrases'][$i]['domain_uuid'] = $domain_uuid;
@ -213,6 +229,7 @@ if (count($_POST) > 0) {
if (!empty($_POST['phrase_detail_data'][$i])) {
$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;
$array['phrase_details'][$i]['domain_uuid'] = $domain_uuid;
@ -220,7 +237,7 @@ if (count($_POST) > 0) {
$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_data'] = $recording_files[$recording_uuid]; //path and filename of recording
$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'];
@ -233,7 +250,7 @@ if (count($_POST) > 0) {
$database->app_name = 'phrases';
$database->app_uuid = '5c6f597c-9b78-11e4-89d3-123b93f75cba';
if (count($array) > 0) {
$database->save($array);
//$database->save($array);
}
}
@ -322,11 +339,31 @@ if (count($_POST) > 0) {
//javascript constants for use in the selection option group
echo "<script>\n";
echo "window.phrase_commands = " . json_encode(['Play', 'Pause', 'Execute'], true) . ";\n";
//existing details
if (!empty($phrase_details)) {
//update the array to create the display name
foreach ($phrase_details as &$row) {
$file = basename($row['phrase_detail_data']);
$basename = substr($file, 0, strlen($file) - 4);
// $display_name = ucfirst(str_replace('_', ' ', $basename));
$display_name = basename(str_replace($_SESSION['switch']['recordings']['dir'].'/'.$_SESSION['domain_name'].'/', '', $row['phrase_detail_data']));
//remove the file ending
if (str_ends_with($display_name, '.wav') || str_ends_with($display_name, '.mp3') || str_ends_with($display_name, '.flac') || str_ends_with($display_name, '.mp4') || str_ends_with($display_name, '.gsm')) {
$display_name = substr($display_name, 0, strlen($display_name) - 4);
}
$row['phrase_detail_display_name'] = ucfirst($display_name);
}
echo "window.phrase_details = " . json_encode($phrase_details, true) . ";\n";
}
//recording files
if ($recordings !== false) {
//recordings
echo "window.phrase_recordings = " . json_encode($recordings, true) . ";\n";
}
//sound files
$file = new file;
$sound_files = $file->sounds();
if (!empty($sound_files)) {
@ -524,7 +561,7 @@ if (count($_POST) > 0) {
echo " </thead>\n";
echo "<tbody id='structure'>\n";
//draggable row
echo "<tr class='draggable-row' id='recordings_row' draggable=true>\n";
echo "<tr class='draggable-row' id='empty_row' draggable=true style='display: none;'>\n";
echo " <td style='border-bottom: none;' nowrap='nowrap'><center><span class='fa-solid fa-arrows-up-down'></span></center></td>";
echo " <td class='vtable' style='border-bottom: none;' align='left' nowrap='nowrap'>\n";
echo " <select name='phrase_detail_function[0]' id='phrase_detail_function[0]' class='formfld' onchange=\"load_action_options(this.selectedIndex);\">\n";

View File

@ -21,10 +21,74 @@ document.addEventListener("DOMContentLoaded", function () {
}
select.appendChild(grp_snd);
// add the existing data
add_existing();
// add empty row
add_row();
// update order
update_order();
// Initialize draggable rows
add_draggable_rows();
});
// Inserts all existing records before the empty one
function add_existing() {
const tbody = document.getElementById('structure');
for (let i=0; i < window.phrase_details.length; i++) {
const newRow = document.getElementById('empty_row').cloneNode(true);
//un-hide the row
newRow.style.display = '';
//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];
selectByText(select_action, 'Play');
//recording select box
const select_recording = select_list[1];
selectByText(select_recording, processFileName(window.phrase_details[i]['phrase_detail_data']));
//add the row to the table body
tbody.appendChild(newRow);
}
}
function processFileName(filePath) {
let fileName = filePath
.replace(/^.*\/([^/]+)\.\w+$/, '$1') // Extract file name
.replace(/_/g, ' '); // Remove underscores
return fileName.charAt(0).toUpperCase() + fileName.slice(1); // Capitalize first letter
}
function selectByValue(selectElement, valueToFind) {
// Loop through the options of the select element
for (let i = 0; i < selectElement.options.length; i++) {
if (selectElement.options[i].value === valueToFind) {
selectElement.selectedIndex = i; // Set the selected index
return; // Exit the loop once found
}
}
console.warn('Value not found in select options');
}
function selectByText(selectElement, textToFind) {
for (let i = 0; i < selectElement.options.length; i++) {
if (selectElement.options[i].text === textToFind) {
selectElement.selectedIndex = i;
return;
}
}
console.warn('Text not found in select options');
}
// Add draggable functionality to rows
function add_draggable_rows() {
const tableBody = document.getElementById('structure');
@ -53,52 +117,56 @@ function add_draggable_rows() {
tableBody.addEventListener('dragend', () => {
draggedRow.classList.remove('dragging');
draggedRow = null;
updateOrder();
update_order();
});
}
// Function to update the 'name' attribute based on row numbers
function updateOrder() {
function update_order() {
const tableBody = document.getElementById('structure');
const rows = tableBody.querySelectorAll('tr');
//iterate over all rows to renumber them
rows.forEach((row, index) => {
//current row number (1-based index)
const row_number = index + 1;
//set 'name' attribute
row.setAttribute('name', 'recording_' + row_number);
//set 'name' attribute and id
row.setAttribute('name', 'row_' + index);
row.id = 'row_' + index;
//get the select boxes
const select_list = row.querySelectorAll('td select'); //action and recording select dropdown boxes
//play, pause, execute select box
const select_action = select_list[0];
//recording select box
const select_recording = select_list[1];
//set the new id and name for action
select_action.id = 'phrase_detail_function[' + index + ']'
select_action.setAttribute('name', 'phrase_detail_function[' + index + ']');
//set the new id and name for recording
select_recording.id = 'phrase_detail_data[' + index + ']'
select_recording.setAttribute('name', 'phrase_detail_data[' + index + ']');
});
}
// Add a new row to the table
function add_row() {
const tbody = document.getElementById('structure');
const newRow = document.getElementById('recordings_row').cloneNode(true);
const newRow = document.getElementById('empty_row').cloneNode(true);
// current index is the count subtract the hidden row
const index = tbody.childElementCount - 1;
//un-hide row
newRow.style.display = '';
//reset id
newRow.id = 'row_' + tbody.childElementCount
newRow.id = 'row_' + index;
//reset 'name' attribute
newRow.setAttribute('name', 'recording_' + tbody.childElementCount);
//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];
//recording select box
const select_recording = select_list[1];
//set the new id and name for action
select_action.id = 'phrase_detail_function[' + tbody.childElementCount + ']'
select_action.setAttribute('name', 'phrase_detail_function[' + tbody.childElementCount + ']');
//set the new id and name for recording
select_recording.id = 'phrase_detail_data[' + tbody.childElementCount + ']'
select_recording.setAttribute('name', 'phrase_detail_data[' + tbody.childElementCount + ']');
newRow.setAttribute('name', 'recording_' + index);
//add the row to the table body
tbody.appendChild(newRow);
@ -114,7 +182,3 @@ function remove_row() {
tbody.lastElementChild.remove();
}
}
function create_new_name() {
}