<?php
include_once 'bu-common.php';
/**
* This page provides the UI for a backup item for both new items and editing items for both
* database style and files. So there are 4 combinations:
*
* $bDbStyle = true, $db_id = 0: Create new database backup item
* $bDbStyle = false, $db_id = 0: Create new files backup item
* $bDbStyle = true, $db_id != 0: Edit an existing database backup item
* $bDbStyle = false, $db_id != 0: Edit an existing files backup item
*
*/
function getbu_cf_content( $bDbStyle = false, $db_id = 0 ){
$id_line='';
$header_cont='';
$lab1_cont = '';
$exfiles_cont = '';
$btn_new_bu = '';
$item_data = null;
$bu_name_value = '';
$bu_src_value = '';
$bu_dest_value = '';
$bu_ex_files = '';
$lastRunTime = '';
$bu_error_list = '';
if($db_id>0){
$id_line = "<div>ID</div><div>$db_id</div>";
$item_data = get_bu_item( $db_id ); // [ BUName, Dir_Src, Dir_Dest, Files_Ex, Bu_Type, BuRunning, BuError, LastRunDt ]
$bu_name_value = "value='{$item_data[0]}'";
$bu_src_value = "value='{$item_data[1]}'";
$bu_dest_value = "value='{$item_data[2]}'";
$bu_ex_files = $item_data[3];
$bu_error_list = str_replace( "\n", '<br>', $item_data[6]);
$bu_error_list = "<div>Errors</div><div>$bu_error_list</div>";
$lastRunTime = "<div>Last Run (UTC)</div><div>{$item_data[7]}</div>";
}
if($bDbStyle){
if($db_id>0){
$header_cont = '<div class="section_header" >Edit Database Backup Item</div>';
$btn_new_bu = "btn_edit_bu_db','$db_id";
}else{
$header_cont = '<div class="section_header" >Create New Database Backup Item</div>';
$btn_new_bu = 'btn_new_bu_db';
}
$lab1_cont = '<div>Database name</div>';
}else{
if($db_id>0){
$header_cont = '<div class="section_header" >Edit File Backup Item</div>';
$btn_new_bu = "btn_edit_bu_file','$db_id";
}else{
$header_cont = '<div class="section_header" >Create New File Backup Item</div>';
$btn_new_bu = 'btn_new_bu_file';
}
$lab1_cont = '<div>Source File/Dir</div>';
$exfiles_cont = "<div>Exclude Files</div><div><textarea id=\"new_bu_exf\" rows=\"4\" >$bu_ex_files</textarea></div>";
}
$content = <<<'EOD'
$header_cont
<div class="content_section_text" style="min-height: 350px;" >
<div class="two-col-panel">
$id_line
<div>Backup Name</div>
<div><input id="new_bu_name" type="text" $bu_name_value > (optional)</div>
$lab1_cont
<div><input id="new_bu_source" type="text" $bu_src_value ></div>
<div>Destination Dir</div>
<div><input id="new_bu_dest" type="text" $bu_dest_value ></div>
$exfiles_cont
<div></div>
<div>
<button class="btn_exec" onclick="btn_clk_nav(this, 'btn_run_bu')" >Test</button>
<button class="btn_exec" onclick="btn_clk_nav(this, '$btn_new_bu')" >Save</button>
<button class="btn_exec" onclick="btn_clk_nav(this, 'btn_cancel_new_bu')" >Cancel</button>
</div>
<div>Test output</div>
<div><textarea id="ta_testoutput" rows="8" readonly ></textarea></div>
$lastRunTime
$bu_error_list
</div>
</div>
EOD;
$content = str_replace('$bu_error_list', $bu_error_list, $content);
$content = str_replace('$lastRunTime', $lastRunTime, $content);
$content = str_replace('btn_run_bu\'', "btn_run_bu', $db_id", $content);
$content = str_replace('$bu_name_value', $bu_name_value, $content);
$content = str_replace('$bu_src_value', $bu_src_value, $content);
$content = str_replace('$bu_dest_value', $bu_dest_value, $content);
$content = str_replace('$id_line', $id_line, $content);
$content = str_replace('$btn_new_bu', $btn_new_bu, $content);
$content = str_replace('$exfiles_cont', $exfiles_cont, $content);
$content = str_replace('$lab1_cont', $lab1_cont, $content);
$content = str_replace('$header_cont', $header_cont, $content);
return $content;
}
/**
* Both inserts as well as edits a single backup item.
*/
function save_bu_item( $postVars, $bu_id=0 ){
//error_log( "save_bu_item id = $bu_id, new_bu_dest = {$postVars['new_bu_dest']}" );
$db = new SQLite3('bin/data.db' );
$db_st = null;
if( $postVars['new_bu_is_dbType'] ){
if($bu_id==0){
$db_st = $db->prepare('INSERT INTO BULIST ( BUName, Dir_Src, Dir_Dest, BuType ) values( :bun, :bus, :bud, 1 )');
}else{
$db_st = $db->prepare('update BULIST set BUName=:bun, Dir_Src=:bus, Dir_Dest=:bud where BUID=:buid');
}
}else{
if($bu_id==0){
$db_st = $db->prepare('INSERT INTO BULIST ( BUName, Dir_Src, Dir_Dest, Files_Ex, BuType ) values( :bun, :bus, :bud, :buex, 0 )');
}else{
$db_st = $db->prepare('update BULIST set BUName=:bun, Dir_Src=:bus, Dir_Dest=:bud, Files_Ex=:buex where BUID=:buid');
}
}
if($bu_id>0){
$db_st->bindValue(':buid', $bu_id );
}
$db_st->bindValue(':bun', $postVars['new_bu_name'] );
$db_st->bindValue(':bus', $postVars['new_bu_source'] );
$db_st->bindValue(':bud', $postVars['new_bu_dest'] );
if( !$postVars['new_bu_is_dbType'] ){
$db_st->bindValue(':buex', $postVars['new_bu_exf'] );
}
$db_st->execute();
if( $db->lastErrorCode()!==0){
return $db->lastErrorCode();
}
}
?>