Files
WTS-Public/inc/Editor_Event.php
2026-10-27 20:22:37 -05:00

245 lines
8.8 KiB
PHP

<?php
function EmitEventForm() {
global $EventsPath, $EventFormFields;
global $PlacesPath, $PlaceFormFields;
global $ProgChairPath, $ProgChairFormFields;
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "", TR_ENTRY);
$events = GetDocMap($EventsPath, $EventFormFields, 'ProgramDate');
?>
<blockquote>
<form method='post'>
<input type='hidden' name='Action' value='EventEdit' />
<table border='<?php echo TBL_BORDER; ?>' width='100%'>
<tr>
<td width='20%' nowrap>New Event</td>
<td><input type='date' name='NewEventDate' value='' /></td>
<td nowrap>Pick Place</td>
<td><select name='DinnerPlace' >
<?php printf("\t\t\t\t<option value='%s'>%s</option>\n", '', ''); ?>
<?php
$map = GetDocMap($PlacesPath, $PlaceFormFields, 'PlaceName');
TraceProgram(__FILE__, __FUNCTION__, __LINE__, print_r($map, true));
foreach ($map as $key => $value) {
printf("\t\t\t\t<option value='%s'>%s</option>\n", $key, $value);
}
?> </select>
</td>
<td nowrap>Pick Chair</td>
<td><select name='ProgramChair' >
<?php printf("\t\t\t\t<option value='%s'>%s</option>\n", '', ''); ?>
<?php
$map = GetDocMap($ProgChairPath, $ProgChairFormFields, 'ProgramChair');
TraceProgram(__FILE__, __FUNCTION__, __LINE__, print_r($map, true));
foreach ($map as $key => $value) {
printf("\t\t\t\t<option value='%s'>%s</option>\n", $key, $value);
}
?> </select>
</td>
</tr>
<tr>
<td>Edit Event</td>
<td colspan='5'><select name='ProgramDate' >
<?php printf("\t\t\t\t<option value='%s'>%s</option>\n", '', ''); ?>
<?php
foreach (array_keys($events) as $event) {
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "[$event]");
$mmddyyyy = DateToMDY($event);
printf("\t\t\t\t<option value='%s'>%s</option>\n", $event, $mmddyyyy);
}
?> </select>
</td>
</tr>
<tr>
<td></td>
<td colspan='5'><input type='submit' />&nbsp;&nbsp;&nbsp;<input type='reset' /></td>
</tr>
</table>
</form>
</blockquote>
<?php
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "", TR_EXIT);
}
// X Action=EventNew
// X NewEventDate=YYYY-MM-DD // Could be new or chose an existing one
// X ProgramDate=YYYY-MM-DD // Picked an existing one for edit
// X Action=DeleteEventItem
// X ProgramDate=YYYY-MM-DD
// X Item=item
//
function ShowEventForm() {
global $EventsPath, $EventFormFields;
global $ckEditor_FQFN, $allowedTypes, $UploadPath;
global $CleanRequestData, $CleanFiles;
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "", TR_ENTRY);
// Form Submission could be new or edit existing
$formSubmission = false;
if (isset($CleanRequestData['NewEventDate']) && ($CleanRequestData['NewEventDate'] != '')) {
$ProgramDate = $CleanRequestData['NewEventDate'];
} else {
$ProgramDate = $CleanRequestData['ProgramDate'] ?? '';
}
$FormData = array();
$FormData['ProgramDate'] = $ProgramDate;
TraceProgram(__FILE__, __FUNCTION__, __LINE__, $ProgramDate);
$EventPath = $EventsPath .'/'. $ProgramDate;
if (is_dir($EventPath)) {
// Already there, so we are just editing
$success = ReadTableData($EventPath, $EventFormFields, $evData);
if ($success) {
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "Data read '$EventPath'");
// continue to show the form
} else {
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "Failed to read data at '$EventPath'");
// continue to show the form
}
} else {
// Not there, this is creating a new event
if (mkdir($EventPath, 0777, true)) {
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "Made the folder '$EventPath'");
} else {
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "Failed to mkdir '$EventPath'");
// Jump to a transient page to show an error code
$url = sprintf("Location: %s?ErrorNotice=%s&ErrorCode=%s", $_SERVER['PHP_SELF'], __FILE__, __LINE__);
header("$url");
exit();
}
}
$Action = $CleanRequestData['Action'] ?? '';
switch ($Action) {
case 'DeleteEventItem':
$item = $CleanRequestData['Item'] ?? '';
//$success = ReadTableData($EventPath, $EventFormFields, $evData);
//TraceProgram(__FILE__, __FUNCTION__, __LINE__, "ReadTableData: $success");
//TraceProgram(__FILE__, __FUNCTION__, __LINE__, print_r($evData, true));
$fqfn = $evData[$item];
$targetFile = $EventPath . '/' . $fqfn;
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "unlink '$targetFile'");
unlink($targetFile);
echo "ok";
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "", TR_EXIT);
exit();
break;
case 'EventNew':
case 'EventEdit':
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "New or Submit.");
break;
default:
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "no Action.");
// Jump to a transient page to show an error code
ShowTransientPage(__FILE__, __LINE__);
exit();
break;
}
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "CleanRequestData: " . print_r($CleanRequestData, true));
TraceProgram(__FILE__, __FUNCTION__, __LINE__, $ProgramDate);
if ($ProgramDate == '') {
header("Location: " . htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8'));
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "", TR_EXIT);
// Jump to a transient page to show an error code
exit(); // Stop script execution after redirect
}
$CleanRequestData['ProgramDate'] = $ProgramDate;
TraceProgram(__FILE__, __FUNCTION__, __LINE__, $ProgramDate);
if ($Action == 'DeleteEventItem') {
echo "<font color='red'>DeleteEventItem is not yet supported</font><br/>\n";
}
if ($Action == 'EventEdit') {
// Form Submission check FILES and then EventFormFields
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "");
$success = ReadTableData($EventPath, $EventFormFields, $evData);
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "ReadTableData: $success");
TraceProgram(__FILE__, __FUNCTION__, __LINE__, print_r($evData, true));
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 ($EventFormFields[$fileKey]['FieldType'] == 'pix') {
$targetFile = $EventPath . '/' . $filename;
$un = $EventPath . '/' . $fileKey . ".*";
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "unlink $un");
deleteFilesWithWildcard($EventPath, $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");
}
}
TraceProgram(__FILE__, __FUNCTION__, __LINE__, print_r($CleanRequestData, true));
foreach ($CleanRequestData as $key => $value) {
//TraceProgram(__FILE__, __FUNCTION__, __LINE__, "Request[$key] = '$value'");
if (isset($EventFormFields[$key])) {
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "Request[$key] = '$value'");
$FormData[$key] = $value;
$formSubmission = true;
}
}
}
if ($formSubmission && $ProgramDate != '') {
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "");
SaveTableData($EventPath, $EventFormFields, $FormData);
MoveFiles($UploadPath, $EventPath);
}
$success = ReadTableData($EventPath, $EventFormFields, $FormData);
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "ReadTableData: " . ($success) ? 'success' : 'fail');
EmitHTMLStart("Event Editor");
EmitBodyStart();
EmitPageHeader("Event Editor");
?>
<form action='' method='post' id='EventForm' enctype='multipart/form-data' >
<input type='hidden' name='Action' value='EventEdit' />
<?php
$CK_EditorFieldIDs = EmitForm($EventFormFields, $FormData, $EventPath, $ProgramDate, true);
?>
</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=DeleteEventItem&ProgramDate=" + 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);
}
?>