162 lines
5.5 KiB
PHP
162 lines
5.5 KiB
PHP
<?php
|
|
|
|
|
|
function EmitHeaderForm() {
|
|
global $HeadersPath, $HeaderFormFields;
|
|
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "", TR_ENTRY);
|
|
$args = [
|
|
'FormID' => 'NewHeaderForm', // must be unique
|
|
'ActionValue' => 'HeaderEdit',
|
|
'Field1Label' => 'New Header',
|
|
'Input1Name' => 'HeaderText',
|
|
'Field2Label' => 'Edit Header',
|
|
'Select1Name' => 'ExistingHeader',
|
|
'DocPath' => $HeadersPath,
|
|
'FormFields' => $HeaderFormFields,
|
|
'dbKey' => 'HeaderText',
|
|
];
|
|
EmitAdminFormType2($args);
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "", TR_EXIT);
|
|
}
|
|
|
|
|
|
// X Action=HeaderEdit
|
|
// X HeaderText=<text>
|
|
// # SubHeaderText=<text>
|
|
//
|
|
function ShowHeaderForm() {
|
|
global $HeadersPath, $HeaderFormFields;
|
|
global $ckEditor_FQFN, $allowedTypes, $UploadPath;
|
|
global $CleanRequestData, $CleanFiles;
|
|
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "", TR_ENTRY);
|
|
$actDelete = 'DeleteHeaderItem';
|
|
$FormData = array();
|
|
$Action = $CleanRequestData['Action'] ?? '';
|
|
$HeaderName = '';
|
|
// Form Submission could be new Header or edit existing
|
|
$formSubmission = false;
|
|
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, print_r($CleanRequestData, true));
|
|
if (isset($CleanRequestData['HeaderText']) && $CleanRequestData['HeaderText'] != "") {
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, $CleanRequestData['HeaderText']);
|
|
$HeaderName = $ProgramFolder = $CleanRequestData['HeaderText'];
|
|
} else {
|
|
$ProgramFolder = $CleanRequestData['ExistingHeader'] ?? '';
|
|
}
|
|
$HeaderPath = $HeadersPath .'/'. MakeValidFolderName($ProgramFolder);
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "HeaderPath: '$HeaderPath'");
|
|
if (!is_dir($HeaderPath)) {
|
|
if (mkdir($HeaderPath, 0777, true)) {
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "mkdir '$HeaderPath'");
|
|
} else {
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "Failed to mkdir '$HeaderPath'");
|
|
}
|
|
}
|
|
|
|
if ($Action == $actDelete) {
|
|
$item = $CleanRequestData['Item'] ?? '';
|
|
$success = ReadTableData($HeaderPath, $HeaderFormFields, $evData);
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, $success);
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, print_r($evData, true));
|
|
$fqfn = $evData[$item];
|
|
$targetFile = $HeaderPath . '/' . $fqfn;
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "unlink '$targetFile'");
|
|
unlink($targetFile);
|
|
echo "ok";
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "", TR_EXIT);
|
|
exit();
|
|
}
|
|
if (($Action == 'HeaderEdit') && ($HeaderName != '')) {
|
|
// Form Submission could be new header or edit existing
|
|
|
|
// check FILES and then form fields
|
|
foreach ($CleanFiles as $fileKey => $fileInfo) {
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "Check ". print_r($fileKey, true));
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "Check ". print_r($fileInfo, true));
|
|
if (in_array($fileInfo['type'], array_keys($allowedTypes))) {
|
|
$basename = basename($fileInfo['name']);
|
|
$filename = $fileKey . $allowedTypes[$fileInfo['type']];
|
|
if ($HeaderFormFields[$fileKey]['FieldType'] == 'pix') {
|
|
$targetFile = $HeaderPath . '/' . $filename;
|
|
$un = $HeaderPath . '/' . $fileKey . ".*";
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "unlink $un");
|
|
deleteFilesWithWildcard($HeaderPath, $fileKey . ".*");
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "target $targetFile");
|
|
if (!is_uploaded_file($fileInfo['tmp_name']) || !@copy($fileInfo['tmp_name'], $targetFile)) {
|
|
$err = error_get_last();
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, $err);
|
|
} else {
|
|
$FormData[$fileKey] = $filename;
|
|
$formSubmission = true;
|
|
}
|
|
@unlink($fileInfo['tmp_name']);
|
|
} else {
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "$basename not legitimate for a pix upload");
|
|
}
|
|
} else {
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "Nothing to upload");
|
|
}
|
|
}
|
|
// Form items
|
|
foreach ($CleanRequestData as $key => $value) {
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "check $key");
|
|
if (isset($HeaderFormFields[$key])) {
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "$key => $value");
|
|
$FormData[$key] = $value;
|
|
$formSubmission = true;
|
|
}
|
|
}
|
|
}
|
|
if ($formSubmission) {
|
|
SaveTableData($HeaderPath, $HeaderFormFields, $FormData);
|
|
MoveFiles($UploadPath, $HeaderPath);
|
|
}
|
|
|
|
// Read the event file if it exists
|
|
$success = ReadTableData($HeaderPath, $HeaderFormFields, $FormData);
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, ($success) ? 'ReadTableData() success' : 'ReadTableData() fail');
|
|
EmitHTMLStart("Header Editor");
|
|
EmitBodyStart();
|
|
EmitPageHeader("Header Editor");
|
|
?>
|
|
<form action='' method='post' id='HeaderForm' enctype='multipart/form-data' >
|
|
<input type='hidden' name='Action' value='HeaderSubmit' />
|
|
<?php
|
|
$CK_EditorFieldIDs = EmitForm($HeaderFormFields, $FormData, $HeaderPath, $HeaderName);
|
|
?>
|
|
</form>
|
|
<script src='./inc/UploadPreview.js'></script>
|
|
<script>
|
|
async function DeletePix(event, item) {
|
|
if (!confirm("Delete the " + item + " image?")) {
|
|
return;
|
|
}
|
|
let currentURL = window.location.href;
|
|
currentURL = currentURL.endsWith('?') ? currentURL.slice(0,-1) : currentURL;
|
|
currentURL += "?Action=DeleteHeaderItem&HeaderItem=" + event + "&Item=" + item;
|
|
console.log("currentURL: " + currentURL);
|
|
fetch(currentURL, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
},
|
|
})
|
|
.then(response => {
|
|
console.log('Success:');
|
|
o = document.getElementById(item + "Preview");
|
|
o.src = '';
|
|
o.style.display = 'none';
|
|
})
|
|
.catch(error => console.error('Error:', error));
|
|
}
|
|
</script>
|
|
<?php
|
|
EmitBodyEnd();
|
|
EmitHTMLEnd();
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "", TR_EXIT);
|
|
}
|
|
|
|
?>
|