160 lines
7.5 KiB
PHP
160 lines
7.5 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
require_once __DIR__ . '/session.php';
|
||
require_once __DIR__ . '/db.php';
|
||
|
||
// Redirection si l'utilisateur n'est pas authentifié.
|
||
if (empty($_SESSION['user_id'])) {
|
||
header('Location: ../index.php');
|
||
exit;
|
||
}
|
||
|
||
$pageStylesheets = ['/css/actions.css'];
|
||
$bodyClass = 'actions-page';
|
||
|
||
$database = new App\Database\Database();
|
||
$actionsManager = new App\Database\ActionsManager($database);
|
||
|
||
// Traitement du formulaire d'ajout d'une nouvelle action.
|
||
$creationMessage = null;
|
||
$creationError = null;
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action_create'])) {
|
||
$newAction = [
|
||
'isin' => trim($_POST['isin'] ?? ''),
|
||
'ticker' => trim($_POST['ticker'] ?? ''),
|
||
'yahoo_code' => trim($_POST['yahoo_code'] ?? ''),
|
||
'company_name' => trim($_POST['company_name'] ?? ''),
|
||
];
|
||
|
||
if ($newAction['isin'] === '' || $newAction['ticker'] === '' || $newAction['company_name'] === '') {
|
||
$creationError = 'Veuillez remplir tous les champs obligatoires.';
|
||
} else {
|
||
if ($actionsManager->createAction($newAction)) {
|
||
$creationMessage = 'Nouvelle action ajoutée avec succès.';
|
||
} else {
|
||
$creationError = 'Impossible d’ajouter l’action, veuillez réessayer.';
|
||
}
|
||
}
|
||
}
|
||
|
||
// Traitement du formulaire de mise à jour d'une action.
|
||
$updateMessage = null;
|
||
$updateError = null;
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action_update'], $_POST['action_isin'])) {
|
||
$actionIsin = trim($_POST['action_isin']);
|
||
$updateData = [
|
||
'ticker' => trim($_POST['ticker'] ?? ''),
|
||
'yahoo_code' => trim($_POST['yahoo_code'] ?? ''),
|
||
'company_name' => trim($_POST['company_name'] ?? ''),
|
||
];
|
||
|
||
if ($actionIsin === '' || $updateData['ticker'] === '' || $updateData['company_name'] === '') {
|
||
$updateError = 'Tous les champs requis doivent être remplis pour mettre à jour cette action.';
|
||
} else {
|
||
if ($actionsManager->updateAction($actionIsin, $updateData)) {
|
||
$updateMessage = 'Action mise à jour avec succès.';
|
||
} else {
|
||
$updateError = 'Impossible de mettre à jour l’action. Vérifiez les données et réessayez.';
|
||
}
|
||
}
|
||
}
|
||
|
||
$actions = $actionsManager->listActions();
|
||
|
||
require_once __DIR__ . '/header.php';
|
||
?>
|
||
|
||
<section class="actions-shell">
|
||
<div class="actions-container">
|
||
<div class="actions-header">
|
||
<h1 class="actions-title">Actions financières</h1>
|
||
<a href="/php/menu.php" class="action-button">Retour au menu</a>
|
||
</div>
|
||
|
||
<?php if ($creationMessage): ?>
|
||
<div class="actions-card action-notice action-success"><?= htmlspecialchars($creationMessage, ENT_QUOTES, 'UTF-8') ?></div>
|
||
<?php endif; ?>
|
||
<?php if ($creationError): ?>
|
||
<div class="actions-card action-notice action-error"><?= htmlspecialchars($creationError, ENT_QUOTES, 'UTF-8') ?></div>
|
||
<?php endif; ?>
|
||
<?php if ($updateMessage): ?>
|
||
<div class="actions-card action-notice action-success"><?= htmlspecialchars($updateMessage, ENT_QUOTES, 'UTF-8') ?></div>
|
||
<?php endif; ?>
|
||
<?php if ($updateError): ?>
|
||
<div class="actions-card action-notice action-error"><?= htmlspecialchars($updateError, ENT_QUOTES, 'UTF-8') ?></div>
|
||
<?php endif; ?>
|
||
|
||
<section class="actions-panel" id="formulaire-creation">
|
||
<form id="action-form" method="post" action="" autocomplete="off">
|
||
<input type="hidden" name="action_isin" value="">
|
||
<div class="action-form-row">
|
||
<label class="field-isin">
|
||
ISIN
|
||
<input id="action-isin" type="text" name="isin" placeholder="Ex. FR0000123456" required>
|
||
</label>
|
||
<label class="field-ticker">
|
||
Symbole
|
||
<input id="action-ticker" type="text" name="ticker" placeholder="EXA" required>
|
||
</label>
|
||
<label class="field-yahoo">
|
||
Code Yahoo
|
||
<input id="action-yahoo_code" type="text" name="yahoo_code" placeholder="EXA.PA" required>
|
||
</label>
|
||
<label class="field-company">
|
||
Société
|
||
<input id="action-company_name" type="text" name="company_name" placeholder="Société Alpha" required>
|
||
</label>
|
||
</div>
|
||
<div id="action-form-feedback" class="form-error" aria-live="polite" style="display:none;"></div>
|
||
<div class="actions-panel-footer">
|
||
<button type="submit" name="action_create" class="btn-action btn-primary create-button">Créer une action</button>
|
||
<button type="submit" name="action_update" class="btn-action btn-primary update-button" hidden>Modifier l'action</button>
|
||
<button type="button" class="btn-action btn-cancel cancel-edit-button" hidden>Annuler</button>
|
||
</div>
|
||
</form>
|
||
</section>
|
||
|
||
<section class="actions-card table-container">
|
||
<div class="actions-table-wrapper">
|
||
<table class="actions-table">
|
||
<thead>
|
||
<tr>
|
||
<th>ISIN</th>
|
||
<th>Symbole</th>
|
||
<th>Code Yahoo</th>
|
||
<th>Société</th>
|
||
<th>Mise à jour</th>
|
||
<th>Actions</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<?php foreach ($actions as $action): ?>
|
||
<tr class="action-row"
|
||
data-isin="<?= htmlspecialchars((string) $action['isin'], ENT_QUOTES, 'UTF-8') ?>"
|
||
data-ticker="<?= htmlspecialchars((string) $action['ticker'], ENT_QUOTES, 'UTF-8') ?>"
|
||
data-yahoo_code="<?= htmlspecialchars((string) $action['yahoo_code'], ENT_QUOTES, 'UTF-8') ?>"
|
||
data-company_name="<?= htmlspecialchars((string) $action['company_name'], ENT_QUOTES, 'UTF-8') ?>"
|
||
>
|
||
<td><?= htmlspecialchars((string) $action['isin'], ENT_QUOTES, 'UTF-8') ?></td>
|
||
<td><?= htmlspecialchars((string) $action['ticker'], ENT_QUOTES, 'UTF-8') ?></td>
|
||
<td><?= htmlspecialchars((string) $action['yahoo_code'], ENT_QUOTES, 'UTF-8') ?></td>
|
||
<td><?= htmlspecialchars((string) $action['company_name'], ENT_QUOTES, 'UTF-8') ?></td>
|
||
<td><?= htmlspecialchars((string) $action['updated_at'], ENT_QUOTES, 'UTF-8') ?></td>
|
||
<td>
|
||
<button type="button" class="btn-action btn-edit edit-action-button">Modifier</button>
|
||
</td>
|
||
</tr>
|
||
<?php endforeach; ?>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
</section>
|
||
</div>
|
||
</section>
|
||
|
||
<script src="/js/actions.js" defer></script>
|
||
|
||
<?php require_once __DIR__ . '/footer.php'; ?>
|