35591-vm/history.php
Flatlogic Bot 062a594521 1
2025-11-09 09:51:13 +00:00

106 lines
4.6 KiB
PHP

<?php
require_once 'includes/header.php';
require_once __DIR__ . '/db/config.php';
require_once __DIR__ . '/ai/LocalAIApi.php';
// Redirect to login if not logged in
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit();
}
// Helper function for translation
function translate_from_english($text, $target_lang) {
if ($target_lang === 'en') {
return $text;
}
// Simulate translation
$prompt = "Translate the following text from English to '{$target_lang}'. Only return the translated text, with no extra commentary:\n\n{$text}";
$response = LocalAIApi::createResponse([
'input' => [
['role' => 'system', 'content' => 'You are a translation assistant.'],
['role' => 'user', 'content' => $prompt],
],
]);
if (!empty($response['success'])) {
$decoded = LocalAIApi::decodeJsonFromResponse($response);
return $decoded['choices'][0]['message']['content'] ?? $text;
}
return $text; // Fallback to original text on error
}
$user_id = $_SESSION['user_id'];
// Fetch history from the database
$generations = [];
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT * FROM generations WHERE user_id = ? ORDER BY created_at DESC");
$stmt->execute([$user_id]);
$generations = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
// Optional: handle error
error_log("DB Error: " . $e->getMessage());
}
?>
<main class="container">
<h1 class="page-title"><?php echo t('history_title'); ?></h1>
<?php if (empty($generations)): ?>
<div class="card text-center">
<div class="card-body">
<p><?php echo t('history_empty'); ?></p>
<a href="index.php#generator" class="btn btn-primary"><?php echo t('history_generate_now'); ?></a>
</div>
</div>
<?php else: ?>
<div class="history-list">
<?php foreach ($generations as $index => $generation): ?>
<?php
$output_data = json_decode($generation['description'], true);
if (!$output_data) continue; // Skip if JSON is invalid
// Translate the fields for display
$title = translate_from_english(htmlspecialchars($output_data['title'] ?? ''), $lang);
$short_desc = translate_from_english(htmlspecialchars($output_data['short_description'] ?? ''), $lang);
$description = nl2br(translate_from_english(htmlspecialchars($output_data['description'] ?? ''), $lang));
$measurements = translate_from_english(htmlspecialchars($output_data['measurements'] ?? ''), $lang);
$hashtags = translate_from_english(htmlspecialchars($output_data['hashtags'] ?? ''), $lang);
?>
<div class="card history-item">
<div class="card-header">
<span><?php echo t('generation_date'); ?>: <?php echo date('F j, Y, g:i a', strtotime($generation['created_at'])); ?></span>
<button class="btn btn-sm btn-secondary copy-btn" data-clipboard-target="#history-content-<?php echo $index; ?>"><i class="ph ph-copy"></i> <?php echo t('copy_to_clipboard'); ?></button>
</div>
<div class="card-body" id="history-content-<?php echo $index; ?>">
<div class="result-item">
<label><?php echo t('output_title'); ?></label>
<p><?php echo $title; ?></p>
</div>
<div class="result-item">
<label><?php echo t('output_short_description'); ?></label>
<p><?php echo $short_desc; ?></p>
</div>
<div class="result-item">
<label><?php echo t('output_description'); ?></label>
<p><?php echo $description; ?></p>
</div>
<div class="result-item">
<label><?php echo t('output_measurements'); ?></label>
<p><?php echo $measurements; ?></p>
</div>
<div class="result-item">
<label><?php echo t('output_hashtags'); ?></label>
<p><?php echo $hashtags; ?></p>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</main>
<?php include __DIR__ . '/includes/footer.php'; ?>