Add a recording message textarea

- Add speech to text
- Add text to speech
This commit is contained in:
FusionPBX
2024-03-23 19:24:32 -06:00
committed by GitHub
parent 2756e71e37
commit 1a34181008
3 changed files with 207 additions and 45 deletions

View File

@@ -178,6 +178,11 @@
$apps[$x]['db'][$y]['fields'][$z]['search'] = 'true';
$apps[$x]['db'][$y]['fields'][$z]['description']['en-us'] = "";
$z++;
$apps[$x]['db'][$y]['fields'][$z]['name'] = "recording_message";
$apps[$x]['db'][$y]['fields'][$z]['type'] = "text";
$apps[$x]['db'][$y]['fields'][$z]['search'] = 'true';
$apps[$x]['db'][$y]['fields'][$z]['description']['en-us'] = "";
$z++;
$apps[$x]['db'][$y]['fields'][$z]['name']['text'] = "recording_description";
$apps[$x]['db'][$y]['fields'][$z]['name']['deprecated'] = "descr";
$apps[$x]['db'][$y]['fields'][$z]['type'] = "text";

View File

@@ -17,7 +17,7 @@
The Initial Developer of the Original Code is
Mark J Crane <markjcrane@fusionpbx.com>
Portions created by the Initial Developer are Copyright (C) 2008-2020
Portions created by the Initial Developer are Copyright (C) 2008-2024
the Initial Developer. All Rights Reserved.
Contributor(s):
@@ -42,18 +42,38 @@
$language = new text;
$text = $language->get();
//add the settings object
$settings = new settings(["domain_uuid" => $SESSION['domain_uuid']]);
$speech_enabled = $settings->get('ai', 'speech_enabled');
$transcribe_enabled = $settings->get('ai', 'transcribe_enabled');
//add the audio object and get the voices and languages arrays
if ($speech_enabled == 'true' || $transcribe_enabled == 'true') {
$ai = new ai($settings);
$voices = $ai->get_voices();
$translate_enabled = false;
$language_enabled = false;
//$translate_enabled = $ai->get_translate_enabled();
//$language_enabled = $ai->get_language_enabled();
//$languages = $ai->get_languages();
}
//get recording id
if (!empty($_REQUEST["id"]) && is_uuid($_REQUEST["id"])) {
$recording_uuid = $_REQUEST["id"];
}
//get the form value and set to php variables
if (count($_POST) > 0) {
if (!empty($_POST)) {
$recording_filename = $_POST["recording_filename"];
$recording_filename_original = $_POST["recording_filename_original"];
$recording_name = $_POST["recording_name"];
$recording_voice = $_POST["recording_voice"];
$recording_language = $_POST["recording_language"];
//$translate = $_POST["translate"];
$recording_message = $_POST["recording_message"];
$recording_description = $_POST["recording_description"];
//sanitize recording filename and name
$recording_filename_ext = strtolower(pathinfo($recording_filename, PATHINFO_EXTENSION));
if (!in_array($recording_filename_ext, ['wav','mp3','ogg'])) {
@@ -68,26 +88,29 @@
$recording_name = str_replace("'", '', $recording_name);
}
if (count($_POST) > 0 && empty($_POST["persistformvar"])) {
//get recording uuid to edit
//process the HTTP POST
if (!empty($_POST) && empty($_POST["persistformvar"])) {
//get recording uuid to edit
$recording_uuid = $_POST["recording_uuid"];
//delete the recording
//delete the recording
if (permission_exists('recording_delete')) {
if ($_POST['action'] == 'delete' && is_uuid($recording_uuid)) {
//prepare
$array[0]['checked'] = 'true';
$array[0]['uuid'] = $recording_uuid;
$array[0]['checked'] = 'true';
$array[0]['uuid'] = $recording_uuid;
//delete
$obj = new switch_recordings;
$obj->delete($array);
$obj = new switch_recordings;
$obj->delete($array);
//redirect
header('Location: recordings.php');
exit;
header('Location: recordings.php');
exit;
}
}
//validate the token
//validate the token
$token = new token;
if (!$token->validate($_SERVER['PHP_SELF'])) {
message::add($text['message-invalid_token'],'negative');
@@ -95,10 +118,10 @@ if (count($_POST) > 0 && empty($_POST["persistformvar"])) {
exit;
}
//check for all required data
//check for all required data
$msg = '';
if (empty($recording_filename)) { $msg .= $text['label-edit-file']."<br>\n"; }
if (empty($recording_name)) { $msg .= $text['label-edit-recording']."<br>\n"; }
if (empty($recording_name)) { $msg .= $text['label-edit_recording']."<br>\n"; }
//if (empty($recording_filename)) { $msg .= $text['label-edit_file']."<br>\n"; }
if (!empty($msg) && empty($_POST["persistformvar"])) {
require_once "resources/header.php";
require_once "resources/persist_form_var.php";
@@ -112,43 +135,88 @@ if (count($_POST) > 0 && empty($_POST["persistformvar"])) {
return;
}
//update the database
if (empty($_POST["persistformvar"])) {
if (permission_exists('recording_edit')) {
//if file name is not the same then rename the file
//add the bridge_uuid
if (empty($recording_uuid)) {
$recording_uuid = uuid();
}
//set the default voice
if (empty($recording_voice)) {
$recording_voice = 'alloy';
}
//set the recording format
if (empty($recording_format)) {
$recording_format = 'wav';
}
//update the database
if (empty($_POST["persistformvar"])) {
if (permission_exists('recording_edit')) {
//if file name is not the same then rename the file
if ($recording_filename != $recording_filename_original) {
rename($_SESSION['switch']['recordings']['dir'].'/'.$_SESSION['domain_name'].'/'.$recording_filename_original, $_SESSION['switch']['recordings']['dir'].'/'.$_SESSION['domain_name'].'/'.$recording_filename);
}
//build array
$array['recordings'][0]['domain_uuid'] = $domain_uuid;
$array['recordings'][0]['recording_filename'] = $recording_filename;
$array['recordings'][0]['recording_name'] = $recording_name;
$array['recordings'][0]['recording_description'] = $recording_description;
//build the setting object and get the recording path
$recording_path = $settings->get('switch', 'recordings').'/'.$_SESSION['domain_name'];
//create the file name
if (empty($recording_filename)) {
$recording_filename = $recording_name.'.'.$recording_format;
}
//text to audio - make a new audio file from the message
if ($speech_enabled == 'true' && !empty($recording_voice) && !empty($recording_message)) {
$ai->audio_path = $recording_path;
$ai->audio_filename = $recording_filename;
$ai->audio_format = $recording_format;
$ai->audio_voice = $recording_voice;
//$ai->audio_language = $recording_language;
//$ai->audio_translate = $translate;
$ai->audio_message = $recording_message;
$ai->speech();
}
//audio to text - get the transcription from the audio file
if ($transcribe_enabled == 'true' && empty($recording_message)) {
$ai->audio_path = $recording_path;
$ai->audio_filename = $recording_filename;
$recording_message = $ai->transcribe();
}
//build array
$array['recordings'][0]['domain_uuid'] = $domain_uuid;
$array['recordings'][0]['recording_uuid'] = $recording_uuid;
$array['recordings'][0]['recording_filename'] = $recording_filename;
$array['recordings'][0]['recording_name'] = $recording_name;
if ($speech_enabled == 'true' || $transcribe_enabled == 'true') {
$array['recordings'][0]['recording_message'] = $recording_message;
}
$array['recordings'][0]['recording_description'] = $recording_description;
//execute update
//execute update
$database = new database;
$database->app_name = 'recordings';
$database->app_uuid = '83913217-c7a2-9e90-925d-a866eb40b60e';
$database->save($array);
unset($array);
//set message
//set message
message::add($text['message-update']);
//redirect
//redirect
header("Location: recordings.php");
exit;
}
}
}
}
//pre-populate the form
if (!empty($_GET) && empty($_POST["persistformvar"])) {
$recording_uuid = $_GET["id"];
$sql = "select recording_name, recording_filename, recording_description from v_recordings ";
$sql = "select recording_name, recording_filename, recording_message, recording_description ";
$sql .= "from v_recordings ";
$sql .= "where domain_uuid = :domain_uuid ";
$sql .= "and recording_uuid = :recording_uuid ";
$parameters['domain_uuid'] = $domain_uuid;
@@ -158,6 +226,7 @@ if (count($_POST) > 0 && empty($_POST["persistformvar"])) {
if (is_array($row) && @sizeof($row) != 0) {
$recording_filename = $row["recording_filename"];
$recording_name = $row["recording_name"];
$recording_message = $row["recording_message"];
$recording_description = $row["recording_description"];
}
unset($sql, $parameters, $row);
@@ -203,21 +272,105 @@ if (count($_POST) > 0 && empty($_POST["persistformvar"])) {
echo "</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td class='vncellreq' valign='top' align='left' nowrap>\n";
echo " ".$text['label-file_name']."\n";
echo "</td>\n";
echo "<td class='vtable' align='left'>\n";
echo " <input class='formfld' type='text' name='recording_filename' maxlength='255' value=\"".escape($recording_filename)."\">\n";
echo " <input type='hidden' name='recording_filename_original' value=\"".escape($recording_filename)."\">\n";
echo "<br />\n";
//echo $text['message-file']."\n";
echo "</td>\n";
echo "</tr>\n";
if (!empty($_REQUEST["id"])) {
echo "<tr>\n";
echo "<td class='vncell' valign='top' align='left' nowrap>\n";
echo " ".$text['label-file_name']."\n";
echo "</td>\n";
echo "<td class='vtable' align='left'>\n";
echo " <input class='formfld' type='text' name='recording_filename' maxlength='255' value=\"".escape($recording_filename)."\">\n";
echo " <input type='hidden' name='recording_filename_original' value=\"".escape($recording_filename)."\">\n";
echo "<br />\n";
echo $text['description-file_name']."\n";
echo "</td>\n";
echo "</tr>\n";
}
if ($speech_enabled == 'true' || $transcribe_enabled == 'true') {
echo "<tr>\n";
echo "<td class='vncell' valign='top' align='left' nowrap>\n";
echo " ".$text['label-voice']."\n";
echo "</td>\n";
echo "<td class='vtable' align='left'>\n";
if (!empty($voices)) {
echo " <select class='formfld' name='recording_voice'>\n";
echo " <option value=''></option>\n";
foreach($voices as $voice) {
echo " <option value='".escape($voice)."' ".(($voice == $recording_voice) ? "selected='selected'" : null).">".escape($voice)."</option>\n";
}
echo " </select>\n";
}
else {
echo " <input class='formfld' type='text' name='recording_voice' maxlength='255' value=\"".escape($recording_voice)."\">\n";
}
echo "<br />\n";
echo $text['description-voice']."\n";
echo "</td>\n";
echo "</tr>\n";
if ($language_enabled) {
echo "<tr>\n";
echo "<td class='vncell' valign='top' align='left' nowrap>\n";
echo " ".$text['label-language']."\n";
echo "</td>\n";
echo "<td class='vtable' align='left'>\n";
if (!empty($languages)) {
sort($languages);
echo " <select class='formfld' name='recording_language'>\n";
echo " <option value=''></option>\n";
foreach($languages as $language) {
echo " <option value='".escape($language)."' ".(($language == $recording_language) ? "selected='selected'" : null).">".escape($language)."</option>\n";
}
echo " </select>\n";
}
else {
echo " <input class='formfld' type='text' name='recording_language' maxlength='255' value=\"".escape($recording_language)."\">\n";
}
echo "<br />\n";
echo $text['description-languages']."\n";
echo "</td>\n";
echo "</tr>\n";
}
if ($translate_enabled) {
echo "<tr>\n";
echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
echo " ".$text['label-translate']."\n";
echo "</td>\n";
echo "<td class='vtable' align='left'>\n";
if (substr($_SESSION['theme']['input_toggle_style']['text'], 0, 6) == 'switch') {
echo " <label class='switch'>\n";
echo " <input type='checkbox' id='translate' name='translate' value='true' ".($translate == 'true' ? "checked='checked'" : null).">\n";
echo " <span class='slider'></span>\n";
echo " </label>\n";
}
else {
echo " <select class='formfld' id='translate' name='translate'>\n";
echo " <option value='true' ".($translate == 'true' ? "selected='selected'" : null).">".$text['option-true']."</option>\n";
echo " <option value='false' ".($translate == 'false' ? "selected='selected'" : null).">".$text['option-false']."</option>\n";
echo " </select>\n";
}
echo "<br />\n";
echo $text['description-translate']."\n";
echo "</td>\n";
echo "</tr>\n";
}
echo "<tr>\n";
echo "<td class='vncell' valign='top' align='left' nowrap>\n";
echo " ".$text['label-message']."\n";
echo "</td>\n";
echo "<td class='vtable' align='left'>\n";
echo " <textarea class='formfld' name='recording_message' style='width: 300px; height: 150px;'>".escape($recording_message)."</textarea>\n";
echo "<br />\n";
echo $text['description-message']."\n";
echo "</td>\n";
echo "</tr>\n";
}
echo "<tr>\n";
echo "<td class='vncell' valign='top' align='left' nowrap>\n";
echo " Description\n";
echo " ".$text['label-description']."\n";
echo "</td>\n";
echo "<td class='vtable' align='left'>\n";
echo " <input class='formfld' type='text' name='recording_description' maxlength='255' value=\"".escape($recording_description)."\">\n";
@@ -237,4 +390,4 @@ if (count($_POST) > 0 && empty($_POST["persistformvar"])) {
//include the footer
require_once "resources/footer.php";
?>
?>

View File

@@ -402,12 +402,16 @@
echo "<div class='action_bar' id='action_bar'>\n";
echo " <div class='heading'><b>".$text['title-recordings']." (".$num_rows.")</b></div>\n";
echo " <div class='actions'>\n";
if (permission_exists('recording_add') && $speech_enabled == 'true') {
echo button::create(['type'=>'button','label'=>$text['button-add'],'icon'=>$_SESSION['theme']['button_icon_add'],'id'=>'btn_add','link'=>'recording_edit.php']);
}
if (permission_exists('recording_upload')) {
echo "<form id='form_upload' class='inline' method='post' enctype='multipart/form-data'>\n";
echo "<input name='action' type='hidden' value='upload'>\n";
echo "<input name='type' type='hidden' value='rec'>\n";
echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
echo button::create(['type'=>'button','label'=>$text['button-add'],'icon'=>$_SESSION['theme']['button_icon_add'],'id'=>'btn_add','onclick'=>"$(this).fadeOut(250, function(){ $('span#form_upload').fadeIn(250); document.getElementById('ulfile').click(); });"]);
echo button::create(['type'=>'button','label'=>$text['button-upload'],'icon'=>$_SESSION['theme']['button_icon_add'],'id'=>'btn_add','onclick'=>"$(this).fadeOut(250, function(){ $('span#form_upload').fadeIn(250); document.getElementById('ulfile').click(); });"]);
echo "<span id='form_upload' style='display: none;'>";
echo button::create(['label'=>$text['button-cancel'],'icon'=>$_SESSION['theme']['button_icon_cancel'],'type'=>'button','id'=>'btn_upload_cancel','onclick'=>"$('span#form_upload').fadeOut(250, function(){ document.getElementById('form_upload').reset(); $('#btn_add').fadeIn(250) });"]);
echo "<input type='text' class='txt' style='width: 100px; cursor: pointer;' id='filename' placeholder='Select...' onclick=\"document.getElementById('ulfile').click(); this.blur();\" onfocus='this.blur();'>";
@@ -677,4 +681,4 @@
fclose($fp);
}
?>
?>