Browse Source

gère la création de suppliers

master
vince vince 3 years ago
parent
commit
bf02358a64
1 changed files with 198 additions and 126 deletions
  1. +198
    -126
      index.php

+ 198
- 126
index.php View File

@ -8,7 +8,7 @@ define('REQUEST_REGEX', '/^https?:\/\/.+\/(?<supplier>' . SUPPLIER_REGEX . ')\/?
define('ACTION_REGEX', '/^[a-z]{1,16}$/i');
$baseUrl = trim((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], '/');
$requestUrl = trim(str_replace($_SERVER['QUERY_STRING'], '', $baseUrl), '?');
$requestUrl = trim(array_key_exists('QUERY_STRING', $_SERVER) ? str_replace($_SERVER['QUERY_STRING'], '', $baseUrl) : $baseUrl, '?');
if (preg_match(REQUEST_REGEX, $requestUrl, $match)) {
$requestSupplier = array_key_exists('supplier', $match) ? $match['supplier'] : null;
@ -83,8 +83,12 @@ $excludesFormatter = new \IntlDateFormatter('fr_FR.UTF8', \IntlDateFormatter::SH
if ($hasSupplier) {
if (!isset($config[$supplier]))
if (!isset($config[$supplier])) {
$config[$supplier] = [];
$supplierIsNew = true;
} else {
$supplierIsNew = false;
}
$config[$supplier] = array_merge(
[
@ -201,7 +205,7 @@ try {
} catch (\Exception $exception) {
$hasEvent = false;
}
if (!$isConfig and $hasSupplier) {
if (!$isConfig and !$supplierIsNew and $hasSupplier) {
$start = new \DateTime($config[$supplier]['start']);
if (!$hasEvent) {
@ -294,6 +298,28 @@ if (!$isConfig and $hasSupplier) {
}
if ($supplierIsNew and !empty($suppliers)) {
$closestSuppliers = array_filter(
array_map(
function ($other) use ($supplier) {
return [
'supplier' => $other,
'score' => levenshtein($supplier, $other),
];
},
$suppliers
),
function ($item) {
return $item['score'] > 0;
}
);
usort($closestSuppliers, function ($a, $b) {
if ($a['score'] == $b['score']) {
return 0;
}
return ($a['score'] < $b['score']) ? -1 : 1;
});
}
?><!DOCTYPE html>
<html lang="fr">
@ -437,144 +463,190 @@ if (!$isConfig and $hasSupplier) {
</form>
</div>
</section>
<?php else : ?>
<section class="container-fluid">
<div class="row my-3">
<div class="col">
<h1>
<div class="btn-group float-end" role="group">
<?php if (isset($previousEvent)) : ?>
<a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $previousEvent); ?>" title="Événement précédent">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-left" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M15 8a.5.5 0 0 0-.5-.5H2.707l3.147-3.146a.5.5 0 1 0-.708-.708l-4 4a.5.5 0 0 0 0 .708l4 4a.5.5 0 0 0 .708-.708L2.707 8.5H14.5A.5.5 0 0 0 15 8z"/>
</svg>
</a>
<?php endif; ?>
<a class="btn btn-outline-primary d-none d-sm-inline" href="<?php echo generateUrl($supplier, $event); ?>" title="Cet événement">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-link" viewBox="0 0 16 16">
<path d="M6.354 5.5H4a3 3 0 0 0 0 6h3a3 3 0 0 0 2.83-4H9c-.086 0-.17.01-.25.031A2 2 0 0 1 7 10.5H4a2 2 0 1 1 0-4h1.535c.218-.376.495-.714.82-1z"/>
<path d="M9 5.5a3 3 0 0 0-2.83 4h1.098A2 2 0 0 1 9 6.5h3a2 2 0 1 1 0 4h-1.535a4.02 4.02 0 0 1-.82 1H12a3 3 0 1 0 0-6H9z"/>
</svg>
</a>
<?php if (isset($nextEvent)) : ?>
<a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $nextEvent); ?>" title="Événement suivant">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-right" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M1 8a.5.5 0 0 1 .5-.5h11.793l-3.147-3.146a.5.5 0 0 1 .708-.708l4 4a.5.5 0 0 1 0 .708l-4 4a.5.5 0 0 1-.708-.708L13.293 8.5H1.5A.5.5 0 0 1 1 8z"/>
</svg>
</a>
<?php endif; ?>
</div>
<?php echo $config[$supplier]['title']; ?>
<?php echo $config[$supplier]['subtitle']; ?>
</h1>
<?php if (!empty($config[$supplier]['description'])) : ?>
<p class="lead"><?php echo $config[$supplier]['description']; ?></p>
<?php endif; ?>
<?php else /* !$isConfig */ : ?>
<?php if ($supplierIsNew) : ?>
<section class="container-fluid pt-3">
<div class="alert alert-warning alert-dismissible" role="alert">
Ce fournisseur n'existe pas encore !
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Fermer"></button>
</div>
</div>
</section>
<section class="container-fluid">
<div class="row g-3">
<form class="js-localremember bg-dark text-light" action="<?php echo generateUrl($supplier); ?>" method="post">
<div class="row my-3">
<label for="title" class="col-sm-2 col-form-label">Nom</label>
<div class="col-sm-10">
<input class="form-control" type="text" name="name" required placeholder="Nom" />
<div class="row g-3">
<div class="col-xs-12 col-sm-6">
<div class="card h-100">
<div class="card-body">
<h2 class="card-title">Oops !</h2>
<p class="card-text">Le nom du fournisseur « <tt><?php echo $supplier; ?></tt> » est probablement mal orthographié, c'est pour ça qu'il n'existe pas.</p>
<p class="card-text">
Peut-être sagissait-il de
<?php $max = 3; foreach ($closestSuppliers as $index => $item) : ?>
<?php if ($index < $max) : ?>
<?php if ($index > 0) : ?>
<?php if ($index === min($max, count($closestSuppliers) - 1)) : ?>
ou
<?php else : ?>
,
<?php endif; ?>
<?php endif; ?>
« <tt><a class="card-link" href="<?php echo generateUrl($item['supplier']); ?>"><?php echo $item['supplier']; ?></a></tt> »
<?php endif; ?>
<?php endforeach; ?>
?
</p>
<a class="btn btn-primary" href="<?php echo generateUrl(); ?>">Recommencer</a>
</div>
</div>
</div>
<?php if (!empty($config[$supplier]['choices'])) : ?>
<div class="row mb-3">
<label for="title" class="col-sm-2 col-form-label">Choix</label>
<div class="col-sm-10">
<div class="btn-group" role="group">
<?php foreach ($config[$supplier]['choices'] as $index => $choice) : ?>
<input type="radio" class="btn-check" id="<?php printf('option%d', $index); ?>" autocomplete="off" name="choice" value="<?php echo $choice; ?>" />
<label class="btn btn-outline-light" for="<?php printf('option%d', $index); ?>"><?php echo $choice; ?></label>
<?php endforeach; ?>
<div class="col-xs-12 col-sm-6">
<div class="card h-100">
<div class="card-body">
<h2 class="card-title">C'est normal !</h2>
<p class="card-text">On souhaite le créer.</p>
<p class="card_text">Une fois configuré il sera prêt à être utilisé.</p>
<a class="btn btn-primary" href="<?php echo generateUrl($supplier) . '?action=config'; ?>">Configurer</a>
</div>
</div>
</div>
<?php endif; ?>
<div class="row">
<div class="col mb-3">
<input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
<input type="hidden" name="event" value="<?php echo $event; ?>" />
<?php if (empty($config[$supplier]['choices'])) : ?>
<input type="hidden" name="choice" value="" />
<?php endif; ?>
<button class="btn btn-primary" type="submit" name="action" value="insert">Commander</button>
</div>
</div>
</section>
<?php else /* !$supplierIsNew */ : ?>
<section class="container-fluid">
<div class="row my-3">
<div class="col">
<h1>
<div class="btn-group float-end" role="group">
<?php if (isset($previousEvent)) : ?>
<a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $previousEvent); ?>" title="Événement précédent">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-left" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M15 8a.5.5 0 0 0-.5-.5H2.707l3.147-3.146a.5.5 0 1 0-.708-.708l-4 4a.5.5 0 0 0 0 .708l4 4a.5.5 0 0 0 .708-.708L2.707 8.5H14.5A.5.5 0 0 0 15 8z"/>
</svg>
</a>
<?php endif; ?>
<a class="btn btn-outline-primary d-none d-sm-inline" href="<?php echo generateUrl($supplier, $event); ?>" title="Cet événement">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-link" viewBox="0 0 16 16">
<path d="M6.354 5.5H4a3 3 0 0 0 0 6h3a3 3 0 0 0 2.83-4H9c-.086 0-.17.01-.25.031A2 2 0 0 1 7 10.5H4a2 2 0 1 1 0-4h1.535c.218-.376.495-.714.82-1z"/>
<path d="M9 5.5a3 3 0 0 0-2.83 4h1.098A2 2 0 0 1 9 6.5h3a2 2 0 1 1 0 4h-1.535a4.02 4.02 0 0 1-.82 1H12a3 3 0 1 0 0-6H9z"/>
</svg>
</a>
<?php if (isset($nextEvent)) : ?>
<a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $nextEvent); ?>" title="Événement suivant">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-right" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M1 8a.5.5 0 0 1 .5-.5h11.793l-3.147-3.146a.5.5 0 0 1 .708-.708l4 4a.5.5 0 0 1 0 .708l-4 4a.5.5 0 0 1-.708-.708L13.293 8.5H1.5A.5.5 0 0 1 1 8z"/>
</svg>
</a>
<?php endif; ?>
</div>
<?php echo $config[$supplier]['title']; ?>
<?php echo $config[$supplier]['subtitle']; ?>
</h1>
<?php if (!empty($config[$supplier]['description'])) : ?>
<p class="lead"><?php echo $config[$supplier]['description']; ?></p>
<?php endif; ?>
</div>
</form>
</div>
</section>
<section class="container-fluid">
<div class="row my-3">
<?php if (!empty($items)) : ?>
<div class="col-12">
<div class="table-responsive">
<table class="table table-striped table-hover align-middle">
<thead>
<tr>
<th scope="col">
Nom
</th>
<?php if (!empty($config[$supplier]['choices'])) : ?>
<th scope="col">
Choix
</th>
<?php endif; ?>
<th scope="col">
&nbsp;
</th>
</tr>
</thead>
<tbody>
<?php foreach ($items as $item) : ?>
</div>
</section>
<section class="container-fluid">
<div class="row g-3">
<form class="js-localremember bg-dark text-light" action="<?php echo generateUrl($supplier); ?>" method="post">
<div class="row my-3">
<label for="title" class="col-sm-2 col-form-label">Nom</label>
<div class="col-sm-10">
<input class="form-control" type="text" name="name" required placeholder="Nom" />
</div>
</div>
<?php if (!empty($config[$supplier]['choices'])) : ?>
<div class="row mb-3">
<label for="title" class="col-sm-2 col-form-label">Choix</label>
<div class="col-sm-10">
<div class="btn-group" role="group">
<?php foreach ($config[$supplier]['choices'] as $index => $choice) : ?>
<input type="radio" class="btn-check" id="<?php printf('option%d', $index); ?>" autocomplete="off" name="choice" value="<?php echo $choice; ?>" />
<label class="btn btn-outline-light" for="<?php printf('option%d', $index); ?>"><?php echo $choice; ?></label>
<?php endforeach; ?>
</div>
</div>
</div>
<?php endif; ?>
<div class="row">
<div class="col mb-3">
<input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
<input type="hidden" name="event" value="<?php echo $event; ?>" />
<?php if (empty($config[$supplier]['choices'])) : ?>
<input type="hidden" name="choice" value="" />
<?php endif; ?>
<button class="btn btn-primary" type="submit" name="action" value="insert">Commander</button>
</div>
</div>
</form>
</div>
</section>
<section class="container-fluid">
<div class="row my-3">
<?php if (!empty($items)) : ?>
<div class="col-12">
<div class="table-responsive">
<table class="table table-striped table-hover align-middle">
<thead>
<tr>
<td>
<?php echo $item['name']; ?>
</td>
<th scope="col">
Nom
</th>
<?php if (!empty($config[$supplier]['choices'])) : ?>
<td>
<?php if (!empty($item['choice'])) : ?>
<?php echo $item['choice']; ?>
<?php endif; ?>
</td>
<th scope="col">
Choix
</th>
<?php endif; ?>
<td>
<form onsubmit="return confirm('Souhaitez-vous vraiment annuler cette commande ?');">
<input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
<input type="hidden" name="event" value="<?php echo $event; ?>" />
<input type="hidden" name="name" value="<?php echo $item['name']; ?>" />
<input type="hidden" name="choice" value="<?php echo $item['choice']; ?>" />
<button class="btn btn-secondary float-end" type="submit" name="action" value="delete">Annuler</button>
</form>
</td>
<th scope="col">
&nbsp;
</th>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</thead>
<tbody>
<?php foreach ($items as $item) : ?>
<tr>
<td>
<?php echo $item['name']; ?>
</td>
<?php if (!empty($config[$supplier]['choices'])) : ?>
<td>
<?php if (!empty($item['choice'])) : ?>
<?php echo $item['choice']; ?>
<?php endif; ?>
</td>
<?php endif; ?>
<td>
<form onsubmit="return confirm('Souhaitez-vous vraiment annuler cette commande ?');">
<input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
<input type="hidden" name="event" value="<?php echo $event; ?>" />
<input type="hidden" name="name" value="<?php echo $item['name']; ?>" />
<input type="hidden" name="choice" value="<?php echo $item['choice']; ?>" />
<button class="btn btn-secondary float-end" type="submit" name="action" value="delete">Annuler</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<?php endif; ?>
<div class="col-12">
<ul class="list-group">
<li class="list-group-item d-flex justify-content-between align-items-center">
Commandes
<span class="badge bg-primary rounded-pill"><?php echo count($items); ?></span>
</li>
<?php foreach ($stats as $choice => $count) : ?>
<?php endif; ?>
<div class="col-12">
<ul class="list-group">
<li class="list-group-item d-flex justify-content-between align-items-center">
<?php echo $choice; ?>
<span class="badge bg-secondary rounded-pill"><?php echo $count; ?></span>
Commandes
<span class="badge bg-primary rounded-pill"><?php echo count($items); ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php foreach ($stats as $choice => $count) : ?>
<li class="list-group-item d-flex justify-content-between align-items-center">
<?php echo $choice; ?>
<span class="badge bg-secondary rounded-pill"><?php echo $count; ?></span>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
</div>
</section>
<?php endif; ?>
</section>
<?php endif; /* $supplierIsNew */ ?>
<?php endif; /* $isConfig*/ ?>
<?php endif; ?>
</main>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>


Loading…
Cancel
Save