34132-vm/cv_editor.php
Flatlogic Bot 7a4a20350d MagiCV
2025-09-17 12:13:33 +00:00

178 lines
7.5 KiB
PHP

<?php
session_start();
require_once __DIR__ . '/db/config.php';
$is_guest = !isset($_SESSION['user_id']);
// If a guest tries to edit a CV, redirect them to login.
if ($is_guest && isset($_GET['id'])) {
$_SESSION['error_message'] = 'You must be logged in to edit a CV.';
header('Location: /login.php');
exit;
}
$pdo = db();
$user_role = $_SESSION['user_role'] ?? 'free';
// Fetch templates based on user role
if ($user_role === 'free') {
$templates_stmt = $pdo->query('SELECT id, name, is_premium FROM templates WHERE is_premium = 0');
} else {
// Pro and Admin users can see all templates
$templates_stmt = $pdo->query('SELECT id, name, is_premium FROM templates');
}
$templates = $templates_stmt->fetchAll(PDO::FETCH_ASSOC);
$cv_data = [
'id' => null,
'title' => 'My CV',
'content' => json_encode([
'personal_info' => ['name' => '', 'email' => '', 'phone' => '', 'linkedin' => '' ],
'experience' => [],
'education' => [],
'skills' => ''
]),
'template_id' => null
];
if (isset($_GET['id'])) {
$stmt = $pdo->prepare('SELECT * FROM cvs WHERE id = :id AND user_id = :user_id');
$stmt->execute(['id' => $_GET['id'], 'user_id' => $_SESSION['user_id']]);
$cv = $stmt->fetch(PDO::FETCH_ASSOC);
if ($cv) {
$cv_data = $cv;
}
}
$content = json_decode($cv_data['content'], true);
require_once __DIR__ . '/includes/header.php';
?>
<div class="container">
<h2><?php echo $cv_data['id'] ? 'Edit CV' : 'Create New CV'; ?></h2>
<form action="/save_cv.php" method="post" id="cv-form">
<input type="hidden" name="cv_id" value="<?php echo $cv_data['id']; ?>">
<div class="form-group">
<label for="title">CV Title</label>
<input type="text" name="title" id="title" value="<?php echo htmlspecialchars($cv_data['title']); ?>" required>
</div>
<div class="form-group">
<label for="template_id">Template</label>
<select name="template_id" id="template_id">
<?php foreach ($templates as $template): ?>
<option value="<?php echo $template['id']; ?>" <?php echo ($cv_data['template_id'] == $template['id']) ? 'selected' : ''; ?>>
<?php
echo htmlspecialchars($template['name']);
if ($template['is_premium']) {
echo ' (Pro)';
}
?>
</option>
<?php endforeach; ?>
</select>
</div>
<fieldset>
<legend>Personal Information</legend>
<div class="form-group">
<label for="name">Full Name</label>
<input type="text" name="personal_info[name]" id="name" value="<?php echo htmlspecialchars($content['personal_info']['name'] ?? ''); ?>">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" name="personal_info[email]" id="email" value="<?php echo htmlspecialchars($content['personal_info']['email'] ?? ''); ?>">
</div>
<div class="form-group">
<label for="phone">Phone</label>
<input type="text" name="personal_info[phone]" id="phone" value="<?php echo htmlspecialchars($content['personal_info']['phone'] ?? ''); ?>">
</div>
<div class="form-group">
<label for="linkedin">LinkedIn Profile</label>
<input type="text" name="personal_info[linkedin]" id="linkedin" value="<?php echo htmlspecialchars($content['personal_info']['linkedin'] ?? ''); ?>">
</div>
</fieldset>
<fieldset>
<legend>Work Experience</legend>
<div id="experience-container">
<?php if (!empty($content['experience'])):
foreach ($content['experience'] as $index => $exp):
?>
<div class="experience-item">
<input type="text" name="experience[<?php echo $index; ?>][title]" placeholder="Job Title" value="<?php echo htmlspecialchars($exp['title'] ?? ''); ?>">
<input type="text" name="experience[<?php echo $index; ?>][company]" placeholder="Company" value="<?php echo htmlspecialchars($exp['company'] ?? ''); ?>">
<textarea name="experience[<?php echo $index; ?>][description]" placeholder="Description"><?php echo htmlspecialchars($exp['description'] ?? ''); ?></textarea>
</div>
<?php
endforeach;
endif;
?>
</div>
<button type="button" id="add-experience">Add Experience</button>
</fieldset>
<fieldset>
<legend>Education</legend>
<div id="education-container">
<?php if (!empty($content['education'])):
foreach ($content['education'] as $index => $edu):
?>
<div class="education-item">
<input type="text" name="education[<?php echo $index; ?>][degree]" placeholder="Degree" value="<?php echo htmlspecialchars($edu['degree'] ?? ''); ?>">
<input type="text" name="education[<?php echo $index; ?>][institution]" placeholder="Institution" value="<?php echo htmlspecialchars($edu['institution'] ?? ''); ?>">
</div>
<?php
endforeach;
endif;
?>
</div>
<button type="button" id="add-education">Add Education</button>
</fieldset>
<fieldset>
<legend>Skills</legend>
<div class="form-group">
<textarea name="skills" id="skills" placeholder="e.g., PHP, JavaScript, Project Management"><?php echo htmlspecialchars($content['skills'] ?? ''); ?></textarea>
</div>
</fieldset>
<button type="submit" class="button">Save CV</button>
</form>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
let experienceIndex = <?php echo count($content['experience'] ?? []); ?>;
document.getElementById('add-experience').addEventListener('click', function() {
const container = document.getElementById('experience-container');
const newItem = document.createElement('div');
newItem.className = 'experience-item';
newItem.innerHTML = `
<input type="text" name="experience[${experienceIndex}][title]" placeholder="Job Title">
<input type="text" name="experience[${experienceIndex}][company]" placeholder="Company">
<textarea name="experience[${experienceIndex}][description]" placeholder="Description"></textarea>
`;
container.appendChild(newItem);
experienceIndex++;
});
let educationIndex = <?php echo count($content['education'] ?? []); ?>;
document.getElementById('add-education').addEventListener('click', function() {
const container = document.getElementById('education-container');
const newItem = document.createElement('div');
newItem.className = 'education-item';
newItem.innerHTML = `
<input type="text" name="education[${educationIndex}][degree]" placeholder="Degree">
<input type="text" name="education[${educationIndex}][institution]" placeholder="Institution">
`;
container.appendChild(newItem);
educationIndex++;
});
});
</script>
<?php
require_once __DIR__ . '/includes/footer.php';
?>