Portions created by the Initial Developer are Copyright (C) 2008-2023 the Initial Developer. All Rights Reserved. Contributor(s): Mark J Crane */ //includes files require_once dirname(__DIR__, 2) . "/resources/require.php"; require_once "resources/check_auth.php"; //check permissions if (permission_exists('phrase_add') || permission_exists('phrase_edit')) { //access granted } else { echo "access denied"; exit; } function build_data_array_from_post(settings $settings) { global $domain_uuid, $drop_rows; $phrase_uuid = $_POST['phrase_uuid']; $array = []; $drop_rows = []; $drop_row_count = 0; //load sound files from the switch so we can validate selections $sound_files = (new file)->sounds(); //update the phrase information $array['phrases'][0]['domain_uuid'] = $domain_uuid; $array['phrases'][0]['phrase_uuid'] = $phrase_uuid; $array['phrases'][0]['phrase_name'] = $_POST['phrase_name']; $array['phrases'][0]['phrase_language'] = $_POST['phrase_language']; $array['phrases'][0]['phrase_enabled'] = $_POST['phrase_enabled']; $array['phrases'][0]['phrase_description'] = $_POST['phrase_description']; //recording_files are: // 'recording_uuid' => 'recording.wav' // OR // 'recording_uuid' => '${lua streamfile.lua ' . base64_data .'}' $recording_files = phrases::get_all_domain_recordings($settings); // // Create two arrays - one for rows to delete and one for new/updated rows // for ($i = 0; $i < count($_POST['phrase_detail_function']); $i++) { //check for function to perform $phrase_detail_function = $_POST['phrase_detail_function'][$i]; $phrase_detail_data = null; $recording_uuid_or_file = ''; $phrase_detail_uuid = ''; //check for the empty rows to delete -- 0,false,null is valid if (strlen($_POST['phrase_detail_data'][$i]) === 0 && !empty($_POST['phrase_detail_uuid'][$i]) && empty($_POST['slider'][$i]) && empty($_POST['phrase_detail_text'][$i])) { $drop_rows['phrase_details'][$drop_row_count++]['phrase_detail_uuid'] = $_POST['phrase_detail_uuid'][$i]; continue; } switch ($phrase_detail_function) { case 'play-file': //only save rows with data if (!empty($_POST['phrase_detail_data'][$i])) { $recording_uuid_or_file = $_POST['phrase_detail_data'][$i]; //check for valid recordings and files if (is_uuid($recording_uuid_or_file)) { //recording UUID $phrase_detail_data = $recording_files[$recording_uuid_or_file]; } else { //not a recording so must be valid path inside the switch recording files if (in_array($recording_uuid_or_file, $sound_files)) { //valid switch audio file $phrase_detail_data = $recording_uuid_or_file; } else { //ignore an invalid audio file continue(2); } } //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; } } break; case 'pause': //check for value $phrase_detail_data = $_POST['slider'][$i]; break; case 'execute': //check for the empty rows to delete if (empty($_POST['phrase_detail_text'][$i]) && !empty($_POST['phrase_detail_uuid'][$i])) { $drop_rows['phrase_details'][$drop_row_count++]['phrase_detail_uuid'] = $_POST['phrase_detail_uuid'][$i]; continue(2); } $phrase_detail_data = $_POST['phrase_detail_text'][$i]; break; } $_POST['phrase_detail_tag'] = 'action'; // default, for now $_POST['phrase_detail_group'] = "0"; // one group, for now if ($phrase_detail_data !== null) { if (!empty($_POST['phrase_detail_uuid'][$i])) { //update existing records in the database $phrase_detail_uuid = $_POST['phrase_detail_uuid'][$i]; } else { //new record $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'] = $phrase_detail_function; $array['phrase_details'][$i]['phrase_detail_data'] = $phrase_detail_data; //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']; } } return $array; } //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(); //ensure we have a database object 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 = ''; $phrase_description = ''; //set the action as an add or an update if (!empty($_REQUEST["id"])) { $action = "update"; $phrase_uuid = $_REQUEST["id"]; } else { $action = "add"; } //get the form value and set to php variables if (count($_POST) > 0) { //process the http post data by submitted action if (!empty($_POST['action']) && is_uuid($_POST['phrase_uuid'])) { $array[0]['checked'] = 'true'; $array[0]['uuid'] = $_POST['phrase_uuid']; switch ($_POST['action']) { case 'delete': if (permission_exists('phrase_delete')) { $obj = new phrases; $obj->delete($array); } break; } header('Location: phrases.php'); exit; } if (permission_exists('phrase_domain')) { $domain_uuid = $_POST["domain_uuid"]; } $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 if (count($_POST) > 0 && empty($_POST["persistformvar"])) { //get the uuid if ($action == "update") { $phrase_uuid = $_POST["phrase_uuid"]; } //validate the token $token = new token; if (!$token->validate($_SERVER['PHP_SELF'])) { message::add($text['message-invalid_token'],'negative'); header('Location: phrases.php'); exit; } //check for all required data $msg = ''; if (empty($phrase_name)) { $msg .= $text['message-required']." ".$text['label-name']."
\n"; } if (empty($phrase_language)) { $msg .= $text['message-required']." ".$text['label-language']."
\n"; } if (!empty($msg) && empty($_POST["persistformvar"])) { require_once "resources/header.php"; require_once "resources/persist_form_var.php"; echo "
\n"; echo "
\n"; echo $msg."
"; echo "
\n"; persistformvar($_POST); echo "
\n"; require_once "resources/footer.php"; return; } //add the phrase if (empty($_POST["persistformvar"]) || $_POST["persistformvar"] != "true") { $message = ''; switch ($action) { case 'add': //redirect when they don't have permission to add a phrase if (!permission_exists('phrase_add')) { header('Location: phrases.php'); exit(); } //set user feedback message to add $message = $text['message-add']; $phrase_uuid = uuid(); //do not break case 'update': //redirect when not adding and don't have permission to edit a phrase if (empty($message)) { if (!permission_exists('phrase_edit')) { header('Location: phrases.php'); exit(); } //set user feedback message to update $message = $text['message-update']; } if (!empty($_POST['phrase_detail_function'])) { $array = build_data_array_from_post($settings); } //execute update/insert $p = permissions::new(); $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); unset($array); } if (count($drop_rows) > 0) { $database->delete($drop_rows); unset($drop_rows); } $p->delete('phrase_detail_add', 'temp'); //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($message); header("Location: phrase_edit.php?id=".$phrase_uuid); exit; } } } //pre-populate the form if (count($_GET)>0 && empty($_POST["persistformvar"])) { $phrase_uuid = $_GET["id"]; $sql = "select * from v_phrases "; $sql .= "where ( "; $sql .= " domain_uuid = :domain_uuid or "; $sql .= " domain_uuid is null "; $sql .= ") "; $sql .= "and phrase_uuid = :phrase_uuid "; $parameters['domain_uuid'] = $domain_uuid; $parameters['phrase_uuid'] = $phrase_uuid; $row = $database->select($sql, $parameters, 'row'); if (is_array($row) && @sizeof($row) != 0) { $phrase_name = $row["phrase_name"]; $phrase_language = $row["phrase_language"]; $phrase_enabled = $row["phrase_enabled"]; $phrase_description = $row["phrase_description"]; } unset($sql, $parameters, $row); } //set the defaults if (empty($phrase_enabled)) { $phrase_enabled = 'true'; } //get the phrase details if (!empty($phrase_uuid)) { $sql = "select * from v_phrase_details "; $sql .= "where domain_uuid = :domain_uuid "; $sql .= "and phrase_uuid = :phrase_uuid "; $sql .= "order by phrase_detail_order asc "; $parameters['domain_uuid'] = $_SESSION['domain_uuid']; $parameters['phrase_uuid'] = $phrase_uuid; $phrase_details = $database->select($sql, $parameters, 'all'); unset($sql, $parameters); } //get the recording names from the database. $sql = "select recording_uuid, recording_name, recording_filename, domain_uuid from v_recordings "; $sql .= "where domain_uuid = :domain_uuid "; $sql .= "order by recording_name asc "; $parameters['domain_uuid'] = $_SESSION['domain_uuid']; $recordings = $database->select($sql, $parameters, 'all'); unset($sql, $parameters); //get the switch sound files $file = new file; $sound_files = $file->sounds(); //create token $object = new token; $token = $object->create($_SERVER['PHP_SELF']); //include the header if ($action == 'add') { $document['title'] = $text['title-add_phrase']; } if ($action == 'update') { $document['title'] = $text['title-edit_phrase']; } require_once "resources/header.php"; //javascript constants for use in the selection option group echo "\n"; //javascript to control action form input using drag and drop echo "\n"; //show the content echo "
\n"; echo "
\n"; echo "
"; if ($action == "add") { echo "".$text['title-add_phrase'].""; } if ($action == "update") { echo "".$text['title-edit_phrase'].""; } echo "
\n"; echo "
\n"; echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','link'=>'phrases.php']); if ($action == "update" && permission_exists('phrase_delete')) { echo button::create(['type'=>'button','label'=>$text['button-delete'],'icon'=>$_SESSION['theme']['button_icon_delete'],'name'=>'btn_delete','style'=>'margin-left: 15px;','onclick'=>"modal_open('modal-delete','btn_delete');"]); } echo button::create(['type'=>'submit','onclick'=>'submit_phrase()','label'=>$text['button-save'],'icon'=>$_SESSION['theme']['button_icon_save'],'id'=>'btn_save','style'=>'margin-left: 15px;']); echo "
\n"; echo "
\n"; echo "
\n"; if ($action == "update" && permission_exists('phrase_delete')) { echo modal::create(['id'=>'modal-delete','type'=>'delete','actions'=>button::create(['type'=>'submit','label'=>$text['button-continue'],'icon'=>'check','id'=>'btn_delete','style'=>'float: right; margin-left: 15px;','collapse'=>'never','name'=>'action','value'=>'delete','onclick'=>"modal_close();"])]); } echo "
\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; //structure row echo ""; echo ""; echo ""; echo ""; if (permission_exists('phrase_domain')) { echo "\n"; echo "\n"; echo "\n"; echo "\n"; } echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "
\n"; echo " ".$text['label-name']."\n"; echo "\n"; echo " \n"; echo "
\n"; echo " ".$text['description-name']."\n"; echo "
\n"; echo " ".$text['label-language']."\n"; echo "\n"; echo " \n"; echo "
\n"; echo " ".$text['description-language']."\n"; echo "
".$text['label-structure'].""; //style for dragging rows echo " "; //structure table echo " \n"; //headings echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; //draggable rows are initially empty echo "\n"; echo ""; //show loading echo "\n"; //cloning row and buttons created outside of 'structure' table body echo ""; echo "\n"; echo " "; echo " \n"; echo " \n"; echo "\n"; echo ""; echo ""; echo ""; echo ""; echo ""; echo "\n"; echo "
" . ($text['label-order'] ?? 'Order') . "".$text['label-action']."".($text['label-recording'] ?? 'Recording')."
 
Loading...
 
 
"; echo button::create(['type'=>'button','icon'=>$_SESSION['theme']['button_icon_add'], 'label' => $text['label-add'], 'onclick' => 'add_row()']); echo button::create(['type'=>'button','icon'=>'fa-solid fa-minus', 'label' => $text['label-delete'], 'onclick' => 'remove_row()']); echo "
 
\n"; echo " ".$text['description-structure']."\n"; echo "
\n"; echo "
\n"; echo " ".$text['label-domain']."\n"; echo "\n"; echo " \n"; echo "
\n"; echo " ".$text['label-enabled']."\n"; echo "\n"; if (substr($_SESSION['theme']['input_toggle_style']['text'], 0, 6) == 'switch') { echo " \n"; } else { echo " \n"; } echo "
\n"; echo $text['description-enabled']."\n"; echo "
\n"; echo " ".$text['label-description']."\n"; echo "\n"; echo " \n"; echo "
"; echo "
\n"; echo "

"; if ($action == "update") { echo " \n"; } echo "\n"; echo "
"; //include the footer require_once "resources/footer.php";