Plateforme web de commande de panier bio
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

692 lines
27 KiB

2 years ago
2 years ago
2 years ago
  1. <?php
  2. define('DEFAULT_TITLE', 'Mon panier bio');
  3. define('SUPPLIER_REGEX', '[A-Za-z]\w{0,31}');
  4. define('EVENT_REGEX', '\d{4}\-[01]\d\-[0123]\d');
  5. define('REQUEST_REGEX', '/^https?:\/\/.+\/(?<supplier>' . SUPPLIER_REGEX . ')\/?(?<event>' . EVENT_REGEX . ')?\/?$/');
  6. define('ACTION_REGEX', '/^[a-z]{1,16}$/i');
  7. $baseUrl = trim((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], '/');
  8. $requestUrl = trim(array_key_exists('QUERY_STRING', $_SERVER) ? str_replace($_SERVER['QUERY_STRING'], '', $baseUrl) : $baseUrl, '?');
  9. if (preg_match(REQUEST_REGEX, $requestUrl, $match)) {
  10. $requestSupplier = array_key_exists('supplier', $match) ? $match['supplier'] : null;
  11. $requestEvent = array_key_exists('event', $match) ? $match['event'] : null;
  12. if (!is_null($requestEvent))
  13. $requestUrl = rtrim(str_replace($requestEvent, '', $requestUrl), '/');
  14. if (!is_null($requestSupplier))
  15. $requestUrl = rtrim(str_replace($requestSupplier, '', $requestUrl), '/');
  16. }
  17. function generateUrl($supplier = null, $event = null) {
  18. global $requestUrl;
  19. if (is_null($supplier))
  20. return $requestUrl;
  21. if (is_null($event))
  22. return sprintf('%s/%s', $requestUrl, $supplier);
  23. return sprintf('%s/%s/%s', $requestUrl, $supplier, $event);
  24. }
  25. function findNext($start, $frequency, $excludes = [], $vsNow = true, $maxIterations = 1000, $direction = +1) {
  26. $now = new \DateTime('now');
  27. $current = clone $start;
  28. $frequency = \DateInterval::createFromDateString($frequency);
  29. do {
  30. if ($direction === abs($direction)) {
  31. if (!$vsNow and ($maxIterations-- > 0)) {
  32. $current->add($frequency);
  33. } else {
  34. while (
  35. ($current->getTimestamp() < $now->getTimestamp())
  36. and ($maxIterations-- > 0)
  37. ) $current->add($frequency);
  38. }
  39. } else {
  40. if (!$vsNow and ($maxIterations-- > 0)) {
  41. $current->sub($frequency);
  42. } else {
  43. while (
  44. ($current->getTimestamp() > $now->getTimestamp())
  45. and ($maxIterations-- > 0)
  46. ) $current->sub($frequency);
  47. }
  48. }
  49. $nextEvent = $current->format('Y-m-d');
  50. } while (
  51. in_array($nextEvent, $excludes)
  52. and ($maxIterations > 0)
  53. );
  54. return $current;
  55. }
  56. function findPrevious($start, $frequency, $excludes = [], $vsNow = true, $maxIterations = 1000) {
  57. return findNext($start, $frequency, $excludes, $vsNow, $maxIterations, -1);
  58. }
  59. define('CONFIG_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'config.php');
  60. define('DATA_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'data.php');
  61. if (file_exists(CONFIG_FILE)) require_once CONFIG_FILE;
  62. if (!isset($config)) $config = [];
  63. $action = (isset($_REQUEST['action']) and preg_match(ACTION_REGEX, $_REQUEST['action'])) ? $_REQUEST['action'] : null;
  64. $supplier = array_key_exists('supplier', $_REQUEST) ? $_REQUEST['supplier'] : $requestSupplier;
  65. $hasSupplier = is_string($supplier) and preg_match('/^' . SUPPLIER_REGEX . '$/', $supplier);
  66. $excludesFormatter = new \IntlDateFormatter('fr_FR.UTF8', \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE, 'Europe/Paris');
  67. if ($hasSupplier) {
  68. if (!isset($config[$supplier])) {
  69. $config[$supplier] = [];
  70. $supplierIsNew = true;
  71. } else {
  72. $supplierIsNew = false;
  73. }
  74. $config[$supplier] = array_merge(
  75. [
  76. 'title' => '',
  77. 'subtitle' => '<small class="text-muted text-nowrap d-block d-sm-inline">%date%</small>',
  78. 'description' => '',
  79. 'choices' => [],
  80. 'start' => 'now 00:00:00',
  81. 'frequency' => '1 day',
  82. 'password' => '',
  83. 'excludes' => [],
  84. ],
  85. $config[$supplier]
  86. );
  87. $hasPassword = !empty($config[$supplier]['password']);
  88. if ($action === 'config') {
  89. if ($hasPassword) {
  90. if (!isset($_SERVER['PHP_AUTH_USER'])) {
  91. header(sprintf('WWW-Authenticate: Basic realm="Configuration de mon panier bio pour %s"', $supplier));
  92. header('HTTP/1.0 401 Unauthorized');
  93. printf('Cette configuration est protégée par mot de passe !');
  94. exit;
  95. } elseif (
  96. ($_SERVER['PHP_AUTH_USER'] !== $supplier)
  97. or ($_SERVER['PHP_AUTH_PW'] !== $config[$supplier]['password'])
  98. ) {
  99. header('HTTP/1.0 403 Forbidden');
  100. printf('Cette configuration est protégée par mot de passe !');
  101. exit;
  102. }
  103. }
  104. foreach (array_keys($config[$supplier]) as $key)
  105. if (isset($_REQUEST[$key]))
  106. $config[$supplier][$key] = (!in_array($key, ['title', 'subtitle', 'description']) ? filter_var($_REQUEST[$key], FILTER_SANITIZE_STRING) : $_REQUEST[$key]);
  107. }
  108. if (empty($config[$supplier]['start']))
  109. $config[$supplier]['start'] = 'now 00:00:00';
  110. foreach (['choices', 'excludes'] as $key) {
  111. if (is_string($config[$supplier][$key]))
  112. $config[$supplier][$key] = explode(PHP_EOL, $config[$supplier][$key]);
  113. if (!is_array($config[$supplier][$key]))
  114. $config[$supplier][$key] = [];
  115. $config[$supplier][$key] = array_filter(
  116. $config[$supplier][$key],
  117. function ($choice) {
  118. return is_string($choice) and !empty(trim($choice));
  119. }
  120. );
  121. $config[$supplier][$key] = array_map('trim', $config[$supplier][$key]);
  122. }
  123. $config[$supplier]['excludes'] = array_filter(
  124. array_map(
  125. function ($value) use ($excludesFormatter) {
  126. if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $value))
  127. return $value;
  128. $timestamp = $excludesFormatter->parse($value, $offset);
  129. if ($timestamp !== false)
  130. return (new \DateTimeImmutable('@' . $timestamp, new \DateTimeZone('Europe/Paris')))->format('Y-m-d');
  131. try {
  132. return (new \DateTimeImmutable($value, new \DateTimeZone('Europe/Paris')))->format('Y-m-d');
  133. } catch (\Exception $exception) {
  134. return null;
  135. }
  136. },
  137. $config[$supplier]['excludes']
  138. ),
  139. function ($value) {
  140. return !is_null($value);
  141. }
  142. );
  143. }
  144. $isConfig = false;
  145. if ($action === 'config') {
  146. $output = fopen(CONFIG_FILE, 'w+');
  147. if ($output) {
  148. if (flock($output, LOCK_EX)) {
  149. fwrite($output, '<?php' . PHP_EOL);
  150. fprintf(
  151. $output,
  152. '$config = %s;' . PHP_EOL,
  153. var_export($config, true)
  154. );
  155. flock($output, LOCK_UN);
  156. }
  157. fclose($output);
  158. }
  159. $isConfig = true;
  160. }
  161. $suppliers = array_keys($config);
  162. sort($suppliers);
  163. try {
  164. $event = array_key_exists('event', $_REQUEST) ? $_REQUEST['event'] : $requestEvent;
  165. $hasEvent = (
  166. is_string($event)
  167. and preg_match('/^' . EVENT_REGEX . '$/', $event)
  168. and ((new \DateTimeImmutable($event)) instanceof \DateTimeImmutable)
  169. );
  170. } catch (\Exception $exception) {
  171. $hasEvent = false;
  172. }
  173. if (!$isConfig and !$supplierIsNew and $hasSupplier) {
  174. $start = new \DateTime($config[$supplier]['start']);
  175. if (!$hasEvent) {
  176. $next = findNext($start, $config[$supplier]['frequency'], $config[$supplier]['excludes'], true);
  177. $nextEvent = $next->format('Y-m-d');
  178. header('Location: ' . generateUrl($supplier, $nextEvent));
  179. die();
  180. } else {
  181. $current = new \DateTime($event);
  182. $previous = findPrevious($current, $config[$supplier]['frequency'], $config[$supplier]['excludes'], false);
  183. $previousEvent = $previous->format('Y-m-d');
  184. if (false and !array_key_exists($previousEvent, $data[$supplier]))
  185. unset($previousEvent);
  186. $next = findNext($current, $config[$supplier]['frequency'], $config[$supplier]['excludes'], false);
  187. $nextEvent = $next->format('Y-m-d');
  188. if (false and !array_key_exists($nextEvent, $data[$supplier]))
  189. unset($nextEvent);
  190. }
  191. switch ($action) {
  192. case 'insert' :
  193. case 'delete' :
  194. $isBeginning = (!file_exists(DATA_FILE) or in_array(filesize(DATA_FILE), [ false, 0 ]));
  195. $output = fopen(DATA_FILE, 'a+');
  196. if (!$output) break;
  197. if (!flock($output, LOCK_EX)) break;
  198. if ($isBeginning)
  199. fwrite($output, '<?php' . PHP_EOL);
  200. $item = [];
  201. foreach (['name', 'choice', 'action'] as $field)
  202. $item[$field] = filter_var($_REQUEST[$field], FILTER_SANITIZE_STRING);
  203. $item['timestamp'] = time();
  204. $item['hash'] = md5(implode([ $item['name'], $item['choice'], ]));
  205. fprintf(
  206. $output,
  207. '$data[%s][%s][] = %s;' . PHP_EOL,
  208. var_export($supplier, true),
  209. var_export($event, true),
  210. str_replace(PHP_EOL, '', var_export($item, true))
  211. );
  212. flock($output, LOCK_UN);
  213. fclose($output);
  214. header('Location: ' . generateUrl($supplier, $event));
  215. die();
  216. }
  217. if (!isset($data)) $data = [];
  218. if (file_exists(DATA_FILE)) include DATA_FILE;
  219. $items = [];
  220. $allItems = isset($data[$supplier][$event]) ? $data[$supplier][$event] : [];
  221. usort($allItems, function ($a, $b) {
  222. $a = intval($a['timestamp']);
  223. $b = intval($b['timestamp']);
  224. if ($a === $b)
  225. return 0;
  226. return ($a < $b) ? -1 : 1;
  227. });
  228. foreach ($allItems as $item) {
  229. if ($item['action'] === 'insert') {
  230. $items[] = $item;
  231. } elseif ($item['action'] === 'delete') {
  232. foreach ($items as $index => $prevItem)
  233. if ($prevItem['hash'] === $item['hash'])
  234. unset($items[$index]);
  235. }
  236. }
  237. $date = (new \IntlDateFormatter('fr_FR.UTF8', \IntlDateFormatter::FULL, \IntlDateFormatter::NONE, 'Europe/Paris'))->format(new \DateTime($event));
  238. foreach (['title', 'subtitle', 'description'] as $key) {
  239. while (preg_match('/%([^%]+)%/i', $config[$supplier][$key], $match))
  240. $config[$supplier][$key] = str_replace(
  241. $match[0],
  242. ${$match[1]},
  243. $config[$supplier][$key]
  244. );
  245. }
  246. if (empty($config[$supplier]['title']))
  247. $config[$supplier]['title'] = $supplier;
  248. $stats = [];
  249. foreach ($items as $item)
  250. if (!empty($item['choice']))
  251. $stats[$item['choice']] += 1;
  252. }
  253. if ($supplierIsNew and !empty($suppliers)) {
  254. $closestSuppliers = array_filter(
  255. array_map(
  256. function ($other) use ($supplier) {
  257. return [
  258. 'supplier' => $other,
  259. 'score' => levenshtein($supplier, $other),
  260. ];
  261. },
  262. $suppliers
  263. ),
  264. function ($item) {
  265. return $item['score'] > 0;
  266. }
  267. );
  268. usort($closestSuppliers, function ($a, $b) {
  269. if ($a['score'] == $b['score']) {
  270. return 0;
  271. }
  272. return ($a['score'] < $b['score']) ? -1 : 1;
  273. });
  274. }
  275. ?><!DOCTYPE html>
  276. <html lang="fr">
  277. <head>
  278. <meta charset="UTF-8" />
  279. <meta name="viewport" content="width=device-width, initial-scale=1" />
  280. <title><?php if ($hasSupplier) : ?><?php echo strip_tags($config[$supplier]['title']); ?><?php if (!$isConfig) : ?> — <?php echo strip_tags($config[$supplier]['subtitle']); ?><?php endif; ?><?php else : ?><?php echo DEFAULT_TITLE; ?><?php endif; ?></title>
  281. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  282. </head>
  283. <body>
  284. <header>
  285. <nav class="navbar navbar-dark bg-dark">
  286. <div class="container-fluid">
  287. <a class="navbar-brand" href="<?php echo $hasSupplier ? generateUrl($supplier) : generateUrl(); ?>">
  288. <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="bi bi-basket d-inline-block align-text-top" viewBox="0 0 16 16">
  289. <path d="M5.757 1.071a.5.5 0 0 1 .172.686L3.383 6h9.234L10.07 1.757a.5.5 0 1 1 .858-.514L13.783 6H15a1 1 0 0 1 1 1v1a1 1 0 0 1-1 1v4.5a2.5 2.5 0 0 1-2.5 2.5h-9A2.5 2.5 0 0 1 1 13.5V9a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h1.217L5.07 1.243a.5.5 0 0 1 .686-.172zM2 9v4.5A1.5 1.5 0 0 0 3.5 15h9a1.5 1.5 0 0 0 1.5-1.5V9H2zM1 7v1h14V7H1zm3 3a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3A.5.5 0 0 1 4 10zm2 0a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3A.5.5 0 0 1 6 10zm2 0a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3A.5.5 0 0 1 8 10zm2 0a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3a.5.5 0 0 1 .5-.5zm2 0a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3a.5.5 0 0 1 .5-.5z"/>
  290. </svg>
  291. <?php echo $hasSupplier ? $supplier : DEFAULT_TITLE; ?>
  292. </a>
  293. <?php if ($hasSupplier) : ?>
  294. <span class="navbar-text text-muted">
  295. <?php if ($isConfig) : ?>
  296. <a class="text-reset" href="<?php echo generateUrl($supplier); ?>">Retour</a>
  297. <?php else : ?>
  298. <?php if ($hasPassword) : ?>
  299. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-lock" viewBox="0 0 16 16">
  300. <path d="M8 1a2 2 0 0 1 2 2v4H6V3a2 2 0 0 1 2-2zm3 6V3a3 3 0 0 0-6 0v4a2 2 0 0 0-2 2v5a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2V9a2 2 0 0 0-2-2zM5 8h6a1 1 0 0 1 1 1v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V9a1 1 0 0 1 1-1z"/>
  301. </svg>
  302. <?php else : ?>
  303. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-unlock" viewBox="0 0 16 16">
  304. <path d="M11 1a2 2 0 0 0-2 2v4a2 2 0 0 1 2 2v5a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2h5V3a3 3 0 0 1 6 0v4a.5.5 0 0 1-1 0V3a2 2 0 0 0-2-2zM3 8a1 1 0 0 0-1 1v5a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V9a1 1 0 0 0-1-1H3z"/>
  305. </svg>
  306. <?php endif; ?>
  307. <a tabindex="-1" class="text-reset" href="<?php printf('%s?action=config', generateUrl($supplier)); ?>">Configuration</a>
  308. <?php endif; ?>
  309. </span>
  310. <?php endif; ?>
  311. </div>
  312. </nav>
  313. </header>
  314. <main>
  315. <?php if (!$hasSupplier) : ?>
  316. <section class="container-fluid pt-3">
  317. <div class="alert alert-danger alert-dismissible mb-3" role="alert">
  318. Pas de fournisseur !
  319. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Fermer"></button>
  320. </div>
  321. <div class="row mb-3 g-3">
  322. <div class="col-12">
  323. <form action="<?php echo generateUrl(); ?>" method="post">
  324. <datalist id="supplierList">
  325. <?php foreach ($suppliers as $supplier) : ?>
  326. <option value="<?php echo $supplier; ?>" />
  327. <?php endforeach; ?>
  328. </datalist>
  329. <div class="input-group input-group-lg">
  330. <span class="input-group-text">
  331. <span class="d-none d-sm-inline"><?php echo generateUrl(); ?></span>
  332. <span class="d-inline d-sm-none" title="<?php echo generateUrl(); ?>">&hellip;</span>
  333. /
  334. </span>
  335. <input type="text" class="form-control js-closealerts" name="supplier" list="supplierList" required />
  336. <button class="btn btn-primary" type="submit">Aller&nbsp;&rarr;</button>
  337. </div>
  338. </form>
  339. </div>
  340. <div class="col-12">
  341. <details>
  342. <summary>Documentation</summary>
  343. </details>
  344. </div>
  345. </div>
  346. </section>
  347. <?php else : ?>
  348. <?php if ($isConfig) : ?>
  349. <section class="container-fluid">
  350. <div class="row my-3 g-3">
  351. <div class="col">
  352. <h1>Configuration</h1>
  353. </div>
  354. </div>
  355. </section>
  356. <section class="container-fluid">
  357. <div class="row g-3">
  358. <form action="<?php echo generateUrl($supplier); ?>" method="post">
  359. <div class="row mb-3">
  360. <label for="title" class="col-sm-2 col-form-label">Titre</label>
  361. <div class="col-sm-10">
  362. <input class="form-control" type="text" name="title" value="<?php echo htmlspecialchars($config[$supplier]['title']); ?>" placeholder="<?php echo $supplier; ?>" />
  363. <div class="form-text">Le titre de la page. Par défaut ce sera le nom du fournisseur </div>
  364. </div>
  365. </div>
  366. <div class="row mb-3">
  367. <label for="description" class="col-sm-2 col-form-label">Description</label>
  368. <div class="col-sm-10">
  369. <textarea class="form-control js-ckeditor" name="description" rows="10"><?php echo $config[$supplier]['description']; ?></textarea>
  370. <div class="form-text">La description affichée sous le titre.</div>
  371. </div>
  372. </div>
  373. <div class="row mb-3">
  374. <label for="choices" class="col-sm-2 col-form-label">Choix</label>
  375. <div class="col-sm-10">
  376. <textarea class="form-control" name="choices" rows="5"><?php echo implode(PHP_EOL, $config[$supplier]['choices']); ?></textarea>
  377. <div class="form-text">Les différents choix possibles. Un par ligne. Ou pas.</div>
  378. </div>
  379. </div>
  380. <div class="row mb-3">
  381. <label for="start" class="col-sm-2 col-form-label">Début</label>
  382. <div class="col-sm-10">
  383. <input class="form-control" type="date" name="start" value="<?php echo $config[$supplier]['start']; ?>" />
  384. <div class="form-text">La date du premier événement, si nécessaire de le préciser.</div>
  385. </div>
  386. </div>
  387. <div class="row mb-3">
  388. <label for="frequency" class="col-sm-2 col-form-label">Fréquence</label>
  389. <div class="col-sm-10">
  390. <input class="form-control" type="text" name="frequency" value="<?php echo $config[$supplier]['frequency']; ?>" />
  391. <div class="form-text">La fréquence des événements dans le format <a class="text-reset" href="https://www.php.net/manual/fr/datetime.formats.relative.php" target="_blank">décrit sur cette page</a>.</div>
  392. </div>
  393. </div>
  394. <div class="row mb-3">
  395. <label for="excludes" class="col-sm-2 col-form-label">Exceptions</label>
  396. <div class="col-sm-10">
  397. <textarea class="form-control" name="excludes" rows="5"><?php echo implode(PHP_EOL, array_map(function ($value) use ($excludesFormatter) { return $excludesFormatter->format(new \DateTimeImmutable($value, new \DateTimeZone('Europe/Paris'))); }, $config[$supplier]['excludes'])); ?></textarea>
  398. <div class="form-text">Les dates à exclure. Une par ligne. Ou pas. En tous cas le format c'est celui de l'<a class="text-reset" href="https://unicode-org.github.io/icu/userguide/format_parse/datetime/" target="_blank">ICU</a> : <kbd><?php echo $excludesFormatter->getPattern(); ?></kbd></div>
  399. </div>
  400. </div>
  401. <div class="row mb-3">
  402. <label for="password" class="col-sm-2 col-form-label">Mot de passe</label>
  403. <div class="col-sm-10">
  404. <input class="form-control" type="text" name="password" value="<?php echo $config[$supplier]['password']; ?>" />
  405. <div class="form-text">Ce mot de passe sera demandé pour accéder à la configuration la prochaine fois. Le nom d'utilisateur est le fournisseur courant (en l'occurrence <kbd><?php echo $supplier; ?></kbd>).</div>
  406. </div>
  407. </div>
  408. <div class="row">
  409. <div class="col mb-3">
  410. <button class="btn btn-primary" type="submit" name="action" value="config">Enregistrer</button>
  411. </div>
  412. </div>
  413. </form>
  414. </div>
  415. </section>
  416. <?php else /* !$isConfig */ : ?>
  417. <?php if ($supplierIsNew) : ?>
  418. <section class="container-fluid pt-3">
  419. <div class="alert alert-warning alert-dismissible" role="alert">
  420. Ce fournisseur n'existe pas encore !
  421. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Fermer"></button>
  422. </div>
  423. <div class="row g-3">
  424. <div class="col-xs-12 col-sm-6">
  425. <div class="card h-100">
  426. <div class="card-body">
  427. <h2 class="card-title">Oops !</h2>
  428. <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>
  429. <p class="card-text">
  430. Peut-être sagissait-il de
  431. <?php $max = 3; foreach ($closestSuppliers as $index => $item) : ?>
  432. <?php if ($index < $max) : ?>
  433. <?php if ($index > 0) : ?>
  434. <?php if ($index === min($max, count($closestSuppliers) - 1)) : ?>
  435. ou
  436. <?php else : ?>
  437. ,
  438. <?php endif; ?>
  439. <?php endif; ?>
  440. « <tt><a class="card-link" href="<?php echo generateUrl($item['supplier']); ?>"><?php echo $item['supplier']; ?></a></tt> »
  441. <?php endif; ?>
  442. <?php endforeach; ?>
  443. ?
  444. </p>
  445. <a class="btn btn-primary" href="<?php echo generateUrl(); ?>">Recommencer</a>
  446. </div>
  447. </div>
  448. </div>
  449. <div class="col-xs-12 col-sm-6">
  450. <div class="card h-100">
  451. <div class="card-body">
  452. <h2 class="card-title">C'est normal !</h2>
  453. <p class="card-text">On souhaite le créer.</p>
  454. <p class="card_text">Une fois configuré il sera prêt à être utilisé.</p>
  455. <a class="btn btn-primary" href="<?php echo generateUrl($supplier) . '?action=config'; ?>">Configurer</a>
  456. </div>
  457. </div>
  458. </div>
  459. </div>
  460. </section>
  461. <?php else /* !$supplierIsNew */ : ?>
  462. <section class="container-fluid">
  463. <div class="row my-3">
  464. <div class="col">
  465. <h1>
  466. <div class="btn-group float-end" role="group">
  467. <?php if (isset($previousEvent)) : ?>
  468. <a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $previousEvent); ?>" title="Événement précédent">
  469. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-left" viewBox="0 0 16 16">
  470. <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"/>
  471. </svg>
  472. </a>
  473. <?php endif; ?>
  474. <a class="btn btn-outline-primary d-none d-sm-inline" href="<?php echo generateUrl($supplier, $event); ?>" title="Cet événement">
  475. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-link" viewBox="0 0 16 16">
  476. <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"/>
  477. <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"/>
  478. </svg>
  479. </a>
  480. <?php if (isset($nextEvent)) : ?>
  481. <a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $nextEvent); ?>" title="Événement suivant">
  482. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-right" viewBox="0 0 16 16">
  483. <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"/>
  484. </svg>
  485. </a>
  486. <?php endif; ?>
  487. </div>
  488. <?php echo $config[$supplier]['title']; ?>
  489. <?php echo $config[$supplier]['subtitle']; ?>
  490. </h1>
  491. <?php if (!empty($config[$supplier]['description'])) : ?>
  492. <p class="lead"><?php echo $config[$supplier]['description']; ?></p>
  493. <?php endif; ?>
  494. </div>
  495. </div>
  496. </section>
  497. <section class="container-fluid">
  498. <div class="row g-3">
  499. <form class="js-localremember bg-dark text-light" action="<?php echo generateUrl($supplier); ?>" method="post">
  500. <div class="row my-3">
  501. <label for="title" class="col-sm-2 col-form-label">Nom</label>
  502. <div class="col-sm-10">
  503. <input class="form-control" type="text" name="name" required placeholder="Nom" />
  504. </div>
  505. </div>
  506. <?php if (!empty($config[$supplier]['choices'])) : ?>
  507. <div class="row mb-3">
  508. <label for="title" class="col-sm-2 col-form-label">Choix</label>
  509. <div class="col-sm-10">
  510. <div class="btn-group" role="group">
  511. <?php foreach ($config[$supplier]['choices'] as $index => $choice) : ?>
  512. <input type="radio" class="btn-check" id="<?php printf('option%d', $index); ?>" autocomplete="off" name="choice" value="<?php echo $choice; ?>" />
  513. <label class="btn btn-outline-light" for="<?php printf('option%d', $index); ?>"><?php echo $choice; ?></label>
  514. <?php endforeach; ?>
  515. </div>
  516. </div>
  517. </div>
  518. <?php endif; ?>
  519. <div class="row">
  520. <div class="col mb-3">
  521. <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
  522. <input type="hidden" name="event" value="<?php echo $event; ?>" />
  523. <?php if (empty($config[$supplier]['choices'])) : ?>
  524. <input type="hidden" name="choice" value="" />
  525. <?php endif; ?>
  526. <button class="btn btn-primary" type="submit" name="action" value="insert">Commander</button>
  527. </div>
  528. </div>
  529. </form>
  530. </div>
  531. </section>
  532. <section class="container-fluid">
  533. <div class="row my-3">
  534. <?php if (!empty($items)) : ?>
  535. <div class="col-12">
  536. <div class="table-responsive">
  537. <table class="table table-striped table-hover align-middle">
  538. <thead>
  539. <tr>
  540. <th scope="col">
  541. Nom
  542. </th>
  543. <?php if (!empty($config[$supplier]['choices'])) : ?>
  544. <th scope="col">
  545. Choix
  546. </th>
  547. <?php endif; ?>
  548. <th scope="col">
  549. &nbsp;
  550. </th>
  551. </tr>
  552. </thead>
  553. <tbody>
  554. <?php foreach ($items as $item) : ?>
  555. <tr>
  556. <td>
  557. <?php echo $item['name']; ?>
  558. </td>
  559. <?php if (!empty($config[$supplier]['choices'])) : ?>
  560. <td>
  561. <?php if (!empty($item['choice'])) : ?>
  562. <?php echo $item['choice']; ?>
  563. <?php endif; ?>
  564. </td>
  565. <?php endif; ?>
  566. <td>
  567. <form onsubmit="return confirm('Souhaitez-vous vraiment annuler cette commande ?');">
  568. <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
  569. <input type="hidden" name="event" value="<?php echo $event; ?>" />
  570. <input type="hidden" name="name" value="<?php echo $item['name']; ?>" />
  571. <input type="hidden" name="choice" value="<?php echo $item['choice']; ?>" />
  572. <button class="btn btn-secondary float-end" type="submit" name="action" value="delete">Annuler</button>
  573. </form>
  574. </td>
  575. </tr>
  576. <?php endforeach; ?>
  577. </tbody>
  578. </table>
  579. </div>
  580. </div>
  581. <?php endif; ?>
  582. <div class="col-12">
  583. <ul class="list-group">
  584. <li class="list-group-item d-flex justify-content-between align-items-center">
  585. Commandes
  586. <span class="badge bg-primary rounded-pill"><?php echo count($items); ?></span>
  587. </li>
  588. <?php foreach ($stats as $choice => $count) : ?>
  589. <li class="list-group-item d-flex justify-content-between align-items-center">
  590. <?php echo $choice; ?>
  591. <span class="badge bg-secondary rounded-pill"><?php echo $count; ?></span>
  592. </li>
  593. <?php endforeach; ?>
  594. </ul>
  595. </div>
  596. </div>
  597. </section>
  598. <?php endif; /* $supplierIsNew */ ?>
  599. <?php endif; /* $isConfig*/ ?>
  600. <?php endif; ?>
  601. </main>
  602. <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>
  603. <?php if ($isConfig) : ?>
  604. <script src="https://cdn.ckeditor.com/ckeditor5/31.0.0/classic/ckeditor.js"></script>
  605. <script>
  606. document.querySelectorAll('.js-ckeditor').forEach(function (element) {
  607. ClassicEditor.create(element).catch(error => { console.error(error); });
  608. });
  609. </script>
  610. <?php endif; ?>
  611. <script>
  612. document.querySelectorAll('.js-localremember').forEach(function (form) {
  613. const fields = [ 'name', 'choice' ];
  614. form.addEventListener('submit', function (event) {
  615. fields.forEach(function (field) {
  616. window.localStorage.setItem('mon_panier_bio_' + field, form.elements[field].value);
  617. });
  618. });
  619. fields.forEach(function (field) {
  620. if (
  621. (form.elements[field].value === '')
  622. && (window.localStorage.getItem('mon_panier_bio_' + field) !== null)
  623. ) {
  624. form.elements[field].value = window.localStorage.getItem('mon_panier_bio_' + field);
  625. }
  626. });
  627. });
  628. document.querySelectorAll('.js-closealerts').forEach(function (element) {
  629. element.addEventListener('input', function (event) {
  630. if (event.target.value !== '') {
  631. document.querySelectorAll('.alert').forEach(function (alertElement) {
  632. var alert = bootstrap.Alert.getOrCreateInstance(alertElement)
  633. alert.close();
  634. });
  635. }
  636. });
  637. });
  638. </script>
  639. </body>
  640. </html>